refactor res.locals and settings
This commit is contained in:
@@ -11,9 +11,9 @@ const url = require('url')
|
||||
const log = require('../log')
|
||||
|
||||
router.use(cors())
|
||||
function allowFederation (req,res,next) {
|
||||
function allowFederation (req, res, next) {
|
||||
// is federation enabled ?
|
||||
if (req.settings.enable_federation) {
|
||||
if (settingsController.settings.enable_federation) {
|
||||
return next()
|
||||
}
|
||||
log.debug('Federation disabled')
|
||||
@@ -21,19 +21,20 @@ function allowFederation (req,res,next) {
|
||||
}
|
||||
|
||||
router.get('/webfinger', allowFederation, (req, res) => {
|
||||
const settings = settingsController.settings
|
||||
if (!req.query || !req.query.resource || !req.query.resource.includes('acct:')) {
|
||||
log.debug('Bad webfinger request => ', req.query && req.query.resource)
|
||||
return res.status(400).send('Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.')
|
||||
}
|
||||
|
||||
const resource = req.query.resource
|
||||
const domain = (new url.URL(req.settings.baseurl)).host
|
||||
const domain = (new url.URL(settings.baseurl)).host
|
||||
const [, name, req_domain] = resource.match(/acct:(.*)@(.*)/)
|
||||
if (domain !== req_domain) {
|
||||
log.warn(`Bad webfinger request, requested domain "${req_domain}" instead of "${domain}"`)
|
||||
return res.status(400).send('Bad request. Please make sure "acct:USER@DOMAIN" is what you are sending as the "resource" query parameter.')
|
||||
}
|
||||
if (name !== req.settings.instance_name) {
|
||||
if (name !== settings.instance_name) {
|
||||
log.warn(`User not found: ${name}`)
|
||||
return res.status(404).send(`No record found for ${name}`)
|
||||
}
|
||||
@@ -45,7 +46,7 @@ router.get('/webfinger', allowFederation, (req, res) => {
|
||||
{
|
||||
rel: 'self',
|
||||
type: 'application/activity+json',
|
||||
href: `${req.settings.baseurl}/federation/u/${name}`
|
||||
href: `${settings.baseurl}/federation/u/${name}`
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -54,17 +55,19 @@ router.get('/webfinger', allowFederation, (req, res) => {
|
||||
})
|
||||
|
||||
router.get('/nodeinfo/:nodeinfo_version', async (req, res) => {
|
||||
const settings = settingsController.settings
|
||||
|
||||
const usersCount = (await User.findAndCountAll()).count
|
||||
const eventsCount = (await Event.findAndCountAll()).count
|
||||
const resourcesCount = (await Resource.findAndCountAll()).count
|
||||
|
||||
const ret = {
|
||||
metadata: {
|
||||
nodeDescription: req.settings.description,
|
||||
nodeName: req.settings.title,
|
||||
nodeLabel: req.settings.instance_place
|
||||
nodeDescription: settings.description,
|
||||
nodeName: settings.title,
|
||||
nodeLabel: settings.instance_place
|
||||
},
|
||||
openRegistrations: settingsController.settings.allow_registration,
|
||||
openRegistrations: settings.allow_registration,
|
||||
protocols: ['activitypub'],
|
||||
services: { inbound: [], outbound: ['rss2.0'] },
|
||||
software: {
|
||||
@@ -88,6 +91,8 @@ router.get('/nodeinfo/:nodeinfo_version', async (req, res) => {
|
||||
})
|
||||
|
||||
router.get('/x-nodeinfo2', async (req, res) => {
|
||||
const settings = settingsController.settings
|
||||
|
||||
const usersCount = (await User.findAndCountAll()).count
|
||||
const eventsCount = (await Event.findAndCountAll()).count
|
||||
const resourcesCount = (await Resource.findAndCountAll()).count
|
||||
@@ -95,8 +100,8 @@ router.get('/x-nodeinfo2', async (req, res) => {
|
||||
const ret = {
|
||||
version: '1.0',
|
||||
server: {
|
||||
baseUrl: req.settings.baseurl,
|
||||
name: req.settings.title,
|
||||
baseUrl: settings.baseurl,
|
||||
name: settings.title,
|
||||
software: 'Gancio',
|
||||
version
|
||||
},
|
||||
@@ -114,21 +119,25 @@ router.get('/x-nodeinfo2', async (req, res) => {
|
||||
})
|
||||
|
||||
router.get('/nodeinfo', (req, res) => {
|
||||
const settings = settingsController.settings
|
||||
|
||||
const ret = {
|
||||
links: [
|
||||
{ href: `${req.settings.baseurl}/.well-known/nodeinfo/2.0`, rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0' },
|
||||
{ href: `${req.settings.baseurl}/.well-known/nodeinfo/2.1`, rel: 'http://nodeinfo.diaspora.software/ns/schema/2.1' }
|
||||
{ href: `${settings.baseurl}/.well-known/nodeinfo/2.0`, rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0' },
|
||||
{ href: `${settings.baseurl}/.well-known/nodeinfo/2.1`, rel: 'http://nodeinfo.diaspora.software/ns/schema/2.1' }
|
||||
]
|
||||
}
|
||||
res.json(ret)
|
||||
})
|
||||
|
||||
router.use('/host-meta', (req, res) => {
|
||||
const settings = settingsController.settings
|
||||
|
||||
log.debug('host-meta')
|
||||
res.type('application/xml')
|
||||
res.send(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
|
||||
<Link rel="lrdd" type="application/xrd+xml" template="${req.settings.baseurl}/.well-known/webfinger?resource={uri}"/>
|
||||
<Link rel="lrdd" type="application/xrd+xml" template="${settings.baseurl}/.well-known/webfinger?resource={uri}"/>
|
||||
</XRD>`)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user