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'
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: SettingEntries = {}
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: SettingValue) {
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 PluginModel.count(query),
244 PluginModel.findAll<MPlugin>(query)
245 ]).then(([ total, data ]) => ({ total, data }))
248 static listInstalled (): Promise<MPlugin[]> {
255 return PluginModel.findAll(query)
258 static normalizePluginName (npmName: string) {
259 return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
262 static getTypeFromNpmName (npmName: string) {
263 return npmName.startsWith('peertube-plugin-')
268 static buildNpmName (name: string, type: PluginType) {
269 if (type === PluginType.THEME) return 'peertube-theme-' + name
271 return 'peertube-plugin-' + name
274 getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) {
275 const result: SettingEntries = {}
276 const settings = this.settings || {}
278 for (const r of registeredSettings) {
279 if (r.private !== false) continue
281 result[r.name] = settings[r.name] ?? r.default ?? null
287 toFormattedJSON (this: MPluginFormattable): PeerTubePlugin {
291 version: this.version,
292 latestVersion: this.latestVersion,
293 enabled: this.enabled,
294 uninstalled: this.uninstalled,
295 peertubeEngine: this.peertubeEngine,
296 description: this.description,
297 homepage: this.homepage,
298 settings: this.settings,
299 createdAt: this.createdAt,
300 updatedAt: this.updatedAt