]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/plugins/register-helpers-store.ts
Add plugin settings change watcher
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / register-helpers-store.ts
1 import * as express from 'express'
2 import { logger } from '@server/helpers/logger'
3 import {
4 VIDEO_CATEGORIES,
5 VIDEO_LANGUAGES,
6 VIDEO_LICENCES,
7 VIDEO_PLAYLIST_PRIVACIES,
8 VIDEO_PRIVACIES
9 } from '@server/initializers/constants'
10 import { onExternalUserAuthenticated } from '@server/lib/auth'
11 import { PluginModel } from '@server/models/server/plugin'
12 import { RegisterServerOptions } from '@server/typings/plugins'
13 import { PluginPlaylistPrivacyManager } from '@shared/models/plugins/plugin-playlist-privacy-manager.model'
14 import { PluginSettingsManager } from '@shared/models/plugins/plugin-settings-manager.model'
15 import { PluginStorageManager } from '@shared/models/plugins/plugin-storage-manager.model'
16 import { PluginVideoCategoryManager } from '@shared/models/plugins/plugin-video-category-manager.model'
17 import { PluginVideoLanguageManager } from '@shared/models/plugins/plugin-video-language-manager.model'
18 import { PluginVideoLicenceManager } from '@shared/models/plugins/plugin-video-licence-manager.model'
19 import { PluginVideoPrivacyManager } from '@shared/models/plugins/plugin-video-privacy-manager.model'
20 import {
21 RegisterServerAuthExternalOptions,
22 RegisterServerAuthExternalResult,
23 RegisterServerAuthPassOptions,
24 RegisterServerExternalAuthenticatedResult
25 } from '@shared/models/plugins/register-server-auth.model'
26 import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model'
27 import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
28 import { serverHookObject } from '@shared/models/plugins/server-hook.model'
29 import { buildPluginHelpers } from './plugin-helpers'
30
31 type AlterableVideoConstant = 'language' | 'licence' | 'category' | 'privacy' | 'playlistPrivacy'
32 type VideoConstant = { [key in number | string]: string }
33
34 type UpdatedVideoConstant = {
35 [name in AlterableVideoConstant]: {
36 added: { key: number | string, label: string }[]
37 deleted: { key: number | string, label: string }[]
38 }
39 }
40
41 export class RegisterHelpersStore {
42 private readonly updatedVideoConstants: UpdatedVideoConstant = {
43 playlistPrivacy: { added: [], deleted: [] },
44 privacy: { added: [], deleted: [] },
45 language: { added: [], deleted: [] },
46 licence: { added: [], deleted: [] },
47 category: { added: [], deleted: [] }
48 }
49
50 private readonly settings: RegisterServerSettingOptions[] = []
51
52 private readonly idAndPassAuths: RegisterServerAuthPassOptions[] = []
53 private readonly externalAuths: RegisterServerAuthExternalOptions[] = []
54
55 private readonly onSettingsChangeCallbacks: ((settings: any) => void)[] = []
56
57 private readonly router: express.Router
58
59 constructor (
60 private readonly npmName: string,
61 private readonly plugin: PluginModel,
62 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
63 ) {
64 this.router = express.Router()
65 }
66
67 buildRegisterHelpers (): RegisterServerOptions {
68 const registerHook = this.buildRegisterHook()
69 const registerSetting = this.buildRegisterSetting()
70
71 const getRouter = this.buildGetRouter()
72
73 const settingsManager = this.buildSettingsManager()
74 const storageManager = this.buildStorageManager()
75
76 const videoLanguageManager = this.buildVideoLanguageManager()
77
78 const videoLicenceManager = this.buildVideoLicenceManager()
79 const videoCategoryManager = this.buildVideoCategoryManager()
80
81 const videoPrivacyManager = this.buildVideoPrivacyManager()
82 const playlistPrivacyManager = this.buildPlaylistPrivacyManager()
83
84 const registerIdAndPassAuth = this.buildRegisterIdAndPassAuth()
85 const registerExternalAuth = this.buildRegisterExternalAuth()
86
87 const peertubeHelpers = buildPluginHelpers(this.npmName)
88
89 return {
90 registerHook,
91 registerSetting,
92
93 getRouter,
94
95 settingsManager,
96 storageManager,
97
98 videoLanguageManager,
99 videoCategoryManager,
100 videoLicenceManager,
101
102 videoPrivacyManager,
103 playlistPrivacyManager,
104
105 registerIdAndPassAuth,
106 registerExternalAuth,
107
108 peertubeHelpers
109 }
110 }
111
112 reinitVideoConstants (npmName: string) {
113 const hash = {
114 language: VIDEO_LANGUAGES,
115 licence: VIDEO_LICENCES,
116 category: VIDEO_CATEGORIES,
117 privacy: VIDEO_PRIVACIES,
118 playlistPrivacy: VIDEO_PLAYLIST_PRIVACIES
119 }
120 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category', 'privacy', 'playlistPrivacy' ]
121
122 for (const type of types) {
123 const updatedConstants = this.updatedVideoConstants[type][npmName]
124 if (!updatedConstants) continue
125
126 for (const added of updatedConstants.added) {
127 delete hash[type][added.key]
128 }
129
130 for (const deleted of updatedConstants.deleted) {
131 hash[type][deleted.key] = deleted.label
132 }
133
134 delete this.updatedVideoConstants[type][npmName]
135 }
136 }
137
138 getSettings () {
139 return this.settings
140 }
141
142 getRouter () {
143 return this.router
144 }
145
146 getIdAndPassAuths () {
147 return this.idAndPassAuths
148 }
149
150 getExternalAuths () {
151 return this.externalAuths
152 }
153
154 getOnSettingsChangedCallbacks () {
155 return this.onSettingsChangeCallbacks
156 }
157
158 private buildGetRouter () {
159 return () => this.router
160 }
161
162 private buildRegisterSetting () {
163 return (options: RegisterServerSettingOptions) => {
164 this.settings.push(options)
165 }
166 }
167
168 private buildRegisterHook () {
169 return (options: RegisterServerHookOptions) => {
170 if (serverHookObject[options.target] !== true) {
171 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
172 return
173 }
174
175 return this.onHookAdded(options)
176 }
177 }
178
179 private buildRegisterIdAndPassAuth () {
180 return (options: RegisterServerAuthPassOptions) => {
181 if (!options.authName || typeof options.getWeight !== 'function' || typeof options.login !== 'function') {
182 logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName)
183 return
184 }
185
186 this.idAndPassAuths.push(options)
187 }
188 }
189
190 private buildRegisterExternalAuth () {
191 const self = this
192
193 return (options: RegisterServerAuthExternalOptions) => {
194 if (!options.authName || typeof options.authDisplayName !== 'function' || typeof options.onAuthRequest !== 'function') {
195 logger.error('Cannot register auth plugin %s: authName of getWeight or login are not valid.', this.npmName)
196 return
197 }
198
199 this.externalAuths.push(options)
200
201 return {
202 userAuthenticated (result: RegisterServerExternalAuthenticatedResult): void {
203 onExternalUserAuthenticated({
204 npmName: self.npmName,
205 authName: options.authName,
206 authResult: result
207 }).catch(err => {
208 logger.error('Cannot execute onExternalUserAuthenticated.', { npmName: self.npmName, authName: options.authName, err })
209 })
210 }
211 } as RegisterServerAuthExternalResult
212 }
213 }
214
215 private buildSettingsManager (): PluginSettingsManager {
216 return {
217 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name),
218
219 getSettings: (names: string[]) => PluginModel.getSettings(this.plugin.name, this.plugin.type, names),
220
221 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value),
222
223 onSettingsChange: (cb: (settings: any) => void) => this.onSettingsChangeCallbacks.push(cb)
224 }
225 }
226
227 private buildStorageManager (): PluginStorageManager {
228 return {
229 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
230
231 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
232 }
233 }
234
235 private buildVideoLanguageManager (): PluginVideoLanguageManager {
236 return {
237 addLanguage: (key: string, label: string) => {
238 return this.addConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label })
239 },
240
241 deleteLanguage: (key: string) => {
242 return this.deleteConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
243 }
244 }
245 }
246
247 private buildVideoCategoryManager (): PluginVideoCategoryManager {
248 return {
249 addCategory: (key: number, label: string) => {
250 return this.addConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label })
251 },
252
253 deleteCategory: (key: number) => {
254 return this.deleteConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
255 }
256 }
257 }
258
259 private buildVideoPrivacyManager (): PluginVideoPrivacyManager {
260 return {
261 deletePrivacy: (key: number) => {
262 return this.deleteConstant({ npmName: this.npmName, type: 'privacy', obj: VIDEO_PRIVACIES, key })
263 }
264 }
265 }
266
267 private buildPlaylistPrivacyManager (): PluginPlaylistPrivacyManager {
268 return {
269 deletePlaylistPrivacy: (key: number) => {
270 return this.deleteConstant({ npmName: this.npmName, type: 'playlistPrivacy', obj: VIDEO_PLAYLIST_PRIVACIES, key })
271 }
272 }
273 }
274
275 private buildVideoLicenceManager (): PluginVideoLicenceManager {
276 return {
277 addLicence: (key: number, label: string) => {
278 return this.addConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key, label })
279 },
280
281 deleteLicence: (key: number) => {
282 return this.deleteConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key })
283 }
284 }
285 }
286
287 private addConstant<T extends string | number> (parameters: {
288 npmName: string
289 type: AlterableVideoConstant
290 obj: VideoConstant
291 key: T
292 label: string
293 }) {
294 const { npmName, type, obj, key, label } = parameters
295
296 if (obj[key]) {
297 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
298 return false
299 }
300
301 if (!this.updatedVideoConstants[type][npmName]) {
302 this.updatedVideoConstants[type][npmName] = {
303 added: [],
304 deleted: []
305 }
306 }
307
308 this.updatedVideoConstants[type][npmName].added.push({ key, label })
309 obj[key] = label
310
311 return true
312 }
313
314 private deleteConstant<T extends string | number> (parameters: {
315 npmName: string
316 type: AlterableVideoConstant
317 obj: VideoConstant
318 key: T
319 }) {
320 const { npmName, type, obj, key } = parameters
321
322 if (!obj[key]) {
323 logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key)
324 return false
325 }
326
327 if (!this.updatedVideoConstants[type][npmName]) {
328 this.updatedVideoConstants[type][npmName] = {
329 added: [],
330 deleted: []
331 }
332 }
333
334 this.updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] })
335 delete obj[key]
336
337 return true
338 }
339 }