fix: deleted chat messages (#11962)

* fix: deleted chat messages

* fix spec
isekai-main
Barış Soner Uşaklı 1 year ago committed by GitHub
parent b25793cd41
commit 1a1fd64d9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -50,6 +50,8 @@ MessageObject:
type: number type: number
messageId: messageId:
type: number type: number
isOwner:
type: boolean
fromUser: fromUser:
type: object type: object
properties: properties:

@ -52,6 +52,10 @@ module.exports = function (Messaging) {
if (msg) { if (msg) {
msg.messageId = parseInt(mids[idx], 10); msg.messageId = parseInt(mids[idx], 10);
msg.ip = undefined; 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; return msg;
}) })

@ -28,9 +28,13 @@ module.exports = function (Messaging) {
// Propagate this change to users in the room // Propagate this change to users in the room
const messages = await Messaging.getMessagesData([mid], uid, roomId, true); const messages = await Messaging.getMessagesData([mid], uid, roomId, true);
sockets.in(`chat_room_${roomId}`).emit('event:chats.edit', { if (messages[0]) {
messages: messages, 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', { plugins.hooks.fire('action:messaging.edit', {
message: { ...messages[0], content: payload.content }, message: { ...messages[0], content: payload.content },
}); });

@ -55,10 +55,6 @@ Messaging.getMessages = async (params) => {
const messageData = await Messaging.getMessagesData(mids, uid, roomId, isNew); const messageData = await Messaging.getMessagesData(mids, uid, roomId, isNew);
messageData.forEach((msg) => { messageData.forEach((msg) => {
msg.index = indices[msg.messageId.toString()]; 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; return messageData;

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

@ -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 () => { it('should error out if a message is deleted again', async () => {
const { statusCode, body } = await callv3API('delete', `/chats/${roomId}/messages/${mid}`, {}, 'foo'); const { statusCode, body } = await callv3API('delete', `/chats/${roomId}/messages/${mid}`, {}, 'foo');
assert.strictEqual(statusCode, 400); assert.strictEqual(statusCode, 400);

Loading…
Cancel
Save