diff --git a/apps/meteor/app/discussion/server/hooks/propagateDiscussionMetadata.ts b/apps/meteor/app/discussion/server/hooks/propagateDiscussionMetadata.ts index b6054b6dcccf..485bc9f19506 100644 --- a/apps/meteor/app/discussion/server/hooks/propagateDiscussionMetadata.ts +++ b/apps/meteor/app/discussion/server/hooks/propagateDiscussionMetadata.ts @@ -1,8 +1,23 @@ +import type { IRoom } from '@rocket.chat/core-typings'; import { Messages, Rooms } from '@rocket.chat/models'; import { callbacks } from '../../../../lib/callbacks'; +import { broadcastEventToServices } from '../../../../server/lib/isRunningMs'; +import { broadcastMessageSentEvent } from '../../../../server/modules/watchers/lib/messages'; import { deleteRoom } from '../../../lib/server/functions/deleteRoom'; +const updateAndNotifyParentRoomWithParentMessage = async (room: IRoom): Promise => { + const { value: parentMessage } = await Messages.refreshDiscussionMetadata(room); + if (!parentMessage) { + return; + } + void broadcastMessageSentEvent({ + id: parentMessage._id, + data: parentMessage, + broadcastCallback: (message) => broadcastEventToServices('message.sent', message), + }); +}; + /** * We need to propagate the writing of new message in a discussion to the linking * system message @@ -25,7 +40,7 @@ callbacks.add( return message; } - await Messages.refreshDiscussionMetadata(room); + await updateAndNotifyParentRoomWithParentMessage(room); return message; }, @@ -45,7 +60,7 @@ callbacks.add( }); if (room) { - await Messages.refreshDiscussionMetadata(room); + await updateAndNotifyParentRoomWithParentMessage(room); } } if (message.drid) { diff --git a/apps/meteor/app/discussion/server/methods/createDiscussion.ts b/apps/meteor/app/discussion/server/methods/createDiscussion.ts index ce5c09947a60..1912de5e7cc5 100644 --- a/apps/meteor/app/discussion/server/methods/createDiscussion.ts +++ b/apps/meteor/app/discussion/server/methods/createDiscussion.ts @@ -186,13 +186,13 @@ const create = async ({ discussionMsg = await createDiscussionMessage(prid, user, discussion._id, discussionName); } - if (discussionMsg) { - callbacks.runAsync('afterSaveMessage', discussionMsg, parentRoom); - } - if (reply) { await sendMessage(user, { msg: reply }, discussion); } + + if (discussionMsg) { + callbacks.runAsync('afterSaveMessage', discussionMsg, parentRoom); + } return discussion; }; diff --git a/apps/meteor/app/lib/server/lib/notifyUsersOnMessage.js b/apps/meteor/app/lib/server/lib/notifyUsersOnMessage.js index 8c70ee3f9a4f..b55a272de558 100644 --- a/apps/meteor/app/lib/server/lib/notifyUsersOnMessage.js +++ b/apps/meteor/app/lib/server/lib/notifyUsersOnMessage.js @@ -191,4 +191,9 @@ export async function notifyUsersOnMessage(message, room) { return message; } -callbacks.add('afterSaveMessage', (message, room) => notifyUsersOnMessage(message, room), callbacks.priority.LOW, 'notifyUsersOnMessage'); +callbacks.add( + 'afterSaveMessage', + (message, room) => notifyUsersOnMessage(message, room), + callbacks.priority.MEDIUM, + 'notifyUsersOnMessage', +); diff --git a/apps/meteor/server/models/raw/Messages.ts b/apps/meteor/server/models/raw/Messages.ts index 49930a8c92ea..93ef8982a604 100644 --- a/apps/meteor/server/models/raw/Messages.ts +++ b/apps/meteor/server/models/raw/Messages.ts @@ -27,6 +27,7 @@ import type { UpdateResult, Document, UpdateFilter, + ModifyResult, } from 'mongodb'; import { otrSystemMessages } from '../../../app/otr/lib/constants'; @@ -1593,19 +1594,23 @@ export class MessagesRaw extends BaseRaw implements IMessagesModel { * to race conditions: If multiple updates occur, the current state will be updated * only if the new state of the discussion room is really newer. */ - async refreshDiscussionMetadata(room: Pick): Promise { + async refreshDiscussionMetadata(room: Pick): Promise> { const { _id: drid, msgs: dcount, lm: dlm } = room; const query = { drid, }; - return this.updateMany(query, { - $set: { - dcount, - dlm, + return this.col.findOneAndUpdate( + query, + { + $set: { + dcount, + dlm, + }, }, - }); + { returnDocument: 'after' }, + ); } // ////////////////////////////////////////////////////////////////// diff --git a/packages/model-typings/src/models/IMessagesModel.ts b/packages/model-typings/src/models/IMessagesModel.ts index d681405850fa..9b7680b56120 100644 --- a/packages/model-typings/src/models/IMessagesModel.ts +++ b/packages/model-typings/src/models/IMessagesModel.ts @@ -18,6 +18,7 @@ import type { UpdateResult, Document, Filter, + ModifyResult, } from 'mongodb'; import type { FindPaginated, IBaseModel } from './IBaseModel'; @@ -252,7 +253,7 @@ export interface IMessagesModel extends IBaseModel { getMessageByFileId(fileID: string): Promise; setThreadMessagesAsRead(tmid: string, until: Date): Promise; updateRepliesByThreadId(tmid: string, replies: string[], ts: Date): Promise; - refreshDiscussionMetadata(room: Pick): Promise; + refreshDiscussionMetadata(room: Pick): Promise>; findUnreadThreadMessagesByDate( tmid: string, userId: string,