Skip to content

Commit

Permalink
fix: deleted chat messages (#11962)
Browse files Browse the repository at this point in the history
* fix: deleted chat messages

* fix spec
  • Loading branch information
barisusakli committed Aug 28, 2023
1 parent b25793c commit 1a1fd64
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 13 deletions.
2 changes: 2 additions & 0 deletions public/openapi/components/schemas/Chats.yaml
Expand Up @@ -50,6 +50,8 @@ MessageObject:
type: number
messageId:
type: number
isOwner:
type: boolean
fromUser:
type: object
properties:
Expand Down
4 changes: 4 additions & 0 deletions src/messaging/data.js
Expand Up @@ -52,6 +52,10 @@ module.exports = function (Messaging) {
if (msg) {
msg.messageId = parseInt(mids[idx], 10);
msg.ip = undefined;
msg.isOwner = msg.fromuid === parseInt(uid, 10);
if (msg.deleted && !msg.isOwner) {
msg.content = `<p>[[modules:chat.message-deleted]]</p>`;
}
}
return msg;
})
Expand Down
10 changes: 7 additions & 3 deletions src/messaging/edit.js
Expand Up @@ -28,9 +28,13 @@ module.exports = function (Messaging) {

// Propagate this change to users in the room
const messages = await Messaging.getMessagesData([mid], uid, roomId, true);
sockets.in(`chat_room_${roomId}`).emit('event:chats.edit', {
messages: messages,
});
if (messages[0]) {
const roomName = messages[0].deleted ? `uid_${uid}` : `chat_room_${roomId}`;
sockets.in(roomName).emit('event:chats.edit', {
messages: messages,
});
}

plugins.hooks.fire('action:messaging.edit', {
message: { ...messages[0], content: payload.content },
});
Expand Down
4 changes: 0 additions & 4 deletions src/messaging/index.js
Expand Up @@ -55,10 +55,6 @@ Messaging.getMessages = async (params) => {
const messageData = await Messaging.getMessagesData(mids, uid, roomId, isNew);
messageData.forEach((msg) => {
msg.index = indices[msg.messageId.toString()];
msg.isOwner = msg.fromuid === parseInt(uid, 10);
if (msg.deleted && !msg.isOwner) {
msg.content = `<p>[[modules:chat.message-deleted]]</p>`;
}
});

return messageData;
Expand Down
11 changes: 5 additions & 6 deletions src/socket.io/index.js
@@ -1,5 +1,6 @@
'use strict';

const _ = require('lodash');
const os = require('os');
const nconf = require('nconf');
const winston = require('winston');
Expand Down Expand Up @@ -301,18 +302,16 @@ Sockets.getUidsInRoom = async function (room) {
return [];
}
const ioRoom = Sockets.server.in(room);
const uids = {};
const uids = [];
if (ioRoom) {
const sockets = await ioRoom.fetchSockets();
for (const s of sockets) {
for (const r of s.rooms) {
if (r.startsWith('uid_')) {
uids[r.split('_').pop()] = 1;
}
if (s && s.data && s.data.uid > 0) {
uids.push(s.data.uid);
}
}
}
return Object.keys(uids);
return _.uniq(uids);
};

Sockets.warnDeprecated = (socket, replacement) => {
Expand Down
7 changes: 7 additions & 0 deletions test/messaging.js
Expand Up @@ -659,6 +659,13 @@ describe('Messaging Library', () => {
});
});

it('should not show deleted message to other users', async () => {
const { body } = await callv3API('get', `/chats/${roomId}/messages/${mid}`, {}, 'herp');
const message = body.response;
assert.strictEqual(message.deleted, 1);
assert.strictEqual(message.content, '<p>[[modules:chat.message-deleted]]</p>');
});

it('should error out if a message is deleted again', async () => {
const { statusCode, body } = await callv3API('delete', `/chats/${roomId}/messages/${mid}`, {}, 'foo');
assert.strictEqual(statusCode, 400);
Expand Down

0 comments on commit 1a1fd64

Please sign in to comment.