gancio-upstream/server/federation/index.js

87 lines
2.9 KiB
JavaScript

const express = require('express')
const router = express.Router()
// const cors = require('cors')
const Users = require('./users')
const Places = require('./places')
const { Event, User, Tag, Place } = require('../api/models/models')
const settingsController = require('../api/controller/settings')
const Helpers = require('./helpers')
const Inbox = require('./inbox')
const log = require('../log')
/**
* Federation is calling!
* ref: https://www.w3.org/TR/activitypub/#Overview
*/
// router.use(cors())
// middleware to check if federation is enabled
router.use((_req, res, next) => {
if (settingsController.settings.enable_federation) { return next() }
log.debug('[FEDI] Federation disabled!')
return res.status(401).send('Federation disabled')
})
router.use(express.json({ type: ['application/json', 'application/activity+json', 'application/ld+json'] }))
router.get('/m/:event_id:json(.json)?', async (req, res) => {
log.debug('[AP] Get event details ')
const event_id = req.params.event_id
const json = req.params.json
const acceptHtml = req.accepts('html', 'application/json', 'application/activity+json', 'application/ld+json') === 'html'
if (acceptHtml && !json) { return res.redirect(302, `/event/${event_id}`) }
const event = await Event.findByPk(req.params.event_id, { include: [User, Tag, Place] })
if (!event) { return res.status(404).send('Not found') }
const eventAp = event.toAP(settingsController.settings)
eventAp['@context'] = [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1',
{
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"
}
}
]
res.type('application/activity+json; charset=utf-8')
return res.json(eventAp)
})
// get any message coming from federation
router.post('/u/:name/inbox', Helpers.verifySignature, Inbox)
router.get('/u/:name/outbox', Users.outbox)
// router.get('/u/:name/followers', Users.followers)
router.get('/u/:name', Users.get)
router.get('/p/:id', Places.get)
// Handle 404
router.use((req, res) => {
log.warn(`[FEDI] 404 Page not found: ${req.path}`)
res.status(404).send('404: Page not Found')
})
module.exports = router