mirror of
https://github.com/hcengineering/platform.git
synced 2024-12-22 19:11:33 +03:00
Extract chat notifications trigger (#5558)
Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
parent
361f63c9dd
commit
356dcc005a
@ -78,7 +78,7 @@ export function createModel (builder: Builder): void {
|
|||||||
})
|
})
|
||||||
|
|
||||||
builder.createDoc(serverCore.class.Trigger, core.space.Model, {
|
builder.createDoc(serverCore.class.Trigger, core.space.Model, {
|
||||||
trigger: serverChunter.trigger.OnChatMessageCreate,
|
trigger: serverChunter.trigger.ChatNotificationsHandler,
|
||||||
txMatch: {
|
txMatch: {
|
||||||
_class: core.class.TxCollectionCUD,
|
_class: core.class.TxCollectionCUD,
|
||||||
'tx._class': core.class.TxCreateDoc,
|
'tx._class': core.class.TxCreateDoc,
|
||||||
|
@ -41,7 +41,7 @@ import core, {
|
|||||||
TxRemoveDoc,
|
TxRemoveDoc,
|
||||||
TxUpdateDoc
|
TxUpdateDoc
|
||||||
} from '@hcengineering/core'
|
} from '@hcengineering/core'
|
||||||
import notification, { ClassCollaborators, Collaborators, NotificationContent } from '@hcengineering/notification'
|
import notification, { Collaborators, NotificationContent } from '@hcengineering/notification'
|
||||||
import { getMetadata, IntlString } from '@hcengineering/platform'
|
import { getMetadata, IntlString } from '@hcengineering/platform'
|
||||||
import serverCore, { TriggerControl } from '@hcengineering/server-core'
|
import serverCore, { TriggerControl } from '@hcengineering/server-core'
|
||||||
import {
|
import {
|
||||||
@ -143,15 +143,25 @@ async function OnThreadMessageCreated (tx: Tx, control: TriggerControl): Promise
|
|||||||
return [lastReplyTx, employeeTx]
|
return [lastReplyTx, employeeTx]
|
||||||
}
|
}
|
||||||
|
|
||||||
async function updateCollaborators (
|
async function OnChatMessageCreated (tx: TxCUD<Doc>, control: TriggerControl): Promise<Tx[]> {
|
||||||
targetDoc: Doc | undefined,
|
|
||||||
tx: TxCUD<Doc>,
|
|
||||||
control: TriggerControl,
|
|
||||||
message: ChatMessage,
|
|
||||||
mixin?: ClassCollaborators
|
|
||||||
): Promise<Tx[]> {
|
|
||||||
if (targetDoc === undefined || mixin === undefined) return []
|
|
||||||
const hierarchy = control.hierarchy
|
const hierarchy = control.hierarchy
|
||||||
|
const actualTx = TxProcessor.extractTx(tx) as TxCreateDoc<ChatMessage>
|
||||||
|
|
||||||
|
if (
|
||||||
|
actualTx._class !== core.class.TxCreateDoc ||
|
||||||
|
!hierarchy.isDerived(actualTx.objectClass, chunter.class.ChatMessage)
|
||||||
|
) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
const message = TxProcessor.createDoc2Doc(actualTx)
|
||||||
|
const mixin = hierarchy.classHierarchyMixin(message.attachedToClass, notification.mixin.ClassCollaborators)
|
||||||
|
|
||||||
|
if (mixin === undefined) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
const targetDoc = (await control.findAll(message.attachedToClass, { _id: message.attachedTo }, { limit: 1 }))[0]
|
||||||
const isChannel = hierarchy.isDerived(targetDoc._class, chunter.class.Channel)
|
const isChannel = hierarchy.isDerived(targetDoc._class, chunter.class.Channel)
|
||||||
const res: Tx[] = []
|
const res: Tx[] = []
|
||||||
|
|
||||||
@ -187,8 +197,7 @@ async function updateCollaborators (
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
async function OnChatMessageCreate (tx: TxCUD<Doc>, control: TriggerControl): Promise<Tx[]> {
|
async function ChatNotificationsHandler (tx: TxCUD<Doc>, control: TriggerControl): Promise<Tx[]> {
|
||||||
const hierarchy = control.hierarchy
|
|
||||||
const actualTx = TxProcessor.extractTx(tx) as TxCreateDoc<ChatMessage>
|
const actualTx = TxProcessor.extractTx(tx) as TxCreateDoc<ChatMessage>
|
||||||
|
|
||||||
if (actualTx._class !== core.class.TxCreateDoc) {
|
if (actualTx._class !== core.class.TxCreateDoc) {
|
||||||
@ -196,26 +205,8 @@ async function OnChatMessageCreate (tx: TxCUD<Doc>, control: TriggerControl): Pr
|
|||||||
}
|
}
|
||||||
|
|
||||||
const chatMessage = TxProcessor.createDoc2Doc(actualTx)
|
const chatMessage = TxProcessor.createDoc2Doc(actualTx)
|
||||||
const cache = new Map<Ref<Doc>, Doc>()
|
|
||||||
|
|
||||||
const mixin = hierarchy.classHierarchyMixin(chatMessage.attachedToClass, notification.mixin.ClassCollaborators)
|
return await createCollaboratorNotifications(control.ctx, tx, control, [chatMessage])
|
||||||
|
|
||||||
let targetDoc: Doc | undefined
|
|
||||||
|
|
||||||
if (mixin !== undefined) {
|
|
||||||
targetDoc = (await control.findAll(chatMessage.attachedToClass, { _id: chatMessage.attachedTo }, { limit: 1 }))[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
if (targetDoc !== undefined) {
|
|
||||||
cache.set(targetDoc._id, targetDoc)
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await Promise.all([
|
|
||||||
createCollaboratorNotifications(control.ctx, tx, control, [chatMessage], undefined, cache),
|
|
||||||
updateCollaborators(targetDoc, tx, control, chatMessage, mixin)
|
|
||||||
])
|
|
||||||
|
|
||||||
return res.flat()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function joinChannel (control: TriggerControl, channel: Channel, user: Ref<Account>): Tx[] {
|
function joinChannel (control: TriggerControl, channel: Channel, user: Ref<Account>): Tx[] {
|
||||||
@ -271,11 +262,12 @@ async function OnThreadMessageDeleted (tx: Tx, control: TriggerControl): Promise
|
|||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
*/
|
*/
|
||||||
export async function ChunterTrigger (tx: Tx, control: TriggerControl): Promise<Tx[]> {
|
export async function ChunterTrigger (tx: TxCUD<Doc>, control: TriggerControl): Promise<Tx[]> {
|
||||||
const res = await Promise.all([
|
const res = await Promise.all([
|
||||||
OnThreadMessageCreated(tx, control),
|
OnThreadMessageCreated(tx, control),
|
||||||
OnThreadMessageDeleted(tx, control),
|
OnThreadMessageDeleted(tx, control),
|
||||||
OnCollaboratorsChanged(tx as TxMixin<Doc, Collaborators>, control)
|
OnCollaboratorsChanged(tx as TxMixin<Doc, Collaborators>, control),
|
||||||
|
OnChatMessageCreated(tx, control)
|
||||||
])
|
])
|
||||||
return res.flat()
|
return res.flat()
|
||||||
}
|
}
|
||||||
@ -531,7 +523,7 @@ export default async () => ({
|
|||||||
OnDirectMessageSent,
|
OnDirectMessageSent,
|
||||||
OnChatMessageRemoved,
|
OnChatMessageRemoved,
|
||||||
OnChannelMembersChanged,
|
OnChannelMembersChanged,
|
||||||
OnChatMessageCreate
|
ChatNotificationsHandler
|
||||||
},
|
},
|
||||||
function: {
|
function: {
|
||||||
CommentRemove,
|
CommentRemove,
|
||||||
|
@ -32,7 +32,7 @@ export default plugin(serverChunterId, {
|
|||||||
OnDirectMessageSent: '' as Resource<TriggerFunc>,
|
OnDirectMessageSent: '' as Resource<TriggerFunc>,
|
||||||
OnChatMessageRemoved: '' as Resource<TriggerFunc>,
|
OnChatMessageRemoved: '' as Resource<TriggerFunc>,
|
||||||
OnChannelMembersChanged: '' as Resource<TriggerFunc>,
|
OnChannelMembersChanged: '' as Resource<TriggerFunc>,
|
||||||
OnChatMessageCreate: '' as Resource<TriggerFunc>
|
ChatNotificationsHandler: '' as Resource<TriggerFunc>
|
||||||
},
|
},
|
||||||
function: {
|
function: {
|
||||||
CommentRemove: '' as Resource<ObjectDDParticipantFunc>,
|
CommentRemove: '' as Resource<ObjectDDParticipantFunc>,
|
||||||
|
Loading…
Reference in New Issue
Block a user