diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-01-05 17:12:21 +1100 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-01-05 17:16:55 +1100 |
commit | df449169bde6c0576757700f68f2d406139cc846 (patch) | |
tree | f8bcbaf30ed3ed007d392a51614a134e543e3c17 /src/api/routes/guilds | |
parent | channel flags whoops (diff) | |
download | server-df449169bde6c0576757700f68f2d406139cc846.tar.xz |
Prettier
Diffstat (limited to 'src/api/routes/guilds')
-rw-r--r-- | src/api/routes/guilds/#guild_id/index.ts | 16 | ||||
-rw-r--r-- | src/api/routes/guilds/#guild_id/members/#member_id/index.ts | 86 | ||||
-rw-r--r-- | src/api/routes/guilds/#guild_id/messages/search.ts | 14 | ||||
-rw-r--r-- | src/api/routes/guilds/#guild_id/profile/index.ts | 64 | ||||
-rw-r--r-- | src/api/routes/guilds/#guild_id/roles/#role_id/index.ts | 8 | ||||
-rw-r--r-- | src/api/routes/guilds/#guild_id/roles/index.ts | 10 |
6 files changed, 126 insertions, 72 deletions
diff --git a/src/api/routes/guilds/#guild_id/index.ts b/src/api/routes/guilds/#guild_id/index.ts index 1e976293..79c20678 100644 --- a/src/api/routes/guilds/#guild_id/index.ts +++ b/src/api/routes/guilds/#guild_id/index.ts @@ -69,15 +69,21 @@ router.patch( body.splash, ); - if (body.discovery_splash && body.discovery_splash !== guild.discovery_splash) + if ( + body.discovery_splash && + body.discovery_splash !== guild.discovery_splash + ) body.discovery_splash = await handleFile( `/discovery-splashes/${guild_id}`, body.discovery_splash, ); if (body.features) { - const diff = guild.features.filter(x => !body.features?.includes(x)) - .concat(body.features.filter(x => !guild.features.includes(x))); + const diff = guild.features + .filter((x) => !body.features?.includes(x)) + .concat( + body.features.filter((x) => !guild.features.includes(x)), + ); // TODO move these const MUTABLE_FEATURES = [ @@ -89,7 +95,9 @@ router.patch( for (var feature of diff) { if (MUTABLE_FEATURES.includes(feature)) continue; - throw FosscordApiErrors.FEATURE_IS_IMMUTABLE.withParams(feature); + throw FosscordApiErrors.FEATURE_IS_IMMUTABLE.withParams( + feature, + ); } // for some reason, they don't update in the assign. 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 f1d343d9..0fcdd57c 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 @@ -27,42 +27,56 @@ 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; - - 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.avatar) body.avatar = await handleFile(`/guilds/${guild_id}/users/${member_id}/avatars`, body.avatar as string); - - member.assign(body); - - if ('roles' in body) { - permission.hasThrow("MANAGE_ROLES"); - - body.roles = body.roles || []; - body.roles.filter(x => !!x); - - 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 - } - - 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.avatar) + body.avatar = await handleFile( + `/guilds/${guild_id}/users/${member_id}/avatars`, + body.avatar as string, + ); + + member.assign(body); + + if ("roles" in body) { + permission.hasThrow("MANAGE_ROLES"); + + body.roles = body.roles || []; + body.roles.filter((x) => !!x); + + 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 + } + + 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/messages/search.ts b/src/api/routes/guilds/#guild_id/messages/search.ts index ccee59f7..88488871 100644 --- a/src/api/routes/guilds/#guild_id/messages/search.ts +++ b/src/api/routes/guilds/#guild_id/messages/search.ts @@ -72,12 +72,20 @@ router.get("/", route({}), async (req: Request, res: Response) => { if (channel_id) query.where!.channel = { id: channel_id }; else { // get all channel IDs that this user can access - const channels = await Channel.find({ where: { guild_id: req.params.guild_id }, select: ["id"] }); + const channels = await Channel.find({ + where: { guild_id: req.params.guild_id }, + select: ["id"], + }); const ids = []; for (var channel of channels) { - const perm = await getPermission(req.user_id, req.params.guild_id, channel.id); - if (!perm.has("VIEW_CHANNEL") || !perm.has("READ_MESSAGE_HISTORY")) continue; + const perm = await getPermission( + req.user_id, + req.params.guild_id, + channel.id, + ); + if (!perm.has("VIEW_CHANNEL") || !perm.has("READ_MESSAGE_HISTORY")) + continue; ids.push(channel.id); } diff --git a/src/api/routes/guilds/#guild_id/profile/index.ts b/src/api/routes/guilds/#guild_id/profile/index.ts index ddc30943..20a7fa95 100644 --- a/src/api/routes/guilds/#guild_id/profile/index.ts +++ b/src/api/routes/guilds/#guild_id/profile/index.ts @@ -1,30 +1,48 @@ import { route } from "@fosscord/api"; -import { emitEvent, GuildMemberUpdateEvent, handleFile, Member, MemberChangeProfileSchema, OrmUtils } from "@fosscord/util"; +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); -}); +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/guilds/#guild_id/roles/#role_id/index.ts b/src/api/routes/guilds/#guild_id/roles/#role_id/index.ts index cd5959ff..84648703 100644 --- a/src/api/routes/guilds/#guild_id/roles/#role_id/index.ts +++ b/src/api/routes/guilds/#guild_id/roles/#role_id/index.ts @@ -63,12 +63,14 @@ router.patch( ); else body.icon = undefined; - const role = await Role.findOneOrFail({ where: { id: role_id, guild: { id: guild_id } } }); + const role = await Role.findOneOrFail({ + where: { id: role_id, guild: { id: guild_id } }, + }); role.assign({ ...body, permissions: String( - req.permission!.bitfield & BigInt(body.permissions || "0") - ) + req.permission!.bitfield & BigInt(body.permissions || "0"), + ), }); await Promise.all([ diff --git a/src/api/routes/guilds/#guild_id/roles/index.ts b/src/api/routes/guilds/#guild_id/roles/index.ts index 534a5967..4cd47cf3 100644 --- a/src/api/routes/guilds/#guild_id/roles/index.ts +++ b/src/api/routes/guilds/#guild_id/roles/index.ts @@ -61,9 +61,13 @@ router.post( await Promise.all([ role.save(), // Move all existing roles up one position, to accommodate the new role - Role.createQueryBuilder('roles') - .where({ guild: { id: guild_id }, name: Not("@everyone"), id: Not(role.id) }) - .update({ position: () => 'position + 1' }) + Role.createQueryBuilder("roles") + .where({ + guild: { id: guild_id }, + name: Not("@everyone"), + id: Not(role.id), + }) + .update({ position: () => "position + 1" }) .execute(), emitEvent({ event: "GUILD_ROLE_CREATE", |