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')
|
2020-06-27 02:10:10 +02:00
|
|
|
const Event = require('../api/models/event')
|
|
|
|
const User = require('../api/models/user')
|
|
|
|
const Tag = require('../api/models/tag')
|
|
|
|
const Place = require('../api/models/place')
|
|
|
|
|
2019-09-25 14:38:16 +02:00
|
|
|
const settingsController = require('../api/controller/settings')
|
2019-12-04 01:18:05 +01:00
|
|
|
const Resources = require('./resources')
|
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-09-13 11:08:18 +02:00
|
|
|
|
2019-10-22 01:01:41 +02:00
|
|
|
router.use(cors())
|
2019-10-28 17:33:20 +01:00
|
|
|
|
|
|
|
// is federation enabled? middleware
|
2019-09-25 14:38:16 +02:00
|
|
|
router.use((req, res, next) => {
|
2019-10-28 17:33:20 +01:00
|
|
|
if (settingsController.settings.enable_federation) { return next() }
|
2019-09-25 14:38:16 +02:00
|
|
|
debug('Federation disabled!')
|
2019-09-26 00:24:05 +02:00
|
|
|
res.status(401).send('Federation disabled')
|
|
|
|
next(false)
|
2019-09-25 14:38:16 +02:00
|
|
|
})
|
2019-09-13 11:08:18 +02:00
|
|
|
|
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
|
2019-10-28 17:33:20 +01:00
|
|
|
if (req.accepts('html')) { return res.redirect(301, `/event/${event_id}`) }
|
2019-08-01 15:18:45 +02:00
|
|
|
|
2019-12-18 14:44:09 +01: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') }
|
2020-03-14 18:47:49 +01:00
|
|
|
return res.json(event.toAP(event.user.username, req.settings.locale))
|
2019-08-01 15:18:45 +02:00
|
|
|
})
|
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') {
|
2019-08-08 17:48:12 +02:00
|
|
|
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') {
|
2019-10-27 14:11:57 +01:00
|
|
|
Ego.unboost(req, res)
|
2019-08-01 15:18:45 +02:00
|
|
|
}
|
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-10-28 17:33:20 +01:00
|
|
|
debug('This is a note! I probably should create a comment here')
|
2019-08-01 15:18:45 +02:00
|
|
|
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-12-04 01:18:05 +01:00
|
|
|
await Resources.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
|
2020-02-05 00:49:48 +01:00
|
|
|
if (b.object.type === 'Note') {
|
|
|
|
debug('Create a resource!')
|
2019-12-04 01:18:05 +01:00
|
|
|
await Resources.create(req, res)
|
2020-03-14 18:47:49 +01:00
|
|
|
} else if (b.object.type === 'Event') {
|
|
|
|
debug('Event type is coming!!')
|
2019-08-01 15:18:45 +02:00
|
|
|
} else {
|
2020-02-05 00:49:48 +01:00
|
|
|
// await Resources.create(req, res)
|
2019-10-28 17:33:20 +01:00
|
|
|
debug('Create with unsupported Object or not a reply => %s ', b.object.type)
|
2019-08-01 15:18:45 +02:00
|
|
|
}
|
2019-07-29 22:40:27 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2019-07-29 01:27:47 +02:00
|
|
|
|
2020-03-14 18:47:49 +01:00
|
|
|
function redirect_on_html_accepted (req, res, next) {
|
|
|
|
if (req.accepts('html')) {
|
|
|
|
return res.redirect(settingsController.settings.baseurl)
|
|
|
|
}
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:32:26 +02:00
|
|
|
router.get('/u/:name/outbox', Users.outbox)
|
|
|
|
router.get('/u/:name/followers', Users.followers)
|
2020-03-14 18:47:49 +01:00
|
|
|
router.get('/u/:name', redirect_on_html_accepted, Users.get)
|
2019-07-29 22:40:27 +02:00
|
|
|
|
2020-01-27 00:47:03 +01:00
|
|
|
// Handle 404
|
|
|
|
router.use((req, res) => {
|
|
|
|
debug('404 Page not found: %s', req.path)
|
|
|
|
res.status(404).send('404: Page not Found')
|
|
|
|
})
|
|
|
|
|
2019-07-29 23:35:47 +02:00
|
|
|
module.exports = router
|