Skip to content

Commit

Permalink
chore: adapt discussion feat to work without message events watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosSpessatto committed Sep 15, 2023
1 parent 67b075b commit 28419b8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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<void> => {
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
Expand All @@ -25,7 +40,7 @@ callbacks.add(
return message;
}

await Messages.refreshDiscussionMetadata(room);
await updateAndNotifyParentRoomWithParentMessage(room);

return message;
},
Expand All @@ -45,7 +60,7 @@ callbacks.add(
});

if (room) {
await Messages.refreshDiscussionMetadata(room);
await updateAndNotifyParentRoomWithParentMessage(room);
}
}
if (message.drid) {
Expand Down
8 changes: 4 additions & 4 deletions apps/meteor/app/discussion/server/methods/createDiscussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
7 changes: 6 additions & 1 deletion apps/meteor/app/lib/server/lib/notifyUsersOnMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
17 changes: 11 additions & 6 deletions apps/meteor/server/models/raw/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
UpdateResult,
Document,
UpdateFilter,
ModifyResult,
} from 'mongodb';

import { otrSystemMessages } from '../../../app/otr/lib/constants';
Expand Down Expand Up @@ -1593,19 +1594,23 @@ export class MessagesRaw extends BaseRaw<IMessage> 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<IRoom, '_id' | 'msgs' | 'lm'>): Promise<UpdateResult | Document | false> {
async refreshDiscussionMetadata(room: Pick<IRoom, '_id' | 'msgs' | 'lm'>): Promise<ModifyResult<IMessage>> {
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' },
);
}

// //////////////////////////////////////////////////////////////////
Expand Down
3 changes: 2 additions & 1 deletion packages/model-typings/src/models/IMessagesModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
UpdateResult,
Document,
Filter,
ModifyResult,
} from 'mongodb';

import type { FindPaginated, IBaseModel } from './IBaseModel';
Expand Down Expand Up @@ -252,7 +253,7 @@ export interface IMessagesModel extends IBaseModel<IMessage> {
getMessageByFileId(fileID: string): Promise<IMessage | null>;
setThreadMessagesAsRead(tmid: string, until: Date): Promise<UpdateResult | Document>;
updateRepliesByThreadId(tmid: string, replies: string[], ts: Date): Promise<UpdateResult>;
refreshDiscussionMetadata(room: Pick<IRoom, '_id' | 'msgs' | 'lm'>): Promise<UpdateResult | Document | false>;
refreshDiscussionMetadata(room: Pick<IRoom, '_id' | 'msgs' | 'lm'>): Promise<ModifyResult<IMessage>>;
findUnreadThreadMessagesByDate(
tmid: string,
userId: string,
Expand Down

0 comments on commit 28419b8

Please sign in to comment.