1 import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { getSort, throwIfNotValid } from '../utils'
4 isPluginDescriptionValid,
9 } from '../../helpers/custom-validators/plugins'
10 import { PluginType } from '../../../shared/models/plugins/plugin.type'
11 import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
12 import { FindAndCountOptions, json } from 'sequelize'
13 import { PeerTubePluginIndex } from '../../../shared/models/plugins/peertube-plugin-index.model'
15 @DefaultScope(() => ({
17 exclude: [ 'storage' ]
25 fields: [ 'name', 'type' ],
30 export class PluginModel extends Model<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 () {
96 return PluginModel.findAll(query)
99 static loadByNpmName (npmName: string) {
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) {
115 attributes: [ 'settings' ],
122 return PluginModel.findOne(query)
124 if (!p || !p.settings) return undefined
126 return p.settings[settingName]
130 static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) {
139 [`settings.${settingName}`]: settingValue
142 return PluginModel.update(toSave, query)
143 .then(() => undefined)
146 static getData (pluginName: string, pluginType: PluginType, key: string) {
149 attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings
156 return PluginModel.findOne(query)
158 if (!c) return undefined
164 static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
173 [`storage.${key}`]: data
176 return PluginModel.update(toSave, query)
177 .then(() => undefined)
180 static listForApi (options: {
181 pluginType?: PluginType,
182 uninstalled?: boolean,
187 const { uninstalled = false } = options
188 const query: FindAndCountOptions = {
189 offset: options.start,
190 limit: options.count,
191 order: getSort(options.sort),
197 if (options.pluginType) query.where['type'] = options.pluginType
200 .findAndCountAll(query)
201 .then(({ rows, count }) => {
202 return { total: count, data: rows }
206 static listInstalled () {
213 return PluginModel.findAll(query)
216 static normalizePluginName (npmName: string) {
217 return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
220 static getTypeFromNpmName (npmName: string) {
221 return npmName.startsWith('peertube-plugin-')
226 static buildNpmName (name: string, type: PluginType) {
227 if (type === PluginType.THEME) return 'peertube-theme-' + name
229 return 'peertube-plugin-' + name
232 toFormattedJSON (): PeerTubePlugin {
236 version: this.version,
237 latestVersion: this.latestVersion,
238 enabled: this.enabled,
239 uninstalled: this.uninstalled,
240 peertubeEngine: this.peertubeEngine,
241 description: this.description,
242 homepage: this.homepage,
243 settings: this.settings,
244 createdAt: this.createdAt,
245 updatedAt: this.updatedAt