diff --git a/public/language/en_GB/error.json b/public/language/en_GB/error.json index deba829970..1625aef3a5 100644 --- a/public/language/en_GB/error.json +++ b/public/language/en_GB/error.json @@ -103,6 +103,7 @@ "invalid-chat-message": "Invalid chat message", "chat-message-too-long": "Chat message is too long", "cant-edit-chat-message": "You are not allowed to edit this message", + "cant-remove-last-user": "You can't remove the last user", "reputation-system-disabled": "Reputation system is disabled.", "downvoting-disabled": "Downvoting is disabled", diff --git a/public/src/client/chats.js b/public/src/client/chats.js index 9d1c9e348f..7f245bece4 100644 --- a/public/src/client/chats.js +++ b/public/src/client/chats.js @@ -231,11 +231,22 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', }); tagEl.on('beforeItemRemove', function(event) { - event.cancel = !data.isOwner; + event.cancel = !data.isOwner || data.users.length < 2; + if (!data.owner) { + return app.alertError('[[error:not-allowed]]'); + } + + if (data.users.length < 2) { + return app.alertError('[[error:cant-remove-last-user]]'); + } }); tagEl.on('itemRemoved', function(event) { - socket.emit('modules.chats.removeUserFromRoom', {roomId: data.roomId, username: event.item}); + socket.emit('modules.chats.removeUserFromRoom', {roomId: data.roomId, username: event.item}, function(err) { + if (err) { + return app.alertError(err.message); + } + }); }); var input = $('.users-tag-container').find('.bootstrap-tagsinput input'); diff --git a/src/controllers/accounts/chats.js b/src/controllers/accounts/chats.js index 9b5342f7ab..0b26d510bc 100644 --- a/src/controllers/accounts/chats.js +++ b/src/controllers/accounts/chats.js @@ -58,7 +58,7 @@ chatsController.get = function(req, res, callback) { room.isOwner = parseInt(room.owner, 10) === parseInt(req.uid, 10); room.users = data.users.filter(function(user) { - return user && parseInt(user.uid, 10) !== req.uid; + return user && parseInt(user.uid, 10) && parseInt(user.uid, 10) !== req.uid; }); room.usernames = data.users.map(function(user) { diff --git a/src/messaging/rooms.js b/src/messaging/rooms.js index 35d9af4172..4f8c9861db 100644 --- a/src/messaging/rooms.js +++ b/src/messaging/rooms.js @@ -56,6 +56,10 @@ module.exports = function(Messaging) { db.exists('chat:room:' + roomId + ':uids', callback); }; + Messaging.getUserCountInRoom = function(roomId, callback) { + db.sortedSetCard('chat:room:' + roomId + ':uids', callback); + }; + Messaging.isRoomOwner = function(uid, roomId, callback) { db.getObjectField('chat:room:' + roomId, 'owner', function(err, owner) { if (err) { @@ -87,12 +91,19 @@ module.exports = function(Messaging) { Messaging.removeUsersFromRoom = function(uid, uids, roomId, callback) { async.waterfall([ function (next) { - Messaging.isRoomOwner(uid, roomId, next); + async.parallel({ + isOwner: async.apply(Messaging.isRoomOwner, uid, roomId), + userCount: async.apply(Messaging.getUserCountInRoom, roomId) + }, next); }, - function (isOwner, next) { - if (!isOwner) { + function (results, next) { + if (!results.isOwner) { return next(new Error('[[error:cant-add-users-to-chat-room]]')); } + if (results.userCount === 2) { + return next(new Error('[[error:cant-remove-last-user]]')); + } + db.sortedSetRemove('chat:room:' + roomId + ':uids', uids, next); }, function (next) {