From 63c679d8597c24091d0b220219814cc45217f342 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 6 Jul 2019 14:53:22 -0400 Subject: [PATCH] Deduplicate inboxes when federating activities --- lib/activitypub/federate.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/activitypub/federate.ts b/lib/activitypub/federate.ts index f3ff330..1e3f980 100644 --- a/lib/activitypub/federate.ts +++ b/lib/activitypub/federate.ts @@ -130,15 +130,18 @@ export async function signAndSend(activity: Activity, inbox: string) { } async function sendToFollowers(activity: Create, db: Database) { - // TODO: only send to unique inboxes - db.each("SELECT inbox FROM followers", (err, result) => { + db.all("SELECT inbox FROM followers", (err, results) => { if (err) { console.log("Error getting followers: ", err); return; } - const inbox = "https://" + new URL(result.inbox).host + "/inbox"; - console.log(`Federating ${activity.object.id} to ${inbox}`); - signAndSend(activity, inbox); + const inboxes = results.map(it => "https://" + new URL(it.inbox).host + "/inbox"); + // convert to a Set to deduplicate inboxes + (new Set(inboxes)) + .forEach(inbox => { + console.log(`Federating ${activity.object.id} to ${inbox}`); + signAndSend(activity, inbox); + }); }); }