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'
11 import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model'
12 import { serverHookObject } from '@shared/models/plugins/server-hook.model'
13 import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
14 import * as express from 'express'
16 type AlterableVideoConstant = 'language' | 'licence' | 'category'
17 type VideoConstant = { [key in number | string]: string }
19 type UpdatedVideoConstant = {
20 [name in AlterableVideoConstant]: {
21 added: { key: number | string, label: string }[]
22 deleted: { key: number | string, label: string }[]
26 export class RegisterHelpersStore {
27 private readonly updatedVideoConstants: UpdatedVideoConstant = {
28 language: { added: [], deleted: [] },
29 licence: { added: [], deleted: [] },
30 category: { added: [], deleted: [] }
33 private readonly settings: RegisterServerSettingOptions[] = []
35 private readonly router: express.Router
38 private readonly npmName: string,
39 private readonly plugin: PluginModel,
40 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
42 this.router = express.Router()
45 buildRegisterHelpers (): RegisterServerOptions {
46 const registerHook = this.buildRegisterHook()
47 const registerSetting = this.buildRegisterSetting()
49 const getRouter = this.buildGetRouter()
51 const settingsManager = this.buildSettingsManager()
52 const storageManager = this.buildStorageManager()
54 const videoLanguageManager = this.buildVideoLanguageManager()
56 const videoLicenceManager = this.buildVideoLicenceManager()
57 const videoCategoryManager = this.buildVideoCategoryManager()
59 const peertubeHelpers = buildPluginHelpers(this.npmName)
78 reinitVideoConstants (npmName: string) {
80 language: VIDEO_LANGUAGES,
81 licence: VIDEO_LICENCES,
82 category: VIDEO_CATEGORIES
84 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category' ]
86 for (const type of types) {
87 const updatedConstants = this.updatedVideoConstants[type][npmName]
88 if (!updatedConstants) continue
90 for (const added of updatedConstants.added) {
91 delete hash[type][added.key]
94 for (const deleted of updatedConstants.deleted) {
95 hash[type][deleted.key] = deleted.label
98 delete this.updatedVideoConstants[type][npmName]
110 private buildGetRouter () {
111 return () => this.router
114 private buildRegisterSetting () {
115 return (options: RegisterServerSettingOptions) => {
116 this.settings.push(options)
120 private buildRegisterHook () {
121 return (options: RegisterServerHookOptions) => {
122 if (serverHookObject[options.target] !== true) {
123 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
127 return this.onHookAdded(options)
131 private buildSettingsManager (): PluginSettingsManager {
133 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name),
135 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value)
139 private buildStorageManager (): PluginStorageManager {
141 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
143 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
147 private buildVideoLanguageManager (): PluginVideoLanguageManager {
149 addLanguage: (key: string, label: string) => {
150 return this.addConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label })
153 deleteLanguage: (key: string) => {
154 return this.deleteConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
159 private buildVideoCategoryManager (): PluginVideoCategoryManager {
161 addCategory: (key: number, label: string) => {
162 return this.addConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label })
165 deleteCategory: (key: number) => {
166 return this.deleteConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
171 private buildVideoLicenceManager (): PluginVideoLicenceManager {
173 addLicence: (key: number, label: string) => {
174 return this.addConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key, label })
177 deleteLicence: (key: number) => {
178 return this.deleteConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key })
183 private addConstant<T extends string | number> (parameters: {
185 type: AlterableVideoConstant
190 const { npmName, type, obj, key, label } = parameters
193 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
197 if (!this.updatedVideoConstants[type][npmName]) {
198 this.updatedVideoConstants[type][npmName] = {
204 this.updatedVideoConstants[type][npmName].added.push({ key, label })
210 private deleteConstant<T extends string | number> (parameters: {
212 type: AlterableVideoConstant
216 const { npmName, type, obj, key } = parameters
219 logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key)
223 if (!this.updatedVideoConstants[type][npmName]) {
224 this.updatedVideoConstants[type][npmName] = {
230 this.updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] })