refactor res.locals and settings

This commit is contained in:
lesion
2022-02-26 21:27:40 +01:00
parent 9e2ed063b6
commit cf3e1c69fa
20 changed files with 133 additions and 102 deletions

View File

@@ -2,6 +2,7 @@ const config = require('../config')
const Helpers = require('./helpers')
const crypto = require('crypto')
const log = require('../log')
const settingsController = require('../api/controller/settings')
module.exports = {
// follow request from fediverse
@@ -9,8 +10,8 @@ module.exports = {
const body = req.body
if (typeof body.object !== 'string') { return }
const username = body.object.replace(`${config.baseurl}/federation/u/`, '')
if (username !== req.settings.instance_name) {
log.warn(`Following the wrong user: ${username} instead of ${req.settings.instance_name} (could be a wrong config.baseurl)`)
if (username !== settingsController.settings.instance_name) {
log.warn(`Following the wrong user: ${username} instead of ${settingsController.settings.instance_name} (could be a wrong config.baseurl)`)
return res.status(404).send('User not found')
}
@@ -18,7 +19,7 @@ module.exports = {
// if (!user.followers.includes(body.actor)) {
// await user.addFollowers([req.fedi_user.id])
// await user.update({ followers: [...user.followers, body.actor] })
await req.fedi_user.update({ follower: true })
await res.locals.fedi_user.update({ follower: true })
log.info(`Followed by ${body.actor}`)
const guid = crypto.randomBytes(16).toString('hex')
const message = {
@@ -28,7 +29,7 @@ module.exports = {
actor: `${config.baseurl}/federation/u/${username}`,
object: body
}
Helpers.signAndSend(JSON.stringify(message), req.fedi_user.object.inbox)
Helpers.signAndSend(JSON.stringify(message), res.locals.fedi_user.object.inbox)
res.sendStatus(200)
},
@@ -36,16 +37,16 @@ module.exports = {
async unfollow (req, res) {
const body = req.body
const username = body.object.object.replace(`${config.baseurl}/federation/u/`, '')
if (username !== req.settings.instance_name) {
log.warn(`Unfollowing wrong user: ${username} instead of ${req.settings.instance_name}`)
if (username !== settingsController.settings.instance_name) {
log.warn(`Unfollowing wrong user: ${username} instead of ${settingsController.settings.instance_name}`)
return res.status(404).send('User not found')
}
if (body.actor !== body.object.actor || body.actor !== req.fedi_user.ap_id) {
if (body.actor !== body.object.actor || body.actor !== res.locals.fedi_user.ap_id) {
log.info('Unfollow an user created by a different actor !?!?')
return res.status(400).send('Bad things')
}
await req.fedi_user.update({ follower: false })
await res.locals.fedi_user.update({ follower: false })
log.info(`Unfollowed by ${body.actor}`)
res.sendStatus(200)
}