diff options
author | Chocobozzz <me@florianbigard.com> | 2020-04-09 09:57:32 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-04-09 09:57:32 +0200 |
commit | bc0d801bb7a0cc503c1637f4a07bb51d68d85608 (patch) | |
tree | 6eb486085fb5b536309315b4db06c790acf98ed0 /server/lib/plugins/plugin-manager.ts | |
parent | 00c228363f0db1d181d546eebb0c7ec3eb487976 (diff) | |
download | PeerTube-bc0d801bb7a0cc503c1637f4a07bb51d68d85608.tar.gz PeerTube-bc0d801bb7a0cc503c1637f4a07bb51d68d85608.tar.zst PeerTube-bc0d801bb7a0cc503c1637f4a07bb51d68d85608.zip |
Refactor plugin helpers factory
Diffstat (limited to 'server/lib/plugins/plugin-manager.ts')
-rw-r--r-- | server/lib/plugins/plugin-manager.ts | 150 |
1 files changed, 7 insertions, 143 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index 73f7a71ce..44530d203 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts | |||
@@ -9,12 +9,10 @@ import { | |||
9 | PluginTranslationPaths as PackagePluginTranslations | 9 | PluginTranslationPaths as PackagePluginTranslations |
10 | } from '../../../shared/models/plugins/plugin-package-json.model' | 10 | } from '../../../shared/models/plugins/plugin-package-json.model' |
11 | import { createReadStream, createWriteStream } from 'fs' | 11 | import { createReadStream, createWriteStream } from 'fs' |
12 | import { PLUGIN_GLOBAL_CSS_PATH, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../../initializers/constants' | 12 | import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants' |
13 | import { PluginType } from '../../../shared/models/plugins/plugin.type' | 13 | import { PluginType } from '../../../shared/models/plugins/plugin.type' |
14 | import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' | 14 | import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' |
15 | import { outputFile, readJSON } from 'fs-extra' | 15 | import { outputFile, readJSON } from 'fs-extra' |
16 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' | ||
17 | import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' | ||
18 | import { ServerHook, ServerHookName, serverHookObject } from '../../../shared/models/plugins/server-hook.model' | 16 | import { ServerHook, ServerHookName, serverHookObject } from '../../../shared/models/plugins/server-hook.model' |
19 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' | 17 | import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' |
20 | import { RegisterServerOptions } from '../../typings/plugins/register-server-option.model' | 18 | import { RegisterServerOptions } from '../../typings/plugins/register-server-option.model' |
@@ -22,10 +20,8 @@ import { PluginLibrary } from '../../typings/plugins' | |||
22 | import { ClientHtml } from '../client-html' | 20 | import { ClientHtml } from '../client-html' |
23 | import { RegisterServerHookOptions } from '../../../shared/models/plugins/register-server-hook.model' | 21 | import { RegisterServerHookOptions } from '../../../shared/models/plugins/register-server-hook.model' |
24 | import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model' | 22 | import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model' |
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' | ||
28 | import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model' | 23 | import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model' |
24 | import { buildRegisterHelpers, reinitVideoConstants } from './register-helpers' | ||
29 | 25 | ||
30 | export interface RegisteredPlugin { | 26 | export interface RegisteredPlugin { |
31 | npmName: string | 27 | npmName: string |
@@ -54,17 +50,6 @@ export interface HookInformationValue { | |||
54 | priority: number | 50 | priority: number |
55 | } | 51 | } |
56 | 52 | ||
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 | |||
68 | type PluginLocalesTranslations = { | 53 | type PluginLocalesTranslations = { |
69 | [locale: string]: PluginTranslation | 54 | [locale: string]: PluginTranslation |
70 | } | 55 | } |
@@ -78,12 +63,6 @@ export class PluginManager implements ServerHook { | |||
78 | private hooks: { [name: string]: HookInformationValue[] } = {} | 63 | private hooks: { [name: string]: HookInformationValue[] } = {} |
79 | private translations: PluginLocalesTranslations = {} | 64 | private translations: PluginLocalesTranslations = {} |
80 | 65 | ||
81 | private readonly updatedVideoConstants: UpdatedVideoConstant = { | ||
82 | language: {}, | ||
83 | licence: {}, | ||
84 | category: {} | ||
85 | } | ||
86 | |||
87 | private constructor () { | 66 | private constructor () { |
88 | } | 67 | } |
89 | 68 | ||
@@ -197,7 +176,7 @@ export class PluginManager implements ServerHook { | |||
197 | this.hooks[key] = this.hooks[key].filter(h => h.npmName !== npmName) | 176 | this.hooks[key] = this.hooks[key].filter(h => h.npmName !== npmName) |
198 | } | 177 | } |
199 | 178 | ||
200 | this.reinitVideoConstants(plugin.npmName) | 179 | reinitVideoConstants(plugin.npmName) |
201 | 180 | ||
202 | logger.info('Regenerating registered plugin CSS to global file.') | 181 | logger.info('Regenerating registered plugin CSS to global file.') |
203 | await this.regeneratePluginGlobalCSS() | 182 | await this.regeneratePluginGlobalCSS() |
@@ -472,127 +451,12 @@ export class PluginManager implements ServerHook { | |||
472 | this.settings[npmName].push(options) | 451 | this.settings[npmName].push(options) |
473 | } | 452 | } |
474 | 453 | ||
475 | const settingsManager: PluginSettingsManager = { | 454 | const registerHelpers = buildRegisterHelpers(npmName, plugin) |
476 | getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name), | ||
477 | 455 | ||
478 | setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value) | 456 | return Object.assign(registerHelpers, { |
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 | |||
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 | |||
493 | const videoCategoryManager: PluginVideoCategoryManager = { | ||
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 | |||
505 | const peertubeHelpers = { | ||
506 | logger | ||
507 | } | ||
508 | |||
509 | return { | ||
510 | registerHook, | 457 | registerHook, |
511 | registerSetting, | 458 | registerSetting |
512 | settingsManager, | 459 | }) |
513 | storageManager, | ||
514 | videoLanguageManager, | ||
515 | videoCategoryManager, | ||
516 | videoLicenceManager, | ||
517 | peertubeHelpers | ||
518 | } | ||
519 | } | ||
520 | |||
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 | } | 460 | } |
597 | 461 | ||
598 | private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) { | 462 | private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) { |