From d9d47846cf1a75d32d1b5141968efe0cd139c49c Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Wed, 20 Sep 2023 22:57:15 +0600 Subject: [PATCH] Fix notifications (#3723) Signed-off-by: Denis Bykhov --- server-plugins/chunter-resources/src/index.ts | 14 +++-- .../notification-resources/src/index.ts | 51 ++++++++++--------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/server-plugins/chunter-resources/src/index.ts b/server-plugins/chunter-resources/src/index.ts index fc4ee79ef9..88d7fb10ec 100644 --- a/server-plugins/chunter-resources/src/index.ts +++ b/server-plugins/chunter-resources/src/index.ts @@ -25,6 +25,7 @@ import chunter, { import contact, { Employee, PersonAccount } from '@hcengineering/contact' import core, { Account, + AttachedDoc, Class, concatLink, Doc, @@ -49,11 +50,15 @@ import { getDocCollaborators, getMixinTx, pushNotification } from '@hcengineerin import { workbenchId } from '@hcengineering/workbench' import { getBacklinks } from './backlinks' -function getCreateBacklinksTxes (control: TriggerControl, txFactory: TxFactory, doc: Doc): Tx[] { +function getCreateBacklinksTxes ( + control: TriggerControl, + txFactory: TxFactory, + doc: Doc, + backlinkId: Ref, + backlinkClass: Ref> +): Tx[] { const txes: Tx[] = [] - const backlinkId = doc._id - const backlinkClass = doc._class const attachedDocId = doc._id const attributes = control.hierarchy.getAllAttributes(doc._class) @@ -334,8 +339,9 @@ async function BacklinksCreate (tx: Tx, control: TriggerControl): Promise const txFactory = new TxFactory(control.txFactory.account) const doc = TxProcessor.createDoc2Doc(ctx) + const collTx = tx as TxCollectionCUD - const txes: Tx[] = getCreateBacklinksTxes(control, txFactory, doc) + const txes: Tx[] = getCreateBacklinksTxes(control, txFactory, doc, collTx.objectId, collTx.objectClass) if (txes.length !== 0) { await control.apply(txes, true) } diff --git a/server-plugins/notification-resources/src/index.ts b/server-plugins/notification-resources/src/index.ts index 586ef383a2..0ffb87d78b 100644 --- a/server-plugins/notification-resources/src/index.ts +++ b/server-plugins/notification-resources/src/index.ts @@ -94,7 +94,8 @@ export async function OnBacklinkCreate (tx: Tx, control: TriggerControl): Promis ) res.push(collabTx) } - res = res.concat(await createCollabDocInfo([receiver._id], control, tx as TxCUD, doc)) + const actualTx = TxProcessor.extractTx(tx) as TxCUD + res = res.concat(await createCollabDocInfo([receiver._id], control, actualTx, tx as TxCUD, doc)) } return res } @@ -359,29 +360,26 @@ function fieldUpdated (field: string, ops: DocumentUpdate | MixinUpdate, - extractedTx: TxCUD -): boolean { +function isTypeMatched (control: TriggerControl, type: NotificationType, tx: TxCUD, originTx: TxCUD): boolean { const h = control.hierarchy const targetClass = h.getBaseClass(type.objectClass) if (!type.txClasses.includes(tx._class)) return false - if (!control.hierarchy.isDerived(h.getBaseClass(extractedTx.objectClass), targetClass)) return false - if (tx._class === core.class.TxCollectionCUD && type.attachedToClass !== undefined) { - if (!control.hierarchy.isDerived(h.getBaseClass(tx.objectClass), h.getBaseClass(type.attachedToClass))) return false + if (!control.hierarchy.isDerived(h.getBaseClass(tx.objectClass), targetClass)) return false + if (originTx._class === core.class.TxCollectionCUD && type.attachedToClass !== undefined) { + if (!control.hierarchy.isDerived(h.getBaseClass(originTx.objectClass), h.getBaseClass(type.attachedToClass))) { + return false + } } if (type.field !== undefined) { - if (extractedTx._class === core.class.TxUpdateDoc) { - if (!fieldUpdated(type.field, (extractedTx as TxUpdateDoc).operations)) return false + if (tx._class === core.class.TxUpdateDoc) { + if (!fieldUpdated(type.field, (tx as TxUpdateDoc).operations)) return false } - if (extractedTx._class === core.class.TxMixin) { - if (!fieldUpdated(type.field, (extractedTx as TxMixin).attributes)) return false + if (tx._class === core.class.TxMixin) { + if (!fieldUpdated(type.field, (tx as TxMixin).attributes)) return false } } if (type.txMatch !== undefined) { - const res = matchQuery([extractedTx], type.txMatch, extractedTx._class, control.hierarchy, true) + const res = matchQuery([tx], type.txMatch, tx._class, control.hierarchy, true) if (res.length === 0) return false } return true @@ -390,15 +388,15 @@ function isTypeMatched ( async function getMatchedTypes ( control: TriggerControl, tx: TxCUD, + originTx: TxCUD, isSpace: boolean = false ): Promise { const allTypes = (await control.modelDb.findAll(notification.class.NotificationType, {})).filter((p) => isSpace ? p.spaceSubscribe === true : p.spaceSubscribe !== true ) - const extractedTx = TxProcessor.extractTx(tx) as TxCUD const filtered: NotificationType[] = [] for (const type of allTypes) { - if (isTypeMatched(control, type, tx, extractedTx)) { + if (isTypeMatched(control, type, tx, originTx)) { filtered.push(type) } } @@ -413,13 +411,14 @@ interface NotifyResult { async function isShouldNotify ( control: TriggerControl, tx: TxCUD, + originTx: TxCUD, object: Doc, user: Ref, isSpace: boolean ): Promise { let allowed = false const emailTypes: NotificationType[] = [] - const types = await getMatchedTypes(control, tx, isSpace) + const types = await getMatchedTypes(control, tx, originTx, isSpace) for (const type of types) { if (type.allowedForAuthor !== true && tx.modifiedBy === user) continue if (control.hierarchy.hasMixin(type, serverNotification.mixin.TypeMatch)) { @@ -499,13 +498,14 @@ export function pushNotification ( async function getNotificationTxes ( control: TriggerControl, object: Doc, + tx: TxCUD, originTx: TxCUD, target: Ref, docUpdates: DocUpdates[], isSpace: boolean ): Promise { const res: Tx[] = [] - const allowed = await isShouldNotify(control, originTx, object, target, isSpace) + const allowed = await isShouldNotify(control, tx, originTx, object, target, isSpace) if (allowed.allowed) { pushNotification(control, res, target, object, originTx, docUpdates) } @@ -534,6 +534,7 @@ async function getNotificationTxes ( async function createCollabDocInfo ( collaborators: Ref[], control: TriggerControl, + tx: TxCUD, originTx: TxCUD, object: Doc, isSpace: boolean = false @@ -543,7 +544,7 @@ async function createCollabDocInfo ( const targets = new Set(collaborators) const docUpdates = await control.findAll(notification.class.DocUpdates, { attachedTo: object._id }) for (const target of targets) { - res = res.concat(await getNotificationTxes(control, object, originTx, target, docUpdates, isSpace)) + res = res.concat(await getNotificationTxes(control, object, tx, originTx, target, docUpdates, isSpace)) } return res } @@ -583,7 +584,7 @@ async function getSpaceCollabTxes ( if (mixin !== undefined) { const collabs = control.hierarchy.as(space, notification.mixin.Collaborators) if (collabs.collaborators !== undefined) { - return await createCollabDocInfo(collabs.collaborators, control, originTx, doc, true) + return await createCollabDocInfo(collabs.collaborators, control, tx, originTx, doc, true) } } return [] @@ -605,7 +606,7 @@ export async function createCollaboratorDoc ( const collaborators = await getDocCollaborators(doc, mixin, control) const mixinTx = getMixinTx(tx, control, collaborators) - const notificationTxes = await createCollabDocInfo(collaborators, control, originTx, doc) + const notificationTxes = await createCollabDocInfo(collaborators, control, tx, originTx, doc) res.push(mixinTx) res.push(...notificationTxes) } @@ -703,7 +704,7 @@ async function collectionCollabDoc (tx: TxCollectionCUD, contr if (doc !== undefined) { if (control.hierarchy.hasMixin(doc, notification.mixin.Collaborators)) { const collabMixin = control.hierarchy.as(doc, notification.mixin.Collaborators) - res = res.concat(await createCollabDocInfo(collabMixin.collaborators, control, tx, doc)) + res = res.concat(await createCollabDocInfo(collabMixin.collaborators, control, tx, tx, doc)) } } } @@ -807,12 +808,12 @@ async function updateCollaboratorDoc ( ) } res = res.concat( - await createCollabDocInfo([...collabMixin.collaborators, ...newCollaborators], control, originTx, doc) + await createCollabDocInfo([...collabMixin.collaborators, ...newCollaborators], control, tx, originTx, doc) ) } else { const collaborators = await getDocCollaborators(doc, mixin, control) res.push(getMixinTx(tx, control, collaborators)) - res = res.concat(await createCollabDocInfo(collaborators, control, originTx, doc)) + res = res.concat(await createCollabDocInfo(collaborators, control, tx, originTx, doc)) } res = res.concat(await getSpaceCollabTxes(control, doc, tx, originTx))