From 34867e2154467f37b137fafe833d46db63746f93 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Sat, 30 Sep 2023 01:33:52 +0000 Subject: basic channel federation --- src/activitypub/federation/utils.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/activitypub/federation/utils.ts') diff --git a/src/activitypub/federation/utils.ts b/src/activitypub/federation/utils.ts index 20b37022..e879e863 100644 --- a/src/activitypub/federation/utils.ts +++ b/src/activitypub/federation/utils.ts @@ -2,6 +2,7 @@ import { DEFAULT_FETCH_OPTIONS } from "@spacebar/api"; import { ActorType, BaseClass, + ChannelCreateEvent, Config, Debug, FederationActivity, @@ -13,9 +14,11 @@ import { User, UserSettings, WebfingerResponse, + emitEvent, } from "@spacebar/util"; import { APObject, + APOrderedCollection, APPerson, AnyAPObject, ObjectIsGroup, @@ -27,6 +30,7 @@ import fetch from "node-fetch"; import { ProxyAgent } from "proxy-agent"; import TurndownService from "turndown"; import { federationQueue } from "./queue"; +import { transformGroupToChannel } from "./transforms"; import { APFollowWithInvite } from "./types"; export const ACTIVITYSTREAMS_CONTEXT = "https://www.w3.org/ns/activitystreams"; @@ -256,6 +260,28 @@ export const tryFederatedGuildJoin = async (code: string, user_id: string) => { await federationQueue.distribute(follow.toJSON()); }; +export const createChannelsFromGuildFollows = async ( + endpoint: string, + guild_id: string, +) => { + const collection = (await resolveAPObject(endpoint)) as APOrderedCollection; // TODO: validation + if (!collection.orderedItems) + throw new APError("Guild followers did not contain orderedItems"); + + // resolve every channel + for (const channel of collection.orderedItems) { + if (typeof channel == "string" || !ObjectIsGroup(channel)) continue; + + const guildchannel = await transformGroupToChannel(channel, guild_id); + + await emitEvent({ + event: "CHANNEL_CREATE", + data: guildchannel, + guild_id: guildchannel.guild_id, + } as ChannelCreateEvent); + } +}; + export const APObjectIsSpacebarActor = ( object: AnyAPObject, ): object is APPerson => { -- cgit 1.4.1