summary refs log tree commit diff
path: root/src/api/routes/users/#id
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@protonmail.com>2022-08-29 11:11:40 -0400
committerPuyodead1 <puyodead@protonmail.com>2022-08-29 11:11:40 -0400
commitc2aba2910cb50211a91a057863ef0bd0497ceead (patch)
tree8fa7c143dd0f397ede278f688dc3d86c5195815e /src/api/routes/users/#id
parentOop, deprecated typeorm call (diff)
downloadserver-c2aba2910cb50211a91a057863ef0bd0497ceead.tar.xz
implement guild profiles and fix user profiles
Diffstat (limited to 'src/api/routes/users/#id')
-rw-r--r--src/api/routes/users/#id/profile.ts56
1 files changed, 50 insertions, 6 deletions
diff --git a/src/api/routes/users/#id/profile.ts b/src/api/routes/users/#id/profile.ts
index 766c9880..0b9107e2 100644
--- a/src/api/routes/users/#id/profile.ts
+++ b/src/api/routes/users/#id/profile.ts
@@ -1,5 +1,16 @@
 import { route } from "@fosscord/api";
-import { Member, PublicConnectedAccount, User, UserPublic } from "@fosscord/util";
+import {
+	emitEvent,
+	handleFile,
+	Member,
+	OrmUtils,
+	PrivateUserProjection,
+	PublicConnectedAccount,
+	User,
+	UserProfileModifySchema,
+	UserPublic,
+	UserUpdateEvent
+} from "@fosscord/util";
 import { Request, Response, Router } from "express";
 
 const router: Router = Router();
@@ -64,10 +75,10 @@ router.get("/", route({ test: { response: { body: "UserProfileResponse" } } }),
 
 	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
+				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,
@@ -81,13 +92,46 @@ router.get("/", route({ test: { response: { body: "UserProfileResponse" } } }),
 		  }
 		: 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: 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 = OrmUtils.mergeDeep(user, 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
 	});
 });