mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-02-02 08:19:42 +00:00
Update plugin to use uuid for id, update example plugin with taskmanager and socketauthority test
This commit is contained in:
parent
cfe3deff3b
commit
fc17a74865
11 changed files with 138 additions and 105 deletions
|
|
@ -10,13 +10,14 @@ const ExtractJwt = require('passport-jwt').ExtractJwt
|
|||
const OpenIDClient = require('openid-client')
|
||||
const Database = require('./Database')
|
||||
const Logger = require('./Logger')
|
||||
const PluginManager = require('./managers/PluginManager')
|
||||
|
||||
/**
|
||||
* @class Class for handling all the authentication related functionality.
|
||||
*/
|
||||
class Auth {
|
||||
constructor() {
|
||||
this.pluginData = []
|
||||
|
||||
// Map of openId sessions indexed by oauth2 state-variable
|
||||
this.openIdAuthSession = new Map()
|
||||
this.ignorePatterns = [/\/api\/items\/[^/]+\/cover/, /\/api\/authors\/[^/]+\/image/]
|
||||
|
|
@ -939,7 +940,7 @@ class Auth {
|
|||
userDefaultLibraryId: user.getDefaultLibraryId(libraryIds),
|
||||
serverSettings: Database.serverSettings.toJSONForBrowser(),
|
||||
ereaderDevices: Database.emailSettings.getEReaderDevices(user),
|
||||
plugins: PluginManager.pluginData,
|
||||
plugins: this.pluginData,
|
||||
Source: global.Source
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,20 +0,0 @@
|
|||
class PluginAbstract {
|
||||
constructor() {
|
||||
if (this.constructor === PluginAbstract) {
|
||||
throw new Error('Cannot instantiate abstract class')
|
||||
}
|
||||
}
|
||||
|
||||
init() {
|
||||
throw new Error('Method "init()" not implemented')
|
||||
}
|
||||
|
||||
onAction() {
|
||||
throw new Error('Method "onAction()" not implemented')
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
throw new Error('Method "onDestroy()" not implemented')
|
||||
}
|
||||
}
|
||||
module.exports = PluginAbstract
|
||||
|
|
@ -154,7 +154,9 @@ class Server {
|
|||
}
|
||||
|
||||
// Initialize plugins
|
||||
PluginManager.init()
|
||||
await PluginManager.init()
|
||||
// TODO: Prevents circular dependency for SocketAuthority
|
||||
this.auth.pluginData = PluginManager.pluginData
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -6,32 +6,32 @@ class PluginController {
|
|||
constructor() {}
|
||||
|
||||
/**
|
||||
* POST: /api/plugins/action
|
||||
* POST: /api/plugins/:id/action
|
||||
*
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
*/
|
||||
handleAction(req, res) {
|
||||
const pluginSlug = req.body.pluginSlug
|
||||
const pluginId = req.params.id
|
||||
const actionName = req.body.pluginAction
|
||||
const target = req.body.target
|
||||
const data = req.body.data
|
||||
Logger.info(`[PluginController] Handle plugin action ${pluginSlug} ${actionName} ${target}`, data)
|
||||
PluginManager.onAction(pluginSlug, actionName, target, data)
|
||||
Logger.info(`[PluginController] Handle plugin action ${pluginId} ${actionName} ${target}`, data)
|
||||
PluginManager.onAction(pluginId, actionName, target, data)
|
||||
res.sendStatus(200)
|
||||
}
|
||||
|
||||
/**
|
||||
* POST: /api/plugins/config
|
||||
* POST: /api/plugins/:id/config
|
||||
*
|
||||
* @param {*} req
|
||||
* @param {*} res
|
||||
*/
|
||||
handleConfigSave(req, res) {
|
||||
const pluginSlug = req.body.pluginSlug
|
||||
const pluginId = req.params.id
|
||||
const config = req.body.config
|
||||
Logger.info(`[PluginController] Saving config for plugin ${pluginSlug}`, config)
|
||||
PluginManager.onConfigSave(pluginSlug, config)
|
||||
Logger.info(`[PluginController] Saving config for plugin ${pluginId}`, config)
|
||||
PluginManager.onConfigSave(pluginId, config)
|
||||
res.sendStatus(200)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,17 @@
|
|||
const Path = require('path')
|
||||
const Logger = require('../Logger')
|
||||
const Database = require('../Database')
|
||||
const PluginAbstract = require('../PluginAbstract')
|
||||
const SocketAuthority = require('../SocketAuthority')
|
||||
const TaskManager = require('../managers/TaskManager')
|
||||
const fsExtra = require('../libs/fsExtra')
|
||||
const { isUUID, parseSemverStrict } = require('../utils')
|
||||
|
||||
/**
|
||||
* @typedef PluginContext
|
||||
* @property {import('../../server/Logger')} Logger
|
||||
* @property {import('../../server/Database')} Database
|
||||
* @property {import('../Logger')} Logger
|
||||
* @property {import('../Database')} Database
|
||||
* @property {import('../SocketAuthority')} SocketAuthority
|
||||
* @property {import('../managers/TaskManager')} TaskManager
|
||||
*/
|
||||
|
||||
class PluginManager {
|
||||
|
|
@ -30,7 +33,9 @@ class PluginManager {
|
|||
get pluginContext() {
|
||||
return {
|
||||
Logger,
|
||||
Database
|
||||
Database,
|
||||
SocketAuthority,
|
||||
TaskManager
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -40,7 +45,7 @@ class PluginManager {
|
|||
*
|
||||
* @param {string} dirname
|
||||
* @param {string} pluginPath
|
||||
* @returns {Promise<{manifest: Object, contents: PluginAbstract}>}
|
||||
* @returns {Promise<{manifest: Object, contents: any}>}
|
||||
*/
|
||||
async loadPlugin(dirname, pluginPath) {
|
||||
const pluginFiles = await fsExtra.readdir(pluginPath, { withFileTypes: true }).then((files) => files.filter((file) => !file.isDirectory()))
|
||||
|
|
@ -98,7 +103,11 @@ class PluginManager {
|
|||
|
||||
return {
|
||||
manifest: manifestJson,
|
||||
instance: pluginInstance
|
||||
instance: {
|
||||
init: pluginInstance.init,
|
||||
onAction: pluginInstance.onAction,
|
||||
onConfigSave: pluginInstance.onConfigSave
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -181,10 +190,18 @@ class PluginManager {
|
|||
}
|
||||
}
|
||||
|
||||
onAction(pluginSlug, actionName, target, data) {
|
||||
const plugin = this.plugins.find((plugin) => plugin.manifest.slug === pluginSlug)
|
||||
/**
|
||||
*
|
||||
* @param {string} pluginId
|
||||
* @param {string} actionName
|
||||
* @param {string} target
|
||||
* @param {Object} data
|
||||
* @returns
|
||||
*/
|
||||
onAction(pluginId, actionName, target, data) {
|
||||
const plugin = this.plugins.find((plugin) => plugin.manifest.id === pluginId)
|
||||
if (!plugin) {
|
||||
Logger.error(`[PluginManager] Plugin ${pluginSlug} not found`)
|
||||
Logger.error(`[PluginManager] Plugin ${pluginId} not found`)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -200,10 +217,15 @@ class PluginManager {
|
|||
}
|
||||
}
|
||||
|
||||
onConfigSave(pluginSlug, config) {
|
||||
const plugin = this.plugins.find((plugin) => plugin.manifest.slug === pluginSlug)
|
||||
/**
|
||||
*
|
||||
* @param {string} pluginId
|
||||
* @param {Object} config
|
||||
*/
|
||||
onConfigSave(pluginId, config) {
|
||||
const plugin = this.plugins.find((plugin) => plugin.manifest.id === pluginId)
|
||||
if (!plugin) {
|
||||
Logger.error(`[PluginManager] Plugin ${pluginSlug} not found`)
|
||||
Logger.error(`[PluginManager] Plugin ${pluginId} not found`)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -324,8 +324,8 @@ class ApiRouter {
|
|||
//
|
||||
// Plugin routes
|
||||
//
|
||||
this.router.post('/plugins/action', PluginController.handleAction.bind(this))
|
||||
this.router.post('/plugins/config', PluginController.handleConfigSave.bind(this))
|
||||
this.router.post('/plugins/:id/action', PluginController.handleAction.bind(this))
|
||||
this.router.post('/plugins/:id/config', PluginController.handleConfigSave.bind(this))
|
||||
|
||||
//
|
||||
// Misc Routes
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue