diff --git a/models/server-activity/src/index.ts b/models/server-activity/src/index.ts index ca7a52f492..db61a72077 100644 --- a/models/server-activity/src/index.ts +++ b/models/server-activity/src/index.ts @@ -35,4 +35,8 @@ export function createModel (builder: Builder): void { builder.createDoc(serverCore.class.Trigger, core.space.Model, { trigger: serverActivity.trigger.ActivityMessagesHandler }) + + builder.createDoc(serverCore.class.Trigger, core.space.Model, { + trigger: serverActivity.trigger.OnDocRemoved + }) } diff --git a/server-plugins/activity-resources/src/index.ts b/server-plugins/activity-resources/src/index.ts index 21382236bd..0e31b3c069 100644 --- a/server-plugins/activity-resources/src/index.ts +++ b/server-plugins/activity-resources/src/index.ts @@ -263,10 +263,33 @@ async function ActivityMessagesHandler (tx: TxCUD, control: TriggerControl) return await generateDocUpdateMessages(tx, control) } +async function OnDocRemoved (originTx: TxCUD, control: TriggerControl): Promise { + const tx = TxProcessor.extractTx(originTx) as TxCUD + + if (tx._class !== core.class.TxRemoveDoc) { + return [] + } + + const activityDocMixin = control.hierarchy.classHierarchyMixin(tx.objectClass, activity.mixin.ActivityDoc) + + if (activityDocMixin === undefined) { + return [] + } + + const messages = await control.findAll( + activity.class.ActivityMessage, + { attachedTo: tx.objectId }, + { projection: { _id: 1, _class: 1, space: 1 } } + ) + + return messages.map((message) => control.txFactory.createTxRemoveDoc(message._class, message.space, message._id)) +} + // eslint-disable-next-line @typescript-eslint/explicit-function-return-type export default async () => ({ trigger: { // OnReactionChanged, - ActivityMessagesHandler + ActivityMessagesHandler, + OnDocRemoved } }) diff --git a/server-plugins/activity/src/index.ts b/server-plugins/activity/src/index.ts index 4008e649cd..9506e2722c 100644 --- a/server-plugins/activity/src/index.ts +++ b/server-plugins/activity/src/index.ts @@ -29,6 +29,7 @@ export const serverActivityId = 'server-activity' as Plugin */ export default plugin(serverActivityId, { trigger: { - ActivityMessagesHandler: '' as Resource + ActivityMessagesHandler: '' as Resource, + OnDocRemoved: '' as Resource } })