mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-02-02 16:29:39 +00:00
Updates on plugin apis and example
This commit is contained in:
parent
fc17a74865
commit
048790b33a
11 changed files with 258 additions and 124 deletions
|
|
@ -4,14 +4,22 @@
|
|||
* @param {import('../../../server/managers/PluginManager').PluginContext} context
|
||||
*/
|
||||
module.exports.init = async (context) => {
|
||||
context.Logger.info('[ExamplePlugin] Example plugin initialized')
|
||||
// Set default config on first init
|
||||
if (!context.pluginInstance.config) {
|
||||
context.Logger.info('[ExamplePlugin] First init. Setting default config')
|
||||
context.pluginInstance.config = {
|
||||
requestAddress: '',
|
||||
enable: false
|
||||
}
|
||||
await context.pluginInstance.save()
|
||||
}
|
||||
|
||||
context.Database.mediaProgressModel.addHook('afterSave', (instance, options) => {
|
||||
context.Logger.debug(`[ExamplePlugin] mediaProgressModel afterSave hook for mediaProgress ${instance.id}`)
|
||||
handleMediaProgressUpdate(context, instance)
|
||||
})
|
||||
|
||||
sendAdminMessageToast(context)
|
||||
context.Logger.info('[ExamplePlugin] Example plugin initialized')
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -21,9 +29,14 @@ module.exports.init = async (context) => {
|
|||
* @param {string} actionName
|
||||
* @param {string} target
|
||||
* @param {*} data
|
||||
* @returns {Promise<boolean|{error: string}>}
|
||||
*/
|
||||
module.exports.onAction = async (context, actionName, target, data) => {
|
||||
context.Logger.info('[ExamplePlugin] Example plugin onAction', actionName, target, data)
|
||||
|
||||
createTask(context)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -31,31 +44,85 @@ module.exports.onAction = async (context, actionName, target, data) => {
|
|||
*
|
||||
* @param {import('../../../server/managers/PluginManager').PluginContext} context
|
||||
* @param {*} config
|
||||
* @returns {Promise<boolean|{error: string}>}
|
||||
*/
|
||||
module.exports.onConfigSave = async (context, config) => {
|
||||
context.Logger.info('[ExamplePlugin] Example plugin onConfigSave', config)
|
||||
|
||||
createTask(context)
|
||||
if (!config.requestAddress || typeof config.requestAddress !== 'string') {
|
||||
context.Logger.error('[ExamplePlugin] Invalid request address')
|
||||
return {
|
||||
error: 'Invalid request address'
|
||||
}
|
||||
}
|
||||
if (typeof config.enable !== 'boolean') {
|
||||
context.Logger.error('[ExamplePlugin] Invalid enable value')
|
||||
return {
|
||||
error: 'Invalid enable value'
|
||||
}
|
||||
}
|
||||
|
||||
// Config would need to be validated
|
||||
const updatedConfig = {
|
||||
requestAddress: config.requestAddress,
|
||||
enable: config.enable
|
||||
}
|
||||
context.pluginInstance.config = updatedConfig
|
||||
await context.pluginInstance.save()
|
||||
context.Logger.info('[ExamplePlugin] Example plugin config saved', updatedConfig)
|
||||
return true
|
||||
}
|
||||
|
||||
//
|
||||
// Helper functions
|
||||
//
|
||||
let numProgressSyncs = 0
|
||||
|
||||
/**
|
||||
* Scrobble media progress update
|
||||
* Send media progress update to external requestAddress defined in config
|
||||
*
|
||||
* @param {import('../../../server/managers/PluginManager').PluginContext} context
|
||||
* @param {import('../../../server/models/MediaProgress')} mediaProgress
|
||||
*/
|
||||
async function handleMediaProgressUpdate(context, mediaProgress) {
|
||||
// Need to reload the model instance since it was passed in during init and may have values changed
|
||||
await context.pluginInstance.reload()
|
||||
|
||||
if (!context.pluginInstance.config?.enable) {
|
||||
return
|
||||
}
|
||||
const requestAddress = context.pluginInstance.config.requestAddress
|
||||
if (!requestAddress) {
|
||||
context.Logger.error('[ExamplePlugin] Request address not set')
|
||||
return
|
||||
}
|
||||
|
||||
const mediaItem = await mediaProgress.getMediaItem()
|
||||
if (!mediaItem) {
|
||||
context.Logger.error(`[ExamplePlugin] Media item not found for mediaProgress ${mediaProgress.id}`)
|
||||
} else {
|
||||
const mediaProgressDuration = mediaProgress.duration
|
||||
const progressPercent = mediaProgressDuration > 0 ? (mediaProgress.currentTime / mediaProgressDuration) * 100 : 0
|
||||
context.Logger.info(`[ExamplePlugin] Media progress update for "${mediaItem.title}" ${Math.round(progressPercent)}%`)
|
||||
context.Logger.info(`[ExamplePlugin] Media progress update for "${mediaItem.title}" ${Math.round(progressPercent)}% (total numProgressSyncs: ${numProgressSyncs})`)
|
||||
|
||||
fetch(requestAddress, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
title: mediaItem.title,
|
||||
progress: progressPercent
|
||||
})
|
||||
})
|
||||
.then(() => {
|
||||
context.Logger.info(`[ExamplePlugin] Media progress update sent for "${mediaItem.title}" ${Math.round(progressPercent)}%`)
|
||||
numProgressSyncs++
|
||||
sendAdminMessageToast(context, `Synced "${mediaItem.title}" (total syncs: ${numProgressSyncs})`)
|
||||
})
|
||||
.catch((error) => {
|
||||
context.Logger.error(`[ExamplePlugin] Error sending media progress update: ${error.message}`)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -63,11 +130,10 @@ async function handleMediaProgressUpdate(context, mediaProgress) {
|
|||
* Test socket authority
|
||||
*
|
||||
* @param {import('../../../server/managers/PluginManager').PluginContext} context
|
||||
* @param {string} message
|
||||
*/
|
||||
async function sendAdminMessageToast(context) {
|
||||
setTimeout(() => {
|
||||
context.SocketAuthority.adminEmitter('admin_message', 'Hello from ExamplePlugin!')
|
||||
}, 10000)
|
||||
async function sendAdminMessageToast(context, message) {
|
||||
context.SocketAuthority.adminEmitter('admin_message', message)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -77,8 +143,10 @@ async function sendAdminMessageToast(context) {
|
|||
*/
|
||||
async function createTask(context) {
|
||||
const task = context.TaskManager.createAndAddTask('example_action', { text: 'Example Task' }, { text: 'This is an example task' }, true)
|
||||
const pluginConfigEnabled = !!context.pluginInstance.config.enable
|
||||
|
||||
setTimeout(() => {
|
||||
task.setFinished({ text: 'Example Task Finished' })
|
||||
task.setFinished({ text: `Plugin is ${pluginConfigEnabled ? 'enabled' : 'disabled'}` })
|
||||
context.TaskManager.taskFinished(task)
|
||||
}, 5000)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue