From b2195fafc292d6761c25fe51ca4e0328ab403424 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 12 Jul 2019 14:06:33 +0200 Subject: WIP plugins: add storage manager --- server/lib/plugins/plugin-manager.ts | 14 +++++++++++++- server/models/server/plugin.ts | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) (limited to 'server') diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index 2fa80e878..7576b284c 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts @@ -13,6 +13,7 @@ import { outputFile } from 'fs-extra' import { RegisterSettingOptions } from '../../../shared/models/plugins/register-setting.model' import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' +import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' export interface RegisteredPlugin { npmName: string @@ -307,13 +308,24 @@ export class PluginManager { setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value) } + const storageManager: PluginStorageManager = { + getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key), + + storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data) + } + const library: PluginLibrary = require(join(pluginPath, packageJSON.library)) if (!isLibraryCodeValid(library)) { throw new Error('Library code is not valid (miss register or unregister function)') } - library.register({ registerHook, registerSetting, settingsManager }) + library.register({ + registerHook, + registerSetting, + settingsManager, + storageManager + }) logger.info('Add plugin %s CSS to global file.', npmName) diff --git a/server/models/server/plugin.ts b/server/models/server/plugin.ts index 340d49f3b..bd3d7a81e 100644 --- a/server/models/server/plugin.ts +++ b/server/models/server/plugin.ts @@ -9,7 +9,7 @@ import { } 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' +import { FindAndCountOptions, json } from 'sequelize' @DefaultScope(() => ({ attributes: { @@ -142,6 +142,40 @@ export class PluginModel extends Model { .then(() => undefined) } + static getData (pluginName: string, pluginType: PluginType, key: string) { + const query = { + raw: true, + attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings + where: { + name: pluginName, + type: pluginType + } + } + + return PluginModel.findOne(query) + .then((c: any) => { + if (!c) return undefined + + return c.value + }) + } + + static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) { + const query = { + where: { + name: pluginName, + type: pluginType + } + } + + const toSave = { + [`storage.${key}`]: data + } + + return PluginModel.update(toSave, query) + .then(() => undefined) + } + static listForApi (options: { type?: PluginType, uninstalled?: boolean, -- cgit v1.2.3