gancio-upstream/server/federation/inbox.js

97 lines
3.4 KiB
JavaScript
Raw Normal View History

2021-03-05 14:19:52 +01:00
const Follows = require('./follows')
const Resources = require('./resources')
2023-11-09 16:56:46 +01:00
const Events = require('./events')
2021-03-05 14:19:52 +01:00
const Ego = require('./ego')
const log = require('../log')
module.exports = async (req, res) => {
const message = req.body
2024-01-10 12:36:27 +01:00
// has to have an object and a type property..
2023-12-26 13:04:20 +01:00
if (!message?.object || !message?.type) {
log.warn('[FEDI] message without `object` or `type` property: %s', message)
return res.status(404).send('Wrong AP message: no object or type property')
}
2023-12-26 13:04:20 +01:00
log.debug('[FEDI] Type %s', message.type)
switch (message.type) {
2021-03-05 14:19:52 +01:00
case 'Follow':
Follows.follow(req, res)
break
case 'Accept':
return res.sendStatus(202)
2021-03-05 14:19:52 +01:00
case 'Undo':
// unfollow || unlike || unboost
2023-12-26 13:04:20 +01:00
if (message.object?.type === 'Follow') {
2021-03-05 14:19:52 +01:00
Follows.unfollow(req, res)
2023-12-26 13:04:20 +01:00
} else if (message.object?.type === 'Like') {
2021-03-05 14:19:52 +01:00
Ego.unbookmark(req, res)
2023-12-26 13:04:20 +01:00
} else if (message.object?.type === 'Announce') {
2021-03-05 14:19:52 +01:00
Ego.unboost(req, res)
2023-12-26 13:04:20 +01:00
} else {
log.warn('[FEDI] Undo message type with unsupported object: %s', JSON.stringify(message.object))
2021-03-05 14:19:52 +01:00
}
break
case 'Announce':
Ego.boost(req, res)
break
case 'Note':
2023-12-26 13:04:20 +01:00
log.debug('[FEDI] This is a note and it is not supported: %s', JSON.stringify(message.object))
2021-03-05 14:19:52 +01:00
break
case 'Like':
Ego.bookmark(req, res)
break
case 'Delete':
2024-01-11 13:00:39 +01:00
if (message.object?.type === 'Tombstone') {
message.object.type = message.object?.formerType ?? 'Tombstone'
}
if (message.object?.type==='Note') {
2023-11-09 16:56:46 +01:00
await Resources.remove(req, res)
2023-12-26 13:04:20 +01:00
} else if (message.object?.type === 'Event') {
if (!res.locals.fedi_user.following || !res.locals.fedi_user.trusted) {
log.warn(`[FEDI] APUser not followed or not trusted`)
return res.sendStatus(404)
2024-01-11 13:00:39 +01:00
}
2023-11-09 16:56:46 +01:00
await Events.remove(req, res)
2024-01-11 13:00:39 +01:00
} else {
log.debug('[FEDI] Delete of unknown object: %s', JSON.stringify(message.object))
2023-11-09 16:56:46 +01:00
}
2021-03-05 14:19:52 +01:00
break
2023-11-09 16:56:46 +01:00
case 'Update':
if (message.object?.type === 'Event') {
log.debug(`[FEDI] Event update is coming from ${res.locals.fedi_user.ap_id}`)
2023-12-26 13:04:20 +01:00
if (!res.locals.fedi_user.following || !res.locals.fedi_user.trusted) {
log.warn(`[FEDI] APUser not followed or not trusted`)
return res.sendStatus(404)
}
await Events.update(req, res)
} else if (message.object?.type === 'Note') {
log.debug('[FEDI] Note update is coming from %s', res.locals.fedi_user.ap_id)
await Resources.update(req, res)
2024-01-10 12:36:27 +01:00
} else {
log.debug('[FEDI] Unsupported Update: %s', JSON.stringify(message?.object))
2023-11-09 16:56:46 +01:00
}
break
2021-03-05 14:19:52 +01:00
case 'Create':
// this is a reply
2023-12-26 13:04:20 +01:00
if (message.object?.type === 'Note') {
2021-03-05 14:19:52 +01:00
await Resources.create(req, res)
2023-12-26 13:04:20 +01:00
} else if (message.object?.type === 'Event') {
2023-12-22 20:58:38 +01:00
log.debug(`[FEDI] Event is coming from ${res.locals.fedi_user.ap_id}`)
2023-12-26 13:04:20 +01:00
if (!res.locals.fedi_user.following || !res.locals.fedi_user.trusted) {
2023-12-22 20:58:38 +01:00
log.warn(`[FEDI] APUser not followed nor trusted`)
return res.sendStatus(404)
}
2023-11-09 16:56:46 +01:00
await Events.create(req, res)
2021-03-05 14:19:52 +01:00
} else {
// await Resources.create(req, res)
2024-01-10 12:36:27 +01:00
log.warn('[FEDI] Create with unsupported Object or not a reply => %s', JSON.stringify(message?.object))
return res.sendStatus(404)
2021-03-05 14:19:52 +01:00
}
break
default:
2024-01-10 12:36:27 +01:00
log.error('[FEDI] Unknown message type: %s', message?.type)
res.sendStatus(404)
2021-03-05 14:19:52 +01:00
}
}