]>
Commit | Line | Data |
---|---|---|
345da516 C |
1 | import { PluginModel } from '../../models/server/plugin' |
2 | import { logger } from '../../helpers/logger' | |
f023a19c | 3 | import { basename, join } from 'path' |
345da516 C |
4 | import { CONFIG } from '../../initializers/config' |
5 | import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins' | |
d75db01f C |
6 | import { |
7 | ClientScript, | |
8 | PluginPackageJson, | |
9 | PluginTranslationPaths as PackagePluginTranslations | |
10 | } from '../../../shared/models/plugins/plugin-package-json.model' | |
345da516 | 11 | import { createReadStream, createWriteStream } from 'fs' |
ee286591 | 12 | import { PLUGIN_GLOBAL_CSS_PATH, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../../initializers/constants' |
345da516 | 13 | import { PluginType } from '../../../shared/models/plugins/plugin.type' |
f023a19c | 14 | import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' |
09071200 | 15 | import { outputFile, readJSON } from 'fs-extra' |
ad91e700 | 16 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' |
b2195faf | 17 | import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' |
7663e55a | 18 | import { ServerHook, ServerHookName, serverHookObject } from '../../../shared/models/plugins/server-hook.model' |
b4055e1c | 19 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' |
9ae88819 | 20 | import { RegisterServerOptions } from '../../typings/plugins/register-server-option.model' |
32fe0013 | 21 | import { PluginLibrary } from '../../typings/plugins' |
a8b666e9 | 22 | import { ClientHtml } from '../client-html' |
9ae88819 C |
23 | import { RegisterServerHookOptions } from '../../../shared/models/plugins/register-server-hook.model' |
24 | import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model' | |
ee286591 C |
25 | import { PluginVideoLanguageManager } from '../../../shared/models/plugins/plugin-video-language-manager.model' |
26 | import { PluginVideoCategoryManager } from '../../../shared/models/plugins/plugin-video-category-manager.model' | |
27 | import { PluginVideoLicenceManager } from '../../../shared/models/plugins/plugin-video-licence-manager.model' | |
d75db01f | 28 | import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model' |
345da516 C |
29 | |
30 | export interface RegisteredPlugin { | |
b5f919ac | 31 | npmName: string |
345da516 C |
32 | name: string |
33 | version: string | |
34 | description: string | |
35 | peertubeEngine: string | |
36 | ||
37 | type: PluginType | |
38 | ||
39 | path: string | |
40 | ||
41 | staticDirs: { [name: string]: string } | |
2c053942 | 42 | clientScripts: { [name: string]: ClientScript } |
345da516 C |
43 | |
44 | css: string[] | |
45 | ||
46 | // Only if this is a plugin | |
47 | unregister?: Function | |
48 | } | |
49 | ||
50 | export interface HookInformationValue { | |
b5f919ac | 51 | npmName: string |
345da516 C |
52 | pluginName: string |
53 | handler: Function | |
54 | priority: number | |
55 | } | |
56 | ||
ee286591 C |
57 | type AlterableVideoConstant = 'language' | 'licence' | 'category' |
58 | type VideoConstant = { [ key in number | string ]: string } | |
59 | type UpdatedVideoConstant = { | |
60 | [ name in AlterableVideoConstant ]: { | |
61 | [ npmName: string ]: { | |
62 | added: { key: number | string, label: string }[], | |
63 | deleted: { key: number | string, label: string }[] | |
64 | } | |
65 | } | |
66 | } | |
67 | ||
d75db01f C |
68 | type PluginLocalesTranslations = { |
69 | [ locale: string ]: PluginTranslation | |
70 | } | |
71 | ||
b4055e1c | 72 | export class PluginManager implements ServerHook { |
345da516 C |
73 | |
74 | private static instance: PluginManager | |
75 | ||
76 | private registeredPlugins: { [ name: string ]: RegisteredPlugin } = {} | |
9ae88819 | 77 | private settings: { [ name: string ]: RegisterServerSettingOptions[] } = {} |
345da516 | 78 | private hooks: { [ name: string ]: HookInformationValue[] } = {} |
d75db01f | 79 | private translations: PluginLocalesTranslations = {} |
345da516 | 80 | |
ee286591 C |
81 | private updatedVideoConstants: UpdatedVideoConstant = { |
82 | language: {}, | |
83 | licence: {}, | |
84 | category: {} | |
85 | } | |
86 | ||
345da516 C |
87 | private constructor () { |
88 | } | |
89 | ||
ad91e700 | 90 | // ###################### Getters ###################### |
345da516 | 91 | |
6702a1b2 C |
92 | isRegistered (npmName: string) { |
93 | return !!this.getRegisteredPluginOrTheme(npmName) | |
94 | } | |
95 | ||
b5f919ac C |
96 | getRegisteredPluginOrTheme (npmName: string) { |
97 | return this.registeredPlugins[npmName] | |
7cd4d2ba C |
98 | } |
99 | ||
345da516 | 100 | getRegisteredPlugin (name: string) { |
b5f919ac C |
101 | const npmName = PluginModel.buildNpmName(name, PluginType.PLUGIN) |
102 | const registered = this.getRegisteredPluginOrTheme(npmName) | |
7cd4d2ba C |
103 | |
104 | if (!registered || registered.type !== PluginType.PLUGIN) return undefined | |
105 | ||
106 | return registered | |
345da516 C |
107 | } |
108 | ||
109 | getRegisteredTheme (name: string) { | |
b5f919ac C |
110 | const npmName = PluginModel.buildNpmName(name, PluginType.THEME) |
111 | const registered = this.getRegisteredPluginOrTheme(npmName) | |
345da516 C |
112 | |
113 | if (!registered || registered.type !== PluginType.THEME) return undefined | |
114 | ||
115 | return registered | |
116 | } | |
117 | ||
18a6f04c | 118 | getRegisteredPlugins () { |
7cd4d2ba C |
119 | return this.getRegisteredPluginsOrThemes(PluginType.PLUGIN) |
120 | } | |
121 | ||
122 | getRegisteredThemes () { | |
123 | return this.getRegisteredPluginsOrThemes(PluginType.THEME) | |
18a6f04c C |
124 | } |
125 | ||
b5f919ac C |
126 | getRegisteredSettings (npmName: string) { |
127 | return this.settings[npmName] || [] | |
ad91e700 C |
128 | } |
129 | ||
d75db01f C |
130 | getTranslations (locale: string) { |
131 | return this.translations[locale] || {} | |
132 | } | |
133 | ||
ad91e700 C |
134 | // ###################### Hooks ###################### |
135 | ||
89cd1275 C |
136 | async runHook <T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> { |
137 | if (!this.hooks[hookName]) return Promise.resolve(result) | |
dba85a1e | 138 | |
b4055e1c | 139 | const hookType = getHookType(hookName) |
18a6f04c C |
140 | |
141 | for (const hook of this.hooks[hookName]) { | |
89cd1275 C |
142 | logger.debug('Running hook %s of plugin %s.', hookName, hook.npmName) |
143 | ||
144 | result = await internalRunHook(hook.handler, hookType, result, params, err => { | |
18a6f04c | 145 | logger.error('Cannot run hook %s of plugin %s.', hookName, hook.pluginName, { err }) |
b4055e1c | 146 | }) |
18a6f04c C |
147 | } |
148 | ||
149 | return result | |
150 | } | |
151 | ||
ad91e700 C |
152 | // ###################### Registration ###################### |
153 | ||
154 | async registerPluginsAndThemes () { | |
155 | await this.resetCSSGlobalFile() | |
156 | ||
157 | const plugins = await PluginModel.listEnabledPluginsAndThemes() | |
158 | ||
159 | for (const plugin of plugins) { | |
160 | try { | |
161 | await this.registerPluginOrTheme(plugin) | |
162 | } catch (err) { | |
587568e1 C |
163 | // Try to unregister the plugin |
164 | try { | |
165 | await this.unregister(PluginModel.buildNpmName(plugin.name, plugin.type)) | |
166 | } catch { | |
167 | // we don't care if we cannot unregister it | |
168 | } | |
169 | ||
ad91e700 C |
170 | logger.error('Cannot register plugin %s, skipping.', plugin.name, { err }) |
171 | } | |
172 | } | |
173 | ||
174 | this.sortHooksByPriority() | |
175 | } | |
176 | ||
b5f919ac C |
177 | // Don't need the plugin type since themes cannot register server code |
178 | async unregister (npmName: string) { | |
179 | logger.info('Unregister plugin %s.', npmName) | |
180 | ||
181 | const plugin = this.getRegisteredPluginOrTheme(npmName) | |
345da516 C |
182 | |
183 | if (!plugin) { | |
b5f919ac | 184 | throw new Error(`Unknown plugin ${npmName} to unregister`) |
345da516 C |
185 | } |
186 | ||
60cfd4cb | 187 | delete this.registeredPlugins[plugin.npmName] |
23bdacf8 | 188 | delete this.settings[plugin.npmName] |
60cfd4cb | 189 | |
d75db01f C |
190 | this.deleteTranslations(plugin.npmName) |
191 | ||
b5f919ac C |
192 | if (plugin.type === PluginType.PLUGIN) { |
193 | await plugin.unregister() | |
345da516 | 194 | |
b5f919ac C |
195 | // Remove hooks of this plugin |
196 | for (const key of Object.keys(this.hooks)) { | |
197 | this.hooks[key] = this.hooks[key].filter(h => h.pluginName !== npmName) | |
198 | } | |
2c053942 | 199 | |
ee286591 C |
200 | this.reinitVideoConstants(plugin.npmName) |
201 | ||
b5f919ac C |
202 | logger.info('Regenerating registered plugin CSS to global file.') |
203 | await this.regeneratePluginGlobalCSS() | |
2c053942 | 204 | } |
345da516 C |
205 | } |
206 | ||
ad91e700 C |
207 | // ###################### Installation ###################### |
208 | ||
209 | async install (toInstall: string, version?: string, fromDisk = false) { | |
f023a19c | 210 | let plugin: PluginModel |
b5f919ac | 211 | let npmName: string |
f023a19c C |
212 | |
213 | logger.info('Installing plugin %s.', toInstall) | |
214 | ||
215 | try { | |
216 | fromDisk | |
217 | ? await installNpmPluginFromDisk(toInstall) | |
218 | : await installNpmPlugin(toInstall, version) | |
219 | ||
b5f919ac C |
220 | npmName = fromDisk ? basename(toInstall) : toInstall |
221 | const pluginType = PluginModel.getTypeFromNpmName(npmName) | |
222 | const pluginName = PluginModel.normalizePluginName(npmName) | |
f023a19c | 223 | |
09071200 | 224 | const packageJSON = await this.getPackageJSON(pluginName, pluginType) |
9157d598 C |
225 | |
226 | this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, pluginType); | |
f023a19c C |
227 | |
228 | [ plugin ] = await PluginModel.upsert({ | |
229 | name: pluginName, | |
230 | description: packageJSON.description, | |
dba85a1e | 231 | homepage: packageJSON.homepage, |
f023a19c C |
232 | type: pluginType, |
233 | version: packageJSON.version, | |
234 | enabled: true, | |
235 | uninstalled: false, | |
236 | peertubeEngine: packageJSON.engine.peertube | |
237 | }, { returning: true }) | |
238 | } catch (err) { | |
239 | logger.error('Cannot install plugin %s, removing it...', toInstall, { err }) | |
240 | ||
241 | try { | |
b5f919ac | 242 | await removeNpmPlugin(npmName) |
f023a19c C |
243 | } catch (err) { |
244 | logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err }) | |
245 | } | |
246 | ||
247 | throw err | |
248 | } | |
249 | ||
250 | logger.info('Successful installation of plugin %s.', toInstall) | |
251 | ||
252 | await this.registerPluginOrTheme(plugin) | |
b5f919ac C |
253 | |
254 | return plugin | |
255 | } | |
256 | ||
257 | async update (toUpdate: string, version?: string, fromDisk = false) { | |
258 | const npmName = fromDisk ? basename(toUpdate) : toUpdate | |
259 | ||
260 | logger.info('Updating plugin %s.', npmName) | |
261 | ||
262 | // Unregister old hooks | |
263 | await this.unregister(npmName) | |
264 | ||
265 | return this.install(toUpdate, version, fromDisk) | |
f023a19c C |
266 | } |
267 | ||
dba85a1e C |
268 | async uninstall (npmName: string) { |
269 | logger.info('Uninstalling plugin %s.', npmName) | |
2c053942 | 270 | |
2c053942 | 271 | try { |
b5f919ac | 272 | await this.unregister(npmName) |
2c053942 | 273 | } catch (err) { |
b5f919ac | 274 | logger.warn('Cannot unregister plugin %s.', npmName, { err }) |
2c053942 C |
275 | } |
276 | ||
dba85a1e | 277 | const plugin = await PluginModel.loadByNpmName(npmName) |
2c053942 | 278 | if (!plugin || plugin.uninstalled === true) { |
dba85a1e | 279 | logger.error('Cannot uninstall plugin %s: it does not exist or is already uninstalled.', npmName) |
2c053942 C |
280 | return |
281 | } | |
282 | ||
283 | plugin.enabled = false | |
284 | plugin.uninstalled = true | |
285 | ||
286 | await plugin.save() | |
f023a19c | 287 | |
dba85a1e | 288 | await removeNpmPlugin(npmName) |
2c053942 | 289 | |
dba85a1e | 290 | logger.info('Plugin %s uninstalled.', npmName) |
f023a19c C |
291 | } |
292 | ||
ad91e700 C |
293 | // ###################### Private register ###################### |
294 | ||
345da516 | 295 | private async registerPluginOrTheme (plugin: PluginModel) { |
b5f919ac C |
296 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) |
297 | ||
298 | logger.info('Registering plugin or theme %s.', npmName) | |
345da516 | 299 | |
09071200 | 300 | const packageJSON = await this.getPackageJSON(plugin.name, plugin.type) |
f023a19c | 301 | const pluginPath = this.getPluginPath(plugin.name, plugin.type) |
345da516 | 302 | |
9157d598 | 303 | this.sanitizeAndCheckPackageJSONOrThrow(packageJSON, plugin.type) |
345da516 C |
304 | |
305 | let library: PluginLibrary | |
306 | if (plugin.type === PluginType.PLUGIN) { | |
307 | library = await this.registerPlugin(plugin, pluginPath, packageJSON) | |
308 | } | |
309 | ||
2c053942 C |
310 | const clientScripts: { [id: string]: ClientScript } = {} |
311 | for (const c of packageJSON.clientScripts) { | |
312 | clientScripts[c.script] = c | |
313 | } | |
314 | ||
b5f919ac C |
315 | this.registeredPlugins[ npmName ] = { |
316 | npmName, | |
345da516 C |
317 | name: plugin.name, |
318 | type: plugin.type, | |
319 | version: plugin.version, | |
320 | description: plugin.description, | |
321 | peertubeEngine: plugin.peertubeEngine, | |
322 | path: pluginPath, | |
323 | staticDirs: packageJSON.staticDirs, | |
2c053942 | 324 | clientScripts, |
345da516 C |
325 | css: packageJSON.css, |
326 | unregister: library ? library.unregister : undefined | |
327 | } | |
d75db01f C |
328 | |
329 | await this.addTranslations(plugin, npmName, packageJSON.translations) | |
345da516 C |
330 | } |
331 | ||
332 | private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { | |
b5f919ac C |
333 | const npmName = PluginModel.buildNpmName(plugin.name, plugin.type) |
334 | ||
09071200 C |
335 | // Delete cache if needed |
336 | const modulePath = join(pluginPath, packageJSON.library) | |
337 | delete require.cache[modulePath] | |
338 | const library: PluginLibrary = require(modulePath) | |
f023a19c | 339 | |
345da516 C |
340 | if (!isLibraryCodeValid(library)) { |
341 | throw new Error('Library code is not valid (miss register or unregister function)') | |
342 | } | |
343 | ||
32fe0013 C |
344 | const registerHelpers = this.getRegisterHelpers(npmName, plugin) |
345 | library.register(registerHelpers) | |
346 | .catch(err => logger.error('Cannot register plugin %s.', npmName, { err })) | |
345da516 | 347 | |
b5f919ac | 348 | logger.info('Add plugin %s CSS to global file.', npmName) |
345da516 C |
349 | |
350 | await this.addCSSToGlobalFile(pluginPath, packageJSON.css) | |
351 | ||
352 | return library | |
353 | } | |
354 | ||
d75db01f C |
355 | // ###################### Translations ###################### |
356 | ||
357 | private async addTranslations (plugin: PluginModel, npmName: string, translationPaths: PackagePluginTranslations) { | |
358 | for (const locale of Object.keys(translationPaths)) { | |
359 | const path = translationPaths[locale] | |
360 | const json = await readJSON(join(this.getPluginPath(plugin.name, plugin.type), path)) | |
361 | ||
362 | if (!this.translations[locale]) this.translations[locale] = {} | |
363 | this.translations[locale][npmName] = json | |
364 | ||
365 | logger.info('Added locale %s of plugin %s.', locale, npmName) | |
366 | } | |
367 | } | |
368 | ||
369 | private deleteTranslations (npmName: string) { | |
370 | for (const locale of Object.keys(this.translations)) { | |
371 | delete this.translations[locale][npmName] | |
372 | ||
373 | logger.info('Deleted locale %s of plugin %s.', locale, npmName) | |
374 | } | |
375 | } | |
376 | ||
ad91e700 | 377 | // ###################### CSS ###################### |
345da516 | 378 | |
2c053942 | 379 | private resetCSSGlobalFile () { |
3e753302 C |
380 | ClientHtml.invalidCache() |
381 | ||
2c053942 C |
382 | return outputFile(PLUGIN_GLOBAL_CSS_PATH, '') |
383 | } | |
384 | ||
345da516 C |
385 | private async addCSSToGlobalFile (pluginPath: string, cssRelativePaths: string[]) { |
386 | for (const cssPath of cssRelativePaths) { | |
387 | await this.concatFiles(join(pluginPath, cssPath), PLUGIN_GLOBAL_CSS_PATH) | |
388 | } | |
a8b666e9 C |
389 | |
390 | ClientHtml.invalidCache() | |
345da516 C |
391 | } |
392 | ||
393 | private concatFiles (input: string, output: string) { | |
394 | return new Promise<void>((res, rej) => { | |
2c053942 C |
395 | const inputStream = createReadStream(input) |
396 | const outputStream = createWriteStream(output, { flags: 'a' }) | |
345da516 C |
397 | |
398 | inputStream.pipe(outputStream) | |
399 | ||
400 | inputStream.on('end', () => res()) | |
401 | inputStream.on('error', err => rej(err)) | |
402 | }) | |
403 | } | |
404 | ||
ad91e700 C |
405 | private async regeneratePluginGlobalCSS () { |
406 | await this.resetCSSGlobalFile() | |
407 | ||
1198edf4 | 408 | for (const plugin of this.getRegisteredPlugins()) { |
ad91e700 C |
409 | await this.addCSSToGlobalFile(plugin.path, plugin.css) |
410 | } | |
411 | } | |
412 | ||
413 | // ###################### Utils ###################### | |
414 | ||
415 | private sortHooksByPriority () { | |
416 | for (const hookName of Object.keys(this.hooks)) { | |
417 | this.hooks[hookName].sort((a, b) => { | |
418 | return b.priority - a.priority | |
419 | }) | |
420 | } | |
421 | } | |
422 | ||
f023a19c C |
423 | private getPackageJSON (pluginName: string, pluginType: PluginType) { |
424 | const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json') | |
425 | ||
09071200 | 426 | return readJSON(pluginPath) as Promise<PluginPackageJson> |
f023a19c C |
427 | } |
428 | ||
429 | private getPluginPath (pluginName: string, pluginType: PluginType) { | |
b5f919ac | 430 | const npmName = PluginModel.buildNpmName(pluginName, pluginType) |
f023a19c | 431 | |
b5f919ac | 432 | return join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules', npmName) |
f023a19c C |
433 | } |
434 | ||
ad91e700 | 435 | // ###################### Private getters ###################### |
2c053942 | 436 | |
7cd4d2ba C |
437 | private getRegisteredPluginsOrThemes (type: PluginType) { |
438 | const plugins: RegisteredPlugin[] = [] | |
439 | ||
b5f919ac C |
440 | for (const npmName of Object.keys(this.registeredPlugins)) { |
441 | const plugin = this.registeredPlugins[ npmName ] | |
7cd4d2ba C |
442 | if (plugin.type !== type) continue |
443 | ||
444 | plugins.push(plugin) | |
445 | } | |
446 | ||
447 | return plugins | |
448 | } | |
449 | ||
32fe0013 C |
450 | // ###################### Generate register helpers ###################### |
451 | ||
9ae88819 C |
452 | private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterServerOptions { |
453 | const registerHook = (options: RegisterServerHookOptions) => { | |
7663e55a C |
454 | if (serverHookObject[options.target] !== true) { |
455 | logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, npmName) | |
456 | return | |
457 | } | |
458 | ||
32fe0013 C |
459 | if (!this.hooks[options.target]) this.hooks[options.target] = [] |
460 | ||
461 | this.hooks[options.target].push({ | |
462 | npmName, | |
463 | pluginName: plugin.name, | |
464 | handler: options.handler, | |
465 | priority: options.priority || 0 | |
466 | }) | |
467 | } | |
468 | ||
9ae88819 | 469 | const registerSetting = (options: RegisterServerSettingOptions) => { |
32fe0013 C |
470 | if (!this.settings[npmName]) this.settings[npmName] = [] |
471 | ||
472 | this.settings[npmName].push(options) | |
473 | } | |
474 | ||
475 | const settingsManager: PluginSettingsManager = { | |
476 | getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name), | |
477 | ||
478 | setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value) | |
479 | } | |
480 | ||
481 | const storageManager: PluginStorageManager = { | |
482 | getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key), | |
483 | ||
484 | storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data) | |
485 | } | |
486 | ||
ee286591 C |
487 | const videoLanguageManager: PluginVideoLanguageManager = { |
488 | addLanguage: (key: string, label: string) => this.addConstant({ npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label }), | |
489 | ||
490 | deleteLanguage: (key: string) => this.deleteConstant({ npmName, type: 'language', obj: VIDEO_LANGUAGES, key }) | |
491 | } | |
492 | ||
d75db01f | 493 | const videoCategoryManager: PluginVideoCategoryManager = { |
ee286591 C |
494 | addCategory: (key: number, label: string) => this.addConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label }), |
495 | ||
496 | deleteCategory: (key: number) => this.deleteConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key }) | |
497 | } | |
498 | ||
499 | const videoLicenceManager: PluginVideoLicenceManager = { | |
500 | addLicence: (key: number, label: string) => this.addConstant({ npmName, type: 'licence', obj: VIDEO_LICENCES, key, label }), | |
501 | ||
502 | deleteLicence: (key: number) => this.deleteConstant({ npmName, type: 'licence', obj: VIDEO_LICENCES, key }) | |
503 | } | |
504 | ||
32fe0013 C |
505 | const peertubeHelpers = { |
506 | logger | |
507 | } | |
508 | ||
509 | return { | |
510 | registerHook, | |
511 | registerSetting, | |
512 | settingsManager, | |
513 | storageManager, | |
ee286591 C |
514 | videoLanguageManager, |
515 | videoCategoryManager, | |
516 | videoLicenceManager, | |
32fe0013 C |
517 | peertubeHelpers |
518 | } | |
519 | } | |
520 | ||
ee286591 C |
521 | private addConstant <T extends string | number> (parameters: { |
522 | npmName: string, | |
523 | type: AlterableVideoConstant, | |
524 | obj: VideoConstant, | |
525 | key: T, | |
526 | label: string | |
527 | }) { | |
528 | const { npmName, type, obj, key, label } = parameters | |
529 | ||
530 | if (obj[key]) { | |
531 | logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key) | |
532 | return false | |
533 | } | |
534 | ||
535 | if (!this.updatedVideoConstants[type][npmName]) { | |
536 | this.updatedVideoConstants[type][npmName] = { | |
537 | added: [], | |
538 | deleted: [] | |
539 | } | |
540 | } | |
541 | ||
542 | this.updatedVideoConstants[type][npmName].added.push({ key, label }) | |
543 | obj[key] = label | |
544 | ||
545 | return true | |
546 | } | |
547 | ||
548 | private deleteConstant <T extends string | number> (parameters: { | |
549 | npmName: string, | |
550 | type: AlterableVideoConstant, | |
551 | obj: VideoConstant, | |
552 | key: T | |
553 | }) { | |
554 | const { npmName, type, obj, key } = parameters | |
555 | ||
556 | if (!obj[key]) { | |
557 | logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key) | |
558 | return false | |
559 | } | |
560 | ||
561 | if (!this.updatedVideoConstants[type][npmName]) { | |
562 | this.updatedVideoConstants[type][npmName] = { | |
563 | added: [], | |
564 | deleted: [] | |
565 | } | |
566 | } | |
567 | ||
568 | this.updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] }) | |
569 | delete obj[key] | |
570 | ||
571 | return true | |
572 | } | |
573 | ||
574 | private reinitVideoConstants (npmName: string) { | |
575 | const hash = { | |
576 | language: VIDEO_LANGUAGES, | |
577 | licence: VIDEO_LICENCES, | |
578 | category: VIDEO_CATEGORIES | |
579 | } | |
580 | const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category' ] | |
581 | ||
582 | for (const type of types) { | |
583 | const updatedConstants = this.updatedVideoConstants[type][npmName] | |
584 | if (!updatedConstants) continue | |
585 | ||
586 | for (const added of updatedConstants.added) { | |
587 | delete hash[type][added.key] | |
588 | } | |
589 | ||
590 | for (const deleted of updatedConstants.deleted) { | |
591 | hash[type][deleted.key] = deleted.label | |
592 | } | |
593 | ||
594 | delete this.updatedVideoConstants[type][npmName] | |
595 | } | |
596 | } | |
597 | ||
9157d598 C |
598 | private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) { |
599 | if (!packageJSON.staticDirs) packageJSON.staticDirs = {} | |
600 | if (!packageJSON.css) packageJSON.css = [] | |
601 | if (!packageJSON.clientScripts) packageJSON.clientScripts = [] | |
602 | if (!packageJSON.translations) packageJSON.translations = {} | |
603 | ||
604 | const { result: packageJSONValid, badFields } = isPackageJSONValid(packageJSON, pluginType) | |
605 | if (!packageJSONValid) { | |
606 | const formattedFields = badFields.map(f => `"${f}"`) | |
607 | .join(', ') | |
608 | ||
609 | throw new Error(`PackageJSON is invalid (invalid fields: ${formattedFields}).`) | |
610 | } | |
611 | } | |
612 | ||
345da516 C |
613 | static get Instance () { |
614 | return this.instance || (this.instance = new this()) | |
615 | } | |
616 | } |