]>
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, |
d2d4a5a9 | 20 | SettingsChangeCallback, |
dc3d9022 | 21 | VideoPlaylistPrivacy, |
22 | VideoPrivacy | |
67ed6552 | 23 | } from '@shared/models' |
c729caf6 | 24 | import { VideoTranscodingProfilesManager } from '../transcoding/default-transcoding-profiles' |
1896bca0 | 25 | import { buildPluginHelpers } from './plugin-helpers-builder' |
5e2b2e27 | 26 | |
1896bca0 | 27 | export class RegisterHelpers { |
1896bca0 C |
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 | ||
5e2b2e27 C |
45 | private readonly settings: RegisterServerSettingOptions[] = [] |
46 | ||
a4995eb7 C |
47 | private idAndPassAuths: RegisterServerAuthPassOptions[] = [] |
48 | private externalAuths: RegisterServerAuthExternalOptions[] = [] | |
7fed6375 | 49 | |
d2d4a5a9 | 50 | private readonly onSettingsChangeCallbacks: SettingsChangeCallback[] = [] |
a5896799 | 51 | |
5e2b2e27 | 52 | private readonly router: express.Router |
dc3d9022 | 53 | private readonly videoConstantManagerFactory: VideoConstantManagerFactory |
5e2b2e27 C |
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() | |
dc3d9022 | 61 | this.videoConstantManagerFactory = new VideoConstantManagerFactory(this.npmName) |
5e2b2e27 C |
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 | ||
dc3d9022 | 73 | const videoLanguageManager = this.videoConstantManagerFactory.createVideoConstantManager<string>('language') |
5e2b2e27 | 74 | |
dc3d9022 | 75 | const videoLicenceManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('licence') |
76 | const videoCategoryManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('category') | |
5e2b2e27 | 77 | |
dc3d9022 | 78 | const videoPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPrivacy>('privacy') |
79 | const playlistPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPlaylistPrivacy>('playlistPrivacy') | |
b3af2601 | 80 | |
1896bca0 C |
81 | const transcodingManager = this.buildTranscodingManager() |
82 | ||
7fed6375 C |
83 | const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth() |
84 | const registerExternalAuth = this.buildRegisterExternalAuth() | |
a4995eb7 C |
85 | const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth() |
86 | const unregisterExternalAuth = this.buildUnregisterExternalAuth() | |
7fed6375 | 87 | |
22820226 | 88 | const peertubeHelpers = buildPluginHelpers(this.plugin, this.npmName) |
5e2b2e27 C |
89 | |
90 | return { | |
91 | registerHook, | |
92 | registerSetting, | |
93 | ||
94 | getRouter, | |
95 | ||
96 | settingsManager, | |
97 | storageManager, | |
98 | ||
dc3d9022 | 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 | }, | |
5e2b2e27 | 120 | |
dc3d9022 | 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 | }, | |
b3af2601 | 131 | |
1896bca0 C |
132 | transcodingManager, |
133 | ||
7fed6375 C |
134 | registerIdAndPassAuth, |
135 | registerExternalAuth, | |
a4995eb7 C |
136 | unregisterIdAndPassAuth, |
137 | unregisterExternalAuth, | |
7fed6375 | 138 | |
5e2b2e27 C |
139 | peertubeHelpers |
140 | } | |
141 | } | |
142 | ||
143 | reinitVideoConstants (npmName: string) { | |
dc3d9022 | 144 | this.videoConstantManagerFactory.resetVideoConstants(npmName) |
5e2b2e27 C |
145 | } |
146 | ||
1896bca0 C |
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 | ||
5e2b2e27 C |
163 | getSettings () { |
164 | return this.settings | |
165 | } | |
166 | ||
167 | getRouter () { | |
168 | return this.router | |
169 | } | |
170 | ||
7fed6375 C |
171 | getIdAndPassAuths () { |
172 | return this.idAndPassAuths | |
173 | } | |
174 | ||
175 | getExternalAuths () { | |
176 | return this.externalAuths | |
177 | } | |
178 | ||
a5896799 C |
179 | getOnSettingsChangedCallbacks () { |
180 | return this.onSettingsChangeCallbacks | |
181 | } | |
182 | ||
5e2b2e27 C |
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 | ||
7fed6375 C |
204 | private buildRegisterIdAndPassAuth () { |
205 | return (options: RegisterServerAuthPassOptions) => { | |
e1c55031 | 206 | if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') { |
a4995eb7 | 207 | logger.error('Cannot register auth plugin %s: authName, getWeight or login are not valid.', this.npmName, { options }) |
e1c55031 C |
208 | return |
209 | } | |
210 | ||
7fed6375 C |
211 | this.idAndPassAuths.push(options) |
212 | } | |
213 | } | |
214 | ||
215 | private buildRegisterExternalAuth () { | |
216 | const self = this | |
217 | ||
218 | return (options: RegisterServerAuthExternalOptions) => { | |
a5896799 | 219 | if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') { |
a4995eb7 | 220 | logger.error('Cannot register auth plugin %s: authName, authDisplayName or onAuthRequest are not valid.', this.npmName, { options }) |
9107d791 C |
221 | return |
222 | } | |
223 | ||
7fed6375 C |
224 | this.externalAuths.push(options) |
225 | ||
226 | return { | |
4a8d113b C |
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 | }) | |
7fed6375 C |
235 | } |
236 | } as RegisterServerAuthExternalResult | |
237 | } | |
238 | } | |
239 | ||
a4995eb7 C |
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 | ||
5e2b2e27 C |
252 | private buildSettingsManager (): PluginSettingsManager { |
253 | return { | |
bc90883f | 254 | getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name, this.settings), |
5e2b2e27 | 255 | |
bc90883f | 256 | getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names, this.settings), |
055cfb11 | 257 | |
a5896799 C |
258 | setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value), |
259 | ||
d2d4a5a9 | 260 | onSettingsChange: (cb: SettingsChangeCallback) => this.onSettingsChangeCallbacks.push(cb) |
5e2b2e27 C |
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 | ||
1896bca0 C |
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) | |
2498aaea C |
316 | }, |
317 | ||
318 | removeAllProfilesAndEncoderPriorities () { | |
319 | return self.reinitTranscodingProfilesAndEncoders(self.npmName) | |
1896bca0 C |
320 | } |
321 | } | |
322 | } | |
5e2b2e27 | 323 | } |