Skip to content

Commit

Permalink
chore: add new msg stream for reported msgs (#31087)
Browse files Browse the repository at this point in the history
Co-authored-by: Diego Sampaio <[email protected]>
  • Loading branch information
MarcosSpessatto and sampaiodiego authored Dec 28, 2023
1 parent 371698e commit 846d749
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 18 deletions.
44 changes: 31 additions & 13 deletions apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,37 @@ const computation = Tracker.autorun(() => {
// remove thread refenrece from deleted message
ChatMessage.update({ tmid: msg._id }, { $unset: { tmid: 1 } }, { multi: true });
});
Notifications.onRoom(record.rid, 'deleteMessageBulk', ({ rid, ts, excludePinned, ignoreDiscussion, users }) => {
const query: Mongo.Selector<IMessage> = { rid, ts };
if (excludePinned) {
query.pinned = { $ne: true };
}
if (ignoreDiscussion) {
query.drid = { $exists: false };
}
if (users?.length) {
query['u.username'] = { $in: users };
}
ChatMessage.remove(query);
});
Notifications.onRoom(
record.rid,
'deleteMessageBulk',
({ rid, ts, excludePinned, ignoreDiscussion, users, ids, showDeletedStatus }) => {
const query: Mongo.Selector<IMessage> = { rid };

if (ids) {
query._id = { $in: ids };
} else {
query.ts = ts;
}
if (excludePinned) {
query.pinned = { $ne: true };
}
if (ignoreDiscussion) {
query.drid = { $exists: false };
}
if (users?.length) {
query['u.username'] = { $in: users };
}

if (showDeletedStatus) {
return ChatMessage.update(
query,
{ $set: { t: 'rm', msg: '', urls: [], mentions: [], attachments: [], reactions: {} } },
{ multi: true },
);
}
return ChatMessage.remove(query);
},
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ type NotifyRoomRidDeleteMessageBulkEvent = {
ignoreDiscussion: boolean;
ts: FieldExpression<Date>;
users: string[];
ids?: string[]; // message ids have priority over ts
showDeletedStatus?: boolean;
};

const createDeleteCriteria = (params: NotifyRoomRidDeleteMessageBulkEvent): ((message: IMessage) => boolean) => {
const query: Query<IMessage> = { ts: params.ts };
const query: Query<IMessage> = {};

if (params.ids) {
query._id = { $in: params.ids };
} else {
query.ts = params.ts;
}

if (params.excludePinned) {
query.pinned = { $ne: true };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ type NotifyRoomRidDeleteMessageBulkEvent = {
ignoreDiscussion: boolean;
ts: FieldExpression<Date>;
users: string[];
ids?: string[]; // message ids have priority over ts
showDeletedStatus?: boolean;
};

const createDeleteCriteria = (params: NotifyRoomRidDeleteMessageBulkEvent): ((message: IMessage) => boolean) => {
const query: Query<IMessage> = { ts: params.ts };
const query: Query<IMessage> = {};

if (params.ids) {
query._id = { $in: params.ids };
} else {
query.ts = params.ts;
}

if (params.excludePinned) {
query.pinned = { $ne: true };
Expand Down
25 changes: 23 additions & 2 deletions apps/meteor/server/lib/moderation/deleteReportedMessages.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { api } from '@rocket.chat/core-services';
import type { IUser, IMessage } from '@rocket.chat/core-typings';
import { Messages, Uploads, ReadReceipts } from '@rocket.chat/models';

Expand All @@ -7,8 +8,8 @@ import { settings } from '../../../app/settings/server';
// heavily inspired from message delete taking place in the user deletion process
// in this path we don't care about the apps engine events - it's a "raw" bulk action
export async function deleteReportedMessages(messages: IMessage[], user: IUser): Promise<void> {
const keepHistory = settings.get('Message_KeepHistory');
const showDeletedStatus = settings.get('Message_ShowDeletedStatus');
const keepHistory = settings.get<boolean>('Message_KeepHistory');
const showDeletedStatus = settings.get<boolean>('Message_ShowDeletedStatus');
const files: string[] = [];
const messageIds: string[] = [];
for (const message of messages) {
Expand Down Expand Up @@ -47,4 +48,24 @@ export async function deleteReportedMessages(messages: IMessage[], user: IUser):
if (showDeletedStatus) {
await Messages.setAsDeletedByIdsAndUser(messageIds, user as any);
}

const transformed = messages.reduce((acc, { rid, _id }) => {
if (!acc[rid]) {
acc[rid] = [];
}
acc[rid].push(_id);
return acc;
}, {} as Record<string, string[]>);

Object.entries(transformed).forEach(([rid, messageIds]) => {
void api.broadcast('notify.deleteMessageBulk', rid, {
rid,
excludePinned: true,
ignoreDiscussion: true,
ts: { $gt: new Date() },
users: [],
ids: messageIds,
showDeletedStatus,
});
});
}
12 changes: 11 additions & 1 deletion ee/packages/ddp-client/src/types/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,17 @@ export interface StreamerEvents {
{ key: `${string}/typing`; args: [username: string, typing: boolean] },
{
key: `${string}/deleteMessageBulk`;
args: [args: { rid: IMessage['rid']; excludePinned: boolean; ignoreDiscussion: boolean; ts: Record<string, Date>; users: string[] }];
args: [
args: {
rid: IMessage['rid'];
excludePinned: boolean;
ignoreDiscussion: boolean;
ts: Record<string, Date>;
users: string[];
ids?: string[]; // message ids have priority over ts
showDeletedStatus?: boolean;
},
];
},
{ key: `${string}/deleteMessage`; args: [{ _id: IMessage['_id'] }] },
{ key: `${string}/e2e.keyRequest`; args: [unknown] },
Expand Down
2 changes: 2 additions & 0 deletions packages/core-services/src/events/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export type EventSignatures = {
ignoreDiscussion: boolean;
ts: Record<string, Date>;
users: string[];
ids?: string[]; // message ids have priority over ts
showDeletedStatus?: boolean;
},
): void;
'notify.deleteCustomSound'(data: { soundData: ICustomSound }): void;
Expand Down

0 comments on commit 846d749

Please sign in to comment.