summary refs log tree commit diff
path: root/src/activitypub/federation/utils.ts
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-09-30 01:33:52 +0000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-09-30 01:33:52 +0000
commit34867e2154467f37b137fafe833d46db63746f93 (patch)
treec113dff7ccf22e606b0537616e14dd0a8298e5ae /src/activitypub/federation/utils.ts
parentAccept Follow for federated guild joins. TODO: Channels and role federation (diff)
downloadserver-34867e2154467f37b137fafe833d46db63746f93.tar.xz
basic channel federation
Diffstat (limited to 'src/activitypub/federation/utils.ts')
-rw-r--r--src/activitypub/federation/utils.ts26
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 => {