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