2024-12-21 21:54:43 +01:00
/ * *
* Called on initialization of the plugin
*
* @ param { import ( '../../../server/managers/PluginManager' ) . PluginContext } context
* /
module . exports . init = async ( context ) => {
2024-12-21 23:48:56 +01:00
// 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 ( )
}
2024-12-20 00:48:18 +01:00
2024-12-21 21:54:43 +01:00
context . Database . mediaProgressModel . addHook ( 'afterSave' , ( instance , options ) => {
context . Logger . debug ( ` [ExamplePlugin] mediaProgressModel afterSave hook for mediaProgress ${ instance . id } ` )
handleMediaProgressUpdate ( context , instance )
} )
2024-12-20 00:48:18 +01:00
2024-12-21 23:48:56 +01:00
context . Logger . info ( '[ExamplePlugin] Example plugin initialized' )
2024-12-21 21:54:43 +01:00
}
2024-12-21 00:21:00 +01:00
2024-12-21 21:54:43 +01:00
/ * *
* Called when an extension action is triggered
*
* @ param { import ( '../../../server/managers/PluginManager' ) . PluginContext } context
* @ param { string } actionName
* @ param { string } target
* @ param { * } data
2024-12-21 23:48:56 +01:00
* @ returns { Promise < boolean | { error : string } > }
2024-12-21 21:54:43 +01:00
* /
module . exports . onAction = async ( context , actionName , target , data ) => {
context . Logger . info ( '[ExamplePlugin] Example plugin onAction' , actionName , target , data )
2024-12-21 23:48:56 +01:00
createTask ( context )
return true
2024-12-21 21:54:43 +01:00
}
/ * *
* Called when the plugin config page is saved
*
* @ param { import ( '../../../server/managers/PluginManager' ) . PluginContext } context
* @ param { * } config
2024-12-21 23:48:56 +01:00
* @ returns { Promise < boolean | { error : string } > }
2024-12-21 21:54:43 +01:00
* /
module . exports . onConfigSave = async ( context , config ) => {
context . Logger . info ( '[ExamplePlugin] Example plugin onConfigSave' , config )
2024-12-21 00:21:00 +01:00
2024-12-21 23:48:56 +01:00
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
2024-12-21 21:54:43 +01:00
}
//
// Helper functions
//
2024-12-21 23:48:56 +01:00
let numProgressSyncs = 0
2024-12-21 21:54:43 +01:00
/ * *
2024-12-21 23:48:56 +01:00
* Send media progress update to external requestAddress defined in config
2024-12-21 21:54:43 +01:00
*
* @ param { import ( '../../../server/managers/PluginManager' ) . PluginContext } context
* @ param { import ( '../../../server/models/MediaProgress' ) } mediaProgress
* /
async function handleMediaProgressUpdate ( context , mediaProgress ) {
2024-12-21 23:48:56 +01:00
// 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
}
2024-12-21 21:54:43 +01:00
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
2024-12-21 23:48:56 +01:00
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 } ` )
} )
2024-12-21 00:21:00 +01:00
}
2024-12-20 00:48:18 +01:00
}
2024-12-21 21:54:43 +01:00
/ * *
* Test socket authority
*
* @ param { import ( '../../../server/managers/PluginManager' ) . PluginContext } context
2024-12-21 23:48:56 +01:00
* @ param { string } message
2024-12-21 21:54:43 +01:00
* /
2024-12-21 23:48:56 +01:00
async function sendAdminMessageToast ( context , message ) {
context . SocketAuthority . adminEmitter ( 'admin_message' , message )
2024-12-21 21:54:43 +01:00
}
/ * *
* Test task manager
*
* @ param { import ( '../../../server/managers/PluginManager' ) . PluginContext } context
* /
async function createTask ( context ) {
const task = context . TaskManager . createAndAddTask ( 'example_action' , { text : 'Example Task' } , { text : 'This is an example task' } , true )
2024-12-21 23:48:56 +01:00
const pluginConfigEnabled = ! ! context . pluginInstance . config . enable
2024-12-21 21:54:43 +01:00
setTimeout ( ( ) => {
2024-12-21 23:48:56 +01:00
task . setFinished ( { text : ` Plugin is ${ pluginConfigEnabled ? 'enabled' : 'disabled' } ` } )
2024-12-21 21:54:43 +01:00
context . TaskManager . taskFinished ( task )
} , 5000 )
}