2019-07-29 01:27:47 +02:00
|
|
|
const express = require('express')
|
|
|
|
const router = express.Router()
|
2023-12-26 13:04:20 +01:00
|
|
|
// const cors = require('cors')
|
2019-07-30 18:32:26 +02:00
|
|
|
const Users = require('./users')
|
2023-01-10 18:16:11 +01:00
|
|
|
const { Event, User, Tag, Place } = require('../api/models/models')
|
|
|
|
|
2022-02-26 21:27:40 +01:00
|
|
|
const settingsController = require('../api/controller/settings')
|
2020-06-27 02:10:10 +02:00
|
|
|
|
2019-08-02 13:43:28 +02:00
|
|
|
const Helpers = require('./helpers')
|
2021-03-05 14:19:52 +01:00
|
|
|
const Inbox = require('./inbox')
|
|
|
|
const log = require('../log')
|
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
|
|
|
|
2023-12-26 13:04:20 +01:00
|
|
|
// router.use(cors())
|
2019-10-28 17:33:20 +01:00
|
|
|
|
2023-12-26 13:04:20 +01:00
|
|
|
// middleware to check if federation is enabled
|
2022-06-01 14:13:58 +02:00
|
|
|
router.use((_req, res, next) => {
|
2019-10-28 17:33:20 +01:00
|
|
|
if (settingsController.settings.enable_federation) { return next() }
|
2023-12-15 14:59:51 +01:00
|
|
|
log.debug('[FEDI] Federation disabled!')
|
2021-11-08 10:59:47 +01:00
|
|
|
return res.status(401).send('Federation disabled')
|
2019-09-25 14:38:16 +02:00
|
|
|
})
|
2019-09-13 11:08:18 +02:00
|
|
|
|
2023-12-20 14:08:42 +01:00
|
|
|
router.use(express.json({ type: ['application/json', 'application/activity+json', 'application/ld+json'] }))
|
2019-08-01 15:18:45 +02:00
|
|
|
|
2023-12-26 13:04:20 +01:00
|
|
|
router.get('/m/:event_id:json(.json)?', async (req, res) => {
|
2021-03-05 14:19:52 +01:00
|
|
|
log.debug('[AP] Get event details ')
|
2019-08-01 15:18:45 +02:00
|
|
|
const event_id = req.params.event_id
|
2023-12-26 13:04:20 +01:00
|
|
|
const json = req.params.json
|
2023-12-20 21:57:30 +01:00
|
|
|
const acceptHtml = req.accepts('html', 'application/json', 'application/activity+json', 'application/ld+json') === 'html'
|
2023-12-26 13:04:20 +01:00
|
|
|
if (acceptHtml && !json) { return res.redirect(301, `/event/${event_id}`) }
|
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') }
|
2023-03-28 19:02:08 +02:00
|
|
|
const eventAp = event.toAP(settingsController.settings)
|
2022-06-01 14:13:58 +02:00
|
|
|
eventAp['@context'] = [
|
2024-01-14 22:26:09 +01:00
|
|
|
'https://www.w3.org/ns/activitystreams',
|
|
|
|
'https://w3id.org/security/v1',
|
2023-10-13 13:27:00 +02:00
|
|
|
{
|
2024-01-14 22:26:09 +01:00
|
|
|
toot: 'http://joinmastodon.org/ns#',
|
|
|
|
|
|
|
|
// A property-value pair, e.g. representing a feature of a product or place. We use this to publish this very same instance
|
|
|
|
// https://docs.joinmastodon.org/spec/activitypub/#PropertyValue
|
|
|
|
schema: 'http://schema.org#',
|
|
|
|
ProperyValue: 'schema:PropertyValue',
|
|
|
|
value: 'schema:value',
|
|
|
|
|
|
|
|
// https://docs.joinmastodon.org/spec/activitypub/#discoverable
|
|
|
|
"discoverable": "toot:discoverable",
|
|
|
|
|
|
|
|
// https://docs.joinmastodon.org/spec/activitypub/#Hashtag
|
|
|
|
"Hashtag": "https://www.w3.org/ns/activitystreams#Hashtag",
|
|
|
|
|
|
|
|
manuallyApprovesFollowers: 'as:manuallyApprovesFollowers',
|
|
|
|
|
|
|
|
// focal point - https://docs.joinmastodon.org/spec/activitypub/#focalPoint
|
|
|
|
"focalPoint": {
|
|
|
|
"@container": "@list",
|
|
|
|
"@id": "toot:focalPoint"
|
2023-10-13 13:27:00 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-01 14:13:58 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
res.type('application/activity+json; charset=utf-8')
|
|
|
|
return res.json(eventAp)
|
2019-08-01 15:18:45 +02:00
|
|
|
})
|
2019-07-30 18:32:26 +02:00
|
|
|
|
|
|
|
// get any message coming from federation
|
2021-03-05 14:19:52 +01:00
|
|
|
router.post('/u/:name/inbox', Helpers.verifySignature, Inbox)
|
2019-07-29 01:27:47 +02:00
|
|
|
|
2019-07-30 18:32:26 +02:00
|
|
|
router.get('/u/:name/outbox', Users.outbox)
|
2024-02-28 15:07:52 +01:00
|
|
|
// router.get('/u/:name/followers', Users.followers)
|
2023-12-26 13:04:20 +01:00
|
|
|
|
2021-11-11 16:43:41 +01:00
|
|
|
router.get('/u/:name', Users.get)
|
2019-07-29 22:40:27 +02:00
|
|
|
|
2020-01-27 00:47:03 +01:00
|
|
|
// Handle 404
|
|
|
|
router.use((req, res) => {
|
2023-12-26 13:04:20 +01:00
|
|
|
log.warn(`[FEDI] 404 Page not found: ${req.path}`)
|
2020-01-27 00:47:03 +01:00
|
|
|
res.status(404).send('404: Page not Found')
|
|
|
|
})
|
|
|
|
|
2019-07-29 23:35:47 +02:00
|
|
|
module.exports = router
|