Skip to content

Commit

Permalink
UBERF-5650: do not send mention notification if user already notified (
Browse files Browse the repository at this point in the history
…hcengineering#4821)

Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
Signed-off-by: Tiago Cruz <tcruz@netic.io>
  • Loading branch information
kristina-fefelova authored and tjaoc committed Mar 5, 2024
1 parent 7b3dbc9 commit 9f90703
Showing 1 changed file with 50 additions and 17 deletions.
67 changes: 50 additions & 17 deletions server-plugins/notification-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export async function OnBacklinkCreate (
return []
}

if (sender === receiver) {
return []
}

const backlink = TxProcessor.createDoc2Doc(tx.tx as TxCreateDoc<Backlink>)

if (!hierarchy.isDerived(backlink.backlinkClass, activity.class.ActivityMessage)) {
Expand All @@ -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,
Expand All @@ -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<Doc, Backlink>, control: TriggerControl): Promise<boolean> {
const receiver = await getPersonAccount(tx.objectId as Ref<Employee>, control)

if (receiver === undefined) {
return false
}

const { hierarchy } = control
const backlink = TxProcessor.createDoc2Doc(tx.tx as TxCreateDoc<Backlink>)

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<ActivityMessage>, user: receiver._id },
{ limit: 1 }
)

return res
return exists.length > 0
}

async function isAlreadyNotified (originTx: TxCUD<Doc>, control: TriggerControl): Promise<boolean> {
if (originTx._class !== core.class.TxCollectionCUD) {
return false
}

const hierarchy = control.hierarchy
const isBacklink = await isBacklinkCreated(originTx as TxCollectionCUD<Doc, AttachedDoc>, hierarchy, control)

if (!isBacklink) {
return false
}

return await isBacklinkNotified(originTx as TxCollectionCUD<Doc, Backlink>, control)
}

async function isBacklinkCreated (
Expand Down Expand Up @@ -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<Doc>, control, activityMessages, originTx ?? tx)
Expand Down

0 comments on commit 9f90703

Please sign in to comment.