gancio/server/federation/follows.js

53 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-07-30 18:32:26 +02:00
const config = require('config')
const Helpers = require('./helpers')
2019-09-12 14:59:51 +02:00
const { user: User, fed_users: FedUsers } = require('../api/models')
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/`, '')
2019-10-28 17:33:20 +01:00
const user = await User.findOne({ where: { username }, include: { model: FedUsers, as: 'followers' } })
2019-09-11 19:12:24 +02:00
if (!user) { 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-09-18 12:55:33 +02:00
if (!user.followers.includes(body.actor)) {
2019-11-15 15:55:27 +01:00
await user.addFollowers([req.fedi_user.ap_id])
2019-09-12 14:59:51 +02:00
// await user.update({ followers: [...user.followers, body.actor] })
2019-10-28 17:33:20 +01:00
debug('%s followed by %s (%d)', username, body.actor, user.followers.length + 1)
2019-08-08 16:52:13 +02:00
} else {
debug('duplicate %s followed by %s', username, body.actor)
2019-07-30 18:32:26 +02:00
}
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/${user.username}`,
2019-09-11 19:12:24 +02:00
'object': body
2019-09-11 12:00:13 +02:00
}
2019-11-15 15:55:27 +01:00
Helpers.signAndSend(message, user, 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/`, '')
2019-10-28 17:33:20 +01:00
const user = await User.findOne({ where: { username }, include: { model: FedUsers, as: 'followers' } })
2019-09-11 19:12:24 +02:00
if (!user) { 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')
}
2019-09-12 14:59:51 +02:00
2019-11-15 15:55:27 +01:00
if (req.fedi_user) { await user.removeFollowers(req.fedi_user.ap_id) }
2019-09-12 14:59:51 +02:00
debug('%s unfollowed by %s', username, 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
}