summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@protonmail.com>2022-08-29 11:11:40 -0400
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-12-19 20:37:59 +1100
commitb84aa73852ae1267c9e193cfa6ee365c69d16803 (patch)
tree645ee5855738495476bfc9c003782ffb7c96d081 /src/api
parentConfigurable MFA backup token length (diff)
downloadserver-b84aa73852ae1267c9e193cfa6ee365c69d16803.tar.xz
implement guild profiles and fix user profiles
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/guilds/#guild_id/members/#member_id/index.ts82
-rw-r--r--src/api/routes/guilds/#guild_id/profile/index.ts30
-rw-r--r--src/api/routes/users/#id/profile.ts97
3 files changed, 130 insertions, 79 deletions
diff --git a/src/api/routes/guilds/#guild_id/members/#member_id/index.ts b/src/api/routes/guilds/#guild_id/members/#member_id/index.ts
index 28085752..8e6c3ce7 100644
--- a/src/api/routes/guilds/#guild_id/members/#member_id/index.ts
+++ b/src/api/routes/guilds/#guild_id/members/#member_id/index.ts
@@ -9,6 +9,7 @@ import {
 	Sticker,
 	Emoji,
 	Guild,
+	handleFile,
 	MemberChangeSchema,
 } from "@fosscord/util";
 import { route } from "@fosscord/api";
@@ -26,54 +27,39 @@ router.get("/", route({}), async (req: Request, res: Response) => {
 	return res.json(member);
 });
 
-router.patch(
-	"/",
-	route({ body: "MemberChangeSchema" }),
-	async (req: Request, res: Response) => {
-		let { guild_id, member_id } = req.params;
-		if (member_id === "@me") member_id = req.user_id;
-		const body = req.body as MemberChangeSchema;
-
-		const member = await Member.findOneOrFail({
-			where: { id: member_id, guild_id },
-			relations: ["roles", "user"],
-		});
-		const permission = await getPermission(req.user_id, guild_id);
-		const everyone = await Role.findOneOrFail({
-			where: { guild_id: guild_id, name: "@everyone", position: 0 },
-		});
-
-		if (body.roles) {
-			permission.hasThrow("MANAGE_ROLES");
-
-			if (body.roles.indexOf(everyone.id) === -1)
-				body.roles.push(everyone.id);
-			member.roles = body.roles.map((x) => Role.create({ id: x })); // foreign key constraint will fail if role doesn't exist
-		}
-
-		if ("nick" in body) {
-			permission.hasThrow(
-				req.user_id == member.user.id
-					? "CHANGE_NICKNAME"
-					: "MANAGE_NICKNAMES",
-			);
-			member.nick = body.nick?.trim() || undefined;
-		}
-
-		await member.save();
-
-		member.roles = member.roles.filter((x) => x.id !== everyone.id);
-
-		// do not use promise.all as we have to first write to db before emitting the event to catch errors
-		await emitEvent({
-			event: "GUILD_MEMBER_UPDATE",
-			guild_id,
-			data: { ...member, roles: member.roles.map((x) => x.id) },
-		} as GuildMemberUpdateEvent);
-
-		res.json(member);
-	},
-);
+router.patch("/", route({ body: "MemberChangeSchema" }), async (req: Request, res: Response) => {
+	let { guild_id, member_id } = req.params;
+	if (member_id === "@me") member_id = req.user_id;
+	const body = req.body as MemberChangeSchema;
+
+	let member = await Member.findOneOrFail({ where: { id: member_id, guild_id }, relations: ["roles", "user"] });
+	const permission = await getPermission(req.user_id, guild_id);
+	const everyone = await Role.findOneOrFail({ where: { guild_id: guild_id, name: "@everyone", position: 0 } });
+
+	if (body.roles) {
+		permission.hasThrow("MANAGE_ROLES");
+
+		if (body.roles.indexOf(everyone.id) === -1) body.roles.push(everyone.id);
+		member.roles = body.roles.map((x) => Role.create({ id: x })); // foreign key constraint will fail if role doesn't exist
+	}
+
+	if (body.avatar) body.avatar = await handleFile(`/guilds/${guild_id}/users/${member_id}/avatars`, body.avatar as string);
+
+	member.assign(body);
+
+	await member.save();
+
+	member.roles = member.roles.filter((x) => x.id !== everyone.id);
+
+	// do not use promise.all as we have to first write to db before emitting the event to catch errors
+	await emitEvent({
+		event: "GUILD_MEMBER_UPDATE",
+		guild_id,
+		data: { ...member, roles: member.roles.map((x) => x.id) }
+	} as GuildMemberUpdateEvent);
+
+	res.json(member);
+});
 
 router.put("/", route({}), async (req: Request, res: Response) => {
 	// TODO: Lurker mode
diff --git a/src/api/routes/guilds/#guild_id/profile/index.ts b/src/api/routes/guilds/#guild_id/profile/index.ts
new file mode 100644
index 00000000..ddc30943
--- /dev/null
+++ b/src/api/routes/guilds/#guild_id/profile/index.ts
@@ -0,0 +1,30 @@
+import { route } from "@fosscord/api";
+import { emitEvent, GuildMemberUpdateEvent, handleFile, Member, MemberChangeProfileSchema, OrmUtils } from "@fosscord/util";
+import { Request, Response, Router } from "express";
+
+const router = Router();
+
+router.patch("/:member_id", route({ body: "MemberChangeProfileSchema" }), async (req: Request, res: Response) => {
+	let { guild_id, member_id } = req.params;
+	if (member_id === "@me") member_id = req.user_id;
+	const body = req.body as MemberChangeProfileSchema;
+
+	let member = await Member.findOneOrFail({ where: { id: req.user_id, guild_id }, relations: ["roles", "user"] });
+
+	if (body.banner) body.banner = await handleFile(`/guilds/${guild_id}/users/${req.user_id}/avatars`, body.banner as string);
+
+	member = await OrmUtils.mergeDeep(member, body);
+
+	await member.save();
+
+	// do not use promise.all as we have to first write to db before emitting the event to catch errors
+	await emitEvent({
+		event: "GUILD_MEMBER_UPDATE",
+		guild_id,
+		data: { ...member, roles: member.roles.map((x) => x.id) }
+	} as GuildMemberUpdateEvent);
+
+	res.json(member);
+});
+
+export default router;
diff --git a/src/api/routes/users/#id/profile.ts b/src/api/routes/users/#id/profile.ts
index ebea805b..45cc4412 100644
--- a/src/api/routes/users/#id/profile.ts
+++ b/src/api/routes/users/#id/profile.ts
@@ -6,6 +6,11 @@ import {
 	UserPublic,
 	Member,
 	Guild,
+	UserProfileModifySchema,
+	handleFile,
+	PrivateUserProjection,
+	emitEvent,
+	UserUpdateEvent,
 } from "@fosscord/util";
 import { route } from "@fosscord/api";
 
@@ -84,36 +89,66 @@ router.get(
 			bot: user.bot,
 		};
 
-		const guildMemberDto = guild_member
-			? {
-					avatar: user.avatar, // TODO
-					banner: user.banner, // TODO
-					bio: req.user_bot ? null : user.bio, // TODO
-					communication_disabled_until: null, // TODO
-					deaf: guild_member.deaf,
-					flags: user.flags,
-					is_pending: guild_member.pending,
-					pending: guild_member.pending, // why is this here twice, discord?
-					joined_at: guild_member.joined_at,
-					mute: guild_member.mute,
-					nick: guild_member.nick,
-					premium_since: guild_member.premium_since,
-					roles: guild_member.roles
-						.map((x) => x.id)
-						.filter((id) => id != guild_id),
-					user: userDto,
-			  }
-			: undefined;
-
-		res.json({
-			connected_accounts: user.connected_accounts,
-			premium_guild_since: premium_guild_since, // TODO
-			premium_since: user.premium_since, // TODO
-			mutual_guilds: mutual_guilds, // TODO {id: "", nick: null} when ?with_mutual_guilds=true
-			user: userDto,
-			guild_member: guildMemberDto,
-		});
-	},
-);
+	const guildMemberDto = guild_member
+		? {
+				avatar: guild_member.avatar,
+				banner: guild_member.banner,
+				bio: req.user_bot ? null : guild_member.bio,
+				communication_disabled_until: guild_member.communication_disabled_until,
+				deaf: guild_member.deaf,
+				flags: user.flags,
+				is_pending: guild_member.pending,
+				pending: guild_member.pending, // why is this here twice, discord?
+				joined_at: guild_member.joined_at,
+				mute: guild_member.mute,
+				nick: guild_member.nick,
+				premium_since: guild_member.premium_since,
+				roles: guild_member.roles.map((x) => x.id).filter((id) => id != guild_id),
+				user: userDto
+		  }
+		: undefined;
+
+	const guildMemberProfile = {
+		accent_color: null,
+		banner: guild_member?.banner || null,
+		bio: guild_member?.bio || "",
+		guild_id
+	};
+	res.json({
+		connected_accounts: user.connected_accounts,
+		premium_guild_since: premium_guild_since, // TODO
+		premium_since: user.premium_since, // TODO
+		mutual_guilds: mutual_guilds, // TODO {id: "", nick: null} when ?with_mutual_guilds=true
+		user: userDto,
+		guild_member: guildMemberDto,
+		guild_member_profile: guildMemberProfile
+	});
+});
+
+router.patch("/", route({ body: "UserProfileModifySchema" }), async (req: Request, res: Response) => {
+	const body = req.body as UserProfileModifySchema;
+
+	if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string);
+	let user = await User.findOneOrFail({ where: { id: req.user_id }, select: [...PrivateUserProjection, "data"] });
+
+	user.assign(body);
+	await user.save();
+
+	// @ts-ignore
+	delete user.data;
+
+	// TODO: send update member list event in gateway
+	await emitEvent({
+		event: "USER_UPDATE",
+		user_id: req.user_id,
+		data: user
+	} as UserUpdateEvent);
+
+	res.json({
+		accent_color: user.accent_color,
+		bio: user.bio,
+		banner: user.banner
+	});
+});
 
 export default router;