From 7f4c3b3d2aeeb855d3d63bf4722c4451b6731a82 Mon Sep 17 00:00:00 2001 From: rijkvanzanten Date: Fri, 25 Sep 2020 17:22:40 -0400 Subject: [PATCH] Emit events for system collections --- api/src/emitter.ts | 2 +- api/src/services/items.ts | 86 +++++++++++++++++++-------------------- api/src/services/meta.ts | 2 +- 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/api/src/emitter.ts b/api/src/emitter.ts index ac126496da..95dc3dcdbe 100644 --- a/api/src/emitter.ts +++ b/api/src/emitter.ts @@ -3,6 +3,6 @@ import { EventEmitter2 } from 'eventemitter2'; const emitter = new EventEmitter2({ wildcard: true, verboseMemoryLeak: true, delimiter: '.' }); // No-op function to ensure we never end up with no data -emitter.on('item.*.*.before', (input) => input); +emitter.on('*.*.before', input => input); export default emitter; diff --git a/api/src/services/items.ts b/api/src/services/items.ts index a50da786cc..2ee884083b 100644 --- a/api/src/services/items.ts +++ b/api/src/services/items.ts @@ -28,11 +28,13 @@ export class ItemsService implements AbstractService { collection: string; knex: Knex; accountability: Accountability | null; + eventScope: string; constructor(collection: string, options?: AbstractServiceOptions) { this.collection = collection; this.knex = options?.knex || database; this.accountability = options?.accountability || null; + this.eventScope = this.collection.startsWith('directus_') ? this.collection.substring(9) : 'item'; return this; } @@ -52,20 +54,20 @@ export class ItemsService implements AbstractService { knex: trx, }); - if (this.collection.startsWith('directus_') === false) { - const customProcessed = await emitter.emitAsync( - `item.create.${this.collection}.before`, - payloads, - { - event: `item.create.${this.collection}.before`, - accountability: this.accountability, - collection: this.collection, - item: null, - action: 'create', - payload: payloads, - } - ); + const customProcessed = await emitter.emitAsync( + `${this.eventScope}.create.before`, + payloads, + { + event: `${this.eventScope}.create.before`, + accountability: this.accountability, + collection: this.collection, + item: null, + action: 'create', + payload: payloads, + } + ); + if (customProcessed) { payloads = customProcessed[customProcessed.length - 1]; } @@ -169,18 +171,16 @@ export class ItemsService implements AbstractService { await cache.clear(); } - if (this.collection.startsWith('directus_') === false) { - emitter - .emitAsync(`item.create.${this.collection}`, { - event: `item.create.${this.collection}`, - accountability: this.accountability, - collection: this.collection, - item: primaryKeys, - action: 'create', - payload: payloads, - }) - .catch((err) => logger.warn(err)); - } + emitter + .emitAsync(`${this.eventScope}.create`, { + event: `${this.eventScope}.create`, + accountability: this.accountability, + collection: this.collection, + item: primaryKeys, + action: 'create', + payload: payloads, + }) + .catch((err) => logger.warn(err)); return primaryKeys; }); @@ -267,20 +267,20 @@ export class ItemsService implements AbstractService { let payload = clone(data); - if (this.collection.startsWith('directus_') === false) { - const customProcessed = await emitter.emitAsync( - `item.update.${this.collection}.before`, + const customProcessed = await emitter.emitAsync( + `${this.eventScope}.update.before`, + payload, + { + event: `${this.eventScope}.update.before`, + accountability: this.accountability, + collection: this.collection, + item: null, + action: 'update', payload, - { - event: `item.update.${this.collection}.before`, - accountability: this.accountability, - collection: this.collection, - item: null, - action: 'update', - payload, - } - ); + } + ); + if (customProcessed) { payload = customProcessed[customProcessed.length - 1]; } @@ -368,8 +368,8 @@ export class ItemsService implements AbstractService { } emitter - .emitAsync(`item.update.${this.collection}`, { - event: `item.update.${this.collection}`, + .emitAsync(`${this.eventScope}.update`, { + event: `${this.eventScope}.update`, accountability: this.accountability, collection: this.collection, item: key, @@ -423,8 +423,8 @@ export class ItemsService implements AbstractService { await authorizationService.checkAccess('delete', this.collection, key); } - await emitter.emitAsync(`item.delete.${this.collection}.before`, { - event: `item.update.${this.collection}`, + await emitter.emitAsync(`${this.eventScope}.delete.before`, { + event: `${this.eventScope}.delete.before`, accountability: this.accountability, collection: this.collection, item: keys, @@ -454,8 +454,8 @@ export class ItemsService implements AbstractService { } emitter - .emitAsync(`item.delete.${this.collection}`, { - event: `item.delete.${this.collection}`, + .emitAsync(`${this.eventScope}.delete`, { + event: `${this.eventScope}.delete`, accountability: this.accountability, collection: this.collection, item: keys, diff --git a/api/src/services/meta.ts b/api/src/services/meta.ts index 28afd4df6f..9764dcdcee 100644 --- a/api/src/services/meta.ts +++ b/api/src/services/meta.ts @@ -40,7 +40,7 @@ export class MetaService { const dbQuery = database(collection).count('*', { as: 'count' }); if (query.filter) { - applyFilter(dbQuery, query.filter); + applyFilter(dbQuery, query.filter, collection); } const records = await dbQuery;