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'
13 RegisterServerAuthExternalOptions,
14 RegisterServerAuthExternalResult,
15 RegisterServerAuthPassOptions,
16 RegisterServerExternalAuthenticatedResult,
18 } from '@server/types/plugins'
20 PluginPlaylistPrivacyManager,
21 PluginSettingsManager,
23 PluginVideoCategoryManager,
24 PluginVideoLanguageManager,
25 PluginVideoLicenceManager,
26 PluginVideoPrivacyManager,
27 RegisterServerHookOptions,
28 RegisterServerSettingOptions
29 } from '@shared/models'
30 import { serverHookObject } from '@shared/models/plugins/server-hook.model'
31 import { buildPluginHelpers } from './plugin-helpers'
33 type AlterableVideoConstant = 'language' | 'licence' | 'category' | 'privacy' | 'playlistPrivacy'
34 type VideoConstant = { [key in number | string]: string }
36 type UpdatedVideoConstant = {
37 [name in AlterableVideoConstant]: {
38 added: { key: number | string, label: string }[]
39 deleted: { key: number | string, label: string }[]
43 export class RegisterHelpersStore {
44 private readonly updatedVideoConstants: UpdatedVideoConstant = {
45 playlistPrivacy: { added: [], deleted: [] },
46 privacy: { added: [], deleted: [] },
47 language: { added: [], deleted: [] },
48 licence: { added: [], deleted: [] },
49 category: { added: [], deleted: [] }
52 private readonly settings: RegisterServerSettingOptions[] = []
54 private idAndPassAuths: RegisterServerAuthPassOptions[] = []
55 private externalAuths: RegisterServerAuthExternalOptions[] = []
57 private readonly onSettingsChangeCallbacks: ((settings: any) => void)[] = []
59 private readonly router: express.Router
62 private readonly npmName: string,
63 private readonly plugin: PluginModel,
64 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
66 this.router = express.Router()
69 buildRegisterHelpers (): RegisterServerOptions {
70 const registerHook = this.buildRegisterHook()
71 const registerSetting = this.buildRegisterSetting()
73 const getRouter = this.buildGetRouter()
75 const settingsManager = this.buildSettingsManager()
76 const storageManager = this.buildStorageManager()
78 const videoLanguageManager = this.buildVideoLanguageManager()
80 const videoLicenceManager = this.buildVideoLicenceManager()
81 const videoCategoryManager = this.buildVideoCategoryManager()
83 const videoPrivacyManager = this.buildVideoPrivacyManager()
84 const playlistPrivacyManager = this.buildPlaylistPrivacyManager()
86 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
87 const registerExternalAuth = this.buildRegisterExternalAuth()
88 const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
89 const unregisterExternalAuth = this.buildUnregisterExternalAuth()
91 const peertubeHelpers = buildPluginHelpers(this.npmName)
102 videoLanguageManager,
103 videoCategoryManager,
107 playlistPrivacyManager,
109 registerIdAndPassAuth,
110 registerExternalAuth,
111 unregisterIdAndPassAuth,
112 unregisterExternalAuth,
118 reinitVideoConstants (npmName: string) {
120 language: VIDEO_LANGUAGES,
121 licence: VIDEO_LICENCES,
122 category: VIDEO_CATEGORIES,
123 privacy: VIDEO_PRIVACIES,
124 playlistPrivacy: VIDEO_PLAYLIST_PRIVACIES
126 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category', 'privacy', 'playlistPrivacy' ]
128 for (const type of types) {
129 const updatedConstants = this.updatedVideoConstants[type][npmName]
130 if (!updatedConstants) continue
132 for (const added of updatedConstants.added) {
133 delete hash[type][added.key]
136 for (const deleted of updatedConstants.deleted) {
137 hash[type][deleted.key] = deleted.label
140 delete this.updatedVideoConstants[type][npmName]
152 getIdAndPassAuths () {
153 return this.idAndPassAuths
156 getExternalAuths () {
157 return this.externalAuths
160 getOnSettingsChangedCallbacks () {
161 return this.onSettingsChangeCallbacks
164 private buildGetRouter () {
165 return () => this.router
168 private buildRegisterSetting () {
169 return (options: RegisterServerSettingOptions) => {
170 this.settings.push(options)
174 private buildRegisterHook () {
175 return (options: RegisterServerHookOptions) => {
176 if (serverHookObject[options.target] !== true) {
177 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
181 return this.onHookAdded(options)
185 private buildRegisterIdAndPassAuth () {
186 return (options: RegisterServerAuthPassOptions) => {
187 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
188 logger.error('Cannot register auth plugin %s: authName, getWeight or login are not valid.', this.npmName, { options })
192 this.idAndPassAuths.push(options)
196 private buildRegisterExternalAuth () {
199 return (options: RegisterServerAuthExternalOptions) => {
200 if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
201 logger.error('Cannot register auth plugin %s: authName, authDisplayName or onAuthRequest are not valid.', this.npmName, { options })
205 this.externalAuths.push(options)
208 userAuthenticated (result: RegisterServerExternalAuthenticatedResult): void {
209 onExternalUserAuthenticated({
210 npmName: self.npmName,
211 authName: options.authName,
214 logger.error('Cannot execute onExternalUserAuthenticated.', { npmName: self.npmName, authName: options.authName, err })
217 } as RegisterServerAuthExternalResult
221 private buildUnregisterExternalAuth () {
222 return (authName: string) => {
223 this.externalAuths = this.externalAuths.filter(a => a.authName !== authName)
227 private buildUnregisterIdAndPassAuth () {
228 return (authName: string) => {
229 this.idAndPassAuths = this.idAndPassAuths.filter(a => a.authName !== authName)
233 private buildSettingsManager (): PluginSettingsManager {
235 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name, this.settings),
237 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names, this.settings),
239 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
241 onSettingsChange: (cb: (settings: any) => void) => this.onSettingsChangeCallbacks.push(cb)
245 private buildStorageManager (): PluginStorageManager {
247 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
249 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
253 private buildVideoLanguageManager (): PluginVideoLanguageManager {
255 addLanguage: (key: string, label: string) => {
256 return this.addConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label })
259 deleteLanguage: (key: string) => {
260 return this.deleteConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
265 private buildVideoCategoryManager (): PluginVideoCategoryManager {
267 addCategory: (key: number, label: string) => {
268 return this.addConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label })
271 deleteCategory: (key: number) => {
272 return this.deleteConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
277 private buildVideoPrivacyManager (): PluginVideoPrivacyManager {
279 deletePrivacy: (key: number) => {
280 return this.deleteConstant({ npmName: this.npmName, type: 'privacy', obj: VIDEO_PRIVACIES, key })
285 private buildPlaylistPrivacyManager (): PluginPlaylistPrivacyManager {
287 deletePlaylistPrivacy: (key: number) => {
288 return this.deleteConstant({ npmName: this.npmName, type: 'playlistPrivacy', obj: VIDEO_PLAYLIST_PRIVACIES, key })
293 private buildVideoLicenceManager (): PluginVideoLicenceManager {
295 addLicence: (key: number, label: string) => {
296 return this.addConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key, label })
299 deleteLicence: (key: number) => {
300 return this.deleteConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key })
305 private addConstant<T extends string | number> (parameters: {
307 type: AlterableVideoConstant
312 const { npmName, type, obj, key, label } = parameters
315 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
319 if (!this.updatedVideoConstants[type][npmName]) {
320 this.updatedVideoConstants[type][npmName] = {
326 this.updatedVideoConstants[type][npmName].added.push({ key, label })
332 private deleteConstant<T extends string | number> (parameters: {
334 type: AlterableVideoConstant
338 const { npmName, type, obj, key } = parameters
341 logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key)
345 if (!this.updatedVideoConstants[type][npmName]) {
346 this.updatedVideoConstants[type][npmName] = {
352 this.updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] })