From fcd4428c6bb3982ca3c6d6971a331941683828d5 Mon Sep 17 00:00:00 2001 From: lesion Date: Thu, 2 Dec 2021 12:39:55 +0100 Subject: [PATCH] new backend plugin system! --- plugins/gancioPluginExample.js | 28 ++++++++++++++++++++++++++++ server/api/controller/settings.js | 24 ++++++++++++++++++++++++ server/notifier.js | 8 ++++++-- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 plugins/gancioPluginExample.js diff --git a/plugins/gancioPluginExample.js b/plugins/gancioPluginExample.js new file mode 100644 index 00000000..9cc135cd --- /dev/null +++ b/plugins/gancioPluginExample.js @@ -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 diff --git a/server/api/controller/settings.js b/server/api/controller/settings.js index a377dcf2..d549b76f 100644 --- a/server/api/controller/settings.js +++ b/server/api/controller/settings.js @@ -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) { diff --git a/server/notifier.js b/server/notifier.js index 617bce25..be5e9f56 100644 --- a/server/notifier.js +++ b/server/notifier.js @@ -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}`) @@ -38,7 +41,8 @@ const notifier = { const event = await Event.findByPk(eventId, { include: [Tag, Place, Notification, User] }) - + + notifier.emitter.emit(action, event.get({ plain: true, raw: true })) log.debug(action, event.title) // insert notifications