From 45c1a82909018d1a1c69c1cfa374c665cedd6dad Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Thu, 21 Sep 2023 13:08:21 +0700 Subject: [PATCH] Forced "latest" Collection Entity to have no posts refs https://github.com/TryGhost/Arch/issues/95 We're going to be treating the "latest" Collection as a "virtual" Collection where we will hydrate the posts from the posts repository. This will allow for more performant queries, and less work to keep the "latest" Collection in sync --- ghost/collections/src/Collection.ts | 5 ++++- ghost/collections/test/Collection.test.ts | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ghost/collections/src/Collection.ts b/ghost/collections/src/Collection.ts index 9505e7bda9..5c7e040912 100644 --- a/ghost/collections/src/Collection.ts +++ b/ghost/collections/src/Collection.ts @@ -136,6 +136,9 @@ export class Collection { * @param index {number} - The index to insert the post at, use negative numbers to count from the end. */ addPost(post: CollectionPost, index: number = -0) { + if (this.slug === 'latest') { + return false; + } if (this.type === 'automatic') { const matchesFilter = this.postMatchesFilter(post); @@ -268,7 +271,7 @@ export class Collection { createdAt: Collection.validateDateField(data.created_at, 'created_at'), updatedAt: Collection.validateDateField(data.updated_at, 'updated_at'), deleted: data.deleted || false, - posts: data.posts || [] + posts: data.slug !== 'latest' ? (data.posts || []) : [] }); } } diff --git a/ghost/collections/test/Collection.test.ts b/ghost/collections/test/Collection.test.ts index 29ac2ee920..27c19bd940 100644 --- a/ghost/collections/test/Collection.test.ts +++ b/ghost/collections/test/Collection.test.ts @@ -339,6 +339,27 @@ describe('Collection', function () { assert(collection.posts[collection.posts.length - 2] === '3'); }); + it('Does not add a post to the latest collection', async function () { + const collection = await Collection.create({ + title: 'Testing adding to latest', + slug: 'latest', + type: 'automatic', + filter: '' + }); + + assert.equal(collection.posts.length, 0, 'Collection should have no posts'); + + const added = await collection.addPost({ + id: '0', + featured: false, + published_at: new Date(), + tags: [] + }); + + assert.equal(added, false); + assert.equal(collection.posts.length, 0, 'The non-featured post should not have been added'); + }); + it('Adds a post to an automatic collection when it matches the filter', async function () { const collection = await Collection.create({ title: 'Testing adding posts',