diff --git a/public/language/en-GB/modules.json b/public/language/en-GB/modules.json index 5104549457..b6f970b8e4 100644 --- a/public/language/en-GB/modules.json +++ b/public/language/en-GB/modules.json @@ -32,6 +32,7 @@ "chat.leave-prompt": "Are you sure you wish to leave this chat?", "chat.leave-help": "Leaving this chat will remove you from future correspondence in this chat. If you are re-added in the future, you will not see any chat history from prior to your re-joining.", "chat.in-room": "In this room", + "chat.kick": "Kick", "composer.compose": "Compose", "composer.show_preview": "Show Preview", diff --git a/public/src/client/chats.js b/public/src/client/chats.js index 4b1e3b0fd5..51651853b9 100644 --- a/public/src/client/chats.js +++ b/public/src/client/chats.js @@ -189,6 +189,7 @@ define('forum/chats', [ modal.attr('component', 'chat/manage-modal'); Chats.refreshParticipantsList(roomId, modal); + Chats.addKickHandler(roomId, modal); var searchInput = modal.find('input'); var errorEl = modal.find('.text-danger'); @@ -215,6 +216,23 @@ define('forum/chats', [ }); }; + Chats.addKickHandler = function (roomId, modal) { + modal.on('click', '[data-action="kick"]', function () { + var uid = parseInt(this.getAttribute('data-uid'), 10); + + socket.emit('modules.chats.removeUserFromRoom', { + roomId: roomId, + uid: uid, + }, function (err) { + if (err) { + return app.alertError(err.message); + } + + Chats.refreshParticipantsList(roomId, modal); + }); + }); + }; + Chats.addLeaveHandler = function (roomId, buttonEl) { buttonEl.on('click', function () { bootbox.confirm({ @@ -254,7 +272,7 @@ define('forum/chats', [ }); } - Benchpress.parse('partials/modals/manage_room_users', { + app.parseAndTranslate('partials/modals/manage_room_users', { users: users, }, function (html) { listEl.html(html); diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js index e3e24654a4..99365164f2 100644 --- a/src/socket.io/modules.js +++ b/src/socket.io/modules.js @@ -193,7 +193,21 @@ SocketModules.chats.getUsersInRoom = function (socket, data, callback) { return callback(new Error('[[error:invalid-data]]')); } - Messaging.getUsersInRoom(data.roomId, 0, -1, callback); + async.parallel({ + users: async.apply(Messaging.getUsersInRoom, data.roomId, 0, -1), + isOwner: async.apply(Messaging.isRoomOwner, socket.uid, data.roomId), + }, function (err, payload) { + if (err) { + return callback(err); + } + + payload.users = payload.users.map((user) => { + user.canKick = payload.isOwner; + return user; + }); + + callback(null, payload.users); + }); }; SocketModules.chats.addUserToRoom = function (socket, data, callback) { @@ -250,16 +264,17 @@ SocketModules.chats.removeUserFromRoom = function (socket, data, callback) { if (!data || !data.roomId) { return callback(new Error('[[error:invalid-data]]')); } + async.waterfall([ function (next) { - user.getUidByUsername(data.username, next); + user.exists(data.uid, next); }, - function (uid, next) { - if (!uid) { + function (exists, next) { + if (!exists) { return next(new Error('[[error:no-user]]')); } - Messaging.removeUsersFromRoom(socket.uid, [uid], data.roomId, next); + Messaging.removeUsersFromRoom(socket.uid, [data.uid], data.roomId, next); }, ], callback); }; diff --git a/test/messaging.js b/test/messaging.js index d026ece475..26ae4d867c 100644 --- a/test/messaging.js +++ b/test/messaging.js @@ -234,8 +234,8 @@ describe('Messaging Library', function () { }); it('should fail to remove user from room if user does not exist', function (done) { - socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, username: 'doesnotexist' }, function (err) { - assert.equal(err.message, '[[error:no-user]]'); + socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, uid: 99 }, function (err) { + assert.equal('[[error:no-user]]', err.message); done(); }); }); @@ -246,11 +246,11 @@ describe('Messaging Library', function () { Messaging.isUserInRoom(herpUid, roomId, function (err, isInRoom) { assert.ifError(err); assert(isInRoom); - socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, username: 'herp' }, function (err) { + socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, uid: herpUid }, function (err) { assert.equal(err.message, '[[error:cant-remove-last-user]]'); socketModules.chats.addUserToRoom({ uid: fooUid }, { roomId: roomId, username: 'baz' }, function (err) { assert.ifError(err); - socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, username: 'herp' }, function (err) { + socketModules.chats.removeUserFromRoom({ uid: fooUid }, { roomId: roomId, uid: herpUid }, function (err) { assert.ifError(err); Messaging.isUserInRoom(herpUid, roomId, function (err, isInRoom) { assert.ifError(err);