From 3d833008f51d5500bc2d821aedd1f3e305502cf3 Mon Sep 17 00:00:00 2001 From: Andrey Platov Date: Sun, 10 Oct 2021 12:21:08 +0200 Subject: [PATCH] pass `hierarchy` to trigger Signed-off-by: Andrey Platov --- server/core/src/storage.ts | 2 +- server/core/src/triggers.ts | 6 +++--- server/core/src/types.ts | 4 ++-- server/view-resources/src/index.ts | 22 ++++++++++------------ 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/server/core/src/storage.ts b/server/core/src/storage.ts index fb16c3e63b..22bc5da41a 100644 --- a/server/core/src/storage.ts +++ b/server/core/src/storage.ts @@ -130,7 +130,7 @@ class TServerStorage implements ServerStorage { // store object await this.routeTx(tx) // invoke triggers and store derived objects - const derived = await this.triggers.apply(tx.modifiedBy, tx, this.findAll.bind(this)) + const derived = await this.triggers.apply(tx.modifiedBy, tx, this.findAll.bind(this), this.hierarchy) for (const tx of derived) { await this.routeTx(tx) } diff --git a/server/core/src/triggers.ts b/server/core/src/triggers.ts index 8340841f28..402c83e51f 100644 --- a/server/core/src/triggers.ts +++ b/server/core/src/triggers.ts @@ -14,7 +14,7 @@ // limitations under the License. // -import type { Tx, Doc, TxCreateDoc, Ref, Account } from '@anticrm/core' +import type { Tx, Doc, TxCreateDoc, Ref, Account, Hierarchy } from '@anticrm/core' import core, { TxFactory } from '@anticrm/core' import { getResource } from '@anticrm/platform' @@ -39,8 +39,8 @@ export class Triggers { } } - async apply (account: Ref, tx: Tx, findAll: FindAll): Promise { - const derived = this.triggers.map(trigger => trigger(tx, new TxFactory(account), findAll)) + async apply (account: Ref, tx: Tx, findAll: FindAll, hierarchy: Hierarchy): Promise { + const derived = this.triggers.map(trigger => trigger(tx, new TxFactory(account), findAll, hierarchy)) const result = await Promise.all(derived) return result.flatMap(x => x) } diff --git a/server/core/src/types.ts b/server/core/src/types.ts index e3080b85e4..5f15993bc6 100644 --- a/server/core/src/types.ts +++ b/server/core/src/types.ts @@ -15,7 +15,7 @@ // import type { Tx, Ref, Doc, Class, Space, Timestamp, Account, FindResult, DocumentQuery, FindOptions } from '@anticrm/core' -import { TxFactory } from '@anticrm/core' +import { TxFactory, Hierarchy } from '@anticrm/core' import type { Resource } from '@anticrm/platform' /** @@ -26,7 +26,7 @@ export type FindAll = (clazz: Ref>, query: DocumentQuery /** * @public */ -export type TriggerFunc = (tx: Tx, txFactory: TxFactory, findAll: FindAll) => Promise +export type TriggerFunc = (tx: Tx, txFactory: TxFactory, findAll: FindAll, hierarchy: Hierarchy) => Promise /** * @public diff --git a/server/view-resources/src/index.ts b/server/view-resources/src/index.ts index 83c5c0872a..65e7961502 100644 --- a/server/view-resources/src/index.ts +++ b/server/view-resources/src/index.ts @@ -14,23 +14,21 @@ // limitations under the License. // -import type { Tx, TxFactory } from '@anticrm/core' +import type { Tx, TxFactory, Doc, TxCreateDoc, DocWithState } from '@anticrm/core' +import type { FindAll } from '@anticrm/server-core' -// import core from '@anticrm/core' +import core, { Hierarchy } from '@anticrm/core' /** * @public */ -export async function OnDocWithState (tx: Tx, txFactory: TxFactory): Promise { - // if (tx._class === core.class.TxCreateDoc) { - // const createTx = tx as TxCreateDoc - // if (createTx.objectClass === chunter.class.Message) { - // const content = createTx.attributes.content - // const backlinks = getBacklinks(createTx.objectId, content) - // return backlinks.map(backlink => txFactory.createTxCreateDoc(chunter.class.Backlink, chunter.space.Backlinks, backlink)) - // } - // } - console.log('OnDocWithState here') +export async function OnDocWithState (tx: Tx, txFactory: TxFactory, findAll: FindAll, hierarchy: Hierarchy): Promise { + if (tx._class === core.class.TxCreateDoc) { + const createTx = tx as TxCreateDoc + if (hierarchy.isDerived(createTx.objectClass, core.class.DocWithState)) { + console.log('OnDocWithState derived here') + } + } return [] }