1 import * as express from 'express'
2 import { logger } from '@server/helpers/logger'
3 import { onExternalUserAuthenticated } from '@server/lib/auth/external-auth'
4 import { VideoConstantManagerFactory } from '@server/lib/plugins/video-constant-manager-factory'
5 import { PluginModel } from '@server/models/server/plugin'
7 RegisterServerAuthExternalOptions,
8 RegisterServerAuthExternalResult,
9 RegisterServerAuthPassOptions,
10 RegisterServerExternalAuthenticatedResult,
12 } from '@server/types/plugins'
14 EncoderOptionsBuilder,
15 PluginSettingsManager,
17 RegisterServerHookOptions,
18 RegisterServerSettingOptions,
22 } from '@shared/models'
23 import { VideoTranscodingProfilesManager } from '../transcoding/video-transcoding-profiles'
24 import { buildPluginHelpers } from './plugin-helpers-builder'
26 export class RegisterHelpers {
27 private readonly transcodingProfiles: {
28 [ npmName: string ]: {
35 private readonly transcodingEncoders: {
36 [ npmName: string ]: {
38 streamType: 'audio' | 'video'
44 private readonly settings: RegisterServerSettingOptions[] = []
46 private idAndPassAuths: RegisterServerAuthPassOptions[] = []
47 private externalAuths: RegisterServerAuthExternalOptions[] = []
49 private readonly onSettingsChangeCallbacks: ((settings: any) => Promise<any>)[] = []
51 private readonly router: express.Router
52 private readonly videoConstantManagerFactory: VideoConstantManagerFactory
55 private readonly npmName: string,
56 private readonly plugin: PluginModel,
57 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
59 this.router = express.Router()
60 this.videoConstantManagerFactory = new VideoConstantManagerFactory(this.npmName)
63 buildRegisterHelpers (): RegisterServerOptions {
64 const registerHook = this.buildRegisterHook()
65 const registerSetting = this.buildRegisterSetting()
67 const getRouter = this.buildGetRouter()
69 const settingsManager = this.buildSettingsManager()
70 const storageManager = this.buildStorageManager()
72 const videoLanguageManager = this.videoConstantManagerFactory.createVideoConstantManager<string>('language')
74 const videoLicenceManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('licence')
75 const videoCategoryManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('category')
77 const videoPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPrivacy>('privacy')
78 const playlistPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPlaylistPrivacy>('playlistPrivacy')
80 const transcodingManager = this.buildTranscodingManager()
82 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
83 const registerExternalAuth = this.buildRegisterExternalAuth()
84 const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
85 const unregisterExternalAuth = this.buildUnregisterExternalAuth()
87 const peertubeHelpers = buildPluginHelpers(this.plugin, this.npmName)
98 videoLanguageManager: {
99 ...videoLanguageManager,
100 /** @deprecated use `addConstant` instead **/
101 addLanguage: videoLanguageManager.addConstant,
102 /** @deprecated use `deleteConstant` instead **/
103 deleteLanguage: videoLanguageManager.deleteConstant
105 videoCategoryManager: {
106 ...videoCategoryManager,
107 /** @deprecated use `addConstant` instead **/
108 addCategory: videoCategoryManager.addConstant,
109 /** @deprecated use `deleteConstant` instead **/
110 deleteCategory: videoCategoryManager.deleteConstant
112 videoLicenceManager: {
113 ...videoLicenceManager,
114 /** @deprecated use `addConstant` instead **/
115 addLicence: videoLicenceManager.addConstant,
116 /** @deprecated use `deleteConstant` instead **/
117 deleteLicence: videoLicenceManager.deleteConstant
120 videoPrivacyManager: {
121 ...videoPrivacyManager,
122 /** @deprecated use `deleteConstant` instead **/
123 deletePrivacy: videoPrivacyManager.deleteConstant
125 playlistPrivacyManager: {
126 ...playlistPrivacyManager,
127 /** @deprecated use `deleteConstant` instead **/
128 deletePlaylistPrivacy: playlistPrivacyManager.deleteConstant
133 registerIdAndPassAuth,
134 registerExternalAuth,
135 unregisterIdAndPassAuth,
136 unregisterExternalAuth,
142 reinitVideoConstants (npmName: string) {
143 this.videoConstantManagerFactory.resetVideoConstants(npmName)
146 reinitTranscodingProfilesAndEncoders (npmName: string) {
147 const profiles = this.transcodingProfiles[npmName]
148 if (Array.isArray(profiles)) {
149 for (const profile of profiles) {
150 VideoTranscodingProfilesManager.Instance.removeProfile(profile)
154 const encoders = this.transcodingEncoders[npmName]
155 if (Array.isArray(encoders)) {
156 for (const o of encoders) {
157 VideoTranscodingProfilesManager.Instance.removeEncoderPriority(o.type, o.streamType, o.encoder, o.priority)
170 getIdAndPassAuths () {
171 return this.idAndPassAuths
174 getExternalAuths () {
175 return this.externalAuths
178 getOnSettingsChangedCallbacks () {
179 return this.onSettingsChangeCallbacks
182 private buildGetRouter () {
183 return () => this.router
186 private buildRegisterSetting () {
187 return (options: RegisterServerSettingOptions) => {
188 this.settings.push(options)
192 private buildRegisterHook () {
193 return (options: RegisterServerHookOptions) => {
194 if (serverHookObject[options.target] !== true) {
195 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
199 return this.onHookAdded(options)
203 private buildRegisterIdAndPassAuth () {
204 return (options: RegisterServerAuthPassOptions) => {
205 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
206 logger.error('Cannot register auth plugin %s: authName, getWeight or login are not valid.', this.npmName, { options })
210 this.idAndPassAuths.push(options)
214 private buildRegisterExternalAuth () {
217 return (options: RegisterServerAuthExternalOptions) => {
218 if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
219 logger.error('Cannot register auth plugin %s: authName, authDisplayName or onAuthRequest are not valid.', this.npmName, { options })
223 this.externalAuths.push(options)
226 userAuthenticated (result: RegisterServerExternalAuthenticatedResult): void {
227 onExternalUserAuthenticated({
228 npmName: self.npmName,
229 authName: options.authName,
232 logger.error('Cannot execute onExternalUserAuthenticated.', { npmName: self.npmName, authName: options.authName, err })
235 } as RegisterServerAuthExternalResult
239 private buildUnregisterExternalAuth () {
240 return (authName: string) => {
241 this.externalAuths = this.externalAuths.filter(a => a.authName !== authName)
245 private buildUnregisterIdAndPassAuth () {
246 return (authName: string) => {
247 this.idAndPassAuths = this.idAndPassAuths.filter(a => a.authName !== authName)
251 private buildSettingsManager (): PluginSettingsManager {
253 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name, this.settings),
255 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names, this.settings),
257 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
259 onSettingsChange: (cb: (settings: any) => Promise<any>) => this.onSettingsChangeCallbacks.push(cb)
263 private buildStorageManager (): PluginStorageManager {
265 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
267 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
271 private buildTranscodingManager () {
274 function addProfile (type: 'live' | 'vod', encoder: string, profile: string, builder: EncoderOptionsBuilder) {
275 if (profile === 'default') {
276 logger.error('A plugin cannot add a default live transcoding profile')
280 VideoTranscodingProfilesManager.Instance.addProfile({
287 if (!self.transcodingProfiles[self.npmName]) self.transcodingProfiles[self.npmName] = []
288 self.transcodingProfiles[self.npmName].push({ type, encoder, profile })
293 function addEncoderPriority (type: 'live' | 'vod', streamType: 'audio' | 'video', encoder: string, priority: number) {
294 VideoTranscodingProfilesManager.Instance.addEncoderPriority(type, streamType, encoder, priority)
296 if (!self.transcodingEncoders[self.npmName]) self.transcodingEncoders[self.npmName] = []
297 self.transcodingEncoders[self.npmName].push({ type, streamType, encoder, priority })
301 addLiveProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
302 return addProfile('live', encoder, profile, builder)
305 addVODProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
306 return addProfile('vod', encoder, profile, builder)
309 addLiveEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
310 return addEncoderPriority('live', streamType, encoder, priority)
313 addVODEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
314 return addEncoderPriority('vod', streamType, encoder, priority)
317 removeAllProfilesAndEncoderPriorities () {
318 return self.reinitTranscodingProfilesAndEncoders(self.npmName)