diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-09-30 01:33:52 +0000 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-09-30 01:33:52 +0000 |
commit | 34867e2154467f37b137fafe833d46db63746f93 (patch) | |
tree | c113dff7ccf22e606b0537616e14dd0a8298e5ae /src/activitypub/federation/utils.ts | |
parent | Accept Follow for federated guild joins. TODO: Channels and role federation (diff) | |
download | server-34867e2154467f37b137fafe833d46db63746f93.tar.xz |
basic channel federation
Diffstat (limited to 'src/activitypub/federation/utils.ts')
-rw-r--r-- | src/activitypub/federation/utils.ts | 26 |
1 files changed, 26 insertions, 0 deletions
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 => { |