From ad91e7006e41f8ee5b8dcefee30f99e8ca44133a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 10 Jul 2019 16:59:53 +0200 Subject: WIP plugins: plugin settings on server side --- server/models/server/plugin.ts | 78 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 4 deletions(-) (limited to 'server/models') diff --git a/server/models/server/plugin.ts b/server/models/server/plugin.ts index b3b8276df..059a442de 100644 --- a/server/models/server/plugin.ts +++ b/server/models/server/plugin.ts @@ -1,11 +1,20 @@ -import { AllowNull, Column, CreatedAt, DataType, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' -import { throwIfNotValid } from '../utils' +import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' +import { getSort, throwIfNotValid } from '../utils' import { isPluginDescriptionValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' +import { PluginType } from '../../../shared/models/plugins/plugin.type' +import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model' +import { FindAndCountOptions } from 'sequelize' + +@DefaultScope(() => ({ + attributes: { + exclude: [ 'storage' ] + } +})) @Table({ tableName: 'plugin', @@ -85,14 +94,75 @@ export class PluginModel extends Model { return PluginModel.findOne(query) } - static uninstall (pluginName: string) { + static getSetting (pluginName: string, settingName: string) { + const query = { + attributes: [ 'settings' ], + where: { + name: pluginName + } + } + + return PluginModel.findOne(query) + .then(p => p.settings) + .then(settings => { + if (!settings) return undefined + + return settings[settingName] + }) + } + + static setSetting (pluginName: string, settingName: string, settingValue: string) { const query = { where: { name: pluginName } } - return PluginModel.update({ enabled: false, uninstalled: true }, query) + const toSave = { + [`settings.${settingName}`]: settingValue + } + + return PluginModel.update(toSave, query) + .then(() => undefined) + } + + static listForApi (options: { + type?: PluginType, + uninstalled?: boolean, + start: number, + count: number, + sort: string + }) { + const query: FindAndCountOptions = { + offset: options.start, + limit: options.count, + order: getSort(options.sort), + where: {} + } + + if (options.type) query.where['type'] = options.type + if (options.uninstalled) query.where['uninstalled'] = options.uninstalled + + return PluginModel + .findAndCountAll(query) + .then(({ rows, count }) => { + return { total: count, data: rows } + }) + } + + toFormattedJSON (): PeerTubePlugin { + return { + name: this.name, + type: this.type, + version: this.version, + enabled: this.enabled, + uninstalled: this.uninstalled, + peertubeEngine: this.peertubeEngine, + description: this.description, + settings: this.settings, + createdAt: this.createdAt, + updatedAt: this.updatedAt + } } } -- cgit v1.2.3