|
| 1 | +import { UMB_CONTENT_PUBLISH_MODAL } from '../modal/constants.js'; |
| 2 | +import type { UmbContentDetailModel } from '../../../types.js'; |
| 3 | +import type { MetaEntityActionContentPublishKind, UmbContentPublishingRepository } from './types.js'; |
| 4 | +import { createExtensionApiByAlias } from '@umbraco-cms/backoffice/extension-registry'; |
| 5 | +import { umbOpenModal } from '@umbraco-cms/backoffice/modal'; |
| 6 | +import { UmbEntityActionBase, UmbRequestReloadStructureForEntityEvent } from '@umbraco-cms/backoffice/entity-action'; |
| 7 | +import { UmbLocalizationController } from '@umbraco-cms/backoffice/localization-api'; |
| 8 | +import { UmbVariantId } from '@umbraco-cms/backoffice/variant'; |
| 9 | +import { UMB_ACTION_EVENT_CONTEXT } from '@umbraco-cms/backoffice/action'; |
| 10 | +import { UMB_APP_LANGUAGE_CONTEXT, UmbLanguageCollectionRepository } from '@umbraco-cms/backoffice/language'; |
| 11 | +import { UMB_CURRENT_USER_CONTEXT } from '@umbraco-cms/backoffice/current-user'; |
| 12 | +import { UMB_NOTIFICATION_CONTEXT } from '@umbraco-cms/backoffice/notification'; |
| 13 | +import type { UmbDetailRepository } from '@umbraco-cms/backoffice/repository'; |
| 14 | +import type { UmbEntityVariantOptionModel } from '@umbraco-cms/backoffice/variant'; |
| 15 | +import type { UmbLanguageDetailModel } from '@umbraco-cms/backoffice/language'; |
| 16 | + |
| 17 | +/** |
| 18 | + * @description - Shared entity action for publishing content-like entities (Document, Element, ...). |
| 19 | + * Resolves its detail/publishing repositories by alias from `meta`, so a single implementation can serve every |
| 20 | + * consumer registered against the `contentPublish` kind. |
| 21 | + * @exports |
| 22 | + * @class UmbContentPublishEntityAction |
| 23 | + * @augments UmbEntityActionBase |
| 24 | + */ |
| 25 | +export class UmbContentPublishEntityAction extends UmbEntityActionBase<MetaEntityActionContentPublishKind> { |
| 26 | + override async execute() { |
| 27 | + if (!this.args.unique) throw new Error('The unique identifier is missing'); |
| 28 | + const unique = this.args.unique; |
| 29 | + |
| 30 | + const { languageData, detailData } = await this.#resolveDetailData(unique); |
| 31 | + const { appCulture, currentUserAllowedLanguages, currentUserHasAccessToAllLanguages } = |
| 32 | + await this.#resolveCurrentUserLanguageAccess(); |
| 33 | + |
| 34 | + const options = this.#buildVariantOptions(detailData, languageData, appCulture); |
| 35 | + const selection = this.#determineDefaultSelection(options, appCulture); |
| 36 | + |
| 37 | + const result = await umbOpenModal(this, UMB_CONTENT_PUBLISH_MODAL, { |
| 38 | + data: { |
| 39 | + confirmLabel: '#actions_publish', |
| 40 | + options, |
| 41 | + pickableFilter: (option) => |
| 42 | + this.#isLanguagePickable(option, currentUserAllowedLanguages, currentUserHasAccessToAllLanguages), |
| 43 | + }, |
| 44 | + value: { selection }, |
| 45 | + }).catch(() => undefined); |
| 46 | + |
| 47 | + if (!result?.selection.length) return; |
| 48 | + |
| 49 | + const variantIds = result.selection.map((x) => UmbVariantId.FromString(x)); |
| 50 | + const publishableVariantIds = this.#resolvePublishableVariantIds(detailData, variantIds); |
| 51 | + |
| 52 | + if (!publishableVariantIds.length) return; |
| 53 | + |
| 54 | + await this.#publish(unique, publishableVariantIds); |
| 55 | + await this.#notifyPublished(options, detailData, result.selection); |
| 56 | + await this.#dispatchReloadEvent(unique); |
| 57 | + } |
| 58 | + |
| 59 | + async #resolveDetailData(unique: string): Promise<{ |
| 60 | + languageData: { items: Array<UmbLanguageDetailModel> } | undefined; |
| 61 | + detailData: UmbContentDetailModel; |
| 62 | + }> { |
| 63 | + if (!this.args.meta.detailRepositoryAlias) throw new Error('The detail repository alias is missing in meta'); |
| 64 | + |
| 65 | + const languageRepository = new UmbLanguageCollectionRepository(this._host); |
| 66 | + const { data: languageData } = await languageRepository.requestAllItems(); |
| 67 | + |
| 68 | + const detailRepository = await createExtensionApiByAlias<UmbDetailRepository<UmbContentDetailModel>>( |
| 69 | + this, |
| 70 | + this.args.meta.detailRepositoryAlias, |
| 71 | + ); |
| 72 | + const { data: detailData } = await detailRepository.requestByUnique(unique); |
| 73 | + |
| 74 | + if (!detailData) throw new Error('The item was not found'); |
| 75 | + |
| 76 | + return { languageData, detailData }; |
| 77 | + } |
| 78 | + |
| 79 | + async #resolveCurrentUserLanguageAccess(): Promise<{ |
| 80 | + appCulture: string | undefined; |
| 81 | + currentUserAllowedLanguages: Array<string>; |
| 82 | + currentUserHasAccessToAllLanguages: boolean; |
| 83 | + }> { |
| 84 | + const appLanguageContext = await this.getContext(UMB_APP_LANGUAGE_CONTEXT); |
| 85 | + if (!appLanguageContext) throw new Error('The app language context is missing'); |
| 86 | + const appCulture = appLanguageContext.getAppCulture(); |
| 87 | + |
| 88 | + const currentUserContext = await this.getContext(UMB_CURRENT_USER_CONTEXT); |
| 89 | + if (!currentUserContext) throw new Error('The current user context is missing'); |
| 90 | + const currentUserAllowedLanguages = currentUserContext.getLanguages(); |
| 91 | + const currentUserHasAccessToAllLanguages = currentUserContext.getHasAccessToAllLanguages(); |
| 92 | + |
| 93 | + if (currentUserAllowedLanguages === undefined) throw new Error('The current user languages are missing'); |
| 94 | + if (currentUserHasAccessToAllLanguages === undefined) |
| 95 | + throw new Error('The current user access to all languages is missing'); |
| 96 | + |
| 97 | + return { appCulture, currentUserAllowedLanguages, currentUserHasAccessToAllLanguages }; |
| 98 | + } |
| 99 | + |
| 100 | + #buildVariantOptions( |
| 101 | + detailData: UmbContentDetailModel, |
| 102 | + languageData: { items: Array<UmbLanguageDetailModel> } | undefined, |
| 103 | + appCulture: string | undefined, |
| 104 | + ): Array<UmbEntityVariantOptionModel> { |
| 105 | + // only display culture variants as options |
| 106 | + const cultureVariantOptions = detailData.variants.filter((variant) => variant.segment === null); |
| 107 | + |
| 108 | + return cultureVariantOptions.map<UmbEntityVariantOptionModel>((variant) => ({ |
| 109 | + culture: variant.culture, |
| 110 | + segment: variant.segment, |
| 111 | + language: languageData?.items.find((language) => language.unique === variant.culture) ?? { |
| 112 | + name: appCulture!, |
| 113 | + entityType: 'language', |
| 114 | + fallbackIsoCode: null, |
| 115 | + isDefault: true, |
| 116 | + isMandatory: false, |
| 117 | + unique: appCulture!, |
| 118 | + }, |
| 119 | + variant, |
| 120 | + unique: new UmbVariantId(variant.culture, variant.segment).toString(), |
| 121 | + })); |
| 122 | + } |
| 123 | + |
| 124 | + #determineDefaultSelection( |
| 125 | + options: Array<UmbEntityVariantOptionModel>, |
| 126 | + appCulture: string | undefined, |
| 127 | + ): Array<string> { |
| 128 | + if (!options.length) throw new Error('No variants are available to publish'); |
| 129 | + |
| 130 | + // If the app language is one of the options, select it by default: |
| 131 | + if (appCulture && options.some((o) => o.unique === appCulture)) { |
| 132 | + return [new UmbVariantId(appCulture, null).toString()]; |
| 133 | + } |
| 134 | + |
| 135 | + // If not, select the first option by default: |
| 136 | + return [options[0].unique]; |
| 137 | + } |
| 138 | + |
| 139 | + #isLanguagePickable( |
| 140 | + option: UmbEntityVariantOptionModel, |
| 141 | + currentUserAllowedLanguages: Array<string>, |
| 142 | + currentUserHasAccessToAllLanguages: boolean, |
| 143 | + ): boolean { |
| 144 | + if (!option.culture) return false; |
| 145 | + if (currentUserHasAccessToAllLanguages) return true; |
| 146 | + return currentUserAllowedLanguages.includes(option.culture); |
| 147 | + } |
| 148 | + |
| 149 | + #resolvePublishableVariantIds( |
| 150 | + detailData: UmbContentDetailModel, |
| 151 | + variantIds: Array<UmbVariantId>, |
| 152 | + ): Array<UmbVariantId> { |
| 153 | + // find all segments of a selected culture |
| 154 | + return variantIds.flatMap((variantId) => |
| 155 | + detailData.variants |
| 156 | + .filter((variant) => variantId.culture === variant.culture) |
| 157 | + .map((variant) => UmbVariantId.Create(variant).toSegment(variant.segment)), |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + async #publish(unique: string, publishableVariantIds: Array<UmbVariantId>): Promise<void> { |
| 162 | + if (!this.args.meta.publishingRepositoryAlias) |
| 163 | + throw new Error('The publishing repository alias is missing in meta'); |
| 164 | + |
| 165 | + const publishingRepository = await createExtensionApiByAlias<UmbContentPublishingRepository>( |
| 166 | + this, |
| 167 | + this.args.meta.publishingRepositoryAlias, |
| 168 | + ); |
| 169 | + const { error } = await publishingRepository.publish( |
| 170 | + unique, |
| 171 | + publishableVariantIds.map((variantId) => ({ variantId })), |
| 172 | + ); |
| 173 | + |
| 174 | + if (error) { |
| 175 | + throw error; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + async #notifyPublished( |
| 180 | + options: Array<UmbEntityVariantOptionModel>, |
| 181 | + detailData: UmbContentDetailModel, |
| 182 | + selection: Array<string>, |
| 183 | + ): Promise<void> { |
| 184 | + const notificationContext = await this.getContext(UMB_NOTIFICATION_CONTEXT); |
| 185 | + const localize = new UmbLocalizationController(this); |
| 186 | + |
| 187 | + // If the content is invariant, we need to show a different notification |
| 188 | + const isInvariant = options.length === 1 && options[0].culture === null; |
| 189 | + |
| 190 | + if (isInvariant) { |
| 191 | + notificationContext?.peek('positive', { |
| 192 | + data: { |
| 193 | + headline: localize.term('speechBubbles_editContentPublishedHeader'), |
| 194 | + message: localize.string( |
| 195 | + this.args.meta.publishedNotificationMessage || '#speechBubbles_editContentPublishedText', |
| 196 | + ), |
| 197 | + }, |
| 198 | + }); |
| 199 | + } else { |
| 200 | + const publishedVariants = detailData.variants.filter((variant) => selection.includes(variant.culture!)); |
| 201 | + notificationContext?.peek('positive', { |
| 202 | + data: { |
| 203 | + headline: localize.term('speechBubbles_editContentPublishedHeader'), |
| 204 | + message: localize.string( |
| 205 | + this.args.meta.variantPublishedNotificationMessage || '#speechBubbles_editVariantPublishedText', |
| 206 | + localize.list(publishedVariants.map((v) => UmbVariantId.Create(v).toString() ?? v.name)), |
| 207 | + ), |
| 208 | + }, |
| 209 | + }); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + async #dispatchReloadEvent(unique: string): Promise<void> { |
| 214 | + const actionEventContext = await this.getContext(UMB_ACTION_EVENT_CONTEXT); |
| 215 | + const event = new UmbRequestReloadStructureForEntityEvent({ |
| 216 | + unique, |
| 217 | + entityType: this.args.entityType, |
| 218 | + }); |
| 219 | + |
| 220 | + actionEventContext?.dispatchEvent(event); |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +export { UmbContentPublishEntityAction as api }; |
0 commit comments