]>
Commit | Line | Data |
---|---|---|
97b65ce5 | 1 | import { FindAndCountOptions, json, QueryTypes } from 'sequelize' |
ad91e700 | 2 | import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
26d6bf65 | 3 | import { MPlugin, MPluginFormattable } from '@server/types/models' |
16c016e8 | 4 | import { AttributesOnly } from '@shared/core-utils' |
428ccb8b | 5 | import { PeerTubePlugin, PluginType, RegisterServerSettingOptions } from '../../../shared/models' |
345da516 | 6 | import { |
b5f919ac C |
7 | isPluginDescriptionValid, |
8 | isPluginHomepage, | |
345da516 C |
9 | isPluginNameValid, |
10 | isPluginTypeValid, | |
11 | isPluginVersionValid | |
12 | } from '../../helpers/custom-validators/plugins' | |
bc90883f | 13 | import { getSort, throwIfNotValid } from '../utils' |
ad91e700 C |
14 | |
15 | @DefaultScope(() => ({ | |
16 | attributes: { | |
17 | exclude: [ 'storage' ] | |
18 | } | |
19 | })) | |
345da516 C |
20 | |
21 | @Table({ | |
22 | tableName: 'plugin', | |
23 | indexes: [ | |
24 | { | |
dba85a1e | 25 | fields: [ 'name', 'type' ], |
345da516 C |
26 | unique: true |
27 | } | |
28 | ] | |
29 | }) | |
16c016e8 | 30 | export class PluginModel extends Model<Partial<AttributesOnly<PluginModel>>> { |
345da516 C |
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 | ||
b5f919ac C |
47 | @AllowNull(true) |
48 | @Is('PluginLatestVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version')) | |
49 | @Column | |
50 | latestVersion: string | |
51 | ||
345da516 C |
52 | @AllowNull(false) |
53 | @Column | |
54 | enabled: boolean | |
55 | ||
56 | @AllowNull(false) | |
57 | @Column | |
58 | uninstalled: boolean | |
59 | ||
60 | @AllowNull(false) | |
345da516 C |
61 | @Column |
62 | peertubeEngine: string | |
63 | ||
64 | @AllowNull(true) | |
65 | @Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description')) | |
66 | @Column | |
67 | description: string | |
68 | ||
dba85a1e C |
69 | @AllowNull(false) |
70 | @Is('PluginHomepage', value => throwIfNotValid(value, isPluginHomepage, 'homepage')) | |
71 | @Column | |
72 | homepage: string | |
73 | ||
345da516 C |
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 | ||
b49f22d8 | 88 | static listEnabledPluginsAndThemes (): Promise<MPlugin[]> { |
345da516 C |
89 | const query = { |
90 | where: { | |
91 | enabled: true, | |
92 | uninstalled: false | |
93 | } | |
94 | } | |
95 | ||
96 | return PluginModel.findAll(query) | |
97 | } | |
98 | ||
b49f22d8 | 99 | static loadByNpmName (npmName: string): Promise<MPlugin> { |
dba85a1e C |
100 | const name = this.normalizePluginName(npmName) |
101 | const type = this.getTypeFromNpmName(npmName) | |
102 | ||
2c053942 C |
103 | const query = { |
104 | where: { | |
dba85a1e C |
105 | name, |
106 | type | |
2c053942 C |
107 | } |
108 | } | |
109 | ||
110 | return PluginModel.findOne(query) | |
111 | } | |
112 | ||
bc90883f | 113 | static getSetting (pluginName: string, pluginType: PluginType, settingName: string, registeredSettings: RegisterServerSettingOptions[]) { |
ad91e700 C |
114 | const query = { |
115 | attributes: [ 'settings' ], | |
116 | where: { | |
b5f919ac C |
117 | name: pluginName, |
118 | type: pluginType | |
ad91e700 C |
119 | } |
120 | } | |
121 | ||
122 | return PluginModel.findOne(query) | |
b5f919ac | 123 | .then(p => { |
15b4bcdf | 124 | if (!p || !p.settings || p.settings === undefined) { |
bc90883f C |
125 | const registered = registeredSettings.find(s => s.name === settingName) |
126 | if (!registered || registered.default === undefined) return undefined | |
127 | ||
128 | return registered.default | |
129 | } | |
ad91e700 | 130 | |
b5f919ac | 131 | return p.settings[settingName] |
ad91e700 C |
132 | }) |
133 | } | |
134 | ||
bc90883f C |
135 | static getSettings ( |
136 | pluginName: string, | |
137 | pluginType: PluginType, | |
138 | settingNames: string[], | |
139 | registeredSettings: RegisterServerSettingOptions[] | |
140 | ) { | |
055cfb11 C |
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 => { | |
bc90883f | 151 | const result: { [settingName: string ]: string | boolean } = {} |
055cfb11 | 152 | |
bc90883f | 153 | for (const name of settingNames) { |
15b4bcdf | 154 | if (!p || !p.settings || p.settings[name] === undefined) { |
bc90883f | 155 | const registered = registeredSettings.find(s => s.name === name) |
055cfb11 | 156 | |
bc90883f C |
157 | if (registered?.default !== undefined) { |
158 | result[name] = registered.default | |
159 | } | |
160 | } else { | |
161 | result[name] = p.settings[name] | |
055cfb11 C |
162 | } |
163 | } | |
164 | ||
165 | return result | |
166 | }) | |
167 | } | |
168 | ||
b5f919ac | 169 | static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) { |
f023a19c C |
170 | const query = { |
171 | where: { | |
b5f919ac C |
172 | name: pluginName, |
173 | type: pluginType | |
f023a19c C |
174 | } |
175 | } | |
176 | ||
ad91e700 C |
177 | const toSave = { |
178 | [`settings.${settingName}`]: settingValue | |
179 | } | |
180 | ||
181 | return PluginModel.update(toSave, query) | |
182 | .then(() => undefined) | |
183 | } | |
184 | ||
b2195faf C |
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) | |
109d893f | 196 | .then((c: any) => { |
b2195faf | 197 | if (!c) return undefined |
9fa6ca16 C |
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 | } | |
b2195faf C |
207 | |
208 | return c.value | |
209 | }) | |
210 | } | |
211 | ||
212 | static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) { | |
97b65ce5 C |
213 | const query = 'UPDATE "plugin" SET "storage" = jsonb_set(coalesce("storage", \'{}\'), :key, :data::jsonb) ' + |
214 | 'WHERE "name" = :pluginName AND "type" = :pluginType' | |
b2195faf | 215 | |
97b65ce5 C |
216 | const jsonPath = '{' + key + '}' |
217 | ||
218 | const options = { | |
219 | replacements: { pluginName, pluginType, key: jsonPath, data: JSON.stringify(data) }, | |
220 | type: QueryTypes.UPDATE | |
b2195faf C |
221 | } |
222 | ||
97b65ce5 | 223 | return PluginModel.sequelize.query(query, options) |
b2195faf C |
224 | .then(() => undefined) |
225 | } | |
226 | ||
ad91e700 | 227 | static listForApi (options: { |
a1587156 C |
228 | pluginType?: PluginType |
229 | uninstalled?: boolean | |
230 | start: number | |
231 | count: number | |
ad91e700 C |
232 | sort: string |
233 | }) { | |
8d2be0ed | 234 | const { uninstalled = false } = options |
ad91e700 C |
235 | const query: FindAndCountOptions = { |
236 | offset: options.start, | |
237 | limit: options.count, | |
238 | order: getSort(options.sort), | |
8d2be0ed C |
239 | where: { |
240 | uninstalled | |
241 | } | |
ad91e700 C |
242 | } |
243 | ||
6702a1b2 | 244 | if (options.pluginType) query.where['type'] = options.pluginType |
ad91e700 C |
245 | |
246 | return PluginModel | |
453e83ea | 247 | .findAndCountAll<MPlugin>(query) |
ad91e700 C |
248 | .then(({ rows, count }) => { |
249 | return { total: count, data: rows } | |
250 | }) | |
251 | } | |
252 | ||
b49f22d8 | 253 | static listInstalled (): Promise<MPlugin[]> { |
6702a1b2 C |
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))-/, '') | |
dba85a1e C |
265 | } |
266 | ||
267 | static getTypeFromNpmName (npmName: string) { | |
268 | return npmName.startsWith('peertube-plugin-') | |
269 | ? PluginType.PLUGIN | |
270 | : PluginType.THEME | |
271 | } | |
272 | ||
b5f919ac C |
273 | static buildNpmName (name: string, type: PluginType) { |
274 | if (type === PluginType.THEME) return 'peertube-theme-' + name | |
275 | ||
276 | return 'peertube-plugin-' + name | |
277 | } | |
278 | ||
ba211e73 C |
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 | ||
fa263eef | 286 | result[r.name] = settings[r.name] ?? r.default ?? null |
ba211e73 C |
287 | } |
288 | ||
289 | return result | |
290 | } | |
291 | ||
1ca9f7c3 | 292 | toFormattedJSON (this: MPluginFormattable): PeerTubePlugin { |
ad91e700 C |
293 | return { |
294 | name: this.name, | |
295 | type: this.type, | |
296 | version: this.version, | |
b5f919ac | 297 | latestVersion: this.latestVersion, |
ad91e700 C |
298 | enabled: this.enabled, |
299 | uninstalled: this.uninstalled, | |
300 | peertubeEngine: this.peertubeEngine, | |
301 | description: this.description, | |
dba85a1e | 302 | homepage: this.homepage, |
ad91e700 C |
303 | settings: this.settings, |
304 | createdAt: this.createdAt, | |
305 | updatedAt: this.updatedAt | |
306 | } | |
f023a19c C |
307 | } |
308 | ||
345da516 | 309 | } |