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) {
|
|
|
|
log.warn(`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
|
|
|
|
2019-08-02 13:43:28 +02:00
|
|
|
// check for duplicate
|
2019-12-04 00:50:15 +01:00
|
|
|
// if (!user.followers.includes(body.actor)) {
|
|
|
|
// await user.addFollowers([req.fedi_user.id])
|
|
|
|
// await user.update({ followers: [...user.followers, body.actor] })
|
2022-02-26 21:27:40 +01:00
|
|
|
await res.locals.fedi_user.update({ follower: true })
|
2021-04-28 12:44:26 +02:00
|
|
|
log.info(`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
|
|
|
}
|
2022-02-26 21:27:40 +01:00
|
|
|
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) {
|
|
|
|
log.warn(`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
|
|
|
if (body.actor !== body.object.actor || body.actor !== res.locals.fedi_user.ap_id) {
|
2021-04-28 12:44:26 +02:00
|
|
|
log.info('Unfollow an user created by a different actor !?!?')
|
2019-09-11 13:12:05 +02:00
|
|
|
return res.status(400).send('Bad things')
|
|
|
|
}
|
2022-02-26 21:27:40 +01:00
|
|
|
await res.locals.fedi_user.update({ follower: false })
|
2021-04-28 12:44:26 +02:00
|
|
|
log.info(`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
|
|
|
}
|