]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/plugins/register-helpers.ts
Add ability for plugins to register ws routes
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / register-helpers.ts
CommitLineData
41fb13c3 1import express from 'express'
9d4c60dc 2import { Server } from 'http'
4a8d113b 3import { logger } from '@server/helpers/logger'
f43db2f4 4import { onExternalUserAuthenticated } from '@server/lib/auth/external-auth'
dc3d9022 5import { VideoConstantManagerFactory } from '@server/lib/plugins/video-constant-manager-factory'
5e2b2e27 6import { PluginModel } from '@server/models/server/plugin'
9107d791
C
7import {
8 RegisterServerAuthExternalOptions,
9 RegisterServerAuthExternalResult,
10 RegisterServerAuthPassOptions,
67ed6552 11 RegisterServerExternalAuthenticatedResult,
9d4c60dc
C
12 RegisterServerOptions,
13 RegisterServerWebSocketRouteOptions
67ed6552
C
14} from '@server/types/plugins'
15import {
1896bca0 16 EncoderOptionsBuilder,
67ed6552
C
17 PluginSettingsManager,
18 PluginStorageManager,
67ed6552 19 RegisterServerHookOptions,
428ccb8b 20 RegisterServerSettingOptions,
dc3d9022 21 serverHookObject,
d2d4a5a9 22 SettingsChangeCallback,
dc3d9022 23 VideoPlaylistPrivacy,
24 VideoPrivacy
67ed6552 25} from '@shared/models'
c729caf6 26import { VideoTranscodingProfilesManager } from '../transcoding/default-transcoding-profiles'
1896bca0 27import { buildPluginHelpers } from './plugin-helpers-builder'
5e2b2e27 28
1896bca0 29export class RegisterHelpers {
1896bca0
C
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
5e2b2e27
C
47 private readonly settings: RegisterServerSettingOptions[] = []
48
a4995eb7
C
49 private idAndPassAuths: RegisterServerAuthPassOptions[] = []
50 private externalAuths: RegisterServerAuthExternalOptions[] = []
7fed6375 51
d2d4a5a9 52 private readonly onSettingsChangeCallbacks: SettingsChangeCallback[] = []
a5896799 53
9d4c60dc
C
54 private readonly webSocketRoutes: RegisterServerWebSocketRouteOptions[] = []
55
5e2b2e27 56 private readonly router: express.Router
dc3d9022 57 private readonly videoConstantManagerFactory: VideoConstantManagerFactory
5e2b2e27
C
58
59 constructor (
60 private readonly npmName: string,
61 private readonly plugin: PluginModel,
9d4c60dc 62 private readonly server: Server,
5e2b2e27
C
63 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
64 ) {
65 this.router = express.Router()
dc3d9022 66 this.videoConstantManagerFactory = new VideoConstantManagerFactory(this.npmName)
5e2b2e27
C
67 }
68
69 buildRegisterHelpers (): RegisterServerOptions {
70 const registerHook = this.buildRegisterHook()
71 const registerSetting = this.buildRegisterSetting()
72
73 const getRouter = this.buildGetRouter()
9d4c60dc 74 const registerWebSocketRoute = this.buildRegisterWebSocketRoute()
5e2b2e27
C
75
76 const settingsManager = this.buildSettingsManager()
77 const storageManager = this.buildStorageManager()
78
dc3d9022 79 const videoLanguageManager = this.videoConstantManagerFactory.createVideoConstantManager<string>('language')
5e2b2e27 80
dc3d9022 81 const videoLicenceManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('licence')
82 const videoCategoryManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('category')
5e2b2e27 83
dc3d9022 84 const videoPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPrivacy>('privacy')
85 const playlistPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPlaylistPrivacy>('playlistPrivacy')
b3af2601 86
1896bca0
C
87 const transcodingManager = this.buildTranscodingManager()
88
7fed6375
C
89 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
90 const registerExternalAuth = this.buildRegisterExternalAuth()
a4995eb7
C
91 const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
92 const unregisterExternalAuth = this.buildUnregisterExternalAuth()
7fed6375 93
9d4c60dc 94 const peertubeHelpers = buildPluginHelpers(this.server, this.plugin, this.npmName)
5e2b2e27
C
95
96 return {
97 registerHook,
98 registerSetting,
99
100 getRouter,
9d4c60dc 101 registerWebSocketRoute,
5e2b2e27
C
102
103 settingsManager,
104 storageManager,
105
dc3d9022 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 },
5e2b2e27 127
dc3d9022 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 },
b3af2601 138
1896bca0
C
139 transcodingManager,
140
7fed6375
C
141 registerIdAndPassAuth,
142 registerExternalAuth,
a4995eb7
C
143 unregisterIdAndPassAuth,
144 unregisterExternalAuth,
7fed6375 145
5e2b2e27
C
146 peertubeHelpers
147 }
148 }
149
150 reinitVideoConstants (npmName: string) {
dc3d9022 151 this.videoConstantManagerFactory.resetVideoConstants(npmName)
5e2b2e27
C
152 }
153
1896bca0
C
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
5e2b2e27
C
170 getSettings () {
171 return this.settings
172 }
173
174 getRouter () {
175 return this.router
176 }
177
7fed6375
C
178 getIdAndPassAuths () {
179 return this.idAndPassAuths
180 }
181
182 getExternalAuths () {
183 return this.externalAuths
184 }
185
a5896799
C
186 getOnSettingsChangedCallbacks () {
187 return this.onSettingsChangeCallbacks
188 }
189
9d4c60dc
C
190 getWebSocketRoutes () {
191 return this.webSocketRoutes
192 }
193
5e2b2e27
C
194 private buildGetRouter () {
195 return () => this.router
196 }
197
9d4c60dc
C
198 private buildRegisterWebSocketRoute () {
199 return (options: RegisterServerWebSocketRouteOptions) => {
200 this.webSocketRoutes.push(options)
201 }
202 }
203
5e2b2e27
C
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
7fed6375
C
221 private buildRegisterIdAndPassAuth () {
222 return (options: RegisterServerAuthPassOptions) => {
e1c55031 223 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
a4995eb7 224 logger.error('Cannot register auth plugin %s: authName, getWeight or login are not valid.', this.npmName, { options })
e1c55031
C
225 return
226 }
227
7fed6375
C
228 this.idAndPassAuths.push(options)
229 }
230 }
231
232 private buildRegisterExternalAuth () {
233 const self = this
234
235 return (options: RegisterServerAuthExternalOptions) => {
a5896799 236 if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
a4995eb7 237 logger.error('Cannot register auth plugin %s: authName, authDisplayName or onAuthRequest are not valid.', this.npmName, { options })
9107d791
C
238 return
239 }
240
7fed6375
C
241 this.externalAuths.push(options)
242
243 return {
4a8d113b
C
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 })
7fed6375
C
252 }
253 } as RegisterServerAuthExternalResult
254 }
255 }
256
a4995eb7
C
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
5e2b2e27
C
269 private buildSettingsManager (): PluginSettingsManager {
270 return {
bc90883f 271 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name, this.settings),
5e2b2e27 272
bc90883f 273 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names, this.settings),
055cfb11 274
a5896799
C
275 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
276
d2d4a5a9 277 onSettingsChange: (cb: SettingsChangeCallback) => this.onSettingsChangeCallbacks.push(cb)
5e2b2e27
C
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
1896bca0
C
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)
2498aaea
C
333 },
334
335 removeAllProfilesAndEncoderPriorities () {
336 return self.reinitTranscodingProfilesAndEncoders(self.npmName)
1896bca0
C
337 }
338 }
339 }
5e2b2e27 340}