WIP plugins: add storage manager
authorChocobozzz <me@florianbigard.com>
Fri, 12 Jul 2019 12:06:33 +0000 (14:06 +0200)
committerChocobozzz <chocobozzz@cpy.re>
Wed, 24 Jul 2019 08:58:16 +0000 (10:58 +0200)
server/lib/plugins/plugin-manager.ts
server/models/server/plugin.ts
shared/models/plugins/plugin-storage-manager.model.ts [new file with mode: 0644]
shared/models/plugins/register-options.model.ts

index 2fa80e8789e56680c3af1454cc3dc2ba0705d21b..7576b284cfc74df81785389f7738d4975135c693 100644 (file)
@@ -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)
 
index 340d49f3b251f68bfd5bfd85dd1f01baa5e1db12..bd3d7a81ec46443ad6e11149a98febc55d9998e9 100644 (file)
@@ -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<PluginModel> {
       .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 (file)
index 0000000..8a1f389
--- /dev/null
@@ -0,0 +1,7 @@
+import * as Bluebird from 'bluebird'
+
+export interface PluginStorageManager {
+  getData: (key: string) => Bluebird<string>
+
+  storeData: (key: string, data: any) => Bluebird<any>
+}
index e60ce3fe0e8aeb7ebb0b15e794cd588283b29a7a..e3a7cff086380c09769a0d81e0ade4d348d3d880 100644 (file)
@@ -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
 }