aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-04-10 15:07:54 +0200
committerChocobozzz <me@florianbigard.com>2020-04-10 15:23:25 +0200
commit5e2b2e2775421cd98286d6e2f75cf38aae7a212c (patch)
treed92e32824d83cecbe5e90206738f393b47e55754 /server/lib/plugins
parent9afa0901f11c321e071c42ba3c814a3af4843c55 (diff)
downloadPeerTube-5e2b2e2775421cd98286d6e2f75cf38aae7a212c.tar.gz
PeerTube-5e2b2e2775421cd98286d6e2f75cf38aae7a212c.tar.zst
PeerTube-5e2b2e2775421cd98286d6e2f75cf38aae7a212c.zip
Add ability for plugins to add custom routes
Diffstat (limited to 'server/lib/plugins')
-rw-r--r--server/lib/plugins/plugin-manager.ts51
-rw-r--r--server/lib/plugins/register-helpers-store.ts235
-rw-r--r--server/lib/plugins/register-helpers.ts180
3 files changed, 260 insertions, 206 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts
index 44530d203..37fb07716 100644
--- a/server/lib/plugins/plugin-manager.ts
+++ b/server/lib/plugins/plugin-manager.ts
@@ -13,15 +13,14 @@ import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants'
13import { PluginType } from '../../../shared/models/plugins/plugin.type' 13import { PluginType } from '../../../shared/models/plugins/plugin.type'
14import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' 14import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn'
15import { outputFile, readJSON } from 'fs-extra' 15import { outputFile, readJSON } from 'fs-extra'
16import { ServerHook, ServerHookName, serverHookObject } from '../../../shared/models/plugins/server-hook.model' 16import { ServerHook, ServerHookName } from '../../../shared/models/plugins/server-hook.model'
17import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks' 17import { getHookType, internalRunHook } from '../../../shared/core-utils/plugins/hooks'
18import { RegisterServerOptions } from '../../typings/plugins/register-server-option.model' 18import { RegisterServerOptions } from '../../typings/plugins/register-server-option.model'
19import { PluginLibrary } from '../../typings/plugins' 19import { PluginLibrary } from '../../typings/plugins'
20import { ClientHtml } from '../client-html' 20import { ClientHtml } from '../client-html'
21import { RegisterServerHookOptions } from '../../../shared/models/plugins/register-server-hook.model'
22import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model'
23import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model' 21import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model'
24import { buildRegisterHelpers, reinitVideoConstants } from './register-helpers' 22import { RegisterHelpersStore } from './register-helpers-store'
23import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model'
25 24
26export interface RegisteredPlugin { 25export interface RegisteredPlugin {
27 npmName: string 26 npmName: string
@@ -59,10 +58,11 @@ export class PluginManager implements ServerHook {
59 private static instance: PluginManager 58 private static instance: PluginManager
60 59
61 private registeredPlugins: { [name: string]: RegisteredPlugin } = {} 60 private registeredPlugins: { [name: string]: RegisteredPlugin } = {}
62 private settings: { [name: string]: RegisterServerSettingOptions[] } = {}
63 private hooks: { [name: string]: HookInformationValue[] } = {} 61 private hooks: { [name: string]: HookInformationValue[] } = {}
64 private translations: PluginLocalesTranslations = {} 62 private translations: PluginLocalesTranslations = {}
65 63
64 private registerHelpersStore: { [npmName: string]: RegisterHelpersStore } = {}
65
66 private constructor () { 66 private constructor () {
67 } 67 }
68 68
@@ -103,7 +103,17 @@ export class PluginManager implements ServerHook {
103 } 103 }
104 104
105 getRegisteredSettings (npmName: string) { 105 getRegisteredSettings (npmName: string) {
106 return this.settings[npmName] || [] 106 const store = this.registerHelpersStore[npmName]
107 if (store) return store.getSettings()
108
109 return []
110 }
111
112 getRouter (npmName: string) {
113 const store = this.registerHelpersStore[npmName]
114 if (!store) return null
115
116 return store.getRouter()
107 } 117 }
108 118
109 getTranslations (locale: string) { 119 getTranslations (locale: string) {
@@ -164,7 +174,6 @@ export class PluginManager implements ServerHook {
164 } 174 }
165 175
166 delete this.registeredPlugins[plugin.npmName] 176 delete this.registeredPlugins[plugin.npmName]
167 delete this.settings[plugin.npmName]
168 177
169 this.deleteTranslations(plugin.npmName) 178 this.deleteTranslations(plugin.npmName)
170 179
@@ -176,7 +185,10 @@ export class PluginManager implements ServerHook {
176 this.hooks[key] = this.hooks[key].filter(h => h.npmName !== npmName) 185 this.hooks[key] = this.hooks[key].filter(h => h.npmName !== npmName)
177 } 186 }
178 187
179 reinitVideoConstants(plugin.npmName) 188 const store = this.registerHelpersStore[plugin.npmName]
189 store.reinitVideoConstants(plugin.npmName)
190
191 delete this.registerHelpersStore[plugin.npmName]
180 192
181 logger.info('Regenerating registered plugin CSS to global file.') 193 logger.info('Regenerating registered plugin CSS to global file.')
182 await this.regeneratePluginGlobalCSS() 194 await this.regeneratePluginGlobalCSS()
@@ -429,34 +441,21 @@ export class PluginManager implements ServerHook {
429 // ###################### Generate register helpers ###################### 441 // ###################### Generate register helpers ######################
430 442
431 private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterServerOptions { 443 private getRegisterHelpers (npmName: string, plugin: PluginModel): RegisterServerOptions {
432 const registerHook = (options: RegisterServerHookOptions) => { 444 const onHookAdded = (options: RegisterServerHookOptions) => {
433 if (serverHookObject[options.target] !== true) {
434 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, npmName)
435 return
436 }
437
438 if (!this.hooks[options.target]) this.hooks[options.target] = [] 445 if (!this.hooks[options.target]) this.hooks[options.target] = []
439 446
440 this.hooks[options.target].push({ 447 this.hooks[options.target].push({
441 npmName, 448 npmName: npmName,
442 pluginName: plugin.name, 449 pluginName: plugin.name,
443 handler: options.handler, 450 handler: options.handler,
444 priority: options.priority || 0 451 priority: options.priority || 0
445 }) 452 })
446 } 453 }
447 454
448 const registerSetting = (options: RegisterServerSettingOptions) => { 455 const registerHelpersStore = new RegisterHelpersStore(npmName, plugin, onHookAdded.bind(this))
449 if (!this.settings[npmName]) this.settings[npmName] = [] 456 this.registerHelpersStore[npmName] = registerHelpersStore
450
451 this.settings[npmName].push(options)
452 }
453
454 const registerHelpers = buildRegisterHelpers(npmName, plugin)
455 457
456 return Object.assign(registerHelpers, { 458 return registerHelpersStore.buildRegisterHelpers()
457 registerHook,
458 registerSetting
459 })
460 } 459 }
461 460
462 private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) { 461 private sanitizeAndCheckPackageJSONOrThrow (packageJSON: PluginPackageJson, pluginType: PluginType) {
diff --git a/server/lib/plugins/register-helpers-store.ts b/server/lib/plugins/register-helpers-store.ts
new file mode 100644
index 000000000..c76c0161a
--- /dev/null
+++ b/server/lib/plugins/register-helpers-store.ts
@@ -0,0 +1,235 @@
1import { PluginSettingsManager } from '@shared/models/plugins/plugin-settings-manager.model'
2import { PluginModel } from '@server/models/server/plugin'
3import { PluginStorageManager } from '@shared/models/plugins/plugin-storage-manager.model'
4import { PluginVideoLanguageManager } from '@shared/models/plugins/plugin-video-language-manager.model'
5import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '@server/initializers/constants'
6import { PluginVideoLicenceManager } from '@shared/models/plugins/plugin-video-licence-manager.model'
7import { PluginVideoCategoryManager } from '@shared/models/plugins/plugin-video-category-manager.model'
8import { RegisterServerOptions } from '@server/typings/plugins'
9import { buildPluginHelpers } from './plugin-helpers'
10import { logger } from '@server/helpers/logger'
11import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model'
12import { serverHookObject } from '@shared/models/plugins/server-hook.model'
13import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model'
14import * as express from 'express'
15
16type AlterableVideoConstant = 'language' | 'licence' | 'category'
17type VideoConstant = { [key in number | string]: string }
18
19type UpdatedVideoConstant = {
20 [name in AlterableVideoConstant]: {
21 added: { key: number | string, label: string }[]
22 deleted: { key: number | string, label: string }[]
23 }
24}
25
26export class RegisterHelpersStore {
27 private readonly updatedVideoConstants: UpdatedVideoConstant = {
28 language: { added: [], deleted: [] },
29 licence: { added: [], deleted: [] },
30 category: { added: [], deleted: [] }
31 }
32
33 private readonly settings: RegisterServerSettingOptions[] = []
34
35 private readonly router: express.Router
36
37 constructor (
38 private readonly npmName: string,
39 private readonly plugin: PluginModel,
40 private readonly onHookAdded: (options: RegisterServerHookOptions) => void
41 ) {
42 this.router = express.Router()
43 }
44
45 buildRegisterHelpers (): RegisterServerOptions {
46 const registerHook = this.buildRegisterHook()
47 const registerSetting = this.buildRegisterSetting()
48
49 const getRouter = this.buildGetRouter()
50
51 const settingsManager = this.buildSettingsManager()
52 const storageManager = this.buildStorageManager()
53
54 const videoLanguageManager = this.buildVideoLanguageManager()
55
56 const videoLicenceManager = this.buildVideoLicenceManager()
57 const videoCategoryManager = this.buildVideoCategoryManager()
58
59 const peertubeHelpers = buildPluginHelpers(this.npmName)
60
61 return {
62 registerHook,
63 registerSetting,
64
65 getRouter,
66
67 settingsManager,
68 storageManager,
69
70 videoLanguageManager,
71 videoCategoryManager,
72 videoLicenceManager,
73
74 peertubeHelpers
75 }
76 }
77
78 reinitVideoConstants (npmName: string) {
79 const hash = {
80 language: VIDEO_LANGUAGES,
81 licence: VIDEO_LICENCES,
82 category: VIDEO_CATEGORIES
83 }
84 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category' ]
85
86 for (const type of types) {
87 const updatedConstants = this.updatedVideoConstants[type][npmName]
88 if (!updatedConstants) continue
89
90 for (const added of updatedConstants.added) {
91 delete hash[type][added.key]
92 }
93
94 for (const deleted of updatedConstants.deleted) {
95 hash[type][deleted.key] = deleted.label
96 }
97
98 delete this.updatedVideoConstants[type][npmName]
99 }
100 }
101
102 getSettings () {
103 return this.settings
104 }
105
106 getRouter () {
107 return this.router
108 }
109
110 private buildGetRouter () {
111 return () => this.router
112 }
113
114 private buildRegisterSetting () {
115 return (options: RegisterServerSettingOptions) => {
116 this.settings.push(options)
117 }
118 }
119
120 private buildRegisterHook () {
121 return (options: RegisterServerHookOptions) => {
122 if (serverHookObject[options.target] !== true) {
123 logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName)
124 return
125 }
126
127 return this.onHookAdded(options)
128 }
129 }
130
131 private buildSettingsManager (): PluginSettingsManager {
132 return {
133 getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name),
134
135 setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value)
136 }
137 }
138
139 private buildStorageManager (): PluginStorageManager {
140 return {
141 getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key),
142
143 storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data)
144 }
145 }
146
147 private buildVideoLanguageManager (): PluginVideoLanguageManager {
148 return {
149 addLanguage: (key: string, label: string) => {
150 return this.addConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label })
151 },
152
153 deleteLanguage: (key: string) => {
154 return this.deleteConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
155 }
156 }
157 }
158
159 private buildVideoCategoryManager (): PluginVideoCategoryManager {
160 return {
161 addCategory: (key: number, label: string) => {
162 return this.addConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label })
163 },
164
165 deleteCategory: (key: number) => {
166 return this.deleteConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
167 }
168 }
169 }
170
171 private buildVideoLicenceManager (): PluginVideoLicenceManager {
172 return {
173 addLicence: (key: number, label: string) => {
174 return this.addConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key, label })
175 },
176
177 deleteLicence: (key: number) => {
178 return this.deleteConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key })
179 }
180 }
181 }
182
183 private addConstant<T extends string | number> (parameters: {
184 npmName: string
185 type: AlterableVideoConstant
186 obj: VideoConstant
187 key: T
188 label: string
189 }) {
190 const { npmName, type, obj, key, label } = parameters
191
192 if (obj[key]) {
193 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
194 return false
195 }
196
197 if (!this.updatedVideoConstants[type][npmName]) {
198 this.updatedVideoConstants[type][npmName] = {
199 added: [],
200 deleted: []
201 }
202 }
203
204 this.updatedVideoConstants[type][npmName].added.push({ key, label })
205 obj[key] = label
206
207 return true
208 }
209
210 private deleteConstant<T extends string | number> (parameters: {
211 npmName: string
212 type: AlterableVideoConstant
213 obj: VideoConstant
214 key: T
215 }) {
216 const { npmName, type, obj, key } = parameters
217
218 if (!obj[key]) {
219 logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key)
220 return false
221 }
222
223 if (!this.updatedVideoConstants[type][npmName]) {
224 this.updatedVideoConstants[type][npmName] = {
225 added: [],
226 deleted: []
227 }
228 }
229
230 this.updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] })
231 delete obj[key]
232
233 return true
234 }
235}
diff --git a/server/lib/plugins/register-helpers.ts b/server/lib/plugins/register-helpers.ts
deleted file mode 100644
index 4c0935a05..000000000
--- a/server/lib/plugins/register-helpers.ts
+++ /dev/null
@@ -1,180 +0,0 @@
1import { PluginSettingsManager } from '@shared/models/plugins/plugin-settings-manager.model'
2import { PluginModel } from '@server/models/server/plugin'
3import { PluginStorageManager } from '@shared/models/plugins/plugin-storage-manager.model'
4import { PluginVideoLanguageManager } from '@shared/models/plugins/plugin-video-language-manager.model'
5import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '@server/initializers/constants'
6import { PluginVideoLicenceManager } from '@shared/models/plugins/plugin-video-licence-manager.model'
7import { PluginVideoCategoryManager } from '@shared/models/plugins/plugin-video-category-manager.model'
8import { RegisterServerOptions } from '@server/typings/plugins'
9import { buildPluginHelpers } from './plugin-helpers'
10import { logger } from '@server/helpers/logger'
11
12type AlterableVideoConstant = 'language' | 'licence' | 'category'
13type VideoConstant = { [key in number | string]: string }
14type UpdatedVideoConstant = {
15 [name in AlterableVideoConstant]: {
16 [npmName: string]: {
17 added: { key: number | string, label: string }[]
18 deleted: { key: number | string, label: string }[]
19 }
20 }
21}
22
23const updatedVideoConstants: UpdatedVideoConstant = {
24 language: {},
25 licence: {},
26 category: {}
27}
28
29function buildRegisterHelpers (npmName: string, plugin: PluginModel): Omit<RegisterServerOptions, 'registerHook' | 'registerSetting'> {
30 const settingsManager = buildSettingsManager(plugin)
31 const storageManager = buildStorageManager(plugin)
32
33 const videoLanguageManager = buildVideoLanguageManager(npmName)
34
35 const videoCategoryManager = buildVideoCategoryManager(npmName)
36 const videoLicenceManager = buildVideoLicenceManager(npmName)
37
38 const peertubeHelpers = buildPluginHelpers(npmName)
39
40 return {
41 settingsManager,
42 storageManager,
43 videoLanguageManager,
44 videoCategoryManager,
45 videoLicenceManager,
46 peertubeHelpers
47 }
48}
49
50function reinitVideoConstants (npmName: string) {
51 const hash = {
52 language: VIDEO_LANGUAGES,
53 licence: VIDEO_LICENCES,
54 category: VIDEO_CATEGORIES
55 }
56 const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category' ]
57
58 for (const type of types) {
59 const updatedConstants = updatedVideoConstants[type][npmName]
60 if (!updatedConstants) continue
61
62 for (const added of updatedConstants.added) {
63 delete hash[type][added.key]
64 }
65
66 for (const deleted of updatedConstants.deleted) {
67 hash[type][deleted.key] = deleted.label
68 }
69
70 delete updatedVideoConstants[type][npmName]
71 }
72}
73
74export {
75 buildRegisterHelpers,
76 reinitVideoConstants
77}
78
79// ---------------------------------------------------------------------------
80
81function buildSettingsManager (plugin: PluginModel): PluginSettingsManager {
82 return {
83 getSetting: (name: string) => PluginModel.getSetting(plugin.name, plugin.type, name),
84
85 setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value)
86 }
87}
88
89function buildStorageManager (plugin: PluginModel): PluginStorageManager {
90 return {
91 getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key),
92
93 storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data)
94 }
95}
96
97function buildVideoLanguageManager (npmName: string): PluginVideoLanguageManager {
98 return {
99 addLanguage: (key: string, label: string) => addConstant({ npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label }),
100
101 deleteLanguage: (key: string) => deleteConstant({ npmName, type: 'language', obj: VIDEO_LANGUAGES, key })
102 }
103}
104
105function buildVideoCategoryManager (npmName: string): PluginVideoCategoryManager {
106 return {
107 addCategory: (key: number, label: string) => {
108 return addConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label })
109 },
110
111 deleteCategory: (key: number) => {
112 return deleteConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key })
113 }
114 }
115}
116
117function buildVideoLicenceManager (npmName: string): PluginVideoLicenceManager {
118 return {
119 addLicence: (key: number, label: string) => {
120 return addConstant({ npmName, type: 'licence', obj: VIDEO_LICENCES, key, label })
121 },
122
123 deleteLicence: (key: number) => {
124 return deleteConstant({ npmName, type: 'licence', obj: VIDEO_LICENCES, key })
125 }
126 }
127}
128
129function addConstant<T extends string | number> (parameters: {
130 npmName: string
131 type: AlterableVideoConstant
132 obj: VideoConstant
133 key: T
134 label: string
135}) {
136 const { npmName, type, obj, key, label } = parameters
137
138 if (obj[key]) {
139 logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key)
140 return false
141 }
142
143 if (!updatedVideoConstants[type][npmName]) {
144 updatedVideoConstants[type][npmName] = {
145 added: [],
146 deleted: []
147 }
148 }
149
150 updatedVideoConstants[type][npmName].added.push({ key, label })
151 obj[key] = label
152
153 return true
154}
155
156function deleteConstant<T extends string | number> (parameters: {
157 npmName: string
158 type: AlterableVideoConstant
159 obj: VideoConstant
160 key: T
161}) {
162 const { npmName, type, obj, key } = parameters
163
164 if (!obj[key]) {
165 logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key)
166 return false
167 }
168
169 if (!updatedVideoConstants[type][npmName]) {
170 updatedVideoConstants[type][npmName] = {
171 added: [],
172 deleted: []
173 }
174 }
175
176 updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] })
177 delete obj[key]
178
179 return true
180}