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/core-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
200 if (typeof value === 'string' && value.startsWith('{')) {
202 return JSON.parse(value)
212 static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
213 const query = 'UPDATE "plugin" SET "storage" = jsonb_set(coalesce("storage", \'{}\'), :key, :data::jsonb) ' +
214 'WHERE "name" = :pluginName AND "type" = :pluginType'
216 const jsonPath = '{' + key + '}'
219 replacements: { pluginName, pluginType, key: jsonPath, data: JSON.stringify(data) },
220 type: QueryTypes.UPDATE
223 return PluginModel.sequelize.query(query, options)
224 .then(() => undefined)
227 static listForApi (options: {
228 pluginType?: PluginType
229 uninstalled?: boolean
234 const { uninstalled = false } = options
235 const query: FindAndCountOptions = {
236 offset: options.start,
237 limit: options.count,
238 order: getSort(options.sort),
244 if (options.pluginType) query.where['type'] = options.pluginType
247 .findAndCountAll<MPlugin>(query)
248 .then(({ rows, count }) => {
249 return { total: count, data: rows }
253 static listInstalled (): Promise<MPlugin[]> {
260 return PluginModel.findAll(query)
263 static normalizePluginName (npmName: string) {
264 return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
267 static getTypeFromNpmName (npmName: string) {
268 return npmName.startsWith('peertube-plugin-')
273 static buildNpmName (name: string, type: PluginType) {
274 if (type === PluginType.THEME) return 'peertube-theme-' + name
276 return 'peertube-plugin-' + name
279 getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) {
280 const result: { [ name: string ]: string } = {}
281 const settings = this.settings || {}
283 for (const r of registeredSettings) {
284 if (r.private !== false) continue
286 result[r.name] = settings[r.name] || r.default || null
292 toFormattedJSON (this: MPluginFormattable): PeerTubePlugin {
296 version: this.version,
297 latestVersion: this.latestVersion,
298 enabled: this.enabled,
299 uninstalled: this.uninstalled,
300 peertubeEngine: this.peertubeEngine,
301 description: this.description,
302 homepage: this.homepage,
303 settings: this.settings,
304 createdAt: this.createdAt,
305 updatedAt: this.updatedAt