gancio-upstream/server/federation/index.js

77 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-07-29 01:27:47 +02:00
const express = require('express')
const router = express.Router()
2019-07-30 18:32:26 +02:00
const cors = require('cors')
const Follows = require('./follows')
const Users = require('./users')
2019-09-11 12:00:13 +02:00
const { event: Event, user: User, tag: Tag, place: Place } = require('../api/models')
2019-08-01 15:18:45 +02:00
const Comments = require('./comments')
2019-08-02 13:43:28 +02:00
const Helpers = require('./helpers')
const Ego = require('./ego')
2019-09-11 19:12:24 +02:00
const debug = require('debug')('federation')
2019-07-30 18:32:26 +02:00
/**
* Federation is calling!
* ref: https://www.w3.org/TR/activitypub/#Overview
*/
2019-07-30 18:32:26 +02:00
router.use(cors())
2019-09-11 19:12:24 +02:00
router.use(express.json({ type: ['application/json', 'application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'] }))
2019-08-01 15:18:45 +02:00
router.get('/m/:event_id', async (req, res) => {
const event_id = req.params.event_id
if (req.accepts('html')) return res.redirect(301, `/event/${event_id}`)
2019-08-01 15:18:45 +02:00
2019-09-11 12:00:13 +02:00
const event = await Event.findByPk(req.params.event_id, { include: [ User, Tag, Place ] })
2019-09-11 19:12:24 +02:00
if (!event) { return res.status(404).send('Not found') }
2019-08-01 15:18:45 +02:00
return res.json(event.toAP(event.user.username))
})
2019-07-30 18:32:26 +02:00
// get any message coming from federation
// Federation is calling!
2019-08-02 17:29:55 +02:00
router.post('/u/:name/inbox', Helpers.verifySignature, async (req, res) => {
2019-07-29 22:40:27 +02:00
const b = req.body
2019-09-11 19:12:24 +02:00
debug(b.type)
switch (b.type) {
2019-07-29 22:40:27 +02:00
case 'Follow':
2019-08-08 16:52:13 +02:00
Follows.follow(req, res)
2019-07-30 18:32:26 +02:00
break
case 'Undo':
2019-08-10 15:00:08 +02:00
// unfollow || unlike || unboost
2019-08-01 15:18:45 +02:00
if (b.object.type === 'Follow') {
Follows.unfollow(req, res)
2019-08-01 15:18:45 +02:00
} else if (b.object.type === 'Like') {
2019-08-08 16:52:13 +02:00
Ego.unbookmark(req, res)
2019-08-01 15:18:45 +02:00
} else if (b.object.type === 'Announce') {
console.error('Unboost')
}
2019-07-30 18:32:26 +02:00
break
case 'Announce':
2019-08-02 13:43:28 +02:00
Ego.boost(req, res)
2019-07-30 18:32:26 +02:00
break
case 'Note':
2019-08-01 15:18:45 +02:00
console.error('This is a note ! I probably should not receive this')
break
case 'Like':
2019-08-08 16:52:13 +02:00
Ego.bookmark(req, res)
2019-08-01 15:18:45 +02:00
break
case 'Delete':
2019-09-11 13:12:05 +02:00
await Comments.remove(req, res)
2019-08-01 15:18:45 +02:00
break
2019-07-30 18:32:26 +02:00
case 'Create':
2019-08-01 15:18:45 +02:00
// this is a reply
if (b.object.type === 'Note' && b.object.inReplyTo) {
2019-08-09 14:37:51 +02:00
await Comments.create(req, res)
2019-08-01 15:18:45 +02:00
} else {
console.error('Create what? ', b.object.type)
}
2019-07-29 22:40:27 +02:00
break
}
})
2019-07-29 01:27:47 +02:00
2019-07-30 18:32:26 +02:00
router.get('/u/:name/outbox', Users.outbox)
router.get('/u/:name/followers', Users.followers)
router.get('/u/:name', Users.get)
2019-07-29 22:40:27 +02:00
2019-07-29 23:35:47 +02:00
module.exports = router