split /inbox fediverse management
This commit is contained in:
parent
65cb94dd6a
commit
f5c0ef99f4
3 changed files with 59 additions and 55 deletions
48
server/federation/inbox.js
Normal file
48
server/federation/inbox.js
Normal file
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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')
|
||||
})
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue