mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
fix: #8133, check if user is in room before removing
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user