diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-12 14:06:33 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | b2195fafc292d6761c25fe51ca4e0328ab403424 (patch) | |
tree | 3c390aeb5c5b9648668961871e866bd5e1dde5a4 | |
parent | b5f919ac8eb2a1c20e26582fdfd377d687710d8f (diff) | |
download | PeerTube-b2195fafc292d6761c25fe51ca4e0328ab403424.tar.gz PeerTube-b2195fafc292d6761c25fe51ca4e0328ab403424.tar.zst PeerTube-b2195fafc292d6761c25fe51ca4e0328ab403424.zip |
WIP plugins: add storage manager
-rw-r--r-- | server/lib/plugins/plugin-manager.ts | 14 | ||||
-rw-r--r-- | server/models/server/plugin.ts | 36 | ||||
-rw-r--r-- | shared/models/plugins/plugin-storage-manager.model.ts | 7 | ||||
-rw-r--r-- | shared/models/plugins/register-options.model.ts | 3 |
4 files changed, 58 insertions, 2 deletions
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' | |||
13 | import { RegisterSettingOptions } from '../../../shared/models/plugins/register-setting.model' | 13 | import { RegisterSettingOptions } from '../../../shared/models/plugins/register-setting.model' |
14 | import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' | 14 | import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' |
15 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' | 15 | import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' |
16 | import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model' | ||
16 | 17 | ||
17 | export interface RegisteredPlugin { | 18 | export interface RegisteredPlugin { |
18 | npmName: string | 19 | npmName: string |
@@ -307,13 +308,24 @@ export class PluginManager { | |||
307 | setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value) | 308 | setSetting: (name: string, value: string) => PluginModel.setSetting(plugin.name, plugin.type, name, value) |
308 | } | 309 | } |
309 | 310 | ||
311 | const storageManager: PluginStorageManager = { | ||
312 | getData: (key: string) => PluginModel.getData(plugin.name, plugin.type, key), | ||
313 | |||
314 | storeData: (key: string, data: any) => PluginModel.storeData(plugin.name, plugin.type, key, data) | ||
315 | } | ||
316 | |||
310 | const library: PluginLibrary = require(join(pluginPath, packageJSON.library)) | 317 | const library: PluginLibrary = require(join(pluginPath, packageJSON.library)) |
311 | 318 | ||
312 | if (!isLibraryCodeValid(library)) { | 319 | if (!isLibraryCodeValid(library)) { |
313 | throw new Error('Library code is not valid (miss register or unregister function)') | 320 | throw new Error('Library code is not valid (miss register or unregister function)') |
314 | } | 321 | } |
315 | 322 | ||
316 | library.register({ registerHook, registerSetting, settingsManager }) | 323 | library.register({ |
324 | registerHook, | ||
325 | registerSetting, | ||
326 | settingsManager, | ||
327 | storageManager | ||
328 | }) | ||
317 | 329 | ||
318 | logger.info('Add plugin %s CSS to global file.', npmName) | 330 | logger.info('Add plugin %s CSS to global file.', npmName) |
319 | 331 | ||
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 { | |||
9 | } from '../../helpers/custom-validators/plugins' | 9 | } from '../../helpers/custom-validators/plugins' |
10 | import { PluginType } from '../../../shared/models/plugins/plugin.type' | 10 | import { PluginType } from '../../../shared/models/plugins/plugin.type' |
11 | import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model' | 11 | import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model' |
12 | import { FindAndCountOptions } from 'sequelize' | 12 | import { FindAndCountOptions, json } from 'sequelize' |
13 | 13 | ||
14 | @DefaultScope(() => ({ | 14 | @DefaultScope(() => ({ |
15 | attributes: { | 15 | attributes: { |
@@ -142,6 +142,40 @@ export class PluginModel extends Model<PluginModel> { | |||
142 | .then(() => undefined) | 142 | .then(() => undefined) |
143 | } | 143 | } |
144 | 144 | ||
145 | static getData (pluginName: string, pluginType: PluginType, key: string) { | ||
146 | const query = { | ||
147 | raw: true, | ||
148 | attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings | ||
149 | where: { | ||
150 | name: pluginName, | ||
151 | type: pluginType | ||
152 | } | ||
153 | } | ||
154 | |||
155 | return PluginModel.findOne(query) | ||
156 | .then((c: any) => { | ||
157 | if (!c) return undefined | ||
158 | |||
159 | return c.value | ||
160 | }) | ||
161 | } | ||
162 | |||
163 | static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) { | ||
164 | const query = { | ||
165 | where: { | ||
166 | name: pluginName, | ||
167 | type: pluginType | ||
168 | } | ||
169 | } | ||
170 | |||
171 | const toSave = { | ||
172 | [`storage.${key}`]: data | ||
173 | } | ||
174 | |||
175 | return PluginModel.update(toSave, query) | ||
176 | .then(() => undefined) | ||
177 | } | ||
178 | |||
145 | static listForApi (options: { | 179 | static listForApi (options: { |
146 | type?: PluginType, | 180 | type?: PluginType, |
147 | uninstalled?: boolean, | 181 | 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 @@ | |||
1 | import * as Bluebird from 'bluebird' | ||
2 | |||
3 | export interface PluginStorageManager { | ||
4 | getData: (key: string) => Bluebird<string> | ||
5 | |||
6 | storeData: (key: string, data: any) => Bluebird<any> | ||
7 | } | ||
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 @@ | |||
1 | import { RegisterHookOptions } from './register-hook.model' | 1 | import { RegisterHookOptions } from './register-hook.model' |
2 | import { RegisterSettingOptions } from './register-setting.model' | 2 | import { RegisterSettingOptions } from './register-setting.model' |
3 | import { PluginSettingsManager } from './plugin-settings-manager.model' | 3 | import { PluginSettingsManager } from './plugin-settings-manager.model' |
4 | import { PluginStorageManager } from './plugin-storage-manager.model' | ||
4 | 5 | ||
5 | export type RegisterOptions = { | 6 | export type RegisterOptions = { |
6 | registerHook: (options: RegisterHookOptions) => void | 7 | registerHook: (options: RegisterHookOptions) => void |
@@ -8,4 +9,6 @@ export type RegisterOptions = { | |||
8 | registerSetting: (options: RegisterSettingOptions) => void | 9 | registerSetting: (options: RegisterSettingOptions) => void |
9 | 10 | ||
10 | settingsManager: PluginSettingsManager | 11 | settingsManager: PluginSettingsManager |
12 | |||
13 | storageManager: PluginStorageManager | ||
11 | } | 14 | } |