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'
9 VIDEO_PLAYLIST_PRIVACIES,
11 } from '@server/initializers/constants'
12 import { PluginVideoLicenceManager } from '@shared/models/plugins/plugin-video-licence-manager.model'
13 import { PluginVideoCategoryManager } from '@shared/models/plugins/plugin-video-category-manager.model'
14 import { RegisterServerOptions } from '@server/typings/plugins'
15 import { buildPluginHelpers } from './plugin-helpers'
16 import { logger } from '@server/helpers/logger'
17 import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model'
18 import { serverHookObject } from '@shared/models/plugins/server-hook.model'
19 import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
20 import * as express from 'express'
21 import { PluginVideoPrivacyManager } from '@shared/models/plugins/plugin-video-privacy-manager.model'
22 import { PluginPlaylistPrivacyManager } from '@shared/models/plugins/plugin-playlist-privacy-manager.model'
24 RegisterServerAuthExternalOptions,
25 RegisterServerAuthExternalResult,
26 RegisterServerAuthPassOptions
27 } from '@shared/models/plugins/register-server-auth.model'
28 import { onExternalAuthPlugin } from '@server/lib/auth'
30 type AlterableVideoConstant = 'language' | 'licence' | 'category' | 'privacy' | 'playlistPrivacy'
31 type VideoConstant = { [key in number | string]: string }
33 type UpdatedVideoConstant = {
34 [name in AlterableVideoConstant]: {
35 added: { key: number | string, label: string }[]
36 deleted: { key: number | string, label: string }[]
40 export class RegisterHelpersStore {
41 private readonly updatedVideoConstants: UpdatedVideoConstant = {
42 playlistPrivacy: { added: [], deleted: [] },
43 privacy: { added: [], deleted: [] },
44 language: { added: [], deleted: [] },
45 licence: { added: [], deleted: [] },
46 category: { added: [], deleted: [] }
49 private readonly settings: RegisterServerSettingOptions[] = []
51 private readonly idAndPassAuths: RegisterServerAuthPassOptions[] = []
52 private readonly externalAuths: RegisterServerAuthExternalOptions[] = []
54 private readonly router: express.Router
57 private readonly npmName: string,
58 private readonly plugin: PluginModel,
59 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
61 this.router = express.Router()
64 buildRegisterHelpers (): RegisterServerOptions {
65 const registerHook = this.buildRegisterHook()
66 const registerSetting = this.buildRegisterSetting()
68 const getRouter = this.buildGetRouter()
70 const settingsManager = this.buildSettingsManager()
71 const storageManager = this.buildStorageManager()
73 const videoLanguageManager = this.buildVideoLanguageManager()
75 const videoLicenceManager = this.buildVideoLicenceManager()
76 const videoCategoryManager = this.buildVideoCategoryManager()
78 const videoPrivacyManager = this.buildVideoPrivacyManager()
79 const playlistPrivacyManager = this.buildPlaylistPrivacyManager()
81 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
82 const registerExternalAuth = this.buildRegisterExternalAuth()
84 const peertubeHelpers = buildPluginHelpers(this.npmName)
100 playlistPrivacyManager,
102 registerIdAndPassAuth,
103 registerExternalAuth,
109 reinitVideoConstants (npmName: string) {
111 language: VIDEO_LANGUAGES,
112 licence: VIDEO_LICENCES,
113 category: VIDEO_CATEGORIES,
114 privacy: VIDEO_PRIVACIES,
115 playlistPrivacy: VIDEO_PLAYLIST_PRIVACIES
117 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category', 'privacy', 'playlistPrivacy' ]
119 for (const type of types) {
120 const updatedConstants = this.updatedVideoConstants[type][npmName]
121 if (!updatedConstants) continue
123 for (const added of updatedConstants.added) {
124 delete hash[type][added.key]
127 for (const deleted of updatedConstants.deleted) {
128 hash[type][deleted.key] = deleted.label
131 delete this.updatedVideoConstants[type][npmName]
143 getIdAndPassAuths () {
144 return this.idAndPassAuths
147 getExternalAuths () {
148 return this.externalAuths
151 private buildGetRouter () {
152 return () => this.router
155 private buildRegisterSetting () {
156 return (options: RegisterServerSettingOptions) => {
157 this.settings.push(options)
161 private buildRegisterHook () {
162 return (options: RegisterServerHookOptions) => {
163 if (serverHookObject[options.target] !== true) {
164 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
168 return this.onHookAdded(options)
172 private buildRegisterIdAndPassAuth () {
173 return (options: RegisterServerAuthPassOptions) => {
174 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
175 logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName)
179 this.idAndPassAuths.push(options)
183 private buildRegisterExternalAuth () {
186 return (options: RegisterServerAuthExternalOptions) => {
187 this.externalAuths.push(options)
190 onAuth (options: { username: string, email: string }): void {
191 onExternalAuthPlugin(self.npmName, options.username, options.email)
193 } as RegisterServerAuthExternalResult
197 private buildSettingsManager (): PluginSettingsManager {
199 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name),
201 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names),
203 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value)
207 private buildStorageManager (): PluginStorageManager {
209 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
211 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
215 private buildVideoLanguageManager (): PluginVideoLanguageManager {
217 addLanguage: (key: string, label: string) => {
218 return this.addConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label })
221 deleteLanguage: (key: string) => {
222 return this.deleteConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
227 private buildVideoCategoryManager (): PluginVideoCategoryManager {
229 addCategory: (key: number, label: string) => {
230 return this.addConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label })
233 deleteCategory: (key: number) => {
234 return this.deleteConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
239 private buildVideoPrivacyManager (): PluginVideoPrivacyManager {
241 deletePrivacy: (key: number) => {
242 return this.deleteConstant({ npmName: this.npmName, type: 'privacy', obj: VIDEO_PRIVACIES, key })
247 private buildPlaylistPrivacyManager (): PluginPlaylistPrivacyManager {
249 deletePlaylistPrivacy: (key: number) => {
250 return this.deleteConstant({ npmName: this.npmName, type: 'playlistPrivacy', obj: VIDEO_PLAYLIST_PRIVACIES, key })
255 private buildVideoLicenceManager (): PluginVideoLicenceManager {
257 addLicence: (key: number, label: string) => {
258 return this.addConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key, label })
261 deleteLicence: (key: number) => {
262 return this.deleteConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key })
267 private addConstant<T extends string | number> (parameters: {
269 type: AlterableVideoConstant
274 const { npmName, type, obj, key, label } = parameters
277 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
281 if (!this.updatedVideoConstants[type][npmName]) {
282 this.updatedVideoConstants[type][npmName] = {
288 this.updatedVideoConstants[type][npmName].added.push({ key, label })
294 private deleteConstant<T extends string | number> (parameters: {
296 type: AlterableVideoConstant
300 const { npmName, type, obj, key } = parameters
303 logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key)
307 if (!this.updatedVideoConstants[type][npmName]) {
308 this.updatedVideoConstants[type][npmName] = {
314 this.updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] })