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 } from '../../../shared/models'
7 isPluginDescriptionValid,
12 } from '../../helpers/custom-validators/plugins'
13 import { getSort, throwIfNotValid } from '../utils'
15 @DefaultScope(() => ({
17 exclude: [ 'storage' ]
25 fields: [ 'name', 'type' ],
30 export class PluginModel extends Model<Partial<AttributesOnly<PluginModel>>> {
33 @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name'))
38 @Is('PluginType', value => throwIfNotValid(value, isPluginTypeValid, 'type'))
43 @Is('PluginVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
48 @Is('PluginLatestVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
62 peertubeEngine: string
65 @Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description'))
70 @Is('PluginHomepage', value => throwIfNotValid(value, isPluginHomepage, 'homepage'))
75 @Column(DataType.JSONB)
79 @Column(DataType.JSONB)
88 static listEnabledPluginsAndThemes (): Promise<MPlugin[]> {
96 return PluginModel.findAll(query)
99 static loadByNpmName (npmName: string): Promise<MPlugin> {
100 const name = this.normalizePluginName(npmName)
101 const type = this.getTypeFromNpmName(npmName)
110 return PluginModel.findOne(query)
113 static getSetting (pluginName: string, pluginType: PluginType, settingName: string, registeredSettings: RegisterServerSettingOptions[]) {
115 attributes: [ 'settings' ],
122 return PluginModel.findOne(query)
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
128 return registered.default
131 return p.settings[settingName]
137 pluginType: PluginType,
138 settingNames: string[],
139 registeredSettings: RegisterServerSettingOptions[]
142 attributes: [ 'settings' ],
149 return PluginModel.findOne(query)
151 const result: { [settingName: string ]: string | boolean } = {}
153 for (const name of settingNames) {
154 if (!p || !p.settings || p.settings[name] === undefined) {
155 const registered = registeredSettings.find(s => s.name === name)
157 if (registered?.default !== undefined) {
158 result[name] = registered.default
161 result[name] = p.settings[name]
169 static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) {
178 [`settings.${settingName}`]: settingValue
181 return PluginModel.update(toSave, query)
182 .then(() => undefined)
185 static getData (pluginName: string, pluginType: PluginType, key: string) {
188 attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings
195 return PluginModel.findOne(query)
197 if (!c) return undefined
198 const value = c.value
201 return JSON.parse(value)
208 static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
209 const query = 'UPDATE "plugin" SET "storage" = jsonb_set(coalesce("storage", \'{}\'), :key, :data::jsonb) ' +
210 'WHERE "name" = :pluginName AND "type" = :pluginType'
212 const jsonPath = '{' + key + '}'
215 replacements: { pluginName, pluginType, key: jsonPath, data: JSON.stringify(data) },
216 type: QueryTypes.UPDATE
219 return PluginModel.sequelize.query(query, options)
220 .then(() => undefined)
223 static listForApi (options: {
224 pluginType?: PluginType
225 uninstalled?: boolean
230 const { uninstalled = false } = options
231 const query: FindAndCountOptions = {
232 offset: options.start,
233 limit: options.count,
234 order: getSort(options.sort),
240 if (options.pluginType) query.where['type'] = options.pluginType
243 .findAndCountAll<MPlugin>(query)
244 .then(({ rows, count }) => {
245 return { total: count, data: rows }
249 static listInstalled (): Promise<MPlugin[]> {
256 return PluginModel.findAll(query)
259 static normalizePluginName (npmName: string) {
260 return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
263 static getTypeFromNpmName (npmName: string) {
264 return npmName.startsWith('peertube-plugin-')
269 static buildNpmName (name: string, type: PluginType) {
270 if (type === PluginType.THEME) return 'peertube-theme-' + name
272 return 'peertube-plugin-' + name
275 getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) {
276 const result: { [ name: string ]: string } = {}
277 const settings = this.settings || {}
279 for (const r of registeredSettings) {
280 if (r.private !== false) continue
282 result[r.name] = settings[r.name] ?? r.default ?? null
288 toFormattedJSON (this: MPluginFormattable): PeerTubePlugin {
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