summary refs log tree commit diff
path: root/src/activitypub/federation/transforms.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/transforms.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/transforms.ts')
-rw-r--r--src/activitypub/federation/transforms.ts60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/activitypub/federation/transforms.ts b/src/activitypub/federation/transforms.ts
index 9d4b11d8..a11cc62b 100644
--- a/src/activitypub/federation/transforms.ts
+++ b/src/activitypub/federation/transforms.ts
@@ -1,6 +1,7 @@
 import {
 	ActorType,
 	Channel,
+	ChannelType,
 	Config,
 	DmChannelDTO,
 	FederationKey,
@@ -8,6 +9,7 @@ import {
 	Invite,
 	Member,
 	Message,
+	Role,
 	Snowflake,
 	User,
 	UserSettings,
@@ -343,7 +345,25 @@ export const transformOrganisationToGuild = async (org: APOrganization) => {
 		owner_id: owner.entity.id,
 	});
 
+	const role = Role.create({
+		id: guild.id,
+		guild_id: guild.id,
+		color: 0,
+		hoist: false,
+		managed: false,
+		// NB: in Spacebar, every role will be non-managed, as we use user-groups instead of roles for managed groups
+		mentionable: false,
+		name: "@everyone",
+		permissions: String("2251804225"),
+		position: 0,
+		icon: undefined,
+		unicode_emoji: undefined,
+		flags: 0, // TODO?
+	});
+
 	await Promise.all([guild.save(), keys.save()]);
+	await role.save();
+
 	return guild;
 };
 
@@ -372,6 +392,7 @@ export const transformGuildToOrganisation = async (
 		inbox: `https://${host}/federation/guilds/${guild.id}/inbox`,
 		outbox: `https://${host}/federation/guilds/${guild.id}/outbox`,
 		followers: `https://${host}/federation/guilds/${guild.id}/followers`,
+		following: `https://${host}/federation/guilds/${guild.id}/following`,
 		publicKey: {
 			id: `https://${host}/federation/guilds/${guild.id}#main-key`,
 			owner: `https://${host}/federation/guilds/${guild.id}`,
@@ -379,3 +400,42 @@ export const transformGuildToOrganisation = async (
 		},
 	};
 };
+
+export const transformGroupToChannel = async (
+	group: APGroup,
+	guild_id: string,
+) => {
+	if (!group.id) throw new APError("Channel ( group ) must have ID");
+	if (!group.publicKey || !group.publicKey.publicKeyPem)
+		throw new APError("Federated guild must have public key.");
+
+	const cache = await FederationKey.findOne({
+		where: { federatedId: group.id },
+	});
+	if (cache) return Channel.findOneOrFail({ where: { id: cache.actorId } });
+
+	const keys = FederationKey.create({
+		actorId: Snowflake.generate(),
+		federatedId: group.id,
+		username: group.name,
+		domain: new URL(group.id).hostname,
+		publicKey: group.publicKey.publicKeyPem,
+		type: ActorType.CHANNEL,
+		inbox: group.inbox.toString(),
+		outbox: group.outbox.toString(),
+	});
+
+	const channel = Channel.create({
+		id: keys.actorId,
+		name: group.name,
+		type: ChannelType.GUILD_TEXT, // TODO
+		owner_id: undefined,
+		last_message_id: undefined,
+		position: 0, // TODO
+		guild_id,
+	});
+
+	await Promise.all([keys.save(), channel.save()]);
+
+	return channel;
+};