1 import express from 'express'
2 import { Server } from 'http'
3 import { logger } from '@server/helpers/logger'
4 import { onExternalUserAuthenticated } from '@server/lib/auth/external-auth'
5 import { VideoConstantManagerFactory } from '@server/lib/plugins/video-constant-manager-factory'
6 import { PluginModel } from '@server/models/server/plugin'
8 RegisterServerAuthExternalOptions,
9 RegisterServerAuthExternalResult,
10 RegisterServerAuthPassOptions,
11 RegisterServerExternalAuthenticatedResult,
12 RegisterServerOptions,
13 RegisterServerWebSocketRouteOptions
14 } from '@server/types/plugins'
16 EncoderOptionsBuilder,
17 PluginSettingsManager,
19 RegisterServerHookOptions,
20 RegisterServerSettingOptions,
22 SettingsChangeCallback,
25 } from '@shared/models'
26 import { VideoTranscodingProfilesManager } from '../transcoding/default-transcoding-profiles'
27 import { buildPluginHelpers } from './plugin-helpers-builder'
29 export class RegisterHelpers {
30 private readonly transcodingProfiles: {
31 [ npmName: string ]: {
38 private readonly transcodingEncoders: {
39 [ npmName: string ]: {
41 streamType: 'audio' | 'video'
47 private readonly settings: RegisterServerSettingOptions[] = []
49 private idAndPassAuths: RegisterServerAuthPassOptions[] = []
50 private externalAuths: RegisterServerAuthExternalOptions[] = []
52 private readonly onSettingsChangeCallbacks: SettingsChangeCallback[] = []
54 private readonly webSocketRoutes: RegisterServerWebSocketRouteOptions[] = []
56 private readonly router: express.Router
57 private readonly videoConstantManagerFactory: VideoConstantManagerFactory
60 private readonly npmName: string,
61 private readonly plugin: PluginModel,
62 private readonly server: Server,
63 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
65 this.router = express.Router()
66 this.videoConstantManagerFactory = new VideoConstantManagerFactory(this.npmName)
69 buildRegisterHelpers (): RegisterServerOptions {
70 const registerHook = this.buildRegisterHook()
71 const registerSetting = this.buildRegisterSetting()
73 const getRouter = this.buildGetRouter()
74 const registerWebSocketRoute = this.buildRegisterWebSocketRoute()
76 const settingsManager = this.buildSettingsManager()
77 const storageManager = this.buildStorageManager()
79 const videoLanguageManager = this.videoConstantManagerFactory.createVideoConstantManager<string>('language')
81 const videoLicenceManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('licence')
82 const videoCategoryManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('category')
84 const videoPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPrivacy>('privacy')
85 const playlistPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPlaylistPrivacy>('playlistPrivacy')
87 const transcodingManager = this.buildTranscodingManager()
89 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
90 const registerExternalAuth = this.buildRegisterExternalAuth()
91 const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
92 const unregisterExternalAuth = this.buildUnregisterExternalAuth()
94 const peertubeHelpers = buildPluginHelpers(this.server, this.plugin, this.npmName)
101 registerWebSocketRoute,
106 videoLanguageManager: {
107 ...videoLanguageManager,
108 /** @deprecated use `addConstant` instead **/
109 addLanguage: videoLanguageManager.addConstant,
110 /** @deprecated use `deleteConstant` instead **/
111 deleteLanguage: videoLanguageManager.deleteConstant
113 videoCategoryManager: {
114 ...videoCategoryManager,
115 /** @deprecated use `addConstant` instead **/
116 addCategory: videoCategoryManager.addConstant,
117 /** @deprecated use `deleteConstant` instead **/
118 deleteCategory: videoCategoryManager.deleteConstant
120 videoLicenceManager: {
121 ...videoLicenceManager,
122 /** @deprecated use `addConstant` instead **/
123 addLicence: videoLicenceManager.addConstant,
124 /** @deprecated use `deleteConstant` instead **/
125 deleteLicence: videoLicenceManager.deleteConstant
128 videoPrivacyManager: {
129 ...videoPrivacyManager,
130 /** @deprecated use `deleteConstant` instead **/
131 deletePrivacy: videoPrivacyManager.deleteConstant
133 playlistPrivacyManager: {
134 ...playlistPrivacyManager,
135 /** @deprecated use `deleteConstant` instead **/
136 deletePlaylistPrivacy: playlistPrivacyManager.deleteConstant
141 registerIdAndPassAuth,
142 registerExternalAuth,
143 unregisterIdAndPassAuth,
144 unregisterExternalAuth,
150 reinitVideoConstants (npmName: string) {
151 this.videoConstantManagerFactory.resetVideoConstants(npmName)
154 reinitTranscodingProfilesAndEncoders (npmName: string) {
155 const profiles = this.transcodingProfiles[npmName]
156 if (Array.isArray(profiles)) {
157 for (const profile of profiles) {
158 VideoTranscodingProfilesManager.Instance.removeProfile(profile)
162 const encoders = this.transcodingEncoders[npmName]
163 if (Array.isArray(encoders)) {
164 for (const o of encoders) {
165 VideoTranscodingProfilesManager.Instance.removeEncoderPriority(o.type, o.streamType, o.encoder, o.priority)
178 getIdAndPassAuths () {
179 return this.idAndPassAuths
182 getExternalAuths () {
183 return this.externalAuths
186 getOnSettingsChangedCallbacks () {
187 return this.onSettingsChangeCallbacks
190 getWebSocketRoutes () {
191 return this.webSocketRoutes
194 private buildGetRouter () {
195 return () => this.router
198 private buildRegisterWebSocketRoute () {
199 return (options: RegisterServerWebSocketRouteOptions) => {
200 this.webSocketRoutes.push(options)
204 private buildRegisterSetting () {
205 return (options: RegisterServerSettingOptions) => {
206 this.settings.push(options)
210 private buildRegisterHook () {
211 return (options: RegisterServerHookOptions) => {
212 if (serverHookObject[options.target] !== true) {
213 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
217 return this.onHookAdded(options)
221 private buildRegisterIdAndPassAuth () {
222 return (options: RegisterServerAuthPassOptions) => {
223 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
224 logger.error('Cannot register auth plugin %s: authName, getWeight or login are not valid.', this.npmName, { options })
228 this.idAndPassAuths.push(options)
232 private buildRegisterExternalAuth () {
235 return (options: RegisterServerAuthExternalOptions) => {
236 if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
237 logger.error('Cannot register auth plugin %s: authName, authDisplayName or onAuthRequest are not valid.', this.npmName, { options })
241 this.externalAuths.push(options)
244 userAuthenticated (result: RegisterServerExternalAuthenticatedResult): void {
245 onExternalUserAuthenticated({
246 npmName: self.npmName,
247 authName: options.authName,
250 logger.error('Cannot execute onExternalUserAuthenticated.', { npmName: self.npmName, authName: options.authName, err })
253 } as RegisterServerAuthExternalResult
257 private buildUnregisterExternalAuth () {
258 return (authName: string) => {
259 this.externalAuths = this.externalAuths.filter(a => a.authName !== authName)
263 private buildUnregisterIdAndPassAuth () {
264 return (authName: string) => {
265 this.idAndPassAuths = this.idAndPassAuths.filter(a => a.authName !== authName)
269 private buildSettingsManager (): PluginSettingsManager {
271 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name, this.settings),
273 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names, this.settings),
275 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
277 onSettingsChange: (cb: SettingsChangeCallback) => this.onSettingsChangeCallbacks.push(cb)
281 private buildStorageManager (): PluginStorageManager {
283 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
285 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
289 private buildTranscodingManager () {
292 function addProfile (type: 'live' | 'vod', encoder: string, profile: string, builder: EncoderOptionsBuilder) {
293 if (profile === 'default') {
294 logger.error('A plugin cannot add a default live transcoding profile')
298 VideoTranscodingProfilesManager.Instance.addProfile({
305 if (!self.transcodingProfiles[self.npmName]) self.transcodingProfiles[self.npmName] = []
306 self.transcodingProfiles[self.npmName].push({ type, encoder, profile })
311 function addEncoderPriority (type: 'live' | 'vod', streamType: 'audio' | 'video', encoder: string, priority: number) {
312 VideoTranscodingProfilesManager.Instance.addEncoderPriority(type, streamType, encoder, priority)
314 if (!self.transcodingEncoders[self.npmName]) self.transcodingEncoders[self.npmName] = []
315 self.transcodingEncoders[self.npmName].push({ type, streamType, encoder, priority })
319 addLiveProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
320 return addProfile('live', encoder, profile, builder)
323 addVODProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
324 return addProfile('vod', encoder, profile, builder)
327 addLiveEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
328 return addEncoderPriority('live', streamType, encoder, priority)
331 addVODEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
332 return addEncoderPriority('vod', streamType, encoder, priority)
335 removeAllProfilesAndEncoderPriorities () {
336 return self.reinitTranscodingProfilesAndEncoders(self.npmName)