From b84aa73852ae1267c9e193cfa6ee365c69d16803 Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Mon, 29 Aug 2022 11:11:40 -0400 Subject: implement guild profiles and fix user profiles --- src/api/routes/guilds/#guild_id/profile/index.ts | 30 ++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/api/routes/guilds/#guild_id/profile/index.ts (limited to 'src/api/routes/guilds/#guild_id/profile') 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; -- cgit 1.4.1