mirror of
https://github.com/hcengineering/platform.git
synced 2025-06-07 00:09:34 +00: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 charCount = 0
|
||||||
let isHardStop = false
|
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 => {
|
parsed.descendants((node, _pos, parent): boolean => {
|
||||||
if (isHardStop) {
|
if (isHardStop) {
|
||||||
return false
|
return false
|
||||||
@ -235,22 +248,16 @@ export function stripTags (markup: Markup, textLimit = 0, extensions: Extensions
|
|||||||
|
|
||||||
if (node.type.isText) {
|
if (node.type.isText) {
|
||||||
const text = node.text ?? ''
|
const text = node.text ?? ''
|
||||||
if (textLimit > 0 && charCount + text.length > textLimit) {
|
pushText(text)
|
||||||
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
|
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
} else if (node.type.isBlock) {
|
} else if (node.type.isBlock) {
|
||||||
if (textParts.length > 0 && textParts[textParts.length - 1] !== WHITESPACE) {
|
if (textParts.length > 0 && textParts[textParts.length - 1] !== WHITESPACE) {
|
||||||
textParts.push(WHITESPACE)
|
textParts.push(WHITESPACE)
|
||||||
charCount++
|
charCount++
|
||||||
}
|
}
|
||||||
|
} else if (node.type.name === 'reference') {
|
||||||
|
const label = node.attrs.label ?? ''
|
||||||
|
pushText(label.length > 0 ? `@${label}` : '')
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
@ -49,8 +49,10 @@ import contact, { Person, PersonAccount } from '@hcengineering/contact'
|
|||||||
import {
|
import {
|
||||||
getCommonNotificationTxes,
|
getCommonNotificationTxes,
|
||||||
getPushCollaboratorTx,
|
getPushCollaboratorTx,
|
||||||
isMessageAlreadyNotified,
|
shouldNotifyCommon,
|
||||||
shouldNotifyCommon
|
isShouldNotifyTx,
|
||||||
|
NotifyResult,
|
||||||
|
createPushFromInbox
|
||||||
} from '@hcengineering/server-notification-resources'
|
} from '@hcengineering/server-notification-resources'
|
||||||
|
|
||||||
async function getPersonAccount (person: Ref<Person>, control: TriggerControl): Promise<PersonAccount | undefined> {
|
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,
|
header: activity.string.MentionedYouIn,
|
||||||
messageHtml: reference.message,
|
messageHtml: reference.message,
|
||||||
mentionedIn: reference.attachedDocId,
|
mentionedIn: reference.attachedDocId ?? reference.srcDocId,
|
||||||
mentionedInClass: reference.attachedDocClass
|
mentionedInClass: reference.attachedDocClass ?? reference.srcDocClass,
|
||||||
|
user: receiver._id,
|
||||||
|
isViewed: false
|
||||||
}
|
}
|
||||||
|
|
||||||
const notifyResult = await shouldNotifyCommon(control, receiver._id, notification.ids.MentionCommonNotificationType)
|
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
|
notifyResult.allowed = false
|
||||||
}
|
}
|
||||||
|
if (messageNotifyResult.push) {
|
||||||
|
notifyResult.push = false
|
||||||
|
}
|
||||||
|
if (messageNotifyResult.emails.length > 0) {
|
||||||
|
notifyResult.emails = []
|
||||||
|
}
|
||||||
|
|
||||||
const txes = await getCommonNotificationTxes(
|
const txes = await getCommonNotificationTxes(
|
||||||
control,
|
control,
|
||||||
@ -174,6 +185,32 @@ export async function getPersonNotificationTxes (
|
|||||||
notification.class.MentionInboxNotification
|
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)
|
res.push(...txes)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -238,22 +275,29 @@ async function getCollaboratorsTxes (
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
async function isReferenceAlreadyNotified (
|
async function getMessageNotifyResult (
|
||||||
reference: Data<ActivityReference>,
|
reference: Data<ActivityReference>,
|
||||||
receiver: Ref<Account>,
|
receiver: Ref<Account>,
|
||||||
control: TriggerControl
|
control: TriggerControl,
|
||||||
): Promise<boolean> {
|
originTx: TxCUD<Doc>,
|
||||||
|
doc: Doc
|
||||||
|
): Promise<NotifyResult> {
|
||||||
const { hierarchy } = control
|
const { hierarchy } = control
|
||||||
|
const tx = TxProcessor.extractTx(originTx) as TxCUD<Doc>
|
||||||
|
|
||||||
if (reference.attachedDocClass === undefined || reference.attachedDocId === undefined) {
|
if (
|
||||||
return false
|
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)) {
|
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 {
|
function isMarkupType (type: Ref<Class<Type<any>>>): boolean {
|
||||||
|
@ -110,20 +110,6 @@ export function getPushCollaboratorTx (
|
|||||||
return undefined
|
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 (
|
export async function getCommonNotificationTxes (
|
||||||
control: TriggerControl,
|
control: TriggerControl,
|
||||||
doc: Doc,
|
doc: Doc,
|
||||||
@ -531,7 +517,7 @@ export async function createPushFromInbox (
|
|||||||
_class: Ref<Class<InboxNotification>>,
|
_class: Ref<Class<InboxNotification>>,
|
||||||
senderId: Ref<PersonAccount>,
|
senderId: Ref<PersonAccount>,
|
||||||
_id: Ref<Doc>,
|
_id: Ref<Doc>,
|
||||||
cache: Map<Ref<Doc>, Doc>
|
cache: Map<Ref<Doc>, Doc> = new Map<Ref<Doc>, Doc>()
|
||||||
): Promise<Tx | undefined> {
|
): Promise<Tx | undefined> {
|
||||||
let title: string = ''
|
let title: string = ''
|
||||||
let body: string = ''
|
let body: string = ''
|
||||||
|
Loading…
Reference in New Issue
Block a user