aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins/register-helpers-store.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/plugins/register-helpers-store.ts')
-rw-r--r--server/lib/plugins/register-helpers-store.ts235
1 files changed, 235 insertions, 0 deletions
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}