mirror of
https://github.com/hcengineering/platform.git
synced 2024-11-26 04:23:58 +03:00
Fix mention notifications on edit (#5549)
Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
parent
42fc3a15e9
commit
b27b88bc04
@ -228,6 +228,19 @@ export function stripTags (markup: Markup, textLimit = 0, extensions: Extensions
|
||||
let charCount = 0
|
||||
let isHardStop = false
|
||||
|
||||
const pushText = (text: string): void => {
|
||||
if (textLimit > 0 && charCount + text.length > textLimit) {
|
||||
const toAddCount = textLimit - charCount
|
||||
const textPart = text.substring(0, toAddCount)
|
||||
textParts.push(textPart)
|
||||
textParts.push(ELLIPSIS_CHAR)
|
||||
isHardStop = true
|
||||
} else {
|
||||
textParts.push(text)
|
||||
charCount += text.length
|
||||
}
|
||||
}
|
||||
|
||||
parsed.descendants((node, _pos, parent): boolean => {
|
||||
if (isHardStop) {
|
||||
return false
|
||||
@ -235,22 +248,16 @@ export function stripTags (markup: Markup, textLimit = 0, extensions: Extensions
|
||||
|
||||
if (node.type.isText) {
|
||||
const text = node.text ?? ''
|
||||
if (textLimit > 0 && charCount + text.length > textLimit) {
|
||||
const toAddCount = textLimit - charCount
|
||||
const textPart = text.substring(0, toAddCount)
|
||||
textParts.push(textPart)
|
||||
textParts.push(ELLIPSIS_CHAR)
|
||||
isHardStop = true
|
||||
} else {
|
||||
textParts.push(text)
|
||||
charCount += text.length
|
||||
}
|
||||
pushText(text)
|
||||
return false
|
||||
} else if (node.type.isBlock) {
|
||||
if (textParts.length > 0 && textParts[textParts.length - 1] !== WHITESPACE) {
|
||||
textParts.push(WHITESPACE)
|
||||
charCount++
|
||||
}
|
||||
} else if (node.type.name === 'reference') {
|
||||
const label = node.attrs.label ?? ''
|
||||
pushText(label.length > 0 ? `@${label}` : '')
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
@ -49,8 +49,10 @@ import contact, { Person, PersonAccount } from '@hcengineering/contact'
|
||||
import {
|
||||
getCommonNotificationTxes,
|
||||
getPushCollaboratorTx,
|
||||
isMessageAlreadyNotified,
|
||||
shouldNotifyCommon
|
||||
shouldNotifyCommon,
|
||||
isShouldNotifyTx,
|
||||
NotifyResult,
|
||||
createPushFromInbox
|
||||
} from '@hcengineering/server-notification-resources'
|
||||
|
||||
async function getPersonAccount (person: Ref<Person>, control: TriggerControl): Promise<PersonAccount | undefined> {
|
||||
@ -147,18 +149,27 @@ export async function getPersonNotificationTxes (
|
||||
)
|
||||
}
|
||||
|
||||
const data: Partial<Data<MentionInboxNotification>> = {
|
||||
const data: Omit<Data<MentionInboxNotification>, 'docNotifyContext'> = {
|
||||
header: activity.string.MentionedYouIn,
|
||||
messageHtml: reference.message,
|
||||
mentionedIn: reference.attachedDocId,
|
||||
mentionedInClass: reference.attachedDocClass
|
||||
mentionedIn: reference.attachedDocId ?? reference.srcDocId,
|
||||
mentionedInClass: reference.attachedDocClass ?? reference.srcDocClass,
|
||||
user: receiver._id,
|
||||
isViewed: false
|
||||
}
|
||||
|
||||
const notifyResult = await shouldNotifyCommon(control, receiver._id, notification.ids.MentionCommonNotificationType)
|
||||
const messageNotifyResult = await getMessageNotifyResult(reference, receiver._id, control, originTx, doc)
|
||||
|
||||
if (await isReferenceAlreadyNotified(reference, receiver._id, control)) {
|
||||
if (messageNotifyResult.allowed) {
|
||||
notifyResult.allowed = false
|
||||
}
|
||||
if (messageNotifyResult.push) {
|
||||
notifyResult.push = false
|
||||
}
|
||||
if (messageNotifyResult.emails.length > 0) {
|
||||
notifyResult.emails = []
|
||||
}
|
||||
|
||||
const txes = await getCommonNotificationTxes(
|
||||
control,
|
||||
@ -174,6 +185,32 @@ export async function getPersonNotificationTxes (
|
||||
notification.class.MentionInboxNotification
|
||||
)
|
||||
|
||||
if (!notifyResult.allowed && notifyResult.push) {
|
||||
const exists = (
|
||||
await control.findAll(
|
||||
notification.class.ActivityInboxNotification,
|
||||
{ attachedTo: reference.attachedDocId as Ref<ActivityMessage>, user: receiver._id },
|
||||
{ limit: 1, projection: { _id: 1 } }
|
||||
)
|
||||
)[0]
|
||||
|
||||
if (exists !== undefined) {
|
||||
const pushTx = await createPushFromInbox(
|
||||
control,
|
||||
receiver._id,
|
||||
reference.srcDocId,
|
||||
reference.srcDocClass,
|
||||
{ ...data, docNotifyContext: exists.docNotifyContext },
|
||||
notification.class.MentionInboxNotification,
|
||||
senderId as Ref<PersonAccount>,
|
||||
exists._id,
|
||||
new Map()
|
||||
)
|
||||
if (pushTx !== undefined) {
|
||||
res.push(pushTx)
|
||||
}
|
||||
}
|
||||
}
|
||||
res.push(...txes)
|
||||
return res
|
||||
}
|
||||
@ -238,22 +275,29 @@ async function getCollaboratorsTxes (
|
||||
return res
|
||||
}
|
||||
|
||||
async function isReferenceAlreadyNotified (
|
||||
async function getMessageNotifyResult (
|
||||
reference: Data<ActivityReference>,
|
||||
receiver: Ref<Account>,
|
||||
control: TriggerControl
|
||||
): Promise<boolean> {
|
||||
control: TriggerControl,
|
||||
originTx: TxCUD<Doc>,
|
||||
doc: Doc
|
||||
): Promise<NotifyResult> {
|
||||
const { hierarchy } = control
|
||||
const tx = TxProcessor.extractTx(originTx) as TxCUD<Doc>
|
||||
|
||||
if (reference.attachedDocClass === undefined || reference.attachedDocId === undefined) {
|
||||
return false
|
||||
if (
|
||||
reference.attachedDocClass === undefined ||
|
||||
reference.attachedDocId === undefined ||
|
||||
tx._class !== core.class.TxCreateDoc
|
||||
) {
|
||||
return { allowed: false, emails: [], push: false }
|
||||
}
|
||||
|
||||
if (!hierarchy.isDerived(reference.attachedDocClass, activity.class.ActivityMessage)) {
|
||||
return false
|
||||
return { allowed: false, emails: [], push: false }
|
||||
}
|
||||
|
||||
return await isMessageAlreadyNotified(reference.attachedDocId as Ref<ActivityMessage>, receiver, control)
|
||||
return await isShouldNotifyTx(control, tx, originTx, doc, receiver, false, false, undefined)
|
||||
}
|
||||
|
||||
function isMarkupType (type: Ref<Class<Type<any>>>): boolean {
|
||||
|
@ -110,20 +110,6 @@ export function getPushCollaboratorTx (
|
||||
return undefined
|
||||
}
|
||||
|
||||
export async function isMessageAlreadyNotified (
|
||||
_id: Ref<ActivityMessage>,
|
||||
user: Ref<Account>,
|
||||
control: TriggerControl
|
||||
): Promise<boolean> {
|
||||
const exists = await control.findAll(
|
||||
notification.class.ActivityInboxNotification,
|
||||
{ attachedTo: _id, user },
|
||||
{ limit: 1, projection: { _id: 1 } }
|
||||
)
|
||||
|
||||
return exists.length > 0
|
||||
}
|
||||
|
||||
export async function getCommonNotificationTxes (
|
||||
control: TriggerControl,
|
||||
doc: Doc,
|
||||
@ -531,7 +517,7 @@ export async function createPushFromInbox (
|
||||
_class: Ref<Class<InboxNotification>>,
|
||||
senderId: Ref<PersonAccount>,
|
||||
_id: Ref<Doc>,
|
||||
cache: Map<Ref<Doc>, Doc>
|
||||
cache: Map<Ref<Doc>, Doc> = new Map<Ref<Doc>, Doc>()
|
||||
): Promise<Tx | undefined> {
|
||||
let title: string = ''
|
||||
let body: string = ''
|
||||
|
Loading…
Reference in New Issue
Block a user