-
Notifications
You must be signed in to change notification settings - Fork 5
Auto-publish related items when Podcast or Meetup is published #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jan0707
wants to merge
2
commits into
main
Choose a base branch
from
directus-cascade-publish
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
...ctus-cms/extensions/directus-extension-programmierbar-bundle/src/cascade-publish/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import { defineHook } from '@directus/extensions-sdk' | ||
| import { postSlackMessage } from '../shared/postSlackMessage.ts' | ||
|
|
||
| const HOOK_NAME = 'cascade-publish' | ||
|
|
||
| interface CascadeRelation { | ||
| /** The relation field name on the parent item (e.g. 'speakers', 'picks_of_the_day') */ | ||
| relationField: string | ||
| /** For m2m: the field on the junction record that holds the child item (e.g. 'speaker', 'talk') */ | ||
| childField?: string | ||
| /** The Directus collection of the related items */ | ||
| targetCollection: string | ||
| } | ||
|
|
||
| const PODCAST_RELATIONS: CascadeRelation[] = [ | ||
| { | ||
| relationField: 'speakers', | ||
| childField: 'speaker', | ||
| targetCollection: 'speakers', | ||
| }, | ||
| { | ||
| relationField: 'picks_of_the_day', | ||
| targetCollection: 'picks_of_the_day', | ||
| }, | ||
| ] | ||
|
|
||
| const MEETUP_RELATIONS: CascadeRelation[] = [ | ||
| { | ||
| relationField: 'speakers', | ||
| childField: 'speaker', | ||
| targetCollection: 'speakers', | ||
| }, | ||
| { | ||
| relationField: 'talks', | ||
| childField: 'talk', | ||
| targetCollection: 'talks', | ||
| }, | ||
| ] | ||
|
|
||
| export default defineHook(({ action }, hookContext) => { | ||
| const logger = hookContext.logger | ||
| const ItemsService = hookContext.services.ItemsService | ||
| const getSchema = hookContext.getSchema | ||
| const env = hookContext.env | ||
|
|
||
| action('podcasts.items.create', async (metadata) => { | ||
| await handlePublishAction('podcasts', metadata, PODCAST_RELATIONS) | ||
| }) | ||
|
|
||
| action('podcasts.items.update', async (metadata) => { | ||
| await handlePublishAction('podcasts', metadata, PODCAST_RELATIONS) | ||
| }) | ||
|
|
||
| action('meetups.items.create', async (metadata) => { | ||
| await handlePublishAction('meetups', metadata, MEETUP_RELATIONS) | ||
| }) | ||
|
|
||
| action('meetups.items.update', async (metadata) => { | ||
| await handlePublishAction('meetups', metadata, MEETUP_RELATIONS) | ||
| }) | ||
|
|
||
| async function handlePublishAction( | ||
| parentCollection: string, | ||
| metadata: Record<string, any>, | ||
| relations: CascadeRelation[] | ||
| ) { | ||
| if (metadata.payload.status !== 'published') { | ||
| return | ||
| } | ||
|
|
||
| const parentKeys: string[] = metadata.keys || (metadata.key ? [metadata.key] : []) | ||
| if (parentKeys.length === 0) { | ||
| logger.warn(`${HOOK_NAME}: No key found for ${parentCollection} action`) | ||
| return | ||
| } | ||
|
|
||
| const schema = await getSchema() | ||
|
|
||
| for (const parentKey of parentKeys) { | ||
| logger.info(`${HOOK_NAME}: ${parentCollection} ${parentKey} published, cascading to related items`) | ||
|
|
||
| // Build the fields list for reading the parent with all its relations | ||
| const fields: string[] = [] | ||
| for (const relation of relations) { | ||
| if (relation.childField) { | ||
| fields.push(`${relation.relationField}.${relation.childField}.id`) | ||
| fields.push(`${relation.relationField}.${relation.childField}.status`) | ||
| } else { | ||
| fields.push(`${relation.relationField}.id`) | ||
| fields.push(`${relation.relationField}.status`) | ||
| } | ||
| } | ||
|
|
||
| // Read the parent item with nested relation data | ||
| const parentService = new ItemsService(parentCollection, { schema }) | ||
| const parentItem = await parentService.readOne(parentKey, { fields }) | ||
|
|
||
| const errors: Error[] = [] | ||
|
|
||
| for (const relation of relations) { | ||
| try { | ||
| await cascadePublishRelation(schema, parentItem, relation) | ||
| } catch (error: any) { | ||
| logger.error( | ||
| `${HOOK_NAME}: Failed to cascade ${relation.targetCollection} for ${parentCollection} ${parentKey}: ${error.message}` | ||
| ) | ||
| errors.push(error) | ||
| } | ||
| } | ||
|
|
||
| if (errors.length > 0) { | ||
| try { | ||
| await postSlackMessage( | ||
| `:warning: *${HOOK_NAME}*: Fehler beim automatischen Veröffentlichen von verknüpften Einträgen für ${parentCollection} ${parentKey}.\n` + | ||
| `Fehler: ${errors.map((e) => e.message).join(', ')}\n` + | ||
| `${env.PUBLIC_URL}admin/content/${parentCollection}/${parentKey}` | ||
| ) | ||
| } catch (slackError: any) { | ||
| logger.error(`${HOOK_NAME}: Failed to send Slack notification: ${slackError.message}`) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function cascadePublishRelation(schema: any, parentItem: any, relation: CascadeRelation) { | ||
| const relatedItems = parentItem[relation.relationField] | ||
| if (!Array.isArray(relatedItems) || relatedItems.length === 0) { | ||
| logger.info(`${HOOK_NAME}: No related ${relation.targetCollection} items found`) | ||
| return | ||
| } | ||
|
|
||
| // Extract child items: for m2m, unwrap from junction records; for o2m, use directly | ||
| const childItems: any[] = relation.childField | ||
| ? relatedItems.map((junctionRecord: any) => junctionRecord[relation.childField!]).filter(Boolean) | ||
| : relatedItems | ||
|
|
||
| // Filter for draft items only | ||
| const draftIds = childItems | ||
| .filter((item: any) => item.status === 'draft') | ||
| .map((item: any) => item.id) | ||
| .filter(Boolean) | ||
|
|
||
| if (draftIds.length === 0) { | ||
| logger.info(`${HOOK_NAME}: No draft ${relation.targetCollection} items to publish`) | ||
| return | ||
| } | ||
|
|
||
| const targetService = new ItemsService(relation.targetCollection, { schema }) | ||
| await targetService.updateMany(draftIds, { status: 'published' }) | ||
|
|
||
| logger.info( | ||
| `${HOOK_NAME}: Published ${draftIds.length} ${relation.targetCollection} item(s): ${draftIds.join(', ')}` | ||
| ) | ||
| } | ||
|
|
||
| logger.info(`${HOOK_NAME} hook registered`) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.