2021-09-27 10:42:17 +02:00
|
|
|
const config = require('../config')
|
2019-07-30 18:32:26 +02:00
|
|
|
const Helpers = require('./helpers')
|
2019-07-31 02:00:12 +02:00
|
|
|
const crypto = require('crypto')
|
2021-03-05 14:17:10 +01:00
|
|
|
const log = require('../log')
|
2022-02-26 21:27:40 +01:00
|
|
|
const settingsController = require('../api/controller/settings')
|
2019-07-30 18:32:26 +02:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
// follow request from fediverse
|
2019-08-08 16:52:13 +02:00
|
|
|
async follow (req, res) {
|
|
|
|
const body = req.body
|
2022-03-07 17:47:31 +01:00
|
|
|
const settings = res.locals.settings
|
2019-09-11 19:12:24 +02:00
|
|
|
if (typeof body.object !== 'string') { return }
|
2019-07-30 18:32:26 +02:00
|
|
|
const username = body.object.replace(`${config.baseurl}/federation/u/`, '')
|
2022-03-07 17:47:31 +01:00
|
|
|
if (username !== settings.instance_name) {
|
2023-12-20 21:57:30 +01:00
|
|
|
log.warn(`[FEDI] Following the wrong user: ${username} instead of ${settings.instance_name} (could be a wrong config.baseurl)`)
|
2021-03-05 14:17:10 +01:00
|
|
|
return res.status(404).send('User not found')
|
|
|
|
}
|
2019-08-08 16:52:13 +02:00
|
|
|
|
2022-02-26 21:27:40 +01:00
|
|
|
await res.locals.fedi_user.update({ follower: true })
|
2023-12-20 21:57:30 +01:00
|
|
|
log.info(`[FEDI] Followed by ${body.actor}`)
|
2019-07-31 02:00:12 +02:00
|
|
|
const guid = crypto.randomBytes(16).toString('hex')
|
2019-09-11 19:12:24 +02:00
|
|
|
const message = {
|
2019-07-31 02:00:12 +02:00
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2021-03-05 14:17:10 +01:00
|
|
|
id: `${config.baseurl}/federation/${guid}`,
|
|
|
|
type: 'Accept',
|
|
|
|
actor: `${config.baseurl}/federation/u/${username}`,
|
|
|
|
object: body
|
2019-09-11 12:00:13 +02:00
|
|
|
}
|
2023-12-22 14:53:53 +01:00
|
|
|
await Helpers.signAndSend(JSON.stringify(message), res.locals.fedi_user.object.inbox)
|
2019-08-02 13:43:28 +02:00
|
|
|
res.sendStatus(200)
|
2019-07-30 18:32:26 +02:00
|
|
|
},
|
2019-08-08 16:52:13 +02:00
|
|
|
|
2019-07-30 18:32:26 +02:00
|
|
|
// unfollow request from fediverse
|
2019-09-11 12:00:13 +02:00
|
|
|
async unfollow (req, res) {
|
2022-03-07 17:47:31 +01:00
|
|
|
const settings = res.locals.settings
|
2019-09-11 12:00:13 +02:00
|
|
|
const body = req.body
|
|
|
|
const username = body.object.object.replace(`${config.baseurl}/federation/u/`, '')
|
2022-03-07 17:47:31 +01:00
|
|
|
if (username !== settings.instance_name) {
|
2023-12-22 14:53:53 +01:00
|
|
|
log.warn(`[FEDI] Unfollowing wrong user: ${username} instead of ${settings.instance_name}`)
|
2021-03-05 14:17:10 +01:00
|
|
|
return res.status(404).send('User not found')
|
|
|
|
}
|
2019-09-11 12:00:13 +02:00
|
|
|
|
2022-02-26 21:27:40 +01:00
|
|
|
await res.locals.fedi_user.update({ follower: false })
|
2023-12-22 14:53:53 +01:00
|
|
|
log.info(`[FEDI] Unfollowed by ${body.actor}`)
|
2019-09-11 12:00:13 +02:00
|
|
|
res.sendStatus(200)
|
2019-07-30 18:32:26 +02:00
|
|
|
}
|
2019-07-30 18:56:32 +02:00
|
|
|
}
|