aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-12 14:06:33 +0200
committerChocobozzz <chocobozzz@cpy.re>2019-07-24 10:58:16 +0200
commitb2195fafc292d6761c25fe51ca4e0328ab403424 (patch)
tree3c390aeb5c5b9648668961871e866bd5e1dde5a4 /server
parentb5f919ac8eb2a1c20e26582fdfd377d687710d8f (diff)
downloadPeerTube-b2195fafc292d6761c25fe51ca4e0328ab403424.tar.gz
PeerTube-b2195fafc292d6761c25fe51ca4e0328ab403424.tar.zst
PeerTube-b2195fafc292d6761c25fe51ca4e0328ab403424.zip
WIP plugins: add storage manager
Diffstat (limited to 'server')
-rw-r--r--server/lib/plugins/plugin-manager.ts14
-rw-r--r--server/models/server/plugin.ts36
2 files changed, 48 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'
13import { RegisterSettingOptions } from '../../../shared/models/plugins/register-setting.model' 13import { RegisterSettingOptions } from '../../../shared/models/plugins/register-setting.model'
14import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model' 14import { RegisterHookOptions } from '../../../shared/models/plugins/register-hook.model'
15import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model' 15import { PluginSettingsManager } from '../../../shared/models/plugins/plugin-settings-manager.model'
16import { PluginStorageManager } from '../../../shared/models/plugins/plugin-storage-manager.model'
16 17
17export interface RegisteredPlugin { 18export 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'
10import { PluginType } from '../../../shared/models/plugins/plugin.type' 10import { PluginType } from '../../../shared/models/plugins/plugin.type'
11import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model' 11import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
12import { FindAndCountOptions } from 'sequelize' 12import { 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,