1 import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { getSort, throwIfNotValid } from '../utils'
4 isPluginDescriptionValid, isPluginHomepage,
8 } from '../../helpers/custom-validators/plugins'
9 import { PluginType } from '../../../shared/models/plugins/plugin.type'
10 import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
11 import { FindAndCountOptions } from 'sequelize'
13 @DefaultScope(() => ({
15 exclude: [ 'storage' ]
23 fields: [ 'name', 'type' ],
28 export class PluginModel extends Model<PluginModel> {
31 @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name'))
36 @Is('PluginType', value => throwIfNotValid(value, isPluginTypeValid, 'type'))
41 @Is('PluginVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
55 peertubeEngine: string
58 @Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description'))
63 @Is('PluginHomepage', value => throwIfNotValid(value, isPluginHomepage, 'homepage'))
68 @Column(DataType.JSONB)
72 @Column(DataType.JSONB)
81 static listEnabledPluginsAndThemes () {
89 return PluginModel.findAll(query)
92 static loadByNpmName (npmName: string) {
93 const name = this.normalizePluginName(npmName)
94 const type = this.getTypeFromNpmName(npmName)
103 return PluginModel.findOne(query)
106 static getSetting (pluginName: string, settingName: string) {
108 attributes: [ 'settings' ],
114 return PluginModel.findOne(query)
115 .then(p => p.settings)
117 if (!settings) return undefined
119 return settings[settingName]
123 static setSetting (pluginName: string, settingName: string, settingValue: string) {
131 [`settings.${settingName}`]: settingValue
134 return PluginModel.update(toSave, query)
135 .then(() => undefined)
138 static listForApi (options: {
140 uninstalled?: boolean,
145 const query: FindAndCountOptions = {
146 offset: options.start,
147 limit: options.count,
148 order: getSort(options.sort),
152 if (options.type) query.where['type'] = options.type
153 if (options.uninstalled) query.where['uninstalled'] = options.uninstalled
156 .findAndCountAll(query)
157 .then(({ rows, count }) => {
158 return { total: count, data: rows }
162 static normalizePluginName (name: string) {
163 return name.replace(/^peertube-((theme)|(plugin))-/, '')
166 static getTypeFromNpmName (npmName: string) {
167 return npmName.startsWith('peertube-plugin-')
172 toFormattedJSON (): PeerTubePlugin {
176 version: this.version,
177 enabled: this.enabled,
178 uninstalled: this.uninstalled,
179 peertubeEngine: this.peertubeEngine,
180 description: this.description,
181 homepage: this.homepage,
182 settings: this.settings,
183 createdAt: this.createdAt,
184 updatedAt: this.updatedAt