]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/plugins/register-helpers.ts
Add videos.getFiles plugin helper
[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 VideoPlaylistPrivacy,
21 VideoPrivacy
22 } from '@shared/models'
23 import { VideoTranscodingProfilesManager } from '../transcoding/video-transcoding-profiles'
24 import { buildPluginHelpers } from './plugin-helpers-builder'
25
26 export class RegisterHelpers {
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
44 private readonly settings: RegisterServerSettingOptions[] = []
45
46 private idAndPassAuths: RegisterServerAuthPassOptions[] = []
47 private externalAuths: RegisterServerAuthExternalOptions[] = []
48
49 private readonly onSettingsChangeCallbacks: ((settings: any) => Promise<any>)[] = []
50
51 private readonly router: express.Router
52 private readonly videoConstantManagerFactory: VideoConstantManagerFactory
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()
60 this.videoConstantManagerFactory = new VideoConstantManagerFactory(this.npmName)
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
72 const videoLanguageManager = this.videoConstantManagerFactory.createVideoConstantManager<string>('language')
73
74 const videoLicenceManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('licence')
75 const videoCategoryManager = this.videoConstantManagerFactory.createVideoConstantManager<number>('category')
76
77 const videoPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPrivacy>('privacy')
78 const playlistPrivacyManager = this.videoConstantManagerFactory.createVideoConstantManager<VideoPlaylistPrivacy>('playlistPrivacy')
79
80 const transcodingManager = this.buildTranscodingManager()
81
82 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
83 const registerExternalAuth = this.buildRegisterExternalAuth()
84 const unregisterIdAndPassAuth = this.buildUnregisterIdAndPassAuth()
85 const unregisterExternalAuth = this.buildUnregisterExternalAuth()
86
87 const peertubeHelpers = buildPluginHelpers(this.plugin, this.npmName)
88
89 return {
90 registerHook,
91 registerSetting,
92
93 getRouter,
94
95 settingsManager,
96 storageManager,
97
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 },
119
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 },
130
131 transcodingManager,
132
133 registerIdAndPassAuth,
134 registerExternalAuth,
135 unregisterIdAndPassAuth,
136 unregisterExternalAuth,
137
138 peertubeHelpers
139 }
140 }
141
142 reinitVideoConstants (npmName: string) {
143 this.videoConstantManagerFactory.resetVideoConstants(npmName)
144 }
145
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
162 getSettings () {
163 return this.settings
164 }
165
166 getRouter () {
167 return this.router
168 }
169
170 getIdAndPassAuths () {
171 return this.idAndPassAuths
172 }
173
174 getExternalAuths () {
175 return this.externalAuths
176 }
177
178 getOnSettingsChangedCallbacks () {
179 return this.onSettingsChangeCallbacks
180 }
181
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
203 private buildRegisterIdAndPassAuth () {
204 return (options: RegisterServerAuthPassOptions) => {
205 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
206 logger.error('Cannot register auth plugin %s: authName, getWeight or login are not valid.', this.npmName, { options })
207 return
208 }
209
210 this.idAndPassAuths.push(options)
211 }
212 }
213
214 private buildRegisterExternalAuth () {
215 const self = this
216
217 return (options: RegisterServerAuthExternalOptions) => {
218 if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
219 logger.error('Cannot register auth plugin %s: authName, authDisplayName or onAuthRequest are not valid.', this.npmName, { options })
220 return
221 }
222
223 this.externalAuths.push(options)
224
225 return {
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 })
234 }
235 } as RegisterServerAuthExternalResult
236 }
237 }
238
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
251 private buildSettingsManager (): PluginSettingsManager {
252 return {
253 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name, this.settings),
254
255 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names, this.settings),
256
257 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
258
259 onSettingsChange: (cb: (settings: any) => Promise<any>) => this.onSettingsChangeCallbacks.push(cb)
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
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)
315 },
316
317 removeAllProfilesAndEncoderPriorities () {
318 return self.reinitTranscodingProfilesAndEncoders(self.npmName)
319 }
320 }
321 }
322 }