new backend plugin system!

This commit is contained in:
lesion 2021-12-02 12:39:55 +01:00
parent d43cfbd6f9
commit fcd4428c6b
No known key found for this signature in database
GPG key ID: 352918250B012177
3 changed files with 58 additions and 2 deletions

View file

@ -0,0 +1,28 @@
const plugin = {
gancio: null,
load (gancio) {
console.error('Plugin GancioPluginExample loaded!')
plugin.gancio = gancio
},
onEventCreate (event) {
const eventLink = `${plugin.gancio.settings.baseurl}/event/${event.slug}`
if (!event.is_visible) {
console.error(`Unconfirmed event created: ${event.title} / ${eventLink}`)
} else {
console.error(`Event created: ${event.title} / ${eventLink}`)
}
},
onEventUpdate (event) {
console.error(`Event "${event.title}" updated`)
},
onEventDelete (event) {
console.error(`Event "${event.title}" deleted`)
}
}
module.exports = plugin

View file

@ -106,6 +106,30 @@ const settingsController = {
}
})
}
const plugins_path = path.resolve(process.env.cwd || '', 'plugins')
if (fs.existsSync(plugins_path)) {
const notifier = require('../../notifier')
const pluginsFile = fs.readdirSync(plugins_path).filter(e => path.extname(e).toLowerCase() === '.js')
pluginsFile.forEach( pluginFile => {
log.info(`Loading plugin ${pluginFile}`)
try {
const plugin = require(path.resolve(plugins_path, pluginFile))
plugin.load({ settings: settingsController.settings })
if (typeof plugin.onEventCreate === 'function') {
notifier.emitter.on('Create', plugin.onEventCreate)
}
if (typeof plugin.onEventDelete === 'function') {
notifier.emitter.on('Delete', plugin.onEventDelete)
}
if (typeof plugin.onEventUpdate === 'function') {
notifier.emitter.on('Update', plugin.onEventUpdate)
}
} catch (e) {
log.error(e)
}
})
}
},
async set (key, value, is_secret = false) {

View file

@ -1,5 +1,6 @@
const events = require('events')
const mail = require('./api/mail')
const config = require('./config')
const log = require('./log')
const fediverseHelpers = require('./federation/helpers')
@ -15,6 +16,8 @@ const settingsController = require('./api/controller/settings')
const notifier = {
emitter: new events.EventEmitter(),
sendNotification (notification, event) {
const promises = []
log.info(`Send ${notification.type} notification ${notification.action}`)
@ -39,6 +42,7 @@ const notifier = {
include: [Tag, Place, Notification, User]
})
notifier.emitter.emit(action, event.get({ plain: true, raw: true }))
log.debug(action, event.title)
// insert notifications