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 { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model'
14 import * as Bluebird from 'bluebird'
15 import { MPlugin, MPluginFormattable } from '@server/typings/models'
17 @DefaultScope(() => ({
19 exclude: [ 'storage' ]
27 fields: [ 'name', 'type' ],
32 export class PluginModel extends Model<PluginModel> {
35 @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name'))
40 @Is('PluginType', value => throwIfNotValid(value, isPluginTypeValid, 'type'))
45 @Is('PluginVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
50 @Is('PluginLatestVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
64 peertubeEngine: string
67 @Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description'))
72 @Is('PluginHomepage', value => throwIfNotValid(value, isPluginHomepage, 'homepage'))
77 @Column(DataType.JSONB)
81 @Column(DataType.JSONB)
90 static listEnabledPluginsAndThemes (): Bluebird<MPlugin[]> {
98 return PluginModel.findAll(query)
101 static loadByNpmName (npmName: string): Bluebird<MPlugin> {
102 const name = this.normalizePluginName(npmName)
103 const type = this.getTypeFromNpmName(npmName)
112 return PluginModel.findOne(query)
115 static getSetting (pluginName: string, pluginType: PluginType, settingName: string) {
117 attributes: [ 'settings' ],
124 return PluginModel.findOne(query)
126 if (!p || !p.settings) return undefined
128 return p.settings[settingName]
132 static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) {
141 [`settings.${settingName}`]: settingValue
144 return PluginModel.update(toSave, query)
145 .then(() => undefined)
148 static getData (pluginName: string, pluginType: PluginType, key: string) {
151 attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings
158 return PluginModel.findOne(query)
160 if (!c) return undefined
161 const value = c.value
163 if (typeof value === 'string' && value.startsWith('{')) {
165 return JSON.parse(value)
175 static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
184 [`storage.${key}`]: data
187 return PluginModel.update(toSave, query)
188 .then(() => undefined)
191 static listForApi (options: {
192 pluginType?: PluginType
193 uninstalled?: boolean
198 const { uninstalled = false } = options
199 const query: FindAndCountOptions = {
200 offset: options.start,
201 limit: options.count,
202 order: getSort(options.sort),
208 if (options.pluginType) query.where['type'] = options.pluginType
211 .findAndCountAll<MPlugin>(query)
212 .then(({ rows, count }) => {
213 return { total: count, data: rows }
217 static listInstalled (): Bluebird<MPlugin[]> {
224 return PluginModel.findAll(query)
227 static normalizePluginName (npmName: string) {
228 return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
231 static getTypeFromNpmName (npmName: string) {
232 return npmName.startsWith('peertube-plugin-')
237 static buildNpmName (name: string, type: PluginType) {
238 if (type === PluginType.THEME) return 'peertube-theme-' + name
240 return 'peertube-plugin-' + name
243 getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) {
244 const result: { [ name: string ]: string } = {}
245 const settings = this.settings || {}
247 for (const r of registeredSettings) {
248 if (r.private !== false) continue
250 result[r.name] = settings[r.name] || r.default || null
256 toFormattedJSON (this: MPluginFormattable): PeerTubePlugin {
260 version: this.version,
261 latestVersion: this.latestVersion,
262 enabled: this.enabled,
263 uninstalled: this.uninstalled,
264 peertubeEngine: this.peertubeEngine,
265 description: this.description,
266 homepage: this.homepage,
267 settings: this.settings,
268 createdAt: this.createdAt,
269 updatedAt: this.updatedAt