gancio-upstream/server/federation/follows.js

37 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-07-30 18:32:26 +02:00
const config = require('config')
const Helpers = require('./helpers')
const { user: User } = require('../api/models')
2019-07-31 02:00:12 +02:00
const crypto = require('crypto')
2019-07-30 18:32:26 +02:00
module.exports = {
// follow request from fediverse
async follow (req, res, body, targetOrigin, domain) {
2019-07-30 18:56:32 +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/`, '')
const user = await User.findOne({ where: { username }})
if (!user) {
2019-08-02 13:43:28 +02:00
res.sendStatus(404)
2019-07-30 18:32:26 +02:00
return
}
2019-08-02 13:43:28 +02:00
// check for duplicate
2019-07-30 18:32:26 +02:00
if (user.followers.indexOf(body.actor) === -1) {
console.error('ok this is a new follower: ', body.actor)
await user.update({ followers: [...user.followers, body.actor] })
}
2019-07-31 02:00:12 +02:00
const guid = crypto.randomBytes(16).toString('hex')
let message = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': `${config.baseurl}/federation/${guid}`,
'type': 'Accept',
'actor': `${config.baseurl}/federation/u/${user.username}`,
'object': body,
}
2019-08-02 13:43:28 +02:00
Helpers.signAndSend(message, user, body.actor)
res.sendStatus(200)
2019-07-30 18:32:26 +02:00
},
// unfollow request from fediverse
unfollow () {
console.error('inside unfollow')
}
2019-07-30 18:56:32 +02:00
}