diff --git a/src/socket.io/user/picture.js b/src/socket.io/user/picture.js index 995b184b47..fd862a475a 100644 --- a/src/socket.io/user/picture.js +++ b/src/socket.io/user/picture.js @@ -57,15 +57,17 @@ module.exports = function (SocketUser) { if (!socket.uid || !data.url || !data.uid) { return callback(new Error('[[error:invalid-data]]')); } - - user.isAdminOrSelf(socket.uid, data.uid, function (err) { - if (err) { - return callback(err); + async.waterfall([ + function (next) { + user.isAdminOrSelf(socket.uid, data.uid, next); + }, + function (next) { + user.uploadFromUrl(data.uid, data.url, next); + }, + function (uploadedImage, next) { + next(null, uploadedImage ? uploadedImage.url : null); } - user.uploadFromUrl(data.uid, data.url, function (err, uploadedImage) { - callback(err, uploadedImage ? uploadedImage.url : null); - }); - }); + ], callback); }; SocketUser.removeUploadedPicture = function (socket, data, callback) { @@ -81,7 +83,7 @@ module.exports = function (SocketUser) { user.getUserFields(data.uid, ['uploadedpicture', 'picture'], next); }, function (userData, next) { - if (!userData.uploadedpicture.startsWith('http')) { + if (userData.uploadedpicture && !userData.uploadedpicture.startsWith('http')) { require('fs').unlink(path.join(__dirname, '../../../public', userData.uploadedpicture), function (err) { if (err) { winston.error(err); diff --git a/src/socket.io/user/status.js b/src/socket.io/user/status.js index 2ed389dacc..b3530ffc7d 100644 --- a/src/socket.io/user/status.js +++ b/src/socket.io/user/status.js @@ -1,21 +1,24 @@ 'use strict'; +var async = require('async'); + var user = require('../../user'); var websockets = require('../index'); module.exports = function (SocketUser) { + SocketUser.checkStatus = function (socket, uid, callback) { if (!socket.uid) { return callback(new Error('[[error:invalid-uid]]')); } - - user.getUserFields(uid, ['lastonline', 'status'], function (err, userData) { - if (err) { - return callback(err); + async.waterfall([ + function (next) { + user.getUserFields(uid, ['lastonline', 'status'], next); + }, + function (userData, next) { + next(null, user.getStatus(userData)); } - var status = user.getStatus(userData); - callback(null, status); - }); + ], callback); }; SocketUser.setStatus = function (socket, status, callback) { @@ -32,16 +35,19 @@ module.exports = function (SocketUser) { if (status !== 'offline') { data.lastonline = Date.now(); } - user.setUserFields(socket.uid, data, function (err) { - if (err) { - return callback(err); + + async.waterfall([ + function (next) { + user.setUserFields(socket.uid, data, next); + }, + function (next) { + var data = { + uid: socket.uid, + status: status + }; + websockets.server.emit('event:user_status_change', data); + next(null, data); } - var data = { - uid: socket.uid, - status: status - }; - websockets.server.emit('event:user_status_change', data); - callback(null, data); - }); + ], callback); }; }; \ No newline at end of file diff --git a/test/user.js b/test/user.js index bb834a0cb3..4c66e52298 100644 --- a/test/user.js +++ b/test/user.js @@ -2,8 +2,9 @@ var assert = require('assert'); var async = require('async'); -var db = require('./mocks/databasemock'); +var nconf = require('nconf'); +var db = require('./mocks/databasemock'); var User = require('../src/user'); var Topics = require('../src/topics'); var Categories = require('../src/categories'); @@ -428,6 +429,78 @@ describe('User', function () { }); }); }); + + it('should set user status', function (done) { + io.emit('user.setStatus', 'away', function (err, data) { + assert.ifError(err); + assert.equal(data.uid, uid); + assert.equal(data.status, 'away'); + done(); + }); + }); + + it('should fail for invalid status', function (done) { + io.emit('user.setStatus', '12345', function (err) { + assert.equal(err.message, '[[error:invalid-user-status]]'); + done(); + }); + }); + + it('should get user status', function (done) { + io.emit('user.checkStatus', uid, function (err, status) { + assert.ifError(err); + assert.equal(status, 'away'); + done(); + }); + }); + + it('should change user picture', function (done) { + io.emit('user.changePicture', {type: 'default', uid: uid}, function (err) { + assert.ifError(err); + User.getUserField(uid, 'picture', function (err, picture) { + assert.ifError(err); + assert.equal(picture, ''); + done(); + }); + }); + }); + + it('should upload profile picture', function (done) { + var path = require('path'); + var picture = { + path: path.join(nconf.get('base_dir'), 'public', 'logo.png'), + size: 7189, + name: 'logo.png' + }; + User.uploadPicture(uid, picture, function (err, uploadedPicture) { + assert.ifError(err); + assert.equal(uploadedPicture.url, '/uploads/profile/' + uid + '-profileimg.png'); + assert.equal(uploadedPicture.path, path.join(nconf.get('base_dir'), 'public', 'uploads', 'profile', uid + '-profileimg.png')); + done(); + }); + }); + + it('should get profile pictures', function (done) { + io.emit('user.getProfilePictures', {uid: uid}, function (err, data) { + assert.ifError(err); + assert(data); + assert(Array.isArray(data)); + assert.equal(data[0].type, 'uploaded'); + assert.equal(data[0].text, '[[user:uploaded_picture]]'); + done(); + }); + }); + + it('should remove uploaded picture', function (done) { + io.emit('user.removeUploadedPicture', {uid: uid}, function (err) { + assert.ifError(err); + User.getUserField(uid, 'uploadedpicture', function (err, uploadedpicture) { + assert.ifError(err); + assert.equal(uploadedpicture, ''); + done(); + }); + }); + }); }); describe('.getModerationHistory', function () { @@ -458,6 +531,9 @@ describe('User', function () { }); }); + + + after(function (done) { db.emptydb(done); });