gancio/server/federation/follows.js

54 lines
2 KiB
JavaScript
Raw Normal View History

2019-07-30 18:32:26 +02:00
const config = require('config')
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')
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
2021-03-05 14:17:10 +01:00
log.debug('follow')
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/`, '')
2021-03-05 14:17:10 +01:00
if (username !== req.settings.instance_name) {
log.warn(`Following the wrong user: ${username} instead of ${req.settings.instance_name}`)
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
// 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 })
2021-03-05 14:17:10 +01:00
log.debug(`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
}
2021-03-18 22:40:14 +01:00
Helpers.signAndSend(JSON.stringify(message), req.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) {
const body = req.body
const username = body.object.object.replace(`${config.baseurl}/federation/u/`, '')
2021-03-05 14:17:10 +01:00
if (username !== req.settings.instance_name) {
log.warn(`Unfollowing wrong user: ${username} instead of ${req.settings.instance_name}`)
return res.status(404).send('User not found')
}
2019-09-11 12:00:13 +02:00
2019-11-15 15:55:27 +01:00
if (body.actor !== body.object.actor || body.actor !== req.fedi_user.ap_id) {
2021-03-05 14:17:10 +01:00
log.debug('Unfollow an user created by a different actor !?!?')
2019-09-11 13:12:05 +02:00
return res.status(400).send('Bad things')
}
await req.fedi_user.update({ follower: false })
2021-03-05 14:17:10 +01:00
log.debug(`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
}