diff --git a/public/openapi/components/schemas/Chats.yaml b/public/openapi/components/schemas/Chats.yaml index 36404313ff..be0dfb681b 100644 --- a/public/openapi/components/schemas/Chats.yaml +++ b/public/openapi/components/schemas/Chats.yaml @@ -50,6 +50,8 @@ MessageObject: type: number messageId: type: number + isOwner: + type: boolean fromUser: type: object properties: diff --git a/src/messaging/data.js b/src/messaging/data.js index 94d9c37e8c..0ccaf1aec1 100644 --- a/src/messaging/data.js +++ b/src/messaging/data.js @@ -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 = `

[[modules:chat.message-deleted]]

`; + } } return msg; }) diff --git a/src/messaging/edit.js b/src/messaging/edit.js index 438d43685a..5bf76b65f8 100644 --- a/src/messaging/edit.js +++ b/src/messaging/edit.js @@ -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 }, }); diff --git a/src/messaging/index.js b/src/messaging/index.js index c5e9dfe9d0..e3cf3deab2 100644 --- a/src/messaging/index.js +++ b/src/messaging/index.js @@ -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 = `

[[modules:chat.message-deleted]]

`; - } }); return messageData; diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 80fe78271a..d55b73b37c 100644 --- a/src/socket.io/index.js +++ b/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'); @@ -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) => { diff --git a/test/messaging.js b/test/messaging.js index c829eb5077..395d0f8764 100644 --- a/test/messaging.js +++ b/test/messaging.js @@ -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, '

[[modules:chat.message-deleted]]

'); + }); + 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);