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
30 } from '@shared/models'
31 import { serverHookObject } from '@shared/models/plugins/server-hook.model'
32 import { VideoTranscodingProfilesManager } from '../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]: {
40 added: { key: number | string, label: string }[]
41 deleted: { key: number | string, label: string }[]
45 export class RegisterHelpers {
46 private readonly updatedVideoConstants: UpdatedVideoConstant = {
47 playlistPrivacy: { added: [], deleted: [] },
48 privacy: { added: [], deleted: [] },
49 language: { added: [], deleted: [] },
50 licence: { added: [], deleted: [] },
51 category: { added: [], deleted: [] }
54 private readonly transcodingProfiles: {
55 [ npmName: string ]: {
62 private readonly transcodingEncoders: {
63 [ npmName: string ]: {
65 streamType: 'audio' | 'video'
71 private readonly settings: RegisterServerSettingOptions[] = []
73 private idAndPassAuths: RegisterServerAuthPassOptions[] = []
74 private externalAuths: RegisterServerAuthExternalOptions[] = []
76 private readonly onSettingsChangeCallbacks: ((settings: any) => Promise<any>)[] = []
78 private readonly router: express.Router
81 private readonly npmName: string,
82 private readonly plugin: PluginModel,
83 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
85 this.router = express.Router()
88 buildRegisterHelpers (): RegisterServerOptions {
89 const registerHook = this.buildRegisterHook()
90 const registerSetting = this.buildRegisterSetting()
92 const getRouter = this.buildGetRouter()
94 const settingsManager = this.buildSettingsManager()
95 const storageManager = this.buildStorageManager()
97 const videoLanguageManager = this.buildVideoLanguageManager()
99 const videoLicenceManager = this.buildVideoLicenceManager()
100 const videoCategoryManager = this.buildVideoCategoryManager()
102 const videoPrivacyManager = this.buildVideoPrivacyManager()
103 const playlistPrivacyManager = this.buildPlaylistPrivacyManager()
105 const transcodingManager = this.buildTranscodingManager()
107 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
108 const registerExternalAuth = this.buildRegisterExternalAuth()
109 const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
110 const unregisterExternalAuth = this.buildUnregisterExternalAuth()
112 const peertubeHelpers = buildPluginHelpers(this.plugin, this.npmName)
123 videoLanguageManager,
124 videoCategoryManager,
128 playlistPrivacyManager,
132 registerIdAndPassAuth,
133 registerExternalAuth,
134 unregisterIdAndPassAuth,
135 unregisterExternalAuth,
141 reinitVideoConstants (npmName: string) {
143 language: VIDEO_LANGUAGES,
144 licence: VIDEO_LICENCES,
145 category: VIDEO_CATEGORIES,
146 privacy: VIDEO_PRIVACIES,
147 playlistPrivacy: VIDEO_PLAYLIST_PRIVACIES
149 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category', 'privacy', 'playlistPrivacy' ]
151 for (const type of types) {
152 const updatedConstants = this.updatedVideoConstants[type][npmName]
153 if (!updatedConstants) continue
155 for (const added of updatedConstants.added) {
156 delete hash[type][added.key]
159 for (const deleted of updatedConstants.deleted) {
160 hash[type][deleted.key] = deleted.label
163 delete this.updatedVideoConstants[type][npmName]
167 reinitTranscodingProfilesAndEncoders (npmName: string) {
168 const profiles = this.transcodingProfiles[npmName]
169 if (Array.isArray(profiles)) {
170 for (const profile of profiles) {
171 VideoTranscodingProfilesManager.Instance.removeProfile(profile)
175 const encoders = this.transcodingEncoders[npmName]
176 if (Array.isArray(encoders)) {
177 for (const o of encoders) {
178 VideoTranscodingProfilesManager.Instance.removeEncoderPriority(o.type, o.streamType, o.encoder, o.priority)
191 getIdAndPassAuths () {
192 return this.idAndPassAuths
195 getExternalAuths () {
196 return this.externalAuths
199 getOnSettingsChangedCallbacks () {
200 return this.onSettingsChangeCallbacks
203 private buildGetRouter () {
204 return () => this.router
207 private buildRegisterSetting () {
208 return (options: RegisterServerSettingOptions) => {
209 this.settings.push(options)
213 private buildRegisterHook () {
214 return (options: RegisterServerHookOptions) => {
215 if (serverHookObject[options.target] !== true) {
216 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
220 return this.onHookAdded(options)
224 private buildRegisterIdAndPassAuth () {
225 return (options: RegisterServerAuthPassOptions) => {
226 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
227 logger.error('Cannot register auth plugin %s: authName, getWeight or login are not valid.', this.npmName, { options })
231 this.idAndPassAuths.push(options)
235 private buildRegisterExternalAuth () {
238 return (options: RegisterServerAuthExternalOptions) => {
239 if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
240 logger.error('Cannot register auth plugin %s: authName, authDisplayName or onAuthRequest are not valid.', this.npmName, { options })
244 this.externalAuths.push(options)
247 userAuthenticated (result: RegisterServerExternalAuthenticatedResult): void {
248 onExternalUserAuthenticated({
249 npmName: self.npmName,
250 authName: options.authName,
253 logger.error('Cannot execute onExternalUserAuthenticated.', { npmName: self.npmName, authName: options.authName, err })
256 } as RegisterServerAuthExternalResult
260 private buildUnregisterExternalAuth () {
261 return (authName: string) => {
262 this.externalAuths = this.externalAuths.filter(a => a.authName !== authName)
266 private buildUnregisterIdAndPassAuth () {
267 return (authName: string) => {
268 this.idAndPassAuths = this.idAndPassAuths.filter(a => a.authName !== authName)
272 private buildSettingsManager (): PluginSettingsManager {
274 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name, this.settings),
276 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names, this.settings),
278 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
280 onSettingsChange: (cb: (settings: any) => Promise<any>) => this.onSettingsChangeCallbacks.push(cb)
284 private buildStorageManager (): PluginStorageManager {
286 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
288 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
292 private buildVideoLanguageManager (): PluginVideoLanguageManager {
294 addLanguage: (key: string, label: string) => {
295 return this.addConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label })
298 deleteLanguage: (key: string) => {
299 return this.deleteConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
304 private buildVideoCategoryManager (): PluginVideoCategoryManager {
306 addCategory: (key: number, label: string) => {
307 return this.addConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label })
310 deleteCategory: (key: number) => {
311 return this.deleteConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
316 private buildVideoPrivacyManager (): PluginVideoPrivacyManager {
318 deletePrivacy: (key: number) => {
319 return this.deleteConstant({ npmName: this.npmName, type: 'privacy', obj: VIDEO_PRIVACIES, key })
324 private buildPlaylistPrivacyManager (): PluginPlaylistPrivacyManager {
326 deletePlaylistPrivacy: (key: number) => {
327 return this.deleteConstant({ npmName: this.npmName, type: 'playlistPrivacy', obj: VIDEO_PLAYLIST_PRIVACIES, key })
332 private buildVideoLicenceManager (): PluginVideoLicenceManager {
334 addLicence: (key: number, label: string) => {
335 return this.addConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key, label })
338 deleteLicence: (key: number) => {
339 return this.deleteConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key })
344 private addConstant<T extends string | number> (parameters: {
346 type: AlterableVideoConstant
351 const { npmName, type, obj, key, label } = parameters
354 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
358 if (!this.updatedVideoConstants[type][npmName]) {
359 this.updatedVideoConstants[type][npmName] = {
365 this.updatedVideoConstants[type][npmName].added.push({ key, label })
371 private deleteConstant<T extends string | number> (parameters: {
373 type: AlterableVideoConstant
377 const { npmName, type, obj, key } = parameters
380 logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key)
384 if (!this.updatedVideoConstants[type][npmName]) {
385 this.updatedVideoConstants[type][npmName] = {
391 this.updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] })
397 private buildTranscodingManager () {
400 function addProfile (type: 'live' | 'vod', encoder: string, profile: string, builder: EncoderOptionsBuilder) {
401 if (profile === 'default') {
402 logger.error('A plugin cannot add a default live transcoding profile')
406 VideoTranscodingProfilesManager.Instance.addProfile({
413 if (!self.transcodingProfiles[self.npmName]) self.transcodingProfiles[self.npmName] = []
414 self.transcodingProfiles[self.npmName].push({ type, encoder, profile })
419 function addEncoderPriority (type: 'live' | 'vod', streamType: 'audio' | 'video', encoder: string, priority: number) {
420 VideoTranscodingProfilesManager.Instance.addEncoderPriority(type, streamType, encoder, priority)
422 if (!self.transcodingEncoders[self.npmName]) self.transcodingEncoders[self.npmName] = []
423 self.transcodingEncoders[self.npmName].push({ type, streamType, encoder, priority })
427 addLiveProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
428 return addProfile('live', encoder, profile, builder)
431 addVODProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
432 return addProfile('vod', encoder, profile, builder)
435 addLiveEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
436 return addEncoderPriority('live', streamType, encoder, priority)
439 addVODEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
440 return addEncoderPriority('vod', streamType, encoder, priority)
443 removeAllProfilesAndEncoderPriorities () {
444 return self.reinitTranscodingProfilesAndEncoders(self.npmName)