]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/plugins/register-helpers.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / register-helpers.ts
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'
7 import {
8 RegisterServerAuthExternalOptions,
9 RegisterServerAuthExternalResult,
10 RegisterServerAuthPassOptions,
11 RegisterServerExternalAuthenticatedResult,
12 RegisterServerOptions,
13 RegisterServerWebSocketRouteOptions
14 } from '@server/types/plugins'
15 import {
16 EncoderOptionsBuilder,
17 PluginSettingsManager,
18 PluginStorageManager,
19 RegisterServerHookOptions,
20 RegisterServerSettingOptions,
21 serverHookObject,
22 SettingsChangeCallback,
23 VideoPlaylistPrivacy,
24 VideoPrivacy
25 } from '@shared/models'
26 import { VideoTranscodingProfilesManager } from '../transcoding/default-transcoding-profiles'
27 import { buildPluginHelpers } from './plugin-helpers-builder'
28
29 export class RegisterHelpers {
30 private readonly transcodingProfiles: {
31 [ npmName: string ]: {
32 type: 'vod' | 'live'
33 encoder: string
34 profile: string
35 }[]
36 } = {}
37
38 private readonly transcodingEncoders: {
39 [ npmName: string ]: {
40 type: 'vod' | 'live'
41 streamType: 'audio' | 'video'
42 encoder: string
43 priority: number
44 }[]
45 } = {}
46
47 private readonly settings: RegisterServerSettingOptions[] = []
48
49 private idAndPassAuths: RegisterServerAuthPassOptions[] = []
50 private externalAuths: RegisterServerAuthExternalOptions[] = []
51
52 private readonly onSettingsChangeCallbacks: SettingsChangeCallback[] = []
53
54 private readonly webSocketRoutes: RegisterServerWebSocketRouteOptions[] = []
55
56 private readonly router: express.Router
57 private readonly videoConstantManagerFactory: VideoConstantManagerFactory
58
59 constructor (
60 private readonly npmName: string,
61 private readonly plugin: PluginModel,
62 private readonly server: Server,
63 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
64 ) {
65 this.router = express.Router()
66 this.videoConstantManagerFactory = new VideoConstantManagerFactory(this.npmName)
67 }
68
69 buildRegisterHelpers (): RegisterServerOptions {
70 const registerHook = this.buildRegisterHook()
71 const registerSetting = this.buildRegisterSetting()
72
73 const getRouter = this.buildGetRouter()
74 const registerWebSocketRoute = this.buildRegisterWebSocketRoute()
75
76 const settingsManager = this.buildSettingsManager()
77 const storageManager = this.buildStorageManager()
78
79 const videoLanguageManager = this.videoConstantManagerFactory.createVideoConstantManager<string>('language')
80
81 const videoLicenceManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('licence')
82 const videoCategoryManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('category')
83
84 const videoPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPrivacy>('privacy')
85 const playlistPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPlaylistPrivacy>('playlistPrivacy')
86
87 const transcodingManager = this.buildTranscodingManager()
88
89 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
90 const registerExternalAuth = this.buildRegisterExternalAuth()
91 const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
92 const unregisterExternalAuth = this.buildUnregisterExternalAuth()
93
94 const peertubeHelpers = buildPluginHelpers(this.server, this.plugin, this.npmName)
95
96 return {
97 registerHook,
98 registerSetting,
99
100 getRouter,
101 registerWebSocketRoute,
102
103 settingsManager,
104 storageManager,
105
106 videoLanguageManager: {
107 ...videoLanguageManager,
108 /** @deprecated use `addConstant` instead **/
109 addLanguage: videoLanguageManager.addConstant,
110 /** @deprecated use `deleteConstant` instead **/
111 deleteLanguage: videoLanguageManager.deleteConstant
112 },
113 videoCategoryManager: {
114 ...videoCategoryManager,
115 /** @deprecated use `addConstant` instead **/
116 addCategory: videoCategoryManager.addConstant,
117 /** @deprecated use `deleteConstant` instead **/
118 deleteCategory: videoCategoryManager.deleteConstant
119 },
120 videoLicenceManager: {
121 ...videoLicenceManager,
122 /** @deprecated use `addConstant` instead **/
123 addLicence: videoLicenceManager.addConstant,
124 /** @deprecated use `deleteConstant` instead **/
125 deleteLicence: videoLicenceManager.deleteConstant
126 },
127
128 videoPrivacyManager: {
129 ...videoPrivacyManager,
130 /** @deprecated use `deleteConstant` instead **/
131 deletePrivacy: videoPrivacyManager.deleteConstant
132 },
133 playlistPrivacyManager: {
134 ...playlistPrivacyManager,
135 /** @deprecated use `deleteConstant` instead **/
136 deletePlaylistPrivacy: playlistPrivacyManager.deleteConstant
137 },
138
139 transcodingManager,
140
141 registerIdAndPassAuth,
142 registerExternalAuth,
143 unregisterIdAndPassAuth,
144 unregisterExternalAuth,
145
146 peertubeHelpers
147 }
148 }
149
150 reinitVideoConstants (npmName: string) {
151 this.videoConstantManagerFactory.resetVideoConstants(npmName)
152 }
153
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)
159 }
160 }
161
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)
166 }
167 }
168 }
169
170 getSettings () {
171 return this.settings
172 }
173
174 getRouter () {
175 return this.router
176 }
177
178 getIdAndPassAuths () {
179 return this.idAndPassAuths
180 }
181
182 getExternalAuths () {
183 return this.externalAuths
184 }
185
186 getOnSettingsChangedCallbacks () {
187 return this.onSettingsChangeCallbacks
188 }
189
190 getWebSocketRoutes () {
191 return this.webSocketRoutes
192 }
193
194 private buildGetRouter () {
195 return () => this.router
196 }
197
198 private buildRegisterWebSocketRoute () {
199 return (options: RegisterServerWebSocketRouteOptions) => {
200 this.webSocketRoutes.push(options)
201 }
202 }
203
204 private buildRegisterSetting () {
205 return (options: RegisterServerSettingOptions) => {
206 this.settings.push(options)
207 }
208 }
209
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)
214 return
215 }
216
217 return this.onHookAdded(options)
218 }
219 }
220
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 })
225 return
226 }
227
228 this.idAndPassAuths.push(options)
229 }
230 }
231
232 private buildRegisterExternalAuth () {
233 const self = this
234
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 })
238 return
239 }
240
241 this.externalAuths.push(options)
242
243 return {
244 userAuthenticated (result: RegisterServerExternalAuthenticatedResult): void {
245 onExternalUserAuthenticated({
246 npmName: self.npmName,
247 authName: options.authName,
248 authResult: result
249 }).catch(err => {
250 logger.error('Cannot execute onExternalUserAuthenticated.', { npmName: self.npmName, authName: options.authName, err })
251 })
252 }
253 } as RegisterServerAuthExternalResult
254 }
255 }
256
257 private buildUnregisterExternalAuth () {
258 return (authName: string) => {
259 this.externalAuths = this.externalAuths.filter(a => a.authName !== authName)
260 }
261 }
262
263 private buildUnregisterIdAndPassAuth () {
264 return (authName: string) => {
265 this.idAndPassAuths = this.idAndPassAuths.filter(a => a.authName !== authName)
266 }
267 }
268
269 private buildSettingsManager (): PluginSettingsManager {
270 return {
271 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name, this.settings),
272
273 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names, this.settings),
274
275 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
276
277 onSettingsChange: (cb: SettingsChangeCallback) => this.onSettingsChangeCallbacks.push(cb)
278 }
279 }
280
281 private buildStorageManager (): PluginStorageManager {
282 return {
283 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
284
285 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
286 }
287 }
288
289 private buildTranscodingManager () {
290 const self = this
291
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')
295 return false
296 }
297
298 VideoTranscodingProfilesManager.Instance.addProfile({
299 type,
300 encoder,
301 profile,
302 builder
303 })
304
305 if (!self.transcodingProfiles[self.npmName]) self.transcodingProfiles[self.npmName] = []
306 self.transcodingProfiles[self.npmName].push({ type, encoder, profile })
307
308 return true
309 }
310
311 function addEncoderPriority (type: 'live' | 'vod', streamType: 'audio' | 'video', encoder: string, priority: number) {
312 VideoTranscodingProfilesManager.Instance.addEncoderPriority(type, streamType, encoder, priority)
313
314 if (!self.transcodingEncoders[self.npmName]) self.transcodingEncoders[self.npmName] = []
315 self.transcodingEncoders[self.npmName].push({ type, streamType, encoder, priority })
316 }
317
318 return {
319 addLiveProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
320 return addProfile('live', encoder, profile, builder)
321 },
322
323 addVODProfile (encoder: string, profile: string, builder: EncoderOptionsBuilder) {
324 return addProfile('vod', encoder, profile, builder)
325 },
326
327 addLiveEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
328 return addEncoderPriority('live', streamType, encoder, priority)
329 },
330
331 addVODEncoderPriority (streamType: 'audio' | 'video', encoder: string, priority: number) {
332 return addEncoderPriority('vod', streamType, encoder, priority)
333 },
334
335 removeAllProfilesAndEncoderPriorities () {
336 return self.reinitTranscodingProfilesAndEncoders(self.npmName)
337 }
338 }
339 }
340 }