Commit Graph

60 Commits

Author SHA1 Message Date
Jono M
5e057dee11
Added tests to AdminX framework package (#19022)
refs https://github.com/TryGhost/Product/issues/4159

---

<!-- Leave the line below if you'd like GitHub Copilot to generate a
summary from your commit -->
<!--
copilot:summary
-->
### <samp>🤖[[deprecated]](https://githubnext.com/copilot-for-prs-sunset)
Generated by Copilot at 9e68f4d</samp>

This pull request refactors several components in the `admin-x-settings`
app to use common hooks from the `@tryghost/admin-x-framework` package,
which reduces code duplication and improves consistency. It also updates
the `package.json` file and adds unit tests for the `admin-x-framework`
package, which improves the formatting, testing, and dependency
management. Additionally, it makes some minor changes to the `hooks.ts`,
`FrameworkProvider.tsx`, and `.eslintrc.cjs` files in the
`admin-x-framework` package, which enhance the public API and the
linting configuration.
2023-11-20 11:00:51 +00:00
Fabien "egg" O'Carroll
9b2a94f931 Implemented "virtual" Collection for "latest"
refs https://github.com/TryGhost/Arch/issues/95

Rather than storing all of the relations between the latest collection and
posts, we know that it contains all posts. This means we don't have to keep the
collections posts in sync. Instead we can fetch them from the posts table. This
saves a lot of work during recalculation.
2023-09-22 19:05:37 +07:00
Fabien "egg" O'Carroll
45c1a82909 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
2023-09-22 19:05:37 +07:00
Naz
99f29a169c Removed post delete related event handling
closes https://github.com/TryGhost/Arch/issues/91

- We have on cascade delete (a9f9f6121a/ghost/core/core/server/data/schema/schema.js (L1068)) on `post_id` column which handles post deletion logic automatically on DB level.
- The commented out handlers in the long term should be hooked up with public CollectionService methods on the client side.
2023-09-15 13:50:25 +08:00
Naz
367584f1c2 Optimized PostEditedEvent processing in collections
refs https://github.com/TryGhost/Arch/issues/86

- When PostEditedEvent data contains no visible changes we can skip the matching collections update process alltogether. Each call to `updatePostInMatchingCollections` creates a transaction in addition to fetching all collections. There's no need to process anything when there are no relevant changes in the post edit!
2023-09-08 13:46:44 +08:00
Naz
acccc16614 Added collections update after bulk adding tags
refs https://github.com/TryGhost/Arch/issues/77

- During initial development we have missed to support collections update when tags are added to posts in bulk. It's especially valid usecase since we can define automatic collection with a filter containing not yet existing tags.
2023-08-23 17:12:08 +08:00
Fabien "egg" O'Carroll
62d5ca558d Validated filters for collections
refs https://github.com/TryGhost/Arch/issues/47

This ensures that we only have collections which have a valid filter in terms of
  - Valid NQL string
  - Uses only properties which are valid to filter on
  - Only has an empty filter in the case of the "latest" collection
2023-08-23 15:49:06 +07:00
Fabien "egg" O'Carroll
ad62cd4822 Removed edit method from Collection
We want to keep Entities as basic as possible, particularly when setting
property, so that each property can handle its own validation.
2023-08-17 09:45:41 +01:00
Naz
fe4c0b18cf Removed unnecessary getBulk in PostsRepository
refs https://github.com/TryGhost/Arch/issues/16

- The method ended up being used only once, so it makes no sense to complicate the interface without any gain.
2023-08-01 15:11:00 +08:00
Naz
8635f4efeb Added collections handling for PostsBulkFeaturedEvent/PostsBulkUnfeaturedEvent
refs https://github.com/TryGhost/Arch/issues/16

- When posts produce PostsBulkFeaturedEvent/PostsBulkUnfeaturedEvent the collections having a featured filter should update the posts belonging to them.
2023-08-01 15:11:00 +08:00
Naz
8046c33194 Added collections handling for PostsBulkUnpublishedEvent
refs https://github.com/TryGhost/Arch/issues/16

- When posts produce PostsBulkUnpublishedEvent the collections having a published_at filter should update the posts belonging to them
2023-08-01 15:11:00 +08:00
Naz
9b257f3966 Update of collection posts on bulk destroy
refs https://github.com/TryGhost/Arch/issues/16

- When the bulk destroy is done on posts Collections need to know about the update and remove the stored posts from collections.
2023-08-01 15:11:00 +08:00
Fabien "egg" O'Carroll
fa40485fb1 Handled updating Collections on TagDeletedEvent
closes https://github.com/TryGhost/Arch/issues/62

Because there are many ways in which filters can rely on tags, we will just
recalculate all automatic collections for now, rather than attempting to do
optimised updates.
2023-07-27 16:33:16 +02:00
Fabien "egg" O'Carroll
9288f56649 Improved type definitions
The PostRepository type was using `any` (an anti pattern) rather than
`PostCollection`, and we had optional properties, which are not really
optional. This cleans up the types and updates the tests alongside them.
2023-07-27 16:33:16 +02:00
Fabien "egg" O'Carroll
607ea8dcd7 Added TagDeletedEvent
refs https://github.com/TryGhost/Arch/issues/60

This will be used to update collections when a tag is deleted. Like the Post
events this should not be in the collections package, instead we should have
these as part of the tags and posts packages. These packages don't exist right
now, so I'm following the existing pattern.
2023-07-26 12:54:42 +02:00
Naz
694ab1d32d Added support for expansions in collection filters
refs https://github.com/TryGhost/Arch/issues/46

- Similarly to post filters, collection filters now support both 'tag' and 'tags' nql filter keys when defining a filter for related tag slugs. For example, both `tag:avocado` and `tags:avocado` would both be valid collection filters that would filter by the same 'slug' property of the tags assigned to a post.
- Along with these changes had to rework the tags property of the collection posts to match the shape used in post resources. Moved from:
`tags: ['bacon', 'broc']`
to
`tags:[{slug: 'bacon'}, {slug: 'broc'}]`
2023-07-25 19:01:56 +08:00
Naz
9a38c65ee6 Cleaned up CollectionsService from unused code
refs https://github.com/TryGhost/Arch/issues/16

- Some of the methods became unused due to moving the posts handling out of the collections service and cleaning up the event handling system.
2023-07-21 19:46:22 +08:00
Naz
04c92d2ca5 Removed CollectionResourceChangeEvent handling
refs https://github.com/TryGhost/Arch/issues/16

- The generic "UpdateAllCollections" logic should not be used as the mapped Post's delete/add/edit events should be sufficient in maintaining collection's state
2023-07-21 19:46:22 +08:00
Naz
53f9f954c1 Added tag filter support to collections
refs https://github.com/TryGhost/Arch/issues/41

- When an new collection is created the relational "tags" filter is now picked up properly and appropriate posts matching the tag filter are assigned and stored in the collection. Example collection filter that is now supported: `tags:['bacon']`
- Additionally cleaned up returned collection post DTOs, so we return as little data as possible and add only the fields that are needed
2023-07-18 20:18:54 +08:00
Naz
c733424cc5 Fixed collections ordering
refs https://github.com/TryGhost/Arch/issues/25

- When run against different DB Engines the returned order of collections belonging to a post is not consistent (SQLite vs MySQL). Having a primitive ordering by slug allows to keep the order compatible
2023-07-17 18:43:21 +08:00
Naz
ed91412cd5 Changed "index" collection name to "latest"
https://github.com/TryGhost/Arch/issues/25

- This naming matches how the collection will be visible in the cards and how it's marketed. Naming it "index" even for internal purposes will get confusing.
2023-07-17 18:43:21 +08:00
Naz
cdae2a978d Added ability to fetch collection posts by slug
closes https://github.com/TryGhost/Arch/issues/27

- We need a more convenient method of fetching posts belonging to a collection than by collection's "id". This change adds an alias to the existing endpoint `GET /collections/:id/posts/`. A non-valid ObjectID in the parameter is treated as a slug.
2023-07-11 09:30:01 +08:00
Naz
cb8e009c14 Allowed for index collection to have empty filter
refs https://github.com/TryGhost/Arch/issues/25

- The built in index (soon -> "latest") collection does not require any filtering so that it could contain an index of all posts in the system. All other automatic collections should have a filter defined.
2023-07-10 12:43:52 +08:00
Fabien "egg" O'Carroll
16db3bbf17 Removed deletable property in favour of protected slugs
We couldn't decide on the best way to encode this in the database.
e.g. deletable: true? or type: internal? but type conflicts with the
storage of manual/automatic.

For now we can use this, and add something in future if we're finding problems
2023-06-29 01:40:05 +01:00
Fabien "egg" O'Carroll
cf83d169db Added use of a slug generator to offload calculation of slugs
When we end up wiring this to the database, this generator will also ensure
uniqueness by appending/incrementing a number on the end of the slug. Long term
it would be good to offload this to a shared slug service, this could also
ensure that slugs are unique globally or between multiple tables, if desired
2023-06-29 01:40:05 +01:00
Fabien "egg" O'Carroll
0a3e36cd62 Ensured uniqueness of slug in collections
We require that slugs are unique as slugs can/are used for routing purposes and
act as an identifier for a resource. As this is a core business rule, we want
to encode it in the entity so that it can be unit tested, and be enforced
regardless of underlying persistence layer
2023-06-29 01:40:05 +01:00
Fabien "egg" O'Carroll
d29f512823 Required titles for collections
We don't want to allow collections to be created without a title, and we need
to encoe that business rule in the entity.
2023-06-29 01:40:05 +01:00
Naz
5dd6159ac6 Added handling for 'post.edited' Ghost model event
refs https://github.com/TryGhost/Team/issues/3169

- Adds optomized collection update handling for when post.edited model event is emitted.
2023-06-27 12:52:10 +07:00
Naz
a8e5cbcc3d Extracted post matching to filter to separate method
refs https://github.com/TryGhost/Team/issues/3169

- This method is also useful for external services when checking if collection needs any updates or not.
2023-06-27 12:52:10 +07:00
Naz
cf48d4ef5c Removed ability to change collection type after creation
refs https://github.com/TryGhost/Team/issues/3169

- Being able to change the collection type comes with extra complications we don't wan to support just yet. This came up as a part of the PR review here - 1bc2a604c3 (r1239783573).
2023-06-27 12:52:10 +07:00
Naz
623599af1e Moved DomainEvents to be injected to Collections
refs https://github.com/TryGhost/Team/issues/3169

- With the new architecture all possible dependencies should be  injected rather than required directly - this applies to the DomainEvents module as well.
2023-06-27 12:52:10 +07:00
Naz
36783e456b Added 'post.added' event handling in Collections
refs https://github.com/TryGhost/Team/issues/3169

- This piece of logic handles the 'post.added' model event mapping to Collection's PostAddedEvent domain event and logic related to updating collections when the new post is added.
2023-06-27 12:52:10 +07:00
Naz
42539b954f Added collection editing method
refs https://github.com/TryGhost/Team/issues/3169

- The collection entity properties used to be modified, which is not a good practice for logic encapsulation. Extracted editing logic to it's own method which can also contain validation logic when wrong data is passed when editing a collection.
2023-06-27 12:52:10 +07:00
Naz
58a18d37ea Introduced PostEvent classes to make event handling less generic
refs https://github.com/TryGhost/Team/issues/3169

- When handling a single generic event the code becomes riddled with if statements to detect correct "data" that is being passed with the event. Switching to have a domain event per model event helps solving this problem and makes code more readable.
2023-06-27 12:52:10 +07:00
Naz
e7a0462877 Added model event to domain event bridge
refs https://github.com/TryGhost/Team/issues/3169

- To make the coupling to Ghost's model events as loose as possible added a bridge that maps model events to domain events. These domain events it what the collections module can subscribe to to make necessary updates.
2023-06-27 12:52:10 +07:00
Naz
49d831d971 Optimized handling for post.deleted event in collections
refs https://github.com/TryGhost/Team/issues/3428

- I'm taking an approach of adding specialized support for each event one-by-one.
- The post resource deletion event is the most straight forward and works same for both types of collections.
2023-06-27 12:52:10 +07:00
Naz
25e0cb12c0
Added "getBySlug" method to Collections Service
refs https://github.com/TryGhost/Team/issues/3423

- This is a convenience method that should allow fetching collections by their slug. It is a great developer experience for in case when fetching built-in collections like "featured" and "index", so can avoid an extra call to find the collection and it's ide one wants to use.
2023-06-16 16:31:50 +07:00
Naz
6335033466
Added full post data when returning collection's posts
refs https://github.com/TryGhost/Team/issues/3423

- When querying for posts that belong to a collection we should be returning full post information just like we do for Posts API.
2023-06-15 18:35:58 +07:00
Naz
ddc23b3467 Added collection's posts fetching to collections lib
refs https://github.com/TryGhost/Team/issues/3423

- This is a building block to allow paging, querying etc. of the posts belonging to a collection
2023-06-13 17:23:09 +07:00
Fabien 'egg' O'Carroll
92172aca8e
Wired up collections to posts endpoint (#16945)
- Added support for `include=collections` to the Posts Admin API behind a flag
- Refactored some of the collections work to support it
2023-06-07 15:06:15 +02:00
Naz
fab5b1845c
Made builtin collections un-deletable
closes https://github.com/TryGhost/Team/issues/3376

- It should not be possible to delete a built-in collection.
2023-06-06 22:04:16 +07:00
Naz
2390afc6f1
Added automatic collection recalculation
closes https://github.com/TryGhost/Team/issues/3170

- When resources that are related to automatic collection filter are updated the posts in collections should be updated as well.
- This change adds a super-basic way to track changes in post/tag/author resources and updated all automatic collections when any of those resources change. In the future we can optimize the update process to be more performant, but it's good enough for current needs
2023-06-05 18:33:39 +07:00
Naz
3599cfdd7a
Added test coverage for collection updates
refs https://github.com/TryGhost/Team/issues/3170

- When the collection filter is updated the collection's posts should be updated automatically.
2023-06-05 16:23:02 +07:00
Naz
bfefcfd4df
Added automatic collection creation based on filter
refs https://github.com/TryGhost/Team/issues/3170

- This implementation allows to create an automatic collection with a filter defining automatically populated posts that belong to a collection
- To populate collection using a filter the API client can send a `filter` property along with a collection request
- Filter values are compatible with the filters used in Content API (https://ghost.org/docs/content-api/#filter)
2023-06-05 13:39:52 +07:00
Naz
6e224f3702
Added automatic collection validation
refs https://github.com/TryGhost/Team/issues/3170

- When an automatic collection is created it should always have a valid filter assigned
2023-06-05 13:39:52 +07:00
Naz
e8220b1387
Added DEL /collections/:id/posts/:post_id to Admin API
refs https://github.com/TryGhost/Team/issues/3260

- We need a way to remove posts form collections without fetching the whole collection's content. This API method allows to remove posts from manual collections by collection id and post id.
- As a response it returns up to date collection state without the removed post.
2023-06-01 14:59:05 +07:00
Naz
253f144501 Fixed fromDTO collection mapping
refs https://github.com/TryGhost/Team/issues/3260

- Whenever properties of the DTO are undefined they should be removed to avoid unintentional empty assignments to the stored collection
2023-06-01 14:25:49 +07:00
Naz
a0a7c3a61b Added coverage to all editable collection properties
refs https://github.com/TryGhost/Team/issues/3260

- Made sure we have sufficient coverage for all editable properties of the collection to avoid sneaky bugs
2023-06-01 14:25:49 +07:00
Fabien "egg" O'Carroll
b48a1d0a57 Implemented adding posts to collections; 2023-06-01 14:25:49 +07:00
Fabien "egg" O'Carroll
27f60b4ab5 Added logic for adding posts to collection
Also removes concept of CollectionPost in an effort to simplify the structure
2023-06-01 14:25:49 +07:00