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/external-auth'
11 import { PluginModel } from '@server/models/server/plugin'
13 RegisterServerAuthExternalOptions,
14 RegisterServerAuthExternalResult,
15 RegisterServerAuthPassOptions,
16 RegisterServerExternalAuthenticatedResult,
18 } from '@server/types/plugins'
20 EncoderOptionsBuilder,
21 PluginPlaylistPrivacyManager,
22 PluginSettingsManager,
24 PluginVideoCategoryManager,
25 PluginVideoLanguageManager,
26 PluginVideoLicenceManager,
27 PluginVideoPrivacyManager,
28 RegisterServerHookOptions,
29 RegisterServerSettingOptions,
31 } from '@shared/models'
32 import { VideoTranscodingProfilesManager } from '../transcoding/video-transcoding-profiles'
33 import { buildPluginHelpers } from './plugin-helpers-builder'
35 type AlterableVideoConstant = 'language' | 'licence' | 'category' | 'privacy' | 'playlistPrivacy'
36 type VideoConstant = { [key in number | string]: string }
38 type UpdatedVideoConstant = {
39 [name in AlterableVideoConstant]: {
41 added: { key: number | string, label: string }[]
42 deleted: { key: number | string, label: string }[]
47 export class RegisterHelpers {
48 private readonly updatedVideoConstants: UpdatedVideoConstant = {
56 private readonly transcodingProfiles: {
57 [ npmName: string ]: {
64 private readonly transcodingEncoders: {
65 [ npmName: string ]: {
67 streamType: 'audio' | 'video'
73 private readonly settings: RegisterServerSettingOptions[] = []
75 private idAndPassAuths: RegisterServerAuthPassOptions[] = []
76 private externalAuths: RegisterServerAuthExternalOptions[] = []
78 private readonly onSettingsChangeCallbacks: ((settings: any) => Promise<any>)[] = []
80 private readonly router: express.Router
83 private readonly npmName: string,
84 private readonly plugin: PluginModel,
85 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
87 this.router = express.Router()
90 buildRegisterHelpers (): RegisterServerOptions {
91 const registerHook = this.buildRegisterHook()
92 const registerSetting = this.buildRegisterSetting()
94 const getRouter = this.buildGetRouter()
96 const settingsManager = this.buildSettingsManager()
97 const storageManager = this.buildStorageManager()
99 const videoLanguageManager = this.buildVideoLanguageManager()
101 const videoLicenceManager = this.buildVideoLicenceManager()
102 const videoCategoryManager = this.buildVideoCategoryManager()
104 const videoPrivacyManager = this.buildVideoPrivacyManager()
105 const playlistPrivacyManager = this.buildPlaylistPrivacyManager()
107 const transcodingManager = this.buildTranscodingManager()
109 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
110 const registerExternalAuth = this.buildRegisterExternalAuth()
111 const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
112 const unregisterExternalAuth = this.buildUnregisterExternalAuth()
114 const peertubeHelpers = buildPluginHelpers(this.plugin, this.npmName)
125 videoLanguageManager,
126 videoCategoryManager,
130 playlistPrivacyManager,
134 registerIdAndPassAuth,
135 registerExternalAuth,
136 unregisterIdAndPassAuth,
137 unregisterExternalAuth,
143 reinitVideoConstants (npmName: string) {
145 language: VIDEO_LANGUAGES,
146 licence: VIDEO_LICENCES,
147 category: VIDEO_CATEGORIES,
148 privacy: VIDEO_PRIVACIES,
149 playlistPrivacy: VIDEO_PLAYLIST_PRIVACIES
151 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category', 'privacy', 'playlistPrivacy' ]
153 for (const type of types) {
154 const updatedConstants = this.updatedVideoConstants[type][npmName]
155 if (!updatedConstants) continue
157 for (const added of updatedConstants.added) {
158 delete hash[type][added.key]
161 for (const deleted of updatedConstants.deleted) {
162 hash[type][deleted.key] = deleted.label
165 delete this.updatedVideoConstants[type][npmName]
169 reinitTranscodingProfilesAndEncoders (npmName: string) {
170 const profiles = this.transcodingProfiles[npmName]
171 if (Array.isArray(profiles)) {
172 for (const profile of profiles) {
173 VideoTranscodingProfilesManager.Instance.removeProfile(profile)
177 const encoders = this.transcodingEncoders[npmName]
178 if (Array.isArray(encoders)) {
179 for (const o of encoders) {
180 VideoTranscodingProfilesManager.Instance.removeEncoderPriority(o.type, o.streamType, o.encoder, o.priority)
193 getIdAndPassAuths () {
194 return this.idAndPassAuths
197 getExternalAuths () {
198 return this.externalAuths
201 getOnSettingsChangedCallbacks () {
202 return this.onSettingsChangeCallbacks
205 private buildGetRouter () {
206 return () => this.router
209 private buildRegisterSetting () {
210 return (options: RegisterServerSettingOptions) => {
211 this.settings.push(options)
215 private buildRegisterHook () {
216 return (options: RegisterServerHookOptions) => {
217 if (serverHookObject[options.target] !== true) {
218 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
222 return this.onHookAdded(options)
226 private buildRegisterIdAndPassAuth () {
227 return (options: RegisterServerAuthPassOptions) => {
228 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
229 logger.error('Cannot register auth plugin %s: authName, getWeight or login are not valid.', this.npmName, { options })
233 this.idAndPassAuths.push(options)
237 private buildRegisterExternalAuth () {
240 return (options: RegisterServerAuthExternalOptions) => {
241 if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
242 logger.error('Cannot register auth plugin %s: authName, authDisplayName or onAuthRequest are not valid.', this.npmName, { options })
246 this.externalAuths.push(options)
249 userAuthenticated (result: RegisterServerExternalAuthenticatedResult): void {
250 onExternalUserAuthenticated({
251 npmName: self.npmName,
252 authName: options.authName,
255 logger.error('Cannot execute onExternalUserAuthenticated.', { npmName: self.npmName, authName: options.authName, err })
258 } as RegisterServerAuthExternalResult
262 private buildUnregisterExternalAuth () {
263 return (authName: string) => {
264 this.externalAuths = this.externalAuths.filter(a => a.authName !== authName)
268 private buildUnregisterIdAndPassAuth () {
269 return (authName: string) => {
270 this.idAndPassAuths = this.idAndPassAuths.filter(a => a.authName !== authName)
274 private buildSettingsManager (): PluginSettingsManager {
276 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name, this.settings),
278 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names, this.settings),
280 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
282 onSettingsChange: (cb: (settings: any) => Promise<any>) => this.onSettingsChangeCallbacks.push(cb)
286 private buildStorageManager (): PluginStorageManager {
288 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
290 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
294 private buildVideoLanguageManager (): PluginVideoLanguageManager {
296 addLanguage: (key: string, label: string) => {
297 return this.addConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label })
300 deleteLanguage: (key: string) => {
301 return this.deleteConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
306 private buildVideoCategoryManager (): PluginVideoCategoryManager {
308 addCategory: (key: number, label: string) => {
309 return this.addConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label })
312 deleteCategory: (key: number) => {
313 return this.deleteConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
318 private buildVideoPrivacyManager (): PluginVideoPrivacyManager {
320 deletePrivacy: (key: number) => {
321 return this.deleteConstant({ npmName: this.npmName, type: 'privacy', obj: VIDEO_PRIVACIES, key })
326 private buildPlaylistPrivacyManager (): PluginPlaylistPrivacyManager {
328 deletePlaylistPrivacy: (key: number) => {
329 return this.deleteConstant({ npmName: this.npmName, type: 'playlistPrivacy', obj: VIDEO_PLAYLIST_PRIVACIES, key })
334 private buildVideoLicenceManager (): PluginVideoLicenceManager {
336 addLicence: (key: number, label: string) => {
337 return this.addConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key, label })
340 deleteLicence: (key: number) => {
341 return this.deleteConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key })
346 private addConstant<T extends string | number> (parameters: {
348 type: AlterableVideoConstant
353 const { npmName, type, obj, key, label } = parameters
356 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
360 if (!this.updatedVideoConstants[type][npmName]) {
361 this.updatedVideoConstants[type][npmName] = {
367 this.updatedVideoConstants[type][npmName].added.push({ key, label })
373 private deleteConstant<T extends string | number> (parameters: {
375 type: AlterableVideoConstant
379 const { npmName, type, obj, key } = parameters
382 logger.warn('Cannot delete %s by plugin %s: key %s does not exist.', type, npmName, key)
386 if (!this.updatedVideoConstants[type][npmName]) {
387 this.updatedVideoConstants[type][npmName] = {
393 const updatedConstants = this.updatedVideoConstants[type][npmName]
395 const alreadyAdded = updatedConstants.added.find(a => a.key === key)
397 updatedConstants.added.filter(a => a.key !== key)
398 } else if (obj[key]) {
399 updatedConstants.deleted.push({ key, label: obj[key] })
407 private buildTranscodingManager () {
410 function addProfile (type: 'live' | 'vod', encoder: string, profile: string, builder: EncoderOptionsBuilder) {
411 if (profile === 'default') {
412 logger.error('A plugin cannot add a default live transcoding profile')
416 VideoTranscodingProfilesManager.Instance.addProfile({
423 if (!self.transcodingProfiles[self.npmName]) self.transcodingProfiles[self.npmName] = []
424 self.transcodingProfiles[self.npmName].push({ type, encoder, profile })
429 function addEncoderPriority (type: 'live' | 'vod', streamType: 'audio' | 'video', encoder: string, priority: number) {
430 VideoTranscodingProfilesManager.Instance.addEncoderPriority(type, streamType, encoder, priority)
432 if (!self.transcodingEncoders[self.npmName]) self.transcodingEncoders[self.npmName] = []
433 self.transcodingEncoders[self.npmName].push({ type, streamType, encoder, priority })
437 addLiveProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
438 return addProfile('live', encoder, profile, builder)
441 addVODProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
442 return addProfile('vod', encoder, profile, builder)
445 addLiveEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
446 return addEncoderPriority('live', streamType, encoder, priority)
449 addVODEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
450 return addEncoderPriority('vod', streamType, encoder, priority)
453 removeAllProfilesAndEncoderPriorities () {
454 return self.reinitTranscodingProfilesAndEncoders(self.npmName)