diff --git a/src/messaging/rooms.js b/src/messaging/rooms.js index 5ebb81c5ed..5dbd834f03 100644 --- a/src/messaging/rooms.js +++ b/src/messaging/rooms.js @@ -111,6 +111,9 @@ module.exports = function (Messaging) { }; Messaging.leaveRoom = async (uids, roomId) => { + const isInRoom = await Promise.all(uids.map(uid => Messaging.isUserInRoom(uid, roomId))); + uids = uids.filter((uid, index) => isInRoom[index]); + const keys = uids .map(uid => 'uid:' + uid + ':chat:rooms') .concat(uids.map(uid => 'uid:' + uid + ':chat:rooms:unread')); @@ -125,6 +128,9 @@ module.exports = function (Messaging) { }; Messaging.leaveRooms = async (uid, roomIds) => { + const isInRoom = await Promise.all(roomIds.map(roomId => Messaging.isUserInRoom(uid, roomId))); + roomIds = roomIds.filter((roomId, index) => isInRoom[index]); + const roomKeys = roomIds.map(roomId => 'chat:room:' + roomId + ':uids'); await Promise.all([ db.sortedSetsRemove(roomKeys, uid), diff --git a/test/messaging.js b/test/messaging.js index 27d83c5bec..cc2b101676 100644 --- a/test/messaging.js +++ b/test/messaging.js @@ -222,6 +222,15 @@ describe('Messaging Library', function () { }); }); + it('should send not a user-leave system message when a user tries to leave a room they are not in', async () => { + await socketModules.chats.leave({ uid: bazUid }, roomId); + const messages = await socketModules.chats.getMessages({ uid: fooUid }, { uid: fooUid, roomId: roomId, start: 0 }); + assert.equal(messages.length, 4); + const message = messages.pop(); + assert.strictEqual(message.system, true); + assert.strictEqual(message.content, 'user-leave'); + }); + it('should change owner when owner leaves room', function (done) { socketModules.chats.newRoom({ uid: herpUid }, { touid: fooUid }, function (err, roomId) { assert.ifError(err);