From bb4e2fcadba12d4fe84f51837cfde0bd214c4da7 Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 12 Jun 2023 12:57:52 +0700 Subject: [PATCH] Added "index" built in collection closes https://github.com/TryGhost/Team/issues/3425 - Index collection is needed to support one of the usecases we have in the near future where we'd hold all posts that would be displayed on the "index" page. --- .../collections/built-in-collections.js | 7 +++ .../core/server/services/collections/index.js | 12 ++-- .../__snapshots__/collections.test.js.snap | 63 ++++++++++++++++++- .../test/e2e-api/admin/collections.test.js | 1 + 4 files changed, 75 insertions(+), 8 deletions(-) diff --git a/ghost/core/core/server/services/collections/built-in-collections.js b/ghost/core/core/server/services/collections/built-in-collections.js index af7f71a67c..892a52398a 100644 --- a/ghost/core/core/server/services/collections/built-in-collections.js +++ b/ghost/core/core/server/services/collections/built-in-collections.js @@ -1,4 +1,11 @@ module.exports = [{ + title: 'Index', + slug: 'index', + description: 'Collection with all posts', + type: 'automatic', + deletable: false, + filter: 'status:published' +}, { title: 'Featured Posts', slug: 'featured', description: 'Collection of featured posts', diff --git a/ghost/core/core/server/services/collections/index.js b/ghost/core/core/server/services/collections/index.js index 32b1b4bee2..fe04c22519 100644 --- a/ghost/core/core/server/services/collections/index.js +++ b/ghost/core/core/server/services/collections/index.js @@ -39,12 +39,14 @@ class CollectionsServiceWrapper { } async init() { - const featuredCollections = await this.api.getAll({filter: 'slug:featured'}); + const existingBuiltins = await this.api.getAll({filter: 'slug:featured'}); - if (!featuredCollections.data.length) { - require('./built-in-collections').forEach((collection) => { - this.api.createCollection(collection); - }); + if (!existingBuiltins.data.length) { + const builtInCollections = require('./built-in-collections'); + + for (const collection of builtInCollections) { + await this.api.createCollection(collection); + } } } } diff --git a/ghost/core/test/e2e-api/admin/__snapshots__/collections.test.js.snap b/ghost/core/test/e2e-api/admin/__snapshots__/collections.test.js.snap index d5de4bc9dd..b1efbfa8e7 100644 --- a/ghost/core/test/e2e-api/admin/__snapshots__/collections.test.js.snap +++ b/ghost/core/test/e2e-api/admin/__snapshots__/collections.test.js.snap @@ -182,6 +182,63 @@ Object { exports[`Collections API Can browse Collections 1: [body] 1`] = ` Object { "collections": Array [ + Object { + "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + "description": "Collection with all posts", + "feature_image": null, + "filter": "status:published", + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "posts": Array [ + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": Any, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": Any, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": Any, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": Any, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": Any, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": Any, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": Any, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": Any, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": Any, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": Any, + }, + Object { + "id": StringMatching /\\[a-f0-9\\]\\{24\\}/, + "sort_order": Any, + }, + ], + "slug": "index", + "title": "Index", + "type": "automatic", + "updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, + }, Object { "created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/, "description": "Collection of featured posts", @@ -217,12 +274,12 @@ Object { ], "meta": Object { "pagination": Object { - "limit": 2, + "limit": 3, "next": null, "page": 1, "pages": 1, "prev": null, - "total": 2, + "total": 3, }, }, } @@ -232,7 +289,7 @@ exports[`Collections API Can browse Collections 2: [headers] 1`] = ` Object { "access-control-allow-origin": "http://127.0.0.1:2369", "cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0", - "content-length": "725", + "content-length": "1530", "content-type": "application/json; charset=utf-8", "content-version": StringMatching /v\\\\d\\+\\\\\\.\\\\d\\+/, "etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/, diff --git a/ghost/core/test/e2e-api/admin/collections.test.js b/ghost/core/test/e2e-api/admin/collections.test.js index 5cd0549352..3dd404a5ee 100644 --- a/ghost/core/test/e2e-api/admin/collections.test.js +++ b/ghost/core/test/e2e-api/admin/collections.test.js @@ -84,6 +84,7 @@ describe('Collections API', function () { }) .matchBodySnapshot({ collections: [ + buildMatcher(11, {withSortOrder: true}), buildMatcher(2, {withSortOrder: true}), buildMatcher(0) ]