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 { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
5 import { PluginType } from '../../../shared/models/plugins/plugin.type'
6 import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model'
8 isPluginDescriptionValid,
13 } from '../../helpers/custom-validators/plugins'
14 import { getSort, throwIfNotValid } from '../utils'
16 @DefaultScope(() => ({
18 exclude: [ 'storage' ]
26 fields: [ 'name', 'type' ],
31 export class PluginModel extends Model {
34 @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name'))
39 @Is('PluginType', value => throwIfNotValid(value, isPluginTypeValid, 'type'))
44 @Is('PluginVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
49 @Is('PluginLatestVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
63 peertubeEngine: string
66 @Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description'))
71 @Is('PluginHomepage', value => throwIfNotValid(value, isPluginHomepage, 'homepage'))
76 @Column(DataType.JSONB)
80 @Column(DataType.JSONB)
89 static listEnabledPluginsAndThemes (): Promise<MPlugin[]> {
97 return PluginModel.findAll(query)
100 static loadByNpmName (npmName: string): Promise<MPlugin> {
101 const name = this.normalizePluginName(npmName)
102 const type = this.getTypeFromNpmName(npmName)
111 return PluginModel.findOne(query)
114 static getSetting (pluginName: string, pluginType: PluginType, settingName: string, registeredSettings: RegisterServerSettingOptions[]) {
116 attributes: [ 'settings' ],
123 return PluginModel.findOne(query)
125 if (!p || !p.settings || p.settings === undefined) {
126 const registered = registeredSettings.find(s => s.name === settingName)
127 if (!registered || registered.default === undefined) return undefined
129 return registered.default
132 return p.settings[settingName]
138 pluginType: PluginType,
139 settingNames: string[],
140 registeredSettings: RegisterServerSettingOptions[]
143 attributes: [ 'settings' ],
150 return PluginModel.findOne(query)
152 const result: { [settingName: string ]: string | boolean } = {}
154 for (const name of settingNames) {
155 if (!p || !p.settings || p.settings[name] === undefined) {
156 const registered = registeredSettings.find(s => s.name === name)
158 if (registered?.default !== undefined) {
159 result[name] = registered.default
162 result[name] = p.settings[name]
170 static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) {
179 [`settings.${settingName}`]: settingValue
182 return PluginModel.update(toSave, query)
183 .then(() => undefined)
186 static getData (pluginName: string, pluginType: PluginType, key: string) {
189 attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings
196 return PluginModel.findOne(query)
198 if (!c) return undefined
199 const value = c.value
201 if (typeof value === 'string' && value.startsWith('{')) {
203 return JSON.parse(value)
213 static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
214 const query = 'UPDATE "plugin" SET "storage" = jsonb_set(coalesce("storage", \'{}\'), :key, :data::jsonb) ' +
215 'WHERE "name" = :pluginName AND "type" = :pluginType'
217 const jsonPath = '{' + key + '}'
220 replacements: { pluginName, pluginType, key: jsonPath, data: JSON.stringify(data) },
221 type: QueryTypes.UPDATE
224 return PluginModel.sequelize.query(query, options)
225 .then(() => undefined)
228 static listForApi (options: {
229 pluginType?: PluginType
230 uninstalled?: boolean
235 const { uninstalled = false } = options
236 const query: FindAndCountOptions = {
237 offset: options.start,
238 limit: options.count,
239 order: getSort(options.sort),
245 if (options.pluginType) query.where['type'] = options.pluginType
248 .findAndCountAll<MPlugin>(query)
249 .then(({ rows, count }) => {
250 return { total: count, data: rows }
254 static listInstalled (): Promise<MPlugin[]> {
261 return PluginModel.findAll(query)
264 static normalizePluginName (npmName: string) {
265 return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
268 static getTypeFromNpmName (npmName: string) {
269 return npmName.startsWith('peertube-plugin-')
274 static buildNpmName (name: string, type: PluginType) {
275 if (type === PluginType.THEME) return 'peertube-theme-' + name
277 return 'peertube-plugin-' + name
280 getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) {
281 const result: { [ name: string ]: string } = {}
282 const settings = this.settings || {}
284 for (const r of registeredSettings) {
285 if (r.private !== false) continue
287 result[r.name] = settings[r.name] || r.default || null
293 toFormattedJSON (this: MPluginFormattable): PeerTubePlugin {
297 version: this.version,
298 latestVersion: this.latestVersion,
299 enabled: this.enabled,
300 uninstalled: this.uninstalled,
301 peertubeEngine: this.peertubeEngine,
302 description: this.description,
303 homepage: this.homepage,
304 settings: this.settings,
305 createdAt: this.createdAt,
306 updatedAt: this.updatedAt