WIP plugins: add storage manager
This commit is contained in:
parent
b5f919ac8e
commit
b2195fafc2
|
@ -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)
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
import * as Bluebird from 'bluebird'
|
||||
|
||||
export interface PluginStorageManager {
|
||||
getData: (key: string) => Bluebird<string>
|
||||
|
||||
storeData: (key: string, data: any) => Bluebird<any>
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue