Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add new msg stream for reported msgs #31087

Merged
merged 8 commits into from
Dec 28, 2023
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
Loading