diff --git a/server-plugins/notification-resources/src/index.ts b/server-plugins/notification-resources/src/index.ts index 9d41c6ccb1c..46eac8355f2 100644 --- a/server-plugins/notification-resources/src/index.ts +++ b/server-plugins/notification-resources/src/index.ts @@ -98,6 +98,10 @@ export async function OnBacklinkCreate ( return [] } + if (sender === receiver) { + return [] + } + const backlink = TxProcessor.createDoc2Doc(tx.tx as TxCreateDoc) if (!hierarchy.isDerived(backlink.backlinkClass, activity.class.ActivityMessage)) { @@ -118,15 +122,9 @@ export async function OnBacklinkCreate ( return [] } - const doc = (await control.findAll(message.attachedToClass, { _id: message.attachedTo }))[0] - - if (doc === undefined) { - return [] - } - - let res: Tx[] = [] - + const res: Tx[] = [] const collabMixin = hierarchy.as(message as Doc, notification.mixin.Collaborators) + if (collabMixin.collaborators === undefined || !collabMixin.collaborators.includes(receiver._id)) { const collabTx = control.txFactory.createTxMixin( message._id, @@ -142,18 +140,45 @@ export async function OnBacklinkCreate ( res.push(collabTx) } - const messageTx = ( - await control.findAll(core.class.TxCollectionCUD, { - 'tx.objectId': message._id, - 'tx._class': core.class.TxCreateDoc - }) - )[0] + return res +} + +async function isBacklinkNotified (tx: TxCollectionCUD, control: TriggerControl): Promise { + const receiver = await getPersonAccount(tx.objectId as Ref, control) + + if (receiver === undefined) { + return false + } + + const { hierarchy } = control + const backlink = TxProcessor.createDoc2Doc(tx.tx as TxCreateDoc) + + if (!hierarchy.isDerived(backlink.backlinkClass, activity.class.ActivityMessage)) { + return false + } - res = res.concat( - await createCollabDocInfo([receiver._id], control, messageTx.tx, messageTx, doc, [message as ActivityMessage], true) + const exists = await control.findAll( + notification.class.ActivityInboxNotification, + { attachedTo: backlink.backlinkId as Ref, user: receiver._id }, + { limit: 1 } ) - return res + return exists.length > 0 +} + +async function isAlreadyNotified (originTx: TxCUD, control: TriggerControl): Promise { + if (originTx._class !== core.class.TxCollectionCUD) { + return false + } + + const hierarchy = control.hierarchy + const isBacklink = await isBacklinkCreated(originTx as TxCollectionCUD, hierarchy, control) + + if (!isBacklink) { + return false + } + + return await isBacklinkNotified(originTx as TxCollectionCUD, control) } async function isBacklinkCreated ( @@ -983,6 +1008,14 @@ export async function createCollaboratorNotifications ( return [] } + if (activityMessages.length === 0) { + return [] + } + + if (await isAlreadyNotified(originTx ?? tx, control)) { + return [] + } + switch (tx._class) { case core.class.TxCreateDoc: return await createCollaboratorDoc(tx as TxCreateDoc, control, activityMessages, originTx ?? tx)