gancio/server/federation/follows.js

47 lines
1.7 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')
2019-09-11 21:20:44 +02:00
const debug = require('debug')('federation:follows')
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
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/`, '')
if (username !== 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 })
debug('Followed by %s', 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',
'id': `${config.baseurl}/federation/${guid}`,
'type': 'Accept',
'actor': `${config.baseurl}/federation/u/${username}`,
2019-09-11 19:12:24 +02:00
'object': body
2019-09-11 12:00:13 +02:00
}
Helpers.signAndSend(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/`, '')
if (username !== 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) {
2019-09-11 13:12:05 +02:00
debug('Unfollow an user created by a different actor !?!?')
return res.status(400).send('Bad things')
}
await req.fedi_user.update({ follower: false })
debug('Unfollowed by %s', 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
}