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