1 import * as express from 'express'
2 import { logger } from '@server/helpers/logger'
7 VIDEO_PLAYLIST_PRIVACIES,
9 } from '@server/initializers/constants'
10 import { onExternalUserAuthenticated } from '@server/lib/auth'
11 import { PluginModel } from '@server/models/server/plugin'
12 import { RegisterServerOptions } from '@server/typings/plugins'
13 import { PluginPlaylistPrivacyManager } from '@shared/models/plugins/plugin-playlist-privacy-manager.model'
14 import { PluginSettingsManager } from '@shared/models/plugins/plugin-settings-manager.model'
15 import { PluginStorageManager } from '@shared/models/plugins/plugin-storage-manager.model'
16 import { PluginVideoCategoryManager } from '@shared/models/plugins/plugin-video-category-manager.model'
17 import { PluginVideoLanguageManager } from '@shared/models/plugins/plugin-video-language-manager.model'
18 import { PluginVideoLicenceManager } from '@shared/models/plugins/plugin-video-licence-manager.model'
19 import { PluginVideoPrivacyManager } from '@shared/models/plugins/plugin-video-privacy-manager.model'
21 RegisterServerAuthExternalOptions,
22 RegisterServerAuthExternalResult,
23 RegisterServerAuthPassOptions,
24 RegisterServerExternalAuthenticatedResult
25 } from '@shared/models/plugins/register-server-auth.model'
26 import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model'
27 import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
28 import { serverHookObject } from '@shared/models/plugins/server-hook.model'
29 import { buildPluginHelpers } from './plugin-helpers'
31 type AlterableVideoConstant = 'language' | 'licence' | 'category' | 'privacy' | 'playlistPrivacy'
32 type VideoConstant = { [key in number | string]: string }
34 type UpdatedVideoConstant = {
35 [name in AlterableVideoConstant]: {
36 added: { key: number | string, label: string }[]
37 deleted: { key: number | string, label: string }[]
41 export class RegisterHelpersStore {
42 private readonly updatedVideoConstants: UpdatedVideoConstant = {
43 playlistPrivacy: { added: [], deleted: [] },
44 privacy: { added: [], deleted: [] },
45 language: { added: [], deleted: [] },
46 licence: { added: [], deleted: [] },
47 category: { added: [], deleted: [] }
50 private readonly settings: RegisterServerSettingOptions[] = []
52 private readonly idAndPassAuths: RegisterServerAuthPassOptions[] = []
53 private readonly externalAuths: RegisterServerAuthExternalOptions[] = []
55 private readonly router: express.Router
58 private readonly npmName: string,
59 private readonly plugin: PluginModel,
60 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
62 this.router = express.Router()
65 buildRegisterHelpers (): RegisterServerOptions {
66 const registerHook = this.buildRegisterHook()
67 const registerSetting = this.buildRegisterSetting()
69 const getRouter = this.buildGetRouter()
71 const settingsManager = this.buildSettingsManager()
72 const storageManager = this.buildStorageManager()
74 const videoLanguageManager = this.buildVideoLanguageManager()
76 const videoLicenceManager = this.buildVideoLicenceManager()
77 const videoCategoryManager = this.buildVideoCategoryManager()
79 const videoPrivacyManager = this.buildVideoPrivacyManager()
80 const playlistPrivacyManager = this.buildPlaylistPrivacyManager()
82 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
83 const registerExternalAuth = this.buildRegisterExternalAuth()
85 const peertubeHelpers = buildPluginHelpers(this.npmName)
101 playlistPrivacyManager,
103 registerIdAndPassAuth,
104 registerExternalAuth,
110 reinitVideoConstants (npmName: string) {
112 language: VIDEO_LANGUAGES,
113 licence: VIDEO_LICENCES,
114 category: VIDEO_CATEGORIES,
115 privacy: VIDEO_PRIVACIES,
116 playlistPrivacy: VIDEO_PLAYLIST_PRIVACIES
118 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category', 'privacy', 'playlistPrivacy' ]
120 for (const type of types) {
121 const updatedConstants = this.updatedVideoConstants[type][npmName]
122 if (!updatedConstants) continue
124 for (const added of updatedConstants.added) {
125 delete hash[type][added.key]
128 for (const deleted of updatedConstants.deleted) {
129 hash[type][deleted.key] = deleted.label
132 delete this.updatedVideoConstants[type][npmName]
144 getIdAndPassAuths () {
145 return this.idAndPassAuths
148 getExternalAuths () {
149 return this.externalAuths
152 private buildGetRouter () {
153 return () => this.router
156 private buildRegisterSetting () {
157 return (options: RegisterServerSettingOptions) => {
158 this.settings.push(options)
162 private buildRegisterHook () {
163 return (options: RegisterServerHookOptions) => {
164 if (serverHookObject[options.target] !== true) {
165 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
169 return this.onHookAdded(options)
173 private buildRegisterIdAndPassAuth () {
174 return (options: RegisterServerAuthPassOptions) => {
175 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
176 logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName)
180 this.idAndPassAuths.push(options)
184 private buildRegisterExternalAuth () {
187 return (options: RegisterServerAuthExternalOptions) => {
188 if (!options.authName || !options.onAuthRequest || typeof options.onAuthRequest !== 'function') {
189 logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName)
193 this.externalAuths.push(options)
196 userAuthenticated (result: RegisterServerExternalAuthenticatedResult): void {
197 onExternalUserAuthenticated({
198 npmName: self.npmName,
199 authName: options.authName,
202 logger.error('Cannot execute onExternalUserAuthenticated.', { npmName: self.npmName, authName: options.authName, err })
205 } as RegisterServerAuthExternalResult
209 private buildSettingsManager (): PluginSettingsManager {
211 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name),
213 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names),
215 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value)
219 private buildStorageManager (): PluginStorageManager {
221 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
223 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
227 private buildVideoLanguageManager (): PluginVideoLanguageManager {
229 addLanguage: (key: string, label: string) => {
230 return this.addConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label })
233 deleteLanguage: (key: string) => {
234 return this.deleteConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
239 private buildVideoCategoryManager (): PluginVideoCategoryManager {
241 addCategory: (key: number, label: string) => {
242 return this.addConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label })
245 deleteCategory: (key: number) => {
246 return this.deleteConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
251 private buildVideoPrivacyManager (): PluginVideoPrivacyManager {
253 deletePrivacy: (key: number) => {
254 return this.deleteConstant({ npmName: this.npmName, type: 'privacy', obj: VIDEO_PRIVACIES, key })
259 private buildPlaylistPrivacyManager (): PluginPlaylistPrivacyManager {
261 deletePlaylistPrivacy: (key: number) => {
262 return this.deleteConstant({ npmName: this.npmName, type: 'playlistPrivacy', obj: VIDEO_PLAYLIST_PRIVACIES, key })
267 private buildVideoLicenceManager (): PluginVideoLicenceManager {
269 addLicence: (key: number, label: string) => {
270 return this.addConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key, label })
273 deleteLicence: (key: number) => {
274 return this.deleteConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key })
279 private addConstant<T extends string | number> (parameters: {
281 type: AlterableVideoConstant
286 const { npmName, type, obj, key, label } = parameters
289 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
293 if (!this.updatedVideoConstants[type][npmName]) {
294 this.updatedVideoConstants[type][npmName] = {
300 this.updatedVideoConstants[type][npmName].added.push({ key, label })
306 private deleteConstant<T extends string | number> (parameters: {
308 type: AlterableVideoConstant
312 const { npmName, type, obj, key } = parameters
315 logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key)
319 if (!this.updatedVideoConstants[type][npmName]) {
320 this.updatedVideoConstants[type][npmName] = {
326 this.updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] })