1 import { PluginSettingsManager } from '@shared/models/plugins/plugin-settings-manager.model'
2 import { PluginModel } from '@server/models/server/plugin'
3 import { PluginStorageManager } from '@shared/models/plugins/plugin-storage-manager.model'
4 import { PluginVideoLanguageManager } from '@shared/models/plugins/plugin-video-language-manager.model'
5 import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '@server/initializers/constants'
6 import { PluginVideoLicenceManager } from '@shared/models/plugins/plugin-video-licence-manager.model'
7 import { PluginVideoCategoryManager } from '@shared/models/plugins/plugin-video-category-manager.model'
8 import { RegisterServerOptions } from '@server/typings/plugins'
9 import { buildPluginHelpers } from './plugin-helpers'
10 import { logger } from '@server/helpers/logger'
12 type AlterableVideoConstant = 'language' | 'licence' | 'category'
13 type VideoConstant = { [key in number | string]: string }
14 type UpdatedVideoConstant = {
15 [name in AlterableVideoConstant]: {
17 added: { key: number | string, label: string }[]
18 deleted: { key: number | string, label: string }[]
23 const updatedVideoConstants: UpdatedVideoConstant = {
29 function buildRegisterHelpers (npmName: string, plugin: PluginModel): Omit<RegisterServerOptions, 'registerHook' | 'registerSetting'> {
30 const settingsManager = buildSettingsManager(plugin)
31 const storageManager = buildStorageManager(plugin)
33 const videoLanguageManager = buildVideoLanguageManager(npmName)
35 const videoCategoryManager = buildVideoCategoryManager(npmName)
36 const videoLicenceManager = buildVideoLicenceManager(npmName)
38 const peertubeHelpers = buildPluginHelpers(npmName, plugin)
50 function reinitVideoConstants (npmName: string) {
52 language: VIDEO_LANGUAGES,
53 licence: VIDEO_LICENCES,
54 category: VIDEO_CATEGORIES
56 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category' ]
58 for (const type of types) {
59 const updatedConstants = updatedVideoConstants[type][npmName]
60 if (!updatedConstants) continue
62 for (const added of updatedConstants.added) {
63 delete hash[type][added.key]
66 for (const deleted of updatedConstants.deleted) {
67 hash[type][deleted.key] = deleted.label
70 delete updatedVideoConstants[type][npmName]
79 // ---------------------------------------------------------------------------
81 function buildSettingsManager (plugin: PluginModel): PluginSettingsManager {
83 getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name),
85 setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value)
89 function buildStorageManager (plugin: PluginModel): PluginStorageManager {
91 getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key),
93 storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data)
97 function buildVideoLanguageManager (npmName: string): PluginVideoLanguageManager {
99 addLanguage: (key: string, label: string) => addConstant({ npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label }),
101 deleteLanguage: (key: string) => deleteConstant({ npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
105 function buildVideoCategoryManager (npmName: string): PluginVideoCategoryManager {
107 addCategory: (key: number, label: string) => {
108 return addConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label })
111 deleteCategory: (key: number) => {
112 return deleteConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
117 function buildVideoLicenceManager (npmName: string): PluginVideoLicenceManager {
119 addLicence: (key: number, label: string) => {
120 return addConstant({ npmName, type: 'licence', obj: VIDEO_LICENCES, key, label })
123 deleteLicence: (key: number) => {
124 return deleteConstant({ npmName, type: 'licence', obj: VIDEO_LICENCES, key })
129 function addConstant<T extends string | number> (parameters: {
131 type: AlterableVideoConstant
136 const { npmName, type, obj, key, label } = parameters
139 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
143 if (!updatedVideoConstants[type][npmName]) {
144 updatedVideoConstants[type][npmName] = {
150 updatedVideoConstants[type][npmName].added.push({ key, label })
156 function deleteConstant<T extends string | number> (parameters: {
158 type: AlterableVideoConstant
162 const { npmName, type, obj, key } = parameters
165 logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key)
169 if (!updatedVideoConstants[type][npmName]) {
170 updatedVideoConstants[type][npmName] = {
176 updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] })