Allow user to Follow (Match) a person to a maximum of 3 times being rejected

* TODO: add a block method for a single-time "reject" that eliminates any relationship between parties
This commit is contained in:
Niko 2022-02-16 11:00:53 +01:00
parent 0c0729b24e
commit 56f46070fe
2 changed files with 21 additions and 13 deletions

View File

@ -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;
}

View File

@ -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' })