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