From 673b06119bf684958d6a75ca2c084c08dbc77658 Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Fri, 22 Sep 2023 15:05:44 +0700 Subject: [PATCH] Wired up CollectionPost events to Collection https://github.com/TryGhost/Arch/issues/95 Entities don't dispatch their own events, instead they add to an event array and are dispatched once persisted. --- ghost/collections/src/Collection.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ghost/collections/src/Collection.ts b/ghost/collections/src/Collection.ts index b392c5ac3b..9505e7bda9 100644 --- a/ghost/collections/src/Collection.ts +++ b/ghost/collections/src/Collection.ts @@ -4,9 +4,13 @@ import tpl from '@tryghost/tpl'; import nql from '@tryghost/nql'; import {posts as postExpansions} from '@tryghost/nql-filter-expansions'; import {CollectionPost} from './CollectionPost'; +import {CollectionPostAdded} from './events/CollectionPostAdded'; +import {CollectionPostRemoved} from './events/CollectionPostRemoved'; import ObjectID from 'bson-objectid'; +type CollectionEvent = CollectionPostAdded | CollectionPostRemoved; + const messages = { invalidIDProvided: 'Invalid ID provided for Collection', invalidDateProvided: 'Invalid date provided for {fieldName}', @@ -64,6 +68,7 @@ function validateFilter(filter: string | null, type: 'manual' | 'automatic', isA } export class Collection { + events: CollectionEvent[]; id: string; title: string; private _slug: string; @@ -141,6 +146,11 @@ export class Collection { if (this.posts.includes(post.id)) { this._posts = this.posts.filter(id => id !== post.id); + } else { + this.events.push(CollectionPostAdded.create({ + post_id: post.id, + collection_id: this.id + })); } if (index < 0 || Object.is(index, -0)) { @@ -154,6 +164,10 @@ export class Collection { removePost(id: string) { if (this.posts.includes(id)) { this._posts = this.posts.filter(postId => postId !== id); + this.events.push(CollectionPostRemoved.create({ + post_id: id, + collection_id: this.id + })); } } @@ -162,6 +176,12 @@ export class Collection { } removeAllPosts() { + for (const id of this._posts) { + this.events.push(CollectionPostRemoved.create({ + post_id: id, + collection_id: this.id + })); + } this._posts = []; } @@ -178,6 +198,7 @@ export class Collection { this.updatedAt = data.updatedAt; this.deleted = data.deleted; this._posts = data.posts; + this.events = []; } toJSON() {