From 00633cda71a4393aaaab36d2deeda600e2ed12e4 Mon Sep 17 00:00:00 2001 From: Kristina Date: Fri, 9 Aug 2024 14:39:53 +0400 Subject: [PATCH] Try to fix double notification contexts for pull requests (#6300) Signed-off-by: Kristina Fefelova --- .../notification-resources/src/index.ts | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/server-plugins/notification-resources/src/index.ts b/server-plugins/notification-resources/src/index.ts index a881e5bd1c..b48118131d 100644 --- a/server-plugins/notification-resources/src/index.ts +++ b/server-plugins/notification-resources/src/index.ts @@ -323,8 +323,7 @@ export async function pushInboxNotifications ( modifiedOn: Timestamp, shouldUpdateTimestamp = true ): Promise | undefined> { - const context = contexts.find((context) => context.user === receiver._id && context.objectId === objectId) - + const context = getDocNotifyContext(control, contexts, objectId, receiver._id) let docNotifyContextId: Ref if (context === undefined) { @@ -732,9 +731,7 @@ export async function getNotificationTxes ( ) } } else { - const context = docNotifyContexts.find( - (context) => context.objectId === message.attachedTo && context.user === receiver.account._id - ) + const context = getDocNotifyContext(control, docNotifyContexts, message.attachedTo, receiver.account._id) if (context === undefined) { await createNotifyContext( @@ -1584,7 +1581,7 @@ async function updateCollaborators (ctx: MeasureContext, control: TriggerControl for (const addedUser of addedInfo) { const info = toReceiverInfo(hierarchy, addedUser) if (info === undefined) continue - const context = contexts.find(({ user }) => user === info._id) + const context = getDocNotifyContext(control, contexts, objectId, info._id) if (context !== undefined) continue await createNotifyContext(control, objectId, objectClass, objectSpace, info) } @@ -1701,6 +1698,31 @@ export async function getCollaborators ( } } +function getDocNotifyContext ( + control: TriggerControl, + contexts: DocNotifyContext[], + objectId: Ref, + user: Ref +): DocNotifyContext | undefined { + const context = contexts.find((it) => it.objectId === objectId && it.user === user) + + if (context !== undefined) { + return context + } + + const txes = [...control.txes.apply, ...control.txes.result, ...control.operationContext.derived.txes] as TxCUD[] + + for (const tx of txes) { + if (tx._class === core.class.TxCreateDoc && tx.objectClass === notification.class.DocNotifyContext) { + const doc = TxProcessor.createDoc2Doc(tx as TxCreateDoc) + if (doc.objectId === objectId && doc.user === user) { + return doc + } + } + } + return undefined +} + async function OnActivityMessageRemove (message: ActivityMessage, control: TriggerControl): Promise { if (control.removedMap.has(message.attachedTo)) { return []