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 getSettings (pluginName: string, pluginType: PluginType, settingNames: string[]) {
134 attributes: [ 'settings' ],
141 return PluginModel.findOne(query)
143 if (!p || !p.settings) return {}
145 const result: { [settingName: string ]: string } = {}
147 for (const key of Object.keys(p.settings)) {
148 if (settingNames.includes(key)) {
149 result[key] = p.settings[key]
157 static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) {
166 [`settings.${settingName}`]: settingValue
169 return PluginModel.update(toSave, query)
170 .then(() => undefined)
173 static getData (pluginName: string, pluginType: PluginType, key: string) {
176 attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings
183 return PluginModel.findOne(query)
185 if (!c) return undefined
186 const value = c.value
188 if (typeof value === 'string' && value.startsWith('{')) {
190 return JSON.parse(value)
200 static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
209 [`storage.${key}`]: data
212 return PluginModel.update(toSave, query)
213 .then(() => undefined)
216 static listForApi (options: {
217 pluginType?: PluginType
218 uninstalled?: boolean
223 const { uninstalled = false } = options
224 const query: FindAndCountOptions = {
225 offset: options.start,
226 limit: options.count,
227 order: getSort(options.sort),
233 if (options.pluginType) query.where['type'] = options.pluginType
236 .findAndCountAll<MPlugin>(query)
237 .then(({ rows, count }) => {
238 return { total: count, data: rows }
242 static listInstalled (): Bluebird<MPlugin[]> {
249 return PluginModel.findAll(query)
252 static normalizePluginName (npmName: string) {
253 return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
256 static getTypeFromNpmName (npmName: string) {
257 return npmName.startsWith('peertube-plugin-')
262 static buildNpmName (name: string, type: PluginType) {
263 if (type === PluginType.THEME) return 'peertube-theme-' + name
265 return 'peertube-plugin-' + name
268 getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) {
269 const result: { [ name: string ]: string } = {}
270 const settings = this.settings || {}
272 for (const r of registeredSettings) {
273 if (r.private !== false) continue
275 result[r.name] = settings[r.name] || r.default || null
281 toFormattedJSON (this: MPluginFormattable): PeerTubePlugin {
285 version: this.version,
286 latestVersion: this.latestVersion,
287 enabled: this.enabled,
288 uninstalled: this.uninstalled,
289 peertubeEngine: this.peertubeEngine,
290 description: this.description,
291 homepage: this.homepage,
292 settings: this.settings,
293 createdAt: this.createdAt,
294 updatedAt: this.updatedAt