]>
Commit | Line | Data |
---|---|---|
5e2b2e27 C |
1 | import { PluginSettingsManager } from '@shared/models/plugins/plugin-settings-manager.model' |
2 | import { PluginModel } from '@server/models/server/plugin' | |
3 | import { PluginStorageManager } from '@shared/models/plugins/plugin-storage-manager.model' | |
4 | import { PluginVideoLanguageManager } from '@shared/models/plugins/plugin-video-language-manager.model' | |
b3af2601 C |
5 | import { |
6 | VIDEO_CATEGORIES, | |
7 | VIDEO_LANGUAGES, | |
8 | VIDEO_LICENCES, | |
9 | VIDEO_PLAYLIST_PRIVACIES, | |
10 | VIDEO_PRIVACIES | |
11 | } from '@server/initializers/constants' | |
5e2b2e27 C |
12 | import { PluginVideoLicenceManager } from '@shared/models/plugins/plugin-video-licence-manager.model' |
13 | import { PluginVideoCategoryManager } from '@shared/models/plugins/plugin-video-category-manager.model' | |
14 | import { RegisterServerOptions } from '@server/typings/plugins' | |
15 | import { buildPluginHelpers } from './plugin-helpers' | |
16 | import { logger } from '@server/helpers/logger' | |
17 | import { RegisterServerHookOptions } from '@shared/models/plugins/register-server-hook.model' | |
18 | import { serverHookObject } from '@shared/models/plugins/server-hook.model' | |
19 | import { RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model' | |
20 | import * as express from 'express' | |
b3af2601 C |
21 | import { PluginVideoPrivacyManager } from '@shared/models/plugins/plugin-video-privacy-manager.model' |
22 | import { PluginPlaylistPrivacyManager } from '@shared/models/plugins/plugin-playlist-privacy-manager.model' | |
5e2b2e27 | 23 | |
b3af2601 | 24 | type AlterableVideoConstant = 'language' | 'licence' | 'category' | 'privacy' | 'playlistPrivacy' |
5e2b2e27 C |
25 | type VideoConstant = { [key in number | string]: string } |
26 | ||
27 | type UpdatedVideoConstant = { | |
28 | [name in AlterableVideoConstant]: { | |
29 | added: { key: number | string, label: string }[] | |
30 | deleted: { key: number | string, label: string }[] | |
31 | } | |
32 | } | |
33 | ||
34 | export class RegisterHelpersStore { | |
35 | private readonly updatedVideoConstants: UpdatedVideoConstant = { | |
b3af2601 C |
36 | playlistPrivacy: { added: [], deleted: [] }, |
37 | privacy: { added: [], deleted: [] }, | |
5e2b2e27 C |
38 | language: { added: [], deleted: [] }, |
39 | licence: { added: [], deleted: [] }, | |
40 | category: { added: [], deleted: [] } | |
41 | } | |
42 | ||
43 | private readonly settings: RegisterServerSettingOptions[] = [] | |
44 | ||
45 | private readonly router: express.Router | |
46 | ||
47 | constructor ( | |
48 | private readonly npmName: string, | |
49 | private readonly plugin: PluginModel, | |
50 | private readonly onHookAdded: (options: RegisterServerHookOptions) => void | |
51 | ) { | |
52 | this.router = express.Router() | |
53 | } | |
54 | ||
55 | buildRegisterHelpers (): RegisterServerOptions { | |
56 | const registerHook = this.buildRegisterHook() | |
57 | const registerSetting = this.buildRegisterSetting() | |
58 | ||
59 | const getRouter = this.buildGetRouter() | |
60 | ||
61 | const settingsManager = this.buildSettingsManager() | |
62 | const storageManager = this.buildStorageManager() | |
63 | ||
64 | const videoLanguageManager = this.buildVideoLanguageManager() | |
65 | ||
66 | const videoLicenceManager = this.buildVideoLicenceManager() | |
67 | const videoCategoryManager = this.buildVideoCategoryManager() | |
68 | ||
b3af2601 C |
69 | const videoPrivacyManager = this.buildVideoPrivacyManager() |
70 | const playlistPrivacyManager = this.buildPlaylistPrivacyManager() | |
71 | ||
5e2b2e27 C |
72 | const peertubeHelpers = buildPluginHelpers(this.npmName) |
73 | ||
74 | return { | |
75 | registerHook, | |
76 | registerSetting, | |
77 | ||
78 | getRouter, | |
79 | ||
80 | settingsManager, | |
81 | storageManager, | |
82 | ||
83 | videoLanguageManager, | |
84 | videoCategoryManager, | |
85 | videoLicenceManager, | |
86 | ||
b3af2601 C |
87 | videoPrivacyManager, |
88 | playlistPrivacyManager, | |
89 | ||
5e2b2e27 C |
90 | peertubeHelpers |
91 | } | |
92 | } | |
93 | ||
94 | reinitVideoConstants (npmName: string) { | |
95 | const hash = { | |
96 | language: VIDEO_LANGUAGES, | |
97 | licence: VIDEO_LICENCES, | |
b3af2601 C |
98 | category: VIDEO_CATEGORIES, |
99 | privacy: VIDEO_PRIVACIES, | |
100 | playlistPrivacy: VIDEO_PLAYLIST_PRIVACIES | |
5e2b2e27 | 101 | } |
b3af2601 | 102 | const types: AlterableVideoConstant[] = [ 'language', 'licence', 'category', 'privacy', 'playlistPrivacy' ] |
5e2b2e27 C |
103 | |
104 | for (const type of types) { | |
105 | const updatedConstants = this.updatedVideoConstants[type][npmName] | |
106 | if (!updatedConstants) continue | |
107 | ||
108 | for (const added of updatedConstants.added) { | |
109 | delete hash[type][added.key] | |
110 | } | |
111 | ||
112 | for (const deleted of updatedConstants.deleted) { | |
113 | hash[type][deleted.key] = deleted.label | |
114 | } | |
115 | ||
116 | delete this.updatedVideoConstants[type][npmName] | |
117 | } | |
118 | } | |
119 | ||
120 | getSettings () { | |
121 | return this.settings | |
122 | } | |
123 | ||
124 | getRouter () { | |
125 | return this.router | |
126 | } | |
127 | ||
128 | private buildGetRouter () { | |
129 | return () => this.router | |
130 | } | |
131 | ||
132 | private buildRegisterSetting () { | |
133 | return (options: RegisterServerSettingOptions) => { | |
134 | this.settings.push(options) | |
135 | } | |
136 | } | |
137 | ||
138 | private buildRegisterHook () { | |
139 | return (options: RegisterServerHookOptions) => { | |
140 | if (serverHookObject[options.target] !== true) { | |
141 | logger.warn('Unknown hook %s of plugin %s. Skipping.', options.target, this.npmName) | |
142 | return | |
143 | } | |
144 | ||
145 | return this.onHookAdded(options) | |
146 | } | |
147 | } | |
148 | ||
149 | private buildSettingsManager (): PluginSettingsManager { | |
150 | return { | |
151 | getSetting: (name: string) => PluginModel.getSetting(this.plugin.name, this.plugin.type, name), | |
152 | ||
153 | setSetting: (name: string, value: string) => PluginModel.setSetting(this.plugin.name, this.plugin.type, name, value) | |
154 | } | |
155 | } | |
156 | ||
157 | private buildStorageManager (): PluginStorageManager { | |
158 | return { | |
159 | getData: (key: string) => PluginModel.getData(this.plugin.name, this.plugin.type, key), | |
160 | ||
161 | storeData: (key: string, data: any) => PluginModel.storeData(this.plugin.name, this.plugin.type, key, data) | |
162 | } | |
163 | } | |
164 | ||
165 | private buildVideoLanguageManager (): PluginVideoLanguageManager { | |
166 | return { | |
167 | addLanguage: (key: string, label: string) => { | |
168 | return this.addConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key, label }) | |
169 | }, | |
170 | ||
171 | deleteLanguage: (key: string) => { | |
172 | return this.deleteConstant({ npmName: this.npmName, type: 'language', obj: VIDEO_LANGUAGES, key }) | |
173 | } | |
174 | } | |
175 | } | |
176 | ||
177 | private buildVideoCategoryManager (): PluginVideoCategoryManager { | |
178 | return { | |
179 | addCategory: (key: number, label: string) => { | |
180 | return this.addConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label }) | |
181 | }, | |
182 | ||
183 | deleteCategory: (key: number) => { | |
184 | return this.deleteConstant({ npmName: this.npmName, type: 'category', obj: VIDEO_CATEGORIES, key }) | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
b3af2601 C |
189 | private buildVideoPrivacyManager (): PluginVideoPrivacyManager { |
190 | return { | |
191 | deletePrivacy: (key: number) => { | |
192 | return this.deleteConstant({ npmName: this.npmName, type: 'privacy', obj: VIDEO_PRIVACIES, key }) | |
193 | } | |
194 | } | |
195 | } | |
196 | ||
197 | private buildPlaylistPrivacyManager (): PluginPlaylistPrivacyManager { | |
198 | return { | |
199 | deletePlaylistPrivacy: (key: number) => { | |
200 | return this.deleteConstant({ npmName: this.npmName, type: 'playlistPrivacy', obj: VIDEO_PLAYLIST_PRIVACIES, key }) | |
201 | } | |
202 | } | |
203 | } | |
204 | ||
5e2b2e27 C |
205 | private buildVideoLicenceManager (): PluginVideoLicenceManager { |
206 | return { | |
207 | addLicence: (key: number, label: string) => { | |
208 | return this.addConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key, label }) | |
209 | }, | |
210 | ||
211 | deleteLicence: (key: number) => { | |
212 | return this.deleteConstant({ npmName: this.npmName, type: 'licence', obj: VIDEO_LICENCES, key }) | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | private addConstant<T extends string | number> (parameters: { | |
218 | npmName: string | |
219 | type: AlterableVideoConstant | |
220 | obj: VideoConstant | |
221 | key: T | |
222 | label: string | |
223 | }) { | |
224 | const { npmName, type, obj, key, label } = parameters | |
225 | ||
226 | if (obj[key]) { | |
227 | logger.warn('Cannot add %s %s by plugin %s: key already exists.', type, npmName, key) | |
228 | return false | |
229 | } | |
230 | ||
231 | if (!this.updatedVideoConstants[type][npmName]) { | |
232 | this.updatedVideoConstants[type][npmName] = { | |
233 | added: [], | |
234 | deleted: [] | |
235 | } | |
236 | } | |
237 | ||
238 | this.updatedVideoConstants[type][npmName].added.push({ key, label }) | |
239 | obj[key] = label | |
240 | ||
241 | return true | |
242 | } | |
243 | ||
244 | private deleteConstant<T extends string | number> (parameters: { | |
245 | npmName: string | |
246 | type: AlterableVideoConstant | |
247 | obj: VideoConstant | |
248 | key: T | |
249 | }) { | |
250 | const { npmName, type, obj, key } = parameters | |
251 | ||
252 | if (!obj[key]) { | |
253 | logger.warn('Cannot delete %s %s by plugin %s: key does not exist.', type, npmName, key) | |
254 | return false | |
255 | } | |
256 | ||
257 | if (!this.updatedVideoConstants[type][npmName]) { | |
258 | this.updatedVideoConstants[type][npmName] = { | |
259 | added: [], | |
260 | deleted: [] | |
261 | } | |
262 | } | |
263 | ||
264 | this.updatedVideoConstants[type][npmName].deleted.push({ key, label: obj[key] }) | |
265 | delete obj[key] | |
266 | ||
267 | return true | |
268 | } | |
269 | } |