From 356dcc005a3c2eea8ad804527cb13d035cf53923 Mon Sep 17 00:00:00 2001 From: Kristina Date: Thu, 9 May 2024 12:49:46 +0400 Subject: [PATCH] Extract chat notifications trigger (#5558) Signed-off-by: Kristina Fefelova --- models/server-chunter/src/index.ts | 2 +- server-plugins/chunter-resources/src/index.ts | 58 ++++++++----------- server-plugins/chunter/src/index.ts | 2 +- 3 files changed, 27 insertions(+), 35 deletions(-) diff --git a/models/server-chunter/src/index.ts b/models/server-chunter/src/index.ts index cda4ede775..e7a2fea304 100644 --- a/models/server-chunter/src/index.ts +++ b/models/server-chunter/src/index.ts @@ -78,7 +78,7 @@ export function createModel (builder: Builder): void { }) builder.createDoc(serverCore.class.Trigger, core.space.Model, { - trigger: serverChunter.trigger.OnChatMessageCreate, + trigger: serverChunter.trigger.ChatNotificationsHandler, txMatch: { _class: core.class.TxCollectionCUD, 'tx._class': core.class.TxCreateDoc, diff --git a/server-plugins/chunter-resources/src/index.ts b/server-plugins/chunter-resources/src/index.ts index 9028db8629..552a747006 100644 --- a/server-plugins/chunter-resources/src/index.ts +++ b/server-plugins/chunter-resources/src/index.ts @@ -41,7 +41,7 @@ import core, { TxRemoveDoc, TxUpdateDoc } from '@hcengineering/core' -import notification, { ClassCollaborators, Collaborators, NotificationContent } from '@hcengineering/notification' +import notification, { Collaborators, NotificationContent } from '@hcengineering/notification' import { getMetadata, IntlString } from '@hcengineering/platform' import serverCore, { TriggerControl } from '@hcengineering/server-core' import { @@ -143,15 +143,25 @@ async function OnThreadMessageCreated (tx: Tx, control: TriggerControl): Promise return [lastReplyTx, employeeTx] } -async function updateCollaborators ( - targetDoc: Doc | undefined, - tx: TxCUD, - control: TriggerControl, - message: ChatMessage, - mixin?: ClassCollaborators -): Promise { - if (targetDoc === undefined || mixin === undefined) return [] +async function OnChatMessageCreated (tx: TxCUD, control: TriggerControl): Promise { const hierarchy = control.hierarchy + const actualTx = TxProcessor.extractTx(tx) as TxCreateDoc + + if ( + actualTx._class !== core.class.TxCreateDoc || + !hierarchy.isDerived(actualTx.objectClass, chunter.class.ChatMessage) + ) { + return [] + } + + const message = TxProcessor.createDoc2Doc(actualTx) + const mixin = hierarchy.classHierarchyMixin(message.attachedToClass, notification.mixin.ClassCollaborators) + + if (mixin === undefined) { + return [] + } + + const targetDoc = (await control.findAll(message.attachedToClass, { _id: message.attachedTo }, { limit: 1 }))[0] const isChannel = hierarchy.isDerived(targetDoc._class, chunter.class.Channel) const res: Tx[] = [] @@ -187,8 +197,7 @@ async function updateCollaborators ( return res } -async function OnChatMessageCreate (tx: TxCUD, control: TriggerControl): Promise { - const hierarchy = control.hierarchy +async function ChatNotificationsHandler (tx: TxCUD, control: TriggerControl): Promise { const actualTx = TxProcessor.extractTx(tx) as TxCreateDoc if (actualTx._class !== core.class.TxCreateDoc) { @@ -196,26 +205,8 @@ async function OnChatMessageCreate (tx: TxCUD, control: TriggerControl): Pr } const chatMessage = TxProcessor.createDoc2Doc(actualTx) - const cache = new Map, Doc>() - const mixin = hierarchy.classHierarchyMixin(chatMessage.attachedToClass, notification.mixin.ClassCollaborators) - - let targetDoc: Doc | undefined - - if (mixin !== undefined) { - targetDoc = (await control.findAll(chatMessage.attachedToClass, { _id: chatMessage.attachedTo }, { limit: 1 }))[0] - } - - if (targetDoc !== undefined) { - cache.set(targetDoc._id, targetDoc) - } - - const res = await Promise.all([ - createCollaboratorNotifications(control.ctx, tx, control, [chatMessage], undefined, cache), - updateCollaborators(targetDoc, tx, control, chatMessage, mixin) - ]) - - return res.flat() + return await createCollaboratorNotifications(control.ctx, tx, control, [chatMessage]) } function joinChannel (control: TriggerControl, channel: Channel, user: Ref): Tx[] { @@ -271,11 +262,12 @@ async function OnThreadMessageDeleted (tx: Tx, control: TriggerControl): Promise /** * @public */ -export async function ChunterTrigger (tx: Tx, control: TriggerControl): Promise { +export async function ChunterTrigger (tx: TxCUD, control: TriggerControl): Promise { const res = await Promise.all([ OnThreadMessageCreated(tx, control), OnThreadMessageDeleted(tx, control), - OnCollaboratorsChanged(tx as TxMixin, control) + OnCollaboratorsChanged(tx as TxMixin, control), + OnChatMessageCreated(tx, control) ]) return res.flat() } @@ -531,7 +523,7 @@ export default async () => ({ OnDirectMessageSent, OnChatMessageRemoved, OnChannelMembersChanged, - OnChatMessageCreate + ChatNotificationsHandler }, function: { CommentRemove, diff --git a/server-plugins/chunter/src/index.ts b/server-plugins/chunter/src/index.ts index 2f0b939094..9dc11a94c4 100644 --- a/server-plugins/chunter/src/index.ts +++ b/server-plugins/chunter/src/index.ts @@ -32,7 +32,7 @@ export default plugin(serverChunterId, { OnDirectMessageSent: '' as Resource, OnChatMessageRemoved: '' as Resource, OnChannelMembersChanged: '' as Resource, - OnChatMessageCreate: '' as Resource + ChatNotificationsHandler: '' as Resource }, function: { CommentRemove: '' as Resource,