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/register-helpers.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/register-helpers.ts')
-rw-r--r-- | server/lib/plugins/register-helpers.ts | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/server/lib/plugins/register-helpers.ts b/server/lib/plugins/register-helpers.ts new file mode 100644 index 000000000..58bc96f04 --- /dev/null +++ b/server/lib/plugins/register-helpers.ts | |||
@@ -0,0 +1,180 @@ | |||
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' | ||
5 | import { VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '@server/initializers/constants' | ||
6 | import { PluginVideoLicenceManager } from '@shared/models/plugins/plugin-video-licence-manager.model' | ||
7 | import { PluginVideoCategoryManager } from '@shared/models/plugins/plugin-video-category-manager.model' | ||
8 | import { RegisterServerOptions } from '@server/typings/plugins' | ||
9 | import { buildPluginHelpers } from './plugin-helpers' | ||
10 | import { logger } from '@server/helpers/logger' | ||
11 | |||
12 | type AlterableVideoConstant = 'language' | 'licence' | 'category' | ||
13 | type VideoConstant = { [key in number | string]: string } | ||
14 | type 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 | |||
23 | const updatedVideoConstants: UpdatedVideoConstant = { | ||
24 | language: {}, | ||
25 | licence: {}, | ||
26 | category: {} | ||
27 | } | ||
28 | |||
29 | function 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, plugin) | ||
39 | |||
40 | return { | ||
41 | settingsManager, | ||
42 | storageManager, | ||
43 | videoLanguageManager, | ||
44 | videoCategoryManager, | ||
45 | videoLicenceManager, | ||
46 | peertubeHelpers | ||
47 | } | ||
48 | } | ||
49 | |||
50 | function 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 | |||
74 | export { | ||
75 | buildRegisterHelpers, | ||
76 | reinitVideoConstants | ||
77 | } | ||
78 | |||
79 | // --------------------------------------------------------------------------- | ||
80 | |||
81 | function 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 | |||
89 | function 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 | |||
97 | function 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 | |||
105 | function 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 | |||
117 | function 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 | |||
129 | function 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 | |||
156 | function 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 | } | ||