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