1 import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { getSort, throwIfNotValid } from '../utils'
4 isPluginDescriptionValid,
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' ]
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 @Column(DataType.JSONB)
67 @Column(DataType.JSONB)
76 static listEnabledPluginsAndThemes () {
84 return PluginModel.findAll(query)
87 static load (pluginName: string) {
94 return PluginModel.findOne(query)
97 static getSetting (pluginName: string, settingName: string) {
99 attributes: [ 'settings' ],
105 return PluginModel.findOne(query)
106 .then(p => p.settings)
108 if (!settings) return undefined
110 return settings[settingName]
114 static setSetting (pluginName: string, settingName: string, settingValue: string) {
122 [`settings.${settingName}`]: settingValue
125 return PluginModel.update(toSave, query)
126 .then(() => undefined)
129 static listForApi (options: {
131 uninstalled?: boolean,
136 const query: FindAndCountOptions = {
137 offset: options.start,
138 limit: options.count,
139 order: getSort(options.sort),
143 if (options.type) query.where['type'] = options.type
144 if (options.uninstalled) query.where['uninstalled'] = options.uninstalled
147 .findAndCountAll(query)
148 .then(({ rows, count }) => {
149 return { total: count, data: rows }
153 toFormattedJSON (): PeerTubePlugin {
157 version: this.version,
158 enabled: this.enabled,
159 uninstalled: this.uninstalled,
160 peertubeEngine: this.peertubeEngine,
161 description: this.description,
162 settings: this.settings,
163 createdAt: this.createdAt,
164 updatedAt: this.updatedAt