Skip to content

Commit

Permalink
fix: Notifying the client when we remove a message from a thread
Browse files Browse the repository at this point in the history
Now, everytime a message has a tmid(is a thread), and it gets deleted, we decrease the replyCount of the parent message
then remove the unread thread unread, to make sure all the subscriptions are up to date (and notify to the client)
At the end, we check if the parent message had all of its thread messages deleted, if so, we notify the change in the parent message itself
  • Loading branch information
Gustrb committed Dec 11, 2024
1 parent 6799f09 commit baa7fcf
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions apps/meteor/app/lib/server/functions/deleteMessage.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { AppEvents, Apps } from '@rocket.chat/apps';
import { api, Message } from '@rocket.chat/core-services';
import type { AtLeast, IMessage, IUser } from '@rocket.chat/core-typings';
import { Messages, Rooms, Uploads, Users, ReadReceipts } from '@rocket.chat/models';
import type { AtLeast, IMessage, IRoom, IUser } from '@rocket.chat/core-typings';
import { Messages, Rooms, Uploads, Users, ReadReceipts, Subscriptions } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';

import { callbacks } from '../../../../lib/callbacks';
import { canDeleteMessageAsync } from '../../../authorization/server/functions/canDeleteMessage';
import { FileUpload } from '../../../file-upload/server';
import { settings } from '../../../settings/server';
import { notifyOnRoomChangedById, notifyOnMessageChange } from '../lib/notifyListener';
import { notifyOnRoomChangedById, notifyOnMessageChange, notifyOnSubscriptionChangedByRoomIdAndUserId } from '../lib/notifyListener';

export const deleteMessageValidatingPermission = async (message: AtLeast<IMessage, '_id'>, userId: IUser['_id']): Promise<void> => {
if (!message?._id) {
Expand Down Expand Up @@ -51,7 +51,7 @@ export async function deleteMessage(message: IMessage, user: IUser): Promise<voi
}

if (deletedMsg?.tmid) {
await Messages.decreaseReplyCountById(deletedMsg.tmid, -1);
await deleteThreadMessage(deletedMsg as Required<Pick<IMessage, 'tmid'>>, user, room);
}

const files = (message.files || [message.file]).filter(Boolean); // Keep compatibility with old messages
Expand Down Expand Up @@ -107,3 +107,22 @@ export async function deleteMessage(message: IMessage, user: IUser): Promise<voi
void bridges.getListenerBridge().messageEvent(AppEvents.IPostMessageDeleted, deletedMsg, user);
}
}

async function deleteThreadMessage(message: Required<Pick<IMessage, 'tmid'>>, user: IUser, room: IRoom | null): Promise<void> {
await Messages.decreaseReplyCountById(message.tmid, -1);

if (room) {
const { modifiedCount } = await Subscriptions.removeUnreadThreadsByRoomId(room._id, [message.tmid]);
if (modifiedCount > 0) {
void notifyOnSubscriptionChangedByRoomIdAndUserId(room._id, user._id);
}
}

// Check if this was the last reply in the thread
const thread = await Messages.findOneById(message.tmid, { projection: { tcount: 1 } });
if (thread?.tcount === 0) {
void notifyOnMessageChange({
id: message.tmid,
});
}
}

0 comments on commit baa7fcf

Please sign in to comment.