Automatically accept if local user is following remote user (accepted)

This commit is contained in:
Niko 2022-02-16 17:13:32 +01:00
parent 2f69c506cc
commit c6d48b65a3
2 changed files with 61 additions and 9 deletions

View File

@ -1,3 +1,4 @@
const api = require('../api.js')
const utils = require('../api-utils.js')
async function areActorsValid(actors, ourDomain) {
@ -62,6 +63,15 @@ module.exports = {
if (await areActorsValid(msg.activity.object, true)) {
msg.activity.actor = utils.firstIfArray(msg.activity.actor)
msg.activity.object = utils.firstIfArray(msg.activity.object)
// automatically accept if target user in our instance is following (accepted)
const acceptedFollow = await api.accounts
.getAcceptedFollow(msg.activity.object, msg.activity.actor)
if (acceptedFollow !== null) {
await api.accounts.followAccept(msg.activity)
msg.activity.accepted = true
}
await apex.store.saveObject(msg.activity)
}
return true

View File

@ -4,20 +4,27 @@ const utils = require('../api-utils.js')
const REJECT_TIMES_MAX = 3
async function getValidFollow(req, res) {
async function httpGetValidFollow(req, res) {
return await getValidFollow( apID(res.locals.user.username), req.query.url )
}
async function httpGetRejectedTimes(req, res) {
return await getRejectedTimes( apID(res.locals.user.username), req.query.url )
}
async function getValidFollow(actor, object) {
return await db.table.objects().findOne({
actor: apID(res.locals.user.username),
to: req.query.url,
actor, object,
type: 'Follow',
rejected: undefined,
})
}
async function getRejectedTimes(req, res) {
async function getRejectedTimes(actor, object) {
var times = 0
const follows = await db.table.objects().find({
actor: apID(res.locals.user.username),
to: req.query.url,
actor, object,
type: 'Follow',
rejected: true,
})
@ -25,6 +32,14 @@ async function getRejectedTimes(req, res) {
return times
}
async function getAcceptedFollow(actor, object) {
return await db.table.objects().findOne({
actor, object,
type: 'Follow',
accepted: true,
})
}
async function getAccount(id) {
const person = await db.getAPObject(id, 'Person')
const username = utils.firstIfArray(person.preferredUsername)
@ -44,8 +59,35 @@ async function getAccount(id) {
}
}
async function followAccept(a1, a2) {
let activity
if (typeof a1 === 'string' && typeof a2 === 'string')
activity = await getValidFollow(a1, a2)
else if (a1 instanceof Object && a2 === undefined)
activity = a1
const actor = await apex.store.getObject(activity.object, true)
const payload = {
id: utils.apRandomURL(),
type: 'Accept',
actor: activity.object,
to: activity.actor,
object: activity,
}
if (actor.type === 'Person') {
await apex.addToOutbox(actor, payload) // actual Accept->Follow event
await apex.acceptFollow(actor, activity) // side-effects
}
return false
}
module.exports = {
getValidFollow,
getRejectedTimes,
getAcceptedFollow,
getAccount,
followAccept,
follow: {
post: [auth.enforceSession, async (req, res) => {
if (!utils.isURLValid(req.query.url))
@ -59,10 +101,10 @@ module.exports = {
if (block !== null)
return res.json({ error: 'user_blocked_you' })
if (await getRejectedTimes(req, res) >= REJECT_TIMES_MAX)
if (await httpGetRejectedTimes(req, res) >= REJECT_TIMES_MAX)
return res.json({ error: 'follow_rejected_max' })
const follow = await getValidFollow(req, res)
const follow = await httpGetValidFollow(req, res)
if (follow !== null) {
if (follow.accepted)
return res.json({ error: 'already_following' })
@ -88,7 +130,7 @@ module.exports = {
if (!utils.isURLValid(req.query.url))
return res.json({ error: 'invalid_url' })
const follow = await getValidFollow(req, res)
const follow = await httpGetValidFollow(req, res)
if (follow === null)
return res.json({ error: 'not_following' })