+ // ###################### Generate register helpers ######################
+
+ private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterServerOptions {
+ const registerHook = (options: RegisterServerHookOptions) => {
+ if (serverHookObject[options.target] !== true) {
+ logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, npmName)
+ return
+ }
+
+ if (!this.hooks[options.target]) this.hooks[options.target] = []
+
+ this.hooks[options.target].push({
+ npmName,
+ pluginName: plugin.name,
+ handler: options.handler,
+ priority: options.priority || 0
+ })
+ }
+
+ const registerSetting = (options: RegisterServerSettingOptions) => {
+ if (!this.settings[npmName]) this.settings[npmName] = []
+
+ this.settings[npmName].push(options)
+ }
+
+ const settingsManager: PluginSettingsManager = {
+ getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name),
+
+ setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value)
+ }
+
+ const storageManager: PluginStorageManager = {
+ getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key),
+
+ storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data)
+ }
+
+ const videoLanguageManager: PluginVideoLanguageManager = {
+ addLanguage: (key: string, label: string) => this.addConstant({ npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label }),
+
+ deleteLanguage: (key: string) => this.deleteConstant({ npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
+ }
+
+ const videoCategoryManager: PluginVideoCategoryManager = {
+ addCategory: (key: number, label: string) => this.addConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label }),
+
+ deleteCategory: (key: number) => this.deleteConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
+ }
+
+ const videoLicenceManager: PluginVideoLicenceManager = {
+ addLicence: (key: number, label: string) => this.addConstant({ npmName, type: 'licence', obj: VIDEO_LICENCES, key, label }),
+
+ deleteLicence: (key: number) => this.deleteConstant({ npmName, type: 'licence', obj: VIDEO_LICENCES, key })
+ }
+
+ const peertubeHelpers = {
+ logger
+ }
+
+ return {
+ registerHook,
+ registerSetting,
+ settingsManager,
+ storageManager,
+ videoLanguageManager,
+ videoCategoryManager,
+ videoLicenceManager,
+ peertubeHelpers
+ }
+ }
+
+ private addConstant<T extends string | number> (parameters: {
+ npmName: string
+ type: AlterableVideoConstant
+ obj: VideoConstant
+ key: T
+ label: string
+ }) {
+ const { npmName, type, obj, key, label } = parameters
+
+ if (obj[key]) {
+ logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
+ return false
+ }
+
+ if (!this.updatedVideoConstants[type][npmName]) {
+ this.updatedVideoConstants[type][npmName] = {
+ added: [],
+ deleted: []
+ }
+ }
+
+ this.updatedVideoConstants[type][npmName].added.push({ key, label })
+ obj[key] = label
+
+ return true
+ }
+
+ private deleteConstant<T extends string | number> (parameters: {
+ npmName: string
+ type: AlterableVideoConstant
+ obj: VideoConstant
+ key: T
+ }) {
+ const { npmName, type, obj, key } = parameters
+
+ if (!obj[key]) {
+ logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key)
+ return false
+ }
+
+ if (!this.updatedVideoConstants[type][npmName]) {
+ this.updatedVideoConstants[type][npmName] = {
+ added: [],
+ deleted: []
+ }
+ }
+
+ this.updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] })
+ delete obj[key]
+
+ return true
+ }
+
+ private reinitVideoConstants (npmName: string) {
+ const hash = {
+ language: VIDEO_LANGUAGES,
+ licence: VIDEO_LICENCES,
+ category: VIDEO_CATEGORIES
+ }
+ const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category' ]
+
+ for (const type of types) {
+ const updatedConstants = this.updatedVideoConstants[type][npmName]
+ if (!updatedConstants) continue
+
+ for (const added of updatedConstants.added) {
+ delete hash[type][added.key]
+ }
+
+ for (const deleted of updatedConstants.deleted) {
+ hash[type][deleted.key] = deleted.label
+ }
+
+ delete this.updatedVideoConstants[type][npmName]
+ }
+ }
+
+ private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) {
+ if (!packageJSON.staticDirs) packageJSON.staticDirs = {}
+ if (!packageJSON.css) packageJSON.css = []
+ if (!packageJSON.clientScripts) packageJSON.clientScripts = []
+ if (!packageJSON.translations) packageJSON.translations = {}
+
+ const { result: packageJSONValid, badFields } = isPackageJSONValid(packageJSON, pluginType)
+ if (!packageJSONValid) {
+ const formattedFields = badFields.map(f => `"${f}"`)
+ .join(', ')
+
+ throw new Error(`PackageJSON is invalid (invalid fields: ${formattedFields}).`)
+ }