1 import { logger } from '@server/helpers/logger'
6 VIDEO_PLAYLIST_PRIVACIES,
8 } from '@server/initializers/constants'
9 import { ConstantManager } from '@shared/models/plugins/server/plugin-constant-manager.model'
11 type AlterableVideoConstant = 'language' | 'licence' | 'category' | 'privacy' | 'playlistPrivacy'
12 type VideoConstant = Record<number | string, string>
14 type UpdatedVideoConstant = {
15 [name in AlterableVideoConstant]: {
17 added: VideoConstant[]
18 deleted: VideoConstant[]
23 const constantsHash: { [key in AlterableVideoConstant]: VideoConstant } = {
24 language: VIDEO_LANGUAGES,
25 licence: VIDEO_LICENCES,
26 category: VIDEO_CATEGORIES,
27 privacy: VIDEO_PRIVACIES,
28 playlistPrivacy: VIDEO_PLAYLIST_PRIVACIES
31 export class VideoConstantManagerFactory {
32 private readonly updatedVideoConstants: UpdatedVideoConstant = {
41 private readonly npmName: string
44 public resetVideoConstants (npmName: string) {
45 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category', 'privacy', 'playlistPrivacy' ]
46 for (const type of types) {
47 this.resetConstants({ npmName, type })
51 private resetConstants (parameters: { npmName: string, type: AlterableVideoConstant }) {
52 const { npmName, type } = parameters
53 const updatedConstants = this.updatedVideoConstants[type][npmName]
55 if (!updatedConstants) return
57 for (const added of updatedConstants.added) {
58 delete constantsHash[type][added.key]
61 for (const deleted of updatedConstants.deleted) {
62 constantsHash[type][deleted.key] = deleted.label
65 delete this.updatedVideoConstants[type][npmName]
68 public createVideoConstantManager<K extends number | string>(type: AlterableVideoConstant): ConstantManager<K> {
69 const { npmName } = this
71 addConstant: (key: K, label: string) => this.addConstant({ npmName, type, key, label }),
72 deleteConstant: (key: K) => this.deleteConstant({ npmName, type, key }),
73 getConstantValue: (key: K) => constantsHash[type][key],
74 getConstants: () => constantsHash[type] as Record<K, string>,
75 resetConstants: () => this.resetConstants({ npmName, type })
79 private addConstant<T extends string | number> (parameters: {
81 type: AlterableVideoConstant
85 const { npmName, type, key, label } = parameters
86 const obj = constantsHash[type]
89 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
93 if (!this.updatedVideoConstants[type][npmName]) {
94 this.updatedVideoConstants[type][npmName] = {
100 this.updatedVideoConstants[type][npmName].added.push({ key, label } as VideoConstant)
106 private deleteConstant<T extends string | number> (parameters: {
108 type: AlterableVideoConstant
111 const { npmName, type, key } = parameters
112 const obj = constantsHash[type]
115 logger.warn('Cannot delete %s by plugin %s: key %s does not exist.', type, npmName, key)
119 if (!this.updatedVideoConstants[type][npmName]) {
120 this.updatedVideoConstants[type][npmName] = {
126 const updatedConstants = this.updatedVideoConstants[type][npmName]
128 const alreadyAdded = updatedConstants.added.find(a => a.key === key)
130 updatedConstants.added.filter(a => a.key !== key)
131 } else if (obj[key]) {
132 updatedConstants.deleted.push({ key, label: obj[key] } as VideoConstant)