diff --git a/api/src/activity/federation.js b/api/src/activity/federation.js index 89559b9..7348c81 100644 --- a/api/src/activity/federation.js +++ b/api/src/activity/federation.js @@ -4,8 +4,10 @@ module.exports = { if (msg.activity.type === 'Reject' && msg.object.type === 'Follow') { const follow = await db.table.objects().findOne({ id: msg.object.id }) - follow.type = 'Follow-Rejected' - await db.table.objects().replaceOne({ _id: follow._id }, follow) + if (follow !== null) { + follow.rejected = true + await db.table.objects().replaceOne({ _id: follow._id }, follow) + } return true; } diff --git a/api/src/api/accounts.js b/api/src/api/accounts.js index de6d649..6fc25b8 100644 --- a/api/src/api/accounts.js +++ b/api/src/api/accounts.js @@ -2,20 +2,27 @@ const crypto = require('crypto') const auth = require('../auth.js') const utils = require('../api-utils.js') -async function getFollow(req, res) { +const REJECT_TIMES_MAX = 3 + +async function getValidFollow(req, res) { return await db.table.objects().findOne({ actor: apID(res.locals.user.username), to: req.query.url, type: 'Follow', + rejected: undefined, }) } -async function isFollowRejected(req, res) { - return await db.table.objects().findOne({ +async function getRejectedTimes(req, res) { + var times = 0 + const follows = await db.table.objects().find({ actor: apID(res.locals.user.username), to: req.query.url, - type: 'Follow-Rejected', - }) !== null + type: 'Follow', + rejected: true, + }) + await follows.forEach(() => { times++ }) + return times } module.exports = { @@ -24,12 +31,11 @@ module.exports = { if (!utils.isURLValid(req.query.url)) return res.json({ error: 'invalid_url' }) - const follow = await getFollow(req, res) - if (follow !== null) - return res.json({ error: 'already_following' }) + if (await getRejectedTimes(req, res) >= REJECT_TIMES_MAX) + return res.json({ error: 'follow_rejected_max' }) - if (await isFollowRejected(req, res)) - return res.json({ error: 'follow_rejected' }) + if (await getValidFollow(req, res) !== null) + return res.json({ error: 'follow_already_requested' }) const actor = await apex.store.getObject( apID(res.locals.user.username), true) @@ -50,7 +56,7 @@ module.exports = { if (!utils.isURLValid(req.query.url)) return res.json({ error: 'invalid_url' }) - const follow = await getFollow(req, res) + const follow = await getValidFollow(req, res) if (follow === null) return res.json({ error: 'no_following' })