]>
Commit | Line | Data |
---|---|---|
1 | import { FindAndCountOptions, json, QueryTypes } from 'sequelize' | |
2 | import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' | |
3 | import { MPlugin, MPluginFormattable } from '@server/types/models' | |
4 | import { AttributesOnly } from '@shared/core-utils' | |
5 | import { PeerTubePlugin, PluginType, RegisterServerSettingOptions } from '../../../shared/models' | |
6 | import { | |
7 | isPluginDescriptionValid, | |
8 | isPluginHomepage, | |
9 | isPluginNameValid, | |
10 | isPluginTypeValid, | |
11 | isPluginVersionValid | |
12 | } from '../../helpers/custom-validators/plugins' | |
13 | import { getSort, throwIfNotValid } from '../utils' | |
14 | ||
15 | @DefaultScope(() => ({ | |
16 | attributes: { | |
17 | exclude: [ 'storage' ] | |
18 | } | |
19 | })) | |
20 | ||
21 | @Table({ | |
22 | tableName: 'plugin', | |
23 | indexes: [ | |
24 | { | |
25 | fields: [ 'name', 'type' ], | |
26 | unique: true | |
27 | } | |
28 | ] | |
29 | }) | |
30 | export class PluginModel extends Model<Partial<AttributesOnly<PluginModel>>> { | |
31 | ||
32 | @AllowNull(false) | |
33 | @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name')) | |
34 | @Column | |
35 | name: string | |
36 | ||
37 | @AllowNull(false) | |
38 | @Is('PluginType', value => throwIfNotValid(value, isPluginTypeValid, 'type')) | |
39 | @Column | |
40 | type: number | |
41 | ||
42 | @AllowNull(false) | |
43 | @Is('PluginVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version')) | |
44 | @Column | |
45 | version: string | |
46 | ||
47 | @AllowNull(true) | |
48 | @Is('PluginLatestVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version')) | |
49 | @Column | |
50 | latestVersion: string | |
51 | ||
52 | @AllowNull(false) | |
53 | @Column | |
54 | enabled: boolean | |
55 | ||
56 | @AllowNull(false) | |
57 | @Column | |
58 | uninstalled: boolean | |
59 | ||
60 | @AllowNull(false) | |
61 | @Column | |
62 | peertubeEngine: string | |
63 | ||
64 | @AllowNull(true) | |
65 | @Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description')) | |
66 | @Column | |
67 | description: string | |
68 | ||
69 | @AllowNull(false) | |
70 | @Is('PluginHomepage', value => throwIfNotValid(value, isPluginHomepage, 'homepage')) | |
71 | @Column | |
72 | homepage: string | |
73 | ||
74 | @AllowNull(true) | |
75 | @Column(DataType.JSONB) | |
76 | settings: any | |
77 | ||
78 | @AllowNull(true) | |
79 | @Column(DataType.JSONB) | |
80 | storage: any | |
81 | ||
82 | @CreatedAt | |
83 | createdAt: Date | |
84 | ||
85 | @UpdatedAt | |
86 | updatedAt: Date | |
87 | ||
88 | static listEnabledPluginsAndThemes (): Promise<MPlugin[]> { | |
89 | const query = { | |
90 | where: { | |
91 | enabled: true, | |
92 | uninstalled: false | |
93 | } | |
94 | } | |
95 | ||
96 | return PluginModel.findAll(query) | |
97 | } | |
98 | ||
99 | static loadByNpmName (npmName: string): Promise<MPlugin> { | |
100 | const name = this.normalizePluginName(npmName) | |
101 | const type = this.getTypeFromNpmName(npmName) | |
102 | ||
103 | const query = { | |
104 | where: { | |
105 | name, | |
106 | type | |
107 | } | |
108 | } | |
109 | ||
110 | return PluginModel.findOne(query) | |
111 | } | |
112 | ||
113 | static getSetting (pluginName: string, pluginType: PluginType, settingName: string, registeredSettings: RegisterServerSettingOptions[]) { | |
114 | const query = { | |
115 | attributes: [ 'settings' ], | |
116 | where: { | |
117 | name: pluginName, | |
118 | type: pluginType | |
119 | } | |
120 | } | |
121 | ||
122 | return PluginModel.findOne(query) | |
123 | .then(p => { | |
124 | if (!p || !p.settings || p.settings === undefined) { | |
125 | const registered = registeredSettings.find(s => s.name === settingName) | |
126 | if (!registered || registered.default === undefined) return undefined | |
127 | ||
128 | return registered.default | |
129 | } | |
130 | ||
131 | return p.settings[settingName] | |
132 | }) | |
133 | } | |
134 | ||
135 | static getSettings ( | |
136 | pluginName: string, | |
137 | pluginType: PluginType, | |
138 | settingNames: string[], | |
139 | registeredSettings: RegisterServerSettingOptions[] | |
140 | ) { | |
141 | const query = { | |
142 | attributes: [ 'settings' ], | |
143 | where: { | |
144 | name: pluginName, | |
145 | type: pluginType | |
146 | } | |
147 | } | |
148 | ||
149 | return PluginModel.findOne(query) | |
150 | .then(p => { | |
151 | const result: { [settingName: string ]: string | boolean } = {} | |
152 | ||
153 | for (const name of settingNames) { | |
154 | if (!p || !p.settings || p.settings[name] === undefined) { | |
155 | const registered = registeredSettings.find(s => s.name === name) | |
156 | ||
157 | if (registered?.default !== undefined) { | |
158 | result[name] = registered.default | |
159 | } | |
160 | } else { | |
161 | result[name] = p.settings[name] | |
162 | } | |
163 | } | |
164 | ||
165 | return result | |
166 | }) | |
167 | } | |
168 | ||
169 | static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) { | |
170 | const query = { | |
171 | where: { | |
172 | name: pluginName, | |
173 | type: pluginType | |
174 | } | |
175 | } | |
176 | ||
177 | const toSave = { | |
178 | [`settings.${settingName}`]: settingValue | |
179 | } | |
180 | ||
181 | return PluginModel.update(toSave, query) | |
182 | .then(() => undefined) | |
183 | } | |
184 | ||
185 | static getData (pluginName: string, pluginType: PluginType, key: string) { | |
186 | const query = { | |
187 | raw: true, | |
188 | attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings | |
189 | where: { | |
190 | name: pluginName, | |
191 | type: pluginType | |
192 | } | |
193 | } | |
194 | ||
195 | return PluginModel.findOne(query) | |
196 | .then((c: any) => { | |
197 | if (!c) return undefined | |
198 | const value = c.value | |
199 | ||
200 | if (typeof value === 'string' && value.startsWith('{')) { | |
201 | try { | |
202 | return JSON.parse(value) | |
203 | } catch { | |
204 | return value | |
205 | } | |
206 | } | |
207 | ||
208 | return c.value | |
209 | }) | |
210 | } | |
211 | ||
212 | static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) { | |
213 | const query = 'UPDATE "plugin" SET "storage" = jsonb_set(coalesce("storage", \'{}\'), :key, :data::jsonb) ' + | |
214 | 'WHERE "name" = :pluginName AND "type" = :pluginType' | |
215 | ||
216 | const jsonPath = '{' + key + '}' | |
217 | ||
218 | const options = { | |
219 | replacements: { pluginName, pluginType, key: jsonPath, data: JSON.stringify(data) }, | |
220 | type: QueryTypes.UPDATE | |
221 | } | |
222 | ||
223 | return PluginModel.sequelize.query(query, options) | |
224 | .then(() => undefined) | |
225 | } | |
226 | ||
227 | static listForApi (options: { | |
228 | pluginType?: PluginType | |
229 | uninstalled?: boolean | |
230 | start: number | |
231 | count: number | |
232 | sort: string | |
233 | }) { | |
234 | const { uninstalled = false } = options | |
235 | const query: FindAndCountOptions = { | |
236 | offset: options.start, | |
237 | limit: options.count, | |
238 | order: getSort(options.sort), | |
239 | where: { | |
240 | uninstalled | |
241 | } | |
242 | } | |
243 | ||
244 | if (options.pluginType) query.where['type'] = options.pluginType | |
245 | ||
246 | return PluginModel | |
247 | .findAndCountAll<MPlugin>(query) | |
248 | .then(({ rows, count }) => { | |
249 | return { total: count, data: rows } | |
250 | }) | |
251 | } | |
252 | ||
253 | static listInstalled (): Promise<MPlugin[]> { | |
254 | const query = { | |
255 | where: { | |
256 | uninstalled: false | |
257 | } | |
258 | } | |
259 | ||
260 | return PluginModel.findAll(query) | |
261 | } | |
262 | ||
263 | static normalizePluginName (npmName: string) { | |
264 | return npmName.replace(/^peertube-((theme)|(plugin))-/, '') | |
265 | } | |
266 | ||
267 | static getTypeFromNpmName (npmName: string) { | |
268 | return npmName.startsWith('peertube-plugin-') | |
269 | ? PluginType.PLUGIN | |
270 | : PluginType.THEME | |
271 | } | |
272 | ||
273 | static buildNpmName (name: string, type: PluginType) { | |
274 | if (type === PluginType.THEME) return 'peertube-theme-' + name | |
275 | ||
276 | return 'peertube-plugin-' + name | |
277 | } | |
278 | ||
279 | getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) { | |
280 | const result: { [ name: string ]: string } = {} | |
281 | const settings = this.settings || {} | |
282 | ||
283 | for (const r of registeredSettings) { | |
284 | if (r.private !== false) continue | |
285 | ||
286 | result[r.name] = settings[r.name] || r.default || null | |
287 | } | |
288 | ||
289 | return result | |
290 | } | |
291 | ||
292 | toFormattedJSON (this: MPluginFormattable): PeerTubePlugin { | |
293 | return { | |
294 | name: this.name, | |
295 | type: this.type, | |
296 | version: this.version, | |
297 | latestVersion: this.latestVersion, | |
298 | enabled: this.enabled, | |
299 | uninstalled: this.uninstalled, | |
300 | peertubeEngine: this.peertubeEngine, | |
301 | description: this.description, | |
302 | homepage: this.homepage, | |
303 | settings: this.settings, | |
304 | createdAt: this.createdAt, | |
305 | updatedAt: this.updatedAt | |
306 | } | |
307 | } | |
308 | ||
309 | } |