From 98a4449de4ccac7d0ffe7ab59113abbe2562a405 Mon Sep 17 00:00:00 2001 From: Niko Date: Wed, 16 Feb 2022 11:55:46 +0100 Subject: [PATCH] Support "Block" operations on ActivityPub * TODO: inhibit interactions on our side from a blocked Actor --- api/src/activity/federation.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/api/src/activity/federation.js b/api/src/activity/federation.js index 02fbc42..11e0725 100644 --- a/api/src/activity/federation.js +++ b/api/src/activity/federation.js @@ -1,4 +1,27 @@ +async function areActorsValid(actors, ourDomain) { + if (!actors instanceof Array && typeof actors !== 'string') { + console.error('Unexpected type of "actors" in "federation.doesActorsExistAndOurs"') + return false + } + if (typeof actors === 'string') actors = [actors] + for (var i = 0; i < actors.length; i++) { + let url + try { url = new URL(actors[i]) } + catch (e) { return false } + if (ourDomain && url.hostname !== apex.domain) + return false + + actors[i] = url.href + const actor = await apex.store.getObject(actors[i]) + // TODO: check account is deleted/disabled/banned/...? + if (actor === null || actor.type !== 'Person') + return false + } + return true +} + module.exports = { + areActorsValid, inbox: async (msg) => { // TODO: add logging for every matches params if (msg.activity.type === 'Reject' && msg.object.type === 'Follow') @@ -8,14 +31,20 @@ module.exports = { follow.rejected = true await db.table.objects().replaceOne({ _id: follow._id }, follow) } - return true; + return true + } + if (msg.activity.type === 'Block') + { + if (areActorsValid(msg.activity.object, true)) + await apex.store.saveObject(msg.activity) + return true } if (msg.activity.type === 'Undo') { if (msg.object.type === 'Block') { await db.table.objects().deleteOne( { id: msg.object.id, type: 'Block' }) - return true; + return true } }