From: Chocobozzz Date: Fri, 12 Jul 2019 12:06:33 +0000 (+0200) Subject: WIP plugins: add storage manager X-Git-Tag: v1.4.0-rc.1~114 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=b2195fafc292d6761c25fe51ca4e0328ab403424;p=oweals%2Fpeertube.git WIP plugins: add storage manager --- 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, diff --git a/shared/models/plugins/plugin-storage-manager.model.ts b/shared/models/plugins/plugin-storage-manager.model.ts new file mode 100644 index 000000000..8a1f389f1 --- /dev/null +++ b/shared/models/plugins/plugin-storage-manager.model.ts @@ -0,0 +1,7 @@ +import * as Bluebird from 'bluebird' + +export interface PluginStorageManager { + getData: (key: string) => Bluebird + + storeData: (key: string, data: any) => Bluebird +} diff --git a/shared/models/plugins/register-options.model.ts b/shared/models/plugins/register-options.model.ts index e60ce3fe0..e3a7cff08 100644 --- a/shared/models/plugins/register-options.model.ts +++ b/shared/models/plugins/register-options.model.ts @@ -1,6 +1,7 @@ import { RegisterHookOptions } from './register-hook.model' import { RegisterSettingOptions } from './register-setting.model' import { PluginSettingsManager } from './plugin-settings-manager.model' +import { PluginStorageManager } from './plugin-storage-manager.model' export type RegisterOptions = { registerHook: (options: RegisterHookOptions) => void @@ -8,4 +9,6 @@ export type RegisterOptions = { registerSetting: (options: RegisterSettingOptions) => void settingsManager: PluginSettingsManager + + storageManager: PluginStorageManager }