From f5c0ef99f488d84a94cbbff42122fa670955f3d2 Mon Sep 17 00:00:00 2001 From: les Date: Fri, 5 Mar 2021 14:19:52 +0100 Subject: [PATCH] split /inbox fediverse management --- server/federation/inbox.js | 48 ++++++++++++++++++++++++++++++++ server/federation/index.js | 56 ++++---------------------------------- server/notifier.js | 10 +++---- 3 files changed, 59 insertions(+), 55 deletions(-) create mode 100644 server/federation/inbox.js diff --git a/server/federation/inbox.js b/server/federation/inbox.js new file mode 100644 index 00000000..ae7893e8 --- /dev/null +++ b/server/federation/inbox.js @@ -0,0 +1,48 @@ +const Follows = require('./follows') +const Resources = require('./resources') +const Ego = require('./ego') +const log = require('../log') + +module.exports = async (req, res) => { + const b = req.body + log.debug(b.type) + switch (b.type) { + case 'Follow': + Follows.follow(req, res) + break + case 'Undo': + // unfollow || unlike || unboost + if (b.object.type === 'Follow') { + Follows.unfollow(req, res) + } else if (b.object.type === 'Like') { + Ego.unbookmark(req, res) + } else if (b.object.type === 'Announce') { + Ego.unboost(req, res) + } + break + case 'Announce': + Ego.boost(req, res) + break + case 'Note': + log.debug('This is a note! I probably should create a comment here') + break + case 'Like': + Ego.bookmark(req, res) + break + case 'Delete': + await Resources.remove(req, res) + break + case 'Create': + // this is a reply + if (b.object.type === 'Note') { + log.debug('Create a resource!') + await Resources.create(req, res) + } else if (b.object.type === 'Event') { + log.debug('Event type is coming!!') + } else { + // await Resources.create(req, res) + log.warn(`Create with unsupported Object or not a reply => ${b.object.type}`) + } + break + } +} diff --git a/server/federation/index.js b/server/federation/index.js index 70b1f1f6..62db5b41 100644 --- a/server/federation/index.js +++ b/server/federation/index.js @@ -1,7 +1,6 @@ const express = require('express') const router = express.Router() const cors = require('cors') -const Follows = require('./follows') const Users = require('./users') const Event = require('../api/models/event') const User = require('../api/models/user') @@ -9,10 +8,9 @@ const Tag = require('../api/models/tag') const Place = require('../api/models/place') const settingsController = require('../api/controller/settings') -const Resources = require('./resources') const Helpers = require('./helpers') -const Ego = require('./ego') -const debug = require('debug')('federation') +const Inbox = require('./inbox') +const log = require('../log') /** * Federation is calling! @@ -24,7 +22,7 @@ router.use(cors()) // is federation enabled? middleware router.use((req, res, next) => { if (settingsController.settings.enable_federation) { return next() } - debug('Federation disabled!') + log.debug('Federation disabled!') res.status(401).send('Federation disabled') next(false) }) @@ -32,6 +30,7 @@ router.use((req, res, next) => { router.use(express.json({ type: ['application/json', 'application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'] })) router.get('/m/:event_id', async (req, res) => { + log.debug('[AP] Get event details ') const event_id = req.params.event_id if (req.accepts('html')) { return res.redirect(301, `/event/${event_id}`) } @@ -41,50 +40,7 @@ router.get('/m/:event_id', async (req, res) => { }) // get any message coming from federation -// Federation is calling! -router.post('/u/:name/inbox', Helpers.verifySignature, async (req, res) => { - const b = req.body - debug(b.type) - switch (b.type) { - case 'Follow': - Follows.follow(req, res) - break - case 'Undo': - // unfollow || unlike || unboost - if (b.object.type === 'Follow') { - Follows.unfollow(req, res) - } else if (b.object.type === 'Like') { - Ego.unbookmark(req, res) - } else if (b.object.type === 'Announce') { - Ego.unboost(req, res) - } - break - case 'Announce': - Ego.boost(req, res) - break - case 'Note': - debug('This is a note! I probably should create a comment here') - break - case 'Like': - Ego.bookmark(req, res) - break - case 'Delete': - await Resources.remove(req, res) - break - case 'Create': - // this is a reply - if (b.object.type === 'Note') { - debug('Create a resource!') - await Resources.create(req, res) - } else if (b.object.type === 'Event') { - debug('Event type is coming!!') - } else { - // await Resources.create(req, res) - debug('Create with unsupported Object or not a reply => %s ', b.object.type) - } - break - } -}) +router.post('/u/:name/inbox', Helpers.verifySignature, Inbox) function redirect_on_html_accepted (req, res, next) { if (req.accepts('html')) { @@ -99,7 +55,7 @@ router.get('/u/:name', redirect_on_html_accepted, Users.get) // Handle 404 router.use((req, res) => { - debug('404 Page not found: %s', req.path) + log.warn(`404 Page not found: ${req.path}`) res.status(404).send('404: Page not Found') }) diff --git a/server/notifier.js b/server/notifier.js index 0dcef354..3fc77c3c 100644 --- a/server/notifier.js +++ b/server/notifier.js @@ -1,6 +1,6 @@ const mail = require('./api/mail') const config = require('config') -const debug = require('debug')('notifier') +const log = require('./log') const fediverseHelpers = require('./federation/helpers') const Event = require('./api/models/event') @@ -16,7 +16,7 @@ const notifier = { sendNotification (notification, event) { const promises = [] - debug('Send %s notification %s', notification.type, notification.action) + log.debug(`Send ${notification.type} notification ${notification.action}`) let p switch (notification.type) { // case 'mail': TODO: locale? @@ -38,7 +38,7 @@ const notifier = { include: [Tag, Place, Notification, User] }) - debug('%s -> %s', action, event.title) + log.debug(action, event.title) // insert notifications const notifications = await eventController.getNotifications(event, action) @@ -51,7 +51,7 @@ const notifier = { await notifier.sendNotification(notification, event) notification.event_notification.status = 'sent' } catch (err) { - debug(err) + log.error(err) notification.event_notification.status = 'error' } return notification.event_notification.save() @@ -71,7 +71,7 @@ const notifier = { e.status = 'sent' return e.save() } catch (err) { - debug(err) + log.error(err) e.status = 'error' e.error = err return e.save()