diff options
author | Emma [it/its]@Rory& <root@rory.gay> | 2023-12-11 01:12:54 +0100 |
---|---|---|
committer | Emma [it/its]@Rory& <root@rory.gay> | 2023-12-11 01:12:54 +0100 |
commit | 0a8ceb9e6349284e75545a01ffad608b020f78e2 (patch) | |
tree | 17a9163f963eddabf9168b0b630096b2f7535b64 /src/api/routes/guilds | |
parent | Prettier: use editorconfig (diff) | |
download | server-dev/emma-refactors.tar.xz |
Actually run prettier dev/emma-refactors
Diffstat (limited to 'src/api/routes/guilds')
29 files changed, 238 insertions, 504 deletions
diff --git a/src/api/routes/guilds/#guild_id/bans.ts b/src/api/routes/guilds/#guild_id/bans.ts index 0776ab62..1fcc6806 100644 --- a/src/api/routes/guilds/#guild_id/bans.ts +++ b/src/api/routes/guilds/#guild_id/bans.ts @@ -78,7 +78,7 @@ router.get( }); return res.json(bansObj); - }, + } ); router.get( @@ -115,7 +115,7 @@ router.get( delete ban.ip; return res.json(ban); - }, + } ); router.put( @@ -139,14 +139,8 @@ router.put( const { guild_id } = req.params; const banned_user_id = req.params.user_id; - if ( - req.user_id === banned_user_id && - banned_user_id === req.permission?.cache.guild?.owner_id - ) - throw new HTTPError( - "You are the guild owner, hence can't ban yourself", - 403, - ); + if (req.user_id === banned_user_id && banned_user_id === req.permission?.cache.guild?.owner_id) + throw new HTTPError("You are the guild owner, hence can't ban yourself", 403); if (req.permission?.cache.guild?.owner_id === banned_user_id) throw new HTTPError("You can't ban the owner", 400); @@ -175,7 +169,7 @@ router.put( ]); return res.json(ban); - }, + } ); router.put( @@ -200,10 +194,7 @@ router.put( const banned_user = await User.getPublicUser(req.params.user_id); if (req.permission?.cache.guild?.owner_id === req.params.user_id) - throw new HTTPError( - "You are the guild owner, hence can't ban yourself", - 403, - ); + throw new HTTPError("You are the guild owner, hence can't ban yourself", 403); const ban = Ban.create({ user_id: req.params.user_id, @@ -227,7 +218,7 @@ router.put( ]); return res.json(ban); - }, + } ); router.delete( @@ -273,7 +264,7 @@ router.delete( ]); return res.status(204).send(); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/channels.ts b/src/api/routes/guilds/#guild_id/channels.ts index 68208fee..0ad3e252 100644 --- a/src/api/routes/guilds/#guild_id/channels.ts +++ b/src/api/routes/guilds/#guild_id/channels.ts @@ -42,7 +42,7 @@ router.get( const channels = await Channel.find({ where: { guild_id } }); res.json(channels); - }, + } ); router.post( @@ -67,13 +67,10 @@ router.post( const { guild_id } = req.params; const body = req.body as ChannelModifySchema; - const channel = await Channel.createChannel( - { ...body, guild_id }, - req.user_id, - ); + const channel = await Channel.createChannel({ ...body, guild_id }, req.user_id); res.status(201).json(channel); - }, + } ); router.patch( @@ -102,9 +99,7 @@ router.patch( }); // The channels not listed for this query - const notMentioned = guild.channel_ordering.filter( - (x) => !body.find((c) => c.id == x), - ); + const notMentioned = guild.channel_ordering.filter((x) => !body.find((c) => c.id == x)); const withParents = body.filter((x) => x.parent_id != undefined); const withPositions = body.filter((x) => x.position != undefined); @@ -124,7 +119,7 @@ router.patch( channel_id: channel.id, guild_id, } as ChannelUpdateEvent); - }), + }) ); // have to do the parents after the positions @@ -141,10 +136,7 @@ router.patch( ]); if (opt.lock_permissions) - await Channel.update( - { id: channel.id }, - { permission_overwrites: parent.permission_overwrites }, - ); + await Channel.update({ id: channel.id }, { permission_overwrites: parent.permission_overwrites }); const parentPos = notMentioned.indexOf(parent.id); notMentioned.splice(parentPos + 1, 0, channel.id); @@ -156,16 +148,13 @@ router.patch( channel_id: channel.id, guild_id, } as ChannelUpdateEvent); - }), + }) ); - await Guild.update( - { id: guild_id }, - { channel_ordering: notMentioned }, - ); + await Guild.update({ id: guild_id }, { channel_ordering: notMentioned }); return res.sendStatus(204); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/delete.ts b/src/api/routes/guilds/#guild_id/delete.ts index dee52c81..9f874a5c 100644 --- a/src/api/routes/guilds/#guild_id/delete.ts +++ b/src/api/routes/guilds/#guild_id/delete.ts @@ -45,8 +45,7 @@ router.post( where: { id: guild_id }, select: ["owner_id"], }); - if (guild.owner_id !== req.user_id) - throw new HTTPError("You are not the owner of this guild", 401); + if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401); await Promise.all([ Guild.delete({ id: guild_id }), // this will also delete all guild related data @@ -60,7 +59,7 @@ router.post( ]); return res.sendStatus(204); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/discovery-requirements.ts b/src/api/routes/guilds/#guild_id/discovery-requirements.ts index 741fa9b3..6de571c5 100644 --- a/src/api/routes/guilds/#guild_id/discovery-requirements.ts +++ b/src/api/routes/guilds/#guild_id/discovery-requirements.ts @@ -59,7 +59,7 @@ router.get( }, minimum_size: 0, }); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/emojis.ts b/src/api/routes/guilds/#guild_id/emojis.ts index ef28f989..fa821cf0 100644 --- a/src/api/routes/guilds/#guild_id/emojis.ts +++ b/src/api/routes/guilds/#guild_id/emojis.ts @@ -57,7 +57,7 @@ router.get( }); return res.json(emojis); - }, + } ); router.get( @@ -86,7 +86,7 @@ router.get( }); return res.json(emoji); - }, + } ); router.post( @@ -116,10 +116,7 @@ router.post( }); const { maxEmojis } = Config.get().limits.guild; - if (emoji_count >= maxEmojis) - throw DiscordApiErrors.MAXIMUM_NUMBER_OF_EMOJIS_REACHED.withParams( - maxEmojis, - ); + if (emoji_count >= maxEmojis) throw DiscordApiErrors.MAXIMUM_NUMBER_OF_EMOJIS_REACHED.withParams(maxEmojis); if (body.require_colons == null) body.require_colons = true; const user = await User.findOneOrFail({ where: { id: req.user_id } }); @@ -147,7 +144,7 @@ router.post( } as GuildEmojisUpdateEvent); return res.status(201).json(emoji); - }, + } ); router.patch( @@ -184,7 +181,7 @@ router.patch( } as GuildEmojisUpdateEvent); return res.json(emoji); - }, + } ); router.delete( @@ -216,7 +213,7 @@ router.delete( } as GuildEmojisUpdateEvent); res.sendStatus(204); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/index.ts b/src/api/routes/guilds/#guild_id/index.ts index 839ec363..87388abc 100644 --- a/src/api/routes/guilds/#guild_id/index.ts +++ b/src/api/routes/guilds/#guild_id/index.ts @@ -58,17 +58,13 @@ router.get( Guild.findOneOrFail({ where: { id: guild_id } }), Member.findOne({ where: { guild_id: guild_id, id: req.user_id } }), ]); - if (!member) - throw new HTTPError( - "You are not a member of the guild you are trying to access", - 401, - ); + if (!member) throw new HTTPError("You are not a member of the guild you are trying to access", 401); return res.send({ ...guild, joined_at: member?.joined_at, }); - }, + } ); router.patch( @@ -99,9 +95,7 @@ router.patch( const permission = await getPermission(req.user_id, guild_id); if (!rights.has("MANAGE_GUILDS") && !permission.has("MANAGE_GUILD")) - throw DiscordApiErrors.MISSING_PERMISSIONS.withParams( - "MANAGE_GUILDS", - ); + throw DiscordApiErrors.MISSING_PERMISSIONS.withParams("MANAGE_GUILDS"); const guild = await Guild.findOneOrFail({ where: { id: guild_id }, @@ -110,47 +104,29 @@ router.patch( // TODO: guild update check image - if (body.icon && body.icon != guild.icon) - body.icon = await handleFile(`/icons/${guild_id}`, body.icon); + if (body.icon && body.icon != guild.icon) body.icon = await handleFile(`/icons/${guild_id}`, body.icon); if (body.banner && body.banner !== guild.banner) body.banner = await handleFile(`/banners/${guild_id}`, body.banner); if (body.splash && body.splash !== guild.splash) - body.splash = await handleFile( - `/splashes/${guild_id}`, - body.splash, - ); + body.splash = await handleFile(`/splashes/${guild_id}`, body.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.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)), - ); + .concat(body.features.filter((x) => !guild.features.includes(x))); // TODO move these - const MUTABLE_FEATURES = [ - "COMMUNITY", - "INVITES_DISABLED", - "DISCOVERABLE", - ]; + const MUTABLE_FEATURES = ["COMMUNITY", "INVITES_DISABLED", "DISCOVERABLE"]; for (const feature of diff) { if (MUTABLE_FEATURES.includes(feature)) continue; - throw SpacebarApiErrors.FEATURE_IS_IMMUTABLE.withParams( - feature, - ); + throw SpacebarApiErrors.FEATURE_IS_IMMUTABLE.withParams(feature); } // for some reason, they don't update in the assign. @@ -179,7 +155,7 @@ router.patch( ], }, undefined, - { skipPermissionCheck: true }, + { skipPermissionCheck: true } ); await Guild.insertChannelInOrder(guild.id, channel.id, 0, guild); @@ -212,7 +188,7 @@ router.patch( ], }, undefined, - { skipPermissionCheck: true }, + { skipPermissionCheck: true } ); await Guild.insertChannelInOrder(guild.id, channel.id, 0, guild); @@ -242,7 +218,7 @@ router.patch( ]); return res.json(data); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/invites.ts b/src/api/routes/guilds/#guild_id/invites.ts index a0ffa3f4..5311a77f 100644 --- a/src/api/routes/guilds/#guild_id/invites.ts +++ b/src/api/routes/guilds/#guild_id/invites.ts @@ -41,7 +41,7 @@ router.get( }); return res.json(invites); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/member-verification.ts b/src/api/routes/guilds/#guild_id/member-verification.ts index 2c39093e..b30c379d 100644 --- a/src/api/routes/guilds/#guild_id/member-verification.ts +++ b/src/api/routes/guilds/#guild_id/member-verification.ts @@ -36,7 +36,7 @@ router.get( message: "Unknown Guild Member Verification Form", code: 10068, }); - }, + } ); export default router; 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 c168f2dc..5dd6f2b8 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 @@ -62,13 +62,9 @@ router.get( select: { index: true, // only grab public member props - ...Object.fromEntries( - PublicMemberProjection.map((x) => [x, true]), - ), + ...Object.fromEntries(PublicMemberProjection.map((x) => [x, true])), // and public user props - user: Object.fromEntries( - PublicUserProjection.map((x) => [x, true]), - ), + user: Object.fromEntries(PublicUserProjection.map((x) => [x, true])), roles: { id: true, }, @@ -80,7 +76,7 @@ router.get( user: member.user.toPublicUser(), roles: member.roles.map((x) => x.id), }); - }, + } ); router.patch( @@ -104,8 +100,7 @@ router.patch( }), async (req: Request, res: Response) => { const { guild_id } = req.params; - const member_id = - req.params.member_id === "@me" ? req.user_id : req.params.member_id; + const member_id = req.params.member_id === "@me" ? req.user_id : req.params.member_id; const body = req.body as MemberChangeSchema; const member = await Member.findOneOrFail({ @@ -128,19 +123,13 @@ router.patch( } } - if ( - ("bio" in body || "avatar" in body) && - req.params.member_id != "@me" - ) { + if (("bio" in body || "avatar" in body) && req.params.member_id != "@me") { const rights = await getRights(req.user_id); rights.hasThrow("MANAGE_USERS"); } if (body.avatar) - body.avatar = await handleFile( - `/guilds/${guild_id}/users/${member_id}/avatars`, - body.avatar as string, - ); + body.avatar = await handleFile(`/guilds/${guild_id}/users/${member_id}/avatars`, body.avatar as string); member.assign(body); @@ -152,8 +141,7 @@ router.patch( body.roles = body.roles || []; body.roles.filter((x) => !!x); - if (body.roles.indexOf(everyone.id) === -1) - body.roles.push(everyone.id); + if (body.roles.indexOf(everyone.id) === -1) body.roles.push(everyone.id); // foreign key constraint will fail if role doesn't exist member.roles = body.roles.map((x) => Role.create({ id: x })); } @@ -170,7 +158,7 @@ router.patch( } as GuildMemberUpdateEvent); res.json(member); - }, + } ); router.put( @@ -222,7 +210,7 @@ router.put( await Member.addToGuild(member_id, guild_id); res.send({ ...guild, emojis: emoji, roles: roles, stickers: stickers }); - }, + } ); router.delete( @@ -249,7 +237,7 @@ router.delete( await Member.removeFromGuild(member_id, guild_id); res.sendStatus(204); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/members/#member_id/nick.ts b/src/api/routes/guilds/#guild_id/members/#member_id/nick.ts index 7b8e44d3..8162f36b 100644 --- a/src/api/routes/guilds/#guild_id/members/#member_id/nick.ts +++ b/src/api/routes/guilds/#guild_id/members/#member_id/nick.ts @@ -49,7 +49,7 @@ router.patch( await Member.changeNickname(member_id, guild_id, req.body.nick); res.status(200).send(); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts b/src/api/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts index 46dd70bb..da28e312 100644 --- a/src/api/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts +++ b/src/api/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts @@ -38,7 +38,7 @@ router.delete( await Member.removeRole(member_id, guild_id, role_id); res.sendStatus(204); - }, + } ); router.put( @@ -55,7 +55,7 @@ router.put( await Member.addRole(member_id, guild_id, role_id); res.sendStatus(204); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/members/index.ts b/src/api/routes/guilds/#guild_id/members/index.ts index 9260308d..8ea590d0 100644 --- a/src/api/routes/guilds/#guild_id/members/index.ts +++ b/src/api/routes/guilds/#guild_id/members/index.ts @@ -33,8 +33,7 @@ router.get( query: { limit: { type: "number", - description: - "max number of members to return (1-1000). default 1", + description: "max number of members to return (1-1000). default 1", }, after: { type: "string", @@ -52,8 +51,7 @@ router.get( async (req: Request, res: Response) => { const { guild_id } = req.params; const limit = Number(req.query.limit) || 1; - if (limit > 1000 || limit < 1) - throw new HTTPError("Limit must be between 1 and 1000"); + if (limit > 1000 || limit < 1) throw new HTTPError("Limit must be between 1 and 1000"); const after = `${req.query.after}`; const query = after ? { id: MoreThan(after) } : {}; @@ -67,7 +65,7 @@ router.get( }); return res.json(members); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/messages/search.ts b/src/api/routes/guilds/#guild_id/messages/search.ts index 637d1e43..aec36f85 100644 --- a/src/api/routes/guilds/#guild_id/messages/search.ts +++ b/src/api/routes/guilds/#guild_id/messages/search.ts @@ -54,14 +54,10 @@ router.get( } = req.query; const parsedLimit = Number(limit) || 50; - if (parsedLimit < 1 || parsedLimit > 100) - throw new HTTPError("limit must be between 1 and 100", 422); + if (parsedLimit < 1 || parsedLimit > 100) throw new HTTPError("limit must be between 1 and 100", 422); if (sort_order) { - if ( - typeof sort_order != "string" || - ["desc", "asc"].indexOf(sort_order) == -1 - ) + if (typeof sort_order != "string" || ["desc", "asc"].indexOf(sort_order) == -1) throw FieldErrors({ sort_order: { message: "Value must be one of ('desc', 'asc').", @@ -70,20 +66,13 @@ router.get( }); // todo this is wrong } - const permissions = await getPermission( - req.user_id, - req.params.guild_id, - channel_id as string | undefined, - ); + const permissions = await getPermission(req.user_id, req.params.guild_id, channel_id as string | undefined); permissions.hasThrow("VIEW_CHANNEL"); - if (!permissions.has("READ_MESSAGE_HISTORY")) - return res.json({ messages: [], total_results: 0 }); + if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json({ messages: [], total_results: 0 }); const query: FindManyOptions<Message> = { order: { - timestamp: sort_order - ? (sort_order.toUpperCase() as "ASC" | "DESC") - : "DESC", + timestamp: sort_order ? (sort_order.toUpperCase() as "ASC" | "DESC") : "DESC", }, take: parsedLimit || 0, where: { @@ -114,16 +103,8 @@ router.get( const ids = []; for (const 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); } @@ -170,7 +151,7 @@ router.get( messages: messagesDto, total_results: messages.length, }); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/profile/index.ts b/src/api/routes/guilds/#guild_id/profile/index.ts index 60526259..e34136a4 100644 --- a/src/api/routes/guilds/#guild_id/profile/index.ts +++ b/src/api/routes/guilds/#guild_id/profile/index.ts @@ -57,10 +57,7 @@ router.patch( }); if (body.banner) - body.banner = await handleFile( - `/guilds/${guild_id}/users/${req.user_id}/avatars`, - body.banner as string, - ); + body.banner = await handleFile(`/guilds/${guild_id}/users/${req.user_id}/avatars`, body.banner as string); member = await OrmUtils.mergeDeep(member, body); @@ -74,7 +71,7 @@ router.patch( } as GuildMemberUpdateEvent); res.json(member); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/prune.ts b/src/api/routes/guilds/#guild_id/prune.ts index 2c77340d..7fa31ed2 100644 --- a/src/api/routes/guilds/#guild_id/prune.ts +++ b/src/api/routes/guilds/#guild_id/prune.ts @@ -23,12 +23,7 @@ import { IsNull, LessThan } from "typeorm"; const router = Router(); //Returns all inactive members, respecting role hierarchy -const inactiveMembers = async ( - guild_id: string, - user_id: string, - days: number, - roles: string[] = [], -) => { +const inactiveMembers = async (guild_id: string, user_id: string, days: number, roles: string[] = []) => { const date = new Date(); date.setDate(date.getDate() - days); //Snowflake should have `generateFromTime` method? Or similar? @@ -55,9 +50,7 @@ const inactiveMembers = async ( //I'm sure I can do this in the above db query ( and it would probably be better to do so ), but oh well. if (roles.length && members.length) - members = members.filter((user) => - user.roles?.some((role) => roles.includes(role.id)), - ); + members = members.filter((user) => user.roles?.some((role) => roles.includes(role.id))); const me = await Member.findOneOrFail({ where: { id: user_id, guild_id }, @@ -73,8 +66,8 @@ const inactiveMembers = async ( member.roles?.some( (role) => role.position < myHighestRole || //roles higher than me can't be kicked - me.id === guild.owner_id, //owner can kick anyone - ), + me.id === guild.owner_id //owner can kick anyone + ) ); return members; @@ -95,15 +88,10 @@ router.get( let roles = req.query.include_roles; if (typeof roles === "string") roles = [roles]; //express will return array otherwise - const members = await inactiveMembers( - req.params.guild_id, - req.user_id, - days, - roles as string[], - ); + const members = await inactiveMembers(req.params.guild_id, req.user_id, days, roles as string[]); res.send({ pruned: members.length }); - }, + } ); router.post( @@ -127,19 +115,12 @@ router.post( if (typeof roles === "string") roles = [roles]; const { guild_id } = req.params; - const members = await inactiveMembers( - guild_id, - req.user_id, - days, - roles as string[], - ); + const members = await inactiveMembers(guild_id, req.user_id, days, roles as string[]); - await Promise.all( - members.map((x) => Member.removeFromGuild(x.id, guild_id)), - ); + await Promise.all(members.map((x) => Member.removeFromGuild(x.id, guild_id))); res.send({ purged: members.length }); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/regions.ts b/src/api/routes/guilds/#guild_id/regions.ts index b0ae0602..e40a73e2 100644 --- a/src/api/routes/guilds/#guild_id/regions.ts +++ b/src/api/routes/guilds/#guild_id/regions.ts @@ -38,13 +38,8 @@ router.get( const { guild_id } = req.params; const guild = await Guild.findOneOrFail({ where: { id: guild_id } }); //TODO we should use an enum for guild's features and not hardcoded strings - return res.json( - await getVoiceRegions( - getIpAdress(req), - guild.features.includes("VIP_REGIONS"), - ), - ); - }, + return res.json(await getVoiceRegions(getIpAdress(req), guild.features.includes("VIP_REGIONS"))); + } ); 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 ea1a782a..ae10addf 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 @@ -53,7 +53,7 @@ router.get( where: { guild_id, id: role_id }, }); return res.json(role); - }, + } ); router.delete( @@ -75,8 +75,7 @@ router.delete( }), async (req: Request, res: Response) => { const { guild_id, role_id } = req.params; - if (role_id === guild_id) - throw new HTTPError("You can't delete the @everyone role"); + if (role_id === guild_id) throw new HTTPError("You can't delete the @everyone role"); await Promise.all([ Role.delete({ @@ -94,7 +93,7 @@ router.delete( ]); res.sendStatus(204); - }, + } ); // TODO: check role hierarchy @@ -123,11 +122,7 @@ router.patch( const { role_id, guild_id } = req.params; const body = req.body as RoleModifySchema; - if (body.icon && body.icon.length) - body.icon = await handleFile( - `/role-icons/${role_id}`, - body.icon as string, - ); + if (body.icon && body.icon.length) body.icon = await handleFile(`/role-icons/${role_id}`, body.icon as string); else body.icon = undefined; const role = await Role.findOneOrFail({ @@ -135,10 +130,7 @@ router.patch( }); role.assign({ ...body, - permissions: String( - (req.permission?.bitfield || 0n) & - BigInt(body.permissions || "0"), - ), + permissions: String((req.permission?.bitfield || 0n) & BigInt(body.permissions || "0")), }); await Promise.all([ @@ -154,7 +146,7 @@ router.patch( ]); res.json(role); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/roles/#role_id/members.ts b/src/api/routes/guilds/#guild_id/roles/#role_id/members.ts index 539cd5d8..a34908a6 100644 --- a/src/api/routes/guilds/#guild_id/roles/#role_id/members.ts +++ b/src/api/routes/guilds/#guild_id/roles/#role_id/members.ts @@ -22,41 +22,31 @@ import { route } from "@spacebar/api"; const router = Router(); -router.patch( - "/", - route({ permission: "MANAGE_ROLES" }), - async (req: Request, res: Response) => { - // Payload is JSON containing a list of member_ids, the new list of members to have the role - const { guild_id, role_id } = req.params; - const { member_ids } = req.body; - - // don't mess with @everyone - if (role_id == guild_id) throw DiscordApiErrors.INVALID_ROLE; - - const members = await Member.find({ - where: { guild_id }, - relations: ["roles"], - }); - - const [add, remove] = partition( - members, - (member) => - member_ids.includes(member.id) && - !member.roles.map((role) => role.id).includes(role_id), - ); - - // TODO (erkin): have a bulk add/remove function that adds the roles in a single txn - await Promise.all([ - ...add.map((member) => - Member.addRole(member.id, guild_id, role_id), - ), - ...remove.map((member) => - Member.removeRole(member.id, guild_id, role_id), - ), - ]); - - res.sendStatus(204); - }, -); +router.patch("/", route({ permission: "MANAGE_ROLES" }), async (req: Request, res: Response) => { + // Payload is JSON containing a list of member_ids, the new list of members to have the role + const { guild_id, role_id } = req.params; + const { member_ids } = req.body; + + // don't mess with @everyone + if (role_id == guild_id) throw DiscordApiErrors.INVALID_ROLE; + + const members = await Member.find({ + where: { guild_id }, + relations: ["roles"], + }); + + const [add, remove] = partition( + members, + (member) => member_ids.includes(member.id) && !member.roles.map((role) => role.id).includes(role_id) + ); + + // TODO (erkin): have a bulk add/remove function that adds the roles in a single txn + await Promise.all([ + ...add.map((member) => Member.addRole(member.id, guild_id, role_id)), + ...remove.map((member) => Member.removeRole(member.id, guild_id, role_id)), + ]); + + res.sendStatus(204); +}); export default router; diff --git a/src/api/routes/guilds/#guild_id/roles/index.ts b/src/api/routes/guilds/#guild_id/roles/index.ts index e2c34e7f..1e60c2cf 100644 --- a/src/api/routes/guilds/#guild_id/roles/index.ts +++ b/src/api/routes/guilds/#guild_id/roles/index.ts @@ -68,8 +68,7 @@ router.post( const role_count = await Role.count({ where: { guild_id } }); const { maxRoles } = Config.get().limits.guild; - if (role_count > maxRoles) - throw DiscordApiErrors.MAXIMUM_ROLES.withParams(maxRoles); + if (role_count > maxRoles) throw DiscordApiErrors.MAXIMUM_ROLES.withParams(maxRoles); const role = Role.create({ // values before ...body are default and can be overriden @@ -80,10 +79,7 @@ router.post( ...body, guild_id: guild_id, managed: false, - permissions: String( - (req.permission?.bitfield || 0n) & - BigInt(body.permissions || "0"), - ), + permissions: String((req.permission?.bitfield || 0n) & BigInt(body.permissions || "0")), tags: undefined, icon: undefined, unicode_emoji: undefined, @@ -112,7 +108,7 @@ router.post( ]); res.json(role); - }, + } ); router.patch( @@ -136,11 +132,7 @@ router.patch( const { guild_id } = req.params; const body = req.body as RolePositionUpdateSchema; - await Promise.all( - body.map(async (x) => - Role.update({ guild_id, id: x.id }, { position: x.position }), - ), - ); + await Promise.all(body.map(async (x) => Role.update({ guild_id, id: x.id }, { position: x.position }))); const roles = await Role.find({ where: body.map((x) => ({ id: x.id, guild_id })), @@ -155,12 +147,12 @@ router.patch( guild_id, role: x, }, - } as GuildRoleUpdateEvent), - ), + } as GuildRoleUpdateEvent) + ) ); res.json(roles); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/stickers.ts b/src/api/routes/guilds/#guild_id/stickers.ts index 88f9a40e..92b09aab 100644 --- a/src/api/routes/guilds/#guild_id/stickers.ts +++ b/src/api/routes/guilds/#guild_id/stickers.ts @@ -50,7 +50,7 @@ router.get( await Member.IsInGuildOrFail(req.user_id, guild_id); res.json(await Sticker.find({ where: { guild_id } })); - }, + } ); const bodyParser = multer({ @@ -102,7 +102,7 @@ router.post( await sendStickerUpdateEvent(guild_id); res.json(sticker); - }, + } ); function getStickerFormat(mime_type: string) { @@ -116,9 +116,7 @@ function getStickerFormat(mime_type: string) { case "image/gif": return StickerFormatType.GIF; default: - throw new HTTPError( - "invalid sticker format: must be png, apng or lottie", - ); + throw new HTTPError("invalid sticker format: must be png, apng or lottie"); } } @@ -141,9 +139,9 @@ router.get( res.json( await Sticker.findOneOrFail({ where: { guild_id, id: sticker_id }, - }), + }) ); - }, + } ); router.patch( @@ -175,7 +173,7 @@ router.patch( await sendStickerUpdateEvent(guild_id); return res.json(sticker); - }, + } ); async function sendStickerUpdateEvent(guild_id: string) { @@ -207,7 +205,7 @@ router.delete( await sendStickerUpdateEvent(guild_id); return res.sendStatus(204); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/templates.ts b/src/api/routes/guilds/#guild_id/templates.ts index 85ae0ac9..ae40377e 100644 --- a/src/api/routes/guilds/#guild_id/templates.ts +++ b/src/api/routes/guilds/#guild_id/templates.ts @@ -57,7 +57,7 @@ router.get( }); return res.json(templates); - }, + } ); router.post( @@ -102,7 +102,7 @@ router.post( }).save(); res.json(template); - }, + } ); router.delete( @@ -123,7 +123,7 @@ router.delete( }); res.json(template); - }, + } ); router.put( @@ -148,7 +148,7 @@ router.put( }).save(); res.json(template); - }, + } ); router.patch( @@ -173,7 +173,7 @@ router.patch( }).save(); res.json(template); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/vanity-url.ts b/src/api/routes/guilds/#guild_id/vanity-url.ts index a64ae2c9..1cfa9883 100644 --- a/src/api/routes/guilds/#guild_id/vanity-url.ts +++ b/src/api/routes/guilds/#guild_id/vanity-url.ts @@ -17,13 +17,7 @@ */ import { route } from "@spacebar/api"; -import { - Channel, - ChannelType, - Guild, - Invite, - VanityUrlSchema, -} from "@spacebar/util"; +import { Channel, ChannelType, Guild, Invite, VanityUrlSchema } from "@spacebar/util"; import { Request, Response, Router } from "express"; import { HTTPError } from "lambert-server"; @@ -64,11 +58,9 @@ router.get( }); if (!invite || invite.length == 0) return res.json({ code: null }); - return res.json( - invite.map((x) => ({ code: x.code, uses: x.uses })), - ); + return res.json(invite.map((x) => ({ code: x.code, uses: x.uses }))); } - }, + } ); router.patch( @@ -94,11 +86,9 @@ router.patch( const code = body.code?.replace(InviteRegex, ""); const guild = await Guild.findOneOrFail({ where: { id: guild_id } }); - if (!guild.features.includes("VANITY_URL")) - throw new HTTPError("Your guild doesn't support vanity urls"); + if (!guild.features.includes("VANITY_URL")) throw new HTTPError("Your guild doesn't support vanity urls"); - if (!code || code.length === 0) - throw new HTTPError("Code cannot be null or empty"); + if (!code || code.length === 0) throw new HTTPError("Code cannot be null or empty"); const invite = await Invite.findOne({ where: { code } }); if (invite) throw new HTTPError("Invite already exists"); @@ -112,7 +102,7 @@ router.patch( { guild_id }, { code: code, - }, + } ); return res.json({ code }); @@ -132,7 +122,7 @@ router.patch( }).save(); return res.json({ code: code }); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts b/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts index 60c69075..c13d053b 100644 --- a/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts +++ b/src/api/routes/guilds/#guild_id/voice-states/#user_id/index.ts @@ -52,14 +52,9 @@ router.patch( async (req: Request, res: Response) => { const body = req.body as VoiceStateUpdateSchema; const { guild_id } = req.params; - const user_id = - req.params.user_id === "@me" ? req.user_id : req.params.user_id; + const user_id = req.params.user_id === "@me" ? req.user_id : req.params.user_id; - const perms = await getPermission( - req.user_id, - guild_id, - body.channel_id, - ); + const perms = await getPermission(req.user_id, guild_id, body.channel_id); /* From https://discord.com/developers/docs/resources/guild#modify-current-user-voice-state @@ -98,7 +93,7 @@ router.patch( } as VoiceStateUpdateEvent), ]); return res.sendStatus(204); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/welcome-screen.ts b/src/api/routes/guilds/#guild_id/welcome-screen.ts index 81000b4b..74565a13 100644 --- a/src/api/routes/guilds/#guild_id/welcome-screen.ts +++ b/src/api/routes/guilds/#guild_id/welcome-screen.ts @@ -17,12 +17,7 @@ */ import { route } from "@spacebar/api"; -import { - Channel, - Guild, - GuildUpdateWelcomeScreenSchema, - Member, -} from "@spacebar/util"; +import { Channel, Guild, GuildUpdateWelcomeScreenSchema, Member } from "@spacebar/util"; import { Request, Response, Router } from "express"; const router: Router = Router(); @@ -46,7 +41,7 @@ router.get( await Member.IsInGuildOrFail(req.user_id, guild_id); res.json(guild.welcome_screen); - }, + } ); router.patch( @@ -70,11 +65,9 @@ router.patch( const guild = await Guild.findOneOrFail({ where: { id: guild_id } }); - if (body.enabled != undefined) - guild.welcome_screen.enabled = body.enabled; + if (body.enabled != undefined) guild.welcome_screen.enabled = body.enabled; - if (body.description != undefined) - guild.welcome_screen.description = body.description; + if (body.description != undefined) guild.welcome_screen.description = body.description; if (body.welcome_channels != undefined) { // Ensure channels exist within the guild @@ -83,8 +76,8 @@ router.patch( Channel.findOneOrFail({ where: { id: channel_id, guild_id }, select: { id: true }, - }), - ) || [], + }) + ) || [] ); guild.welcome_screen.welcome_channels = body.welcome_channels; } @@ -92,7 +85,7 @@ router.patch( await guild.save(); res.status(200).json(guild.welcome_screen); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/widget.json.ts b/src/api/routes/guilds/#guild_id/widget.json.ts index 39f49804..c2450699 100644 --- a/src/api/routes/guilds/#guild_id/widget.json.ts +++ b/src/api/routes/guilds/#guild_id/widget.json.ts @@ -81,10 +81,8 @@ router.get( // Only return channels where @everyone has the CONNECT permission if ( doc.permission_overwrites === undefined || - Permissions.channelPermission( - doc.permission_overwrites, - Permissions.FLAGS.CONNECT, - ) === Permissions.FLAGS.CONNECT + Permissions.channelPermission(doc.permission_overwrites, Permissions.FLAGS.CONNECT) === + Permissions.FLAGS.CONNECT ) { channels.push({ id: doc.id, @@ -110,7 +108,7 @@ router.get( res.set("Cache-Control", "public, max-age=300"); return res.json(data); - }, + } ); export default router; diff --git a/src/api/routes/guilds/#guild_id/widget.png.ts b/src/api/routes/guilds/#guild_id/widget.png.ts index c9ba8afc..c5080709 100644 --- a/src/api/routes/guilds/#guild_id/widget.png.ts +++ b/src/api/routes/guilds/#guild_id/widget.png.ts @@ -57,15 +57,8 @@ router.get( // Fetch parameter const style = req.query.style?.toString() || "shield"; - if ( - !["shield", "banner1", "banner2", "banner3", "banner4"].includes( - style, - ) - ) { - throw new HTTPError( - "Value must be one of ('shield', 'banner1', 'banner2', 'banner3', 'banner4').", - 400, - ); + if (!["shield", "banner1", "banner2", "banner3", "banner4"].includes(style)) { + throw new HTTPError("Value must be one of ('shield', 'banner1', 'banner2', 'banner3', 'banner4').", 400); } // Setup canvas @@ -74,17 +67,7 @@ router.get( const sizeOf = require("image-size"); // TODO: Widget style templates need Spacebar branding - const source = path.join( - __dirname, - "..", - "..", - "..", - "..", - "..", - "assets", - "widget", - `${style}.png`, - ); + const source = path.join(__dirname, "..", "..", "..", "..", "..", "assets", "widget", `${style}.png`); if (!fs.existsSync(source)) { throw new HTTPError("Widget template does not exist.", 400); } @@ -100,99 +83,32 @@ router.get( switch (style) { case "shield": ctx.textAlign = "center"; - await drawText( - ctx, - 73, - 13, - "#FFFFFF", - "thin 10px Verdana", - presence, - ); + await drawText(ctx, 73, 13, "#FFFFFF", "thin 10px Verdana", presence); break; case "banner1": if (icon) await drawIcon(ctx, 20, 27, 50, icon); - await drawText( - ctx, - 83, - 51, - "#FFFFFF", - "12px Verdana", - name, - 22, - ); - await drawText( - ctx, - 83, - 66, - "#C9D2F0FF", - "thin 11px Verdana", - presence, - ); + await drawText(ctx, 83, 51, "#FFFFFF", "12px Verdana", name, 22); + await drawText(ctx, 83, 66, "#C9D2F0FF", "thin 11px Verdana", presence); break; case "banner2": if (icon) await drawIcon(ctx, 13, 19, 36, icon); - await drawText( - ctx, - 62, - 34, - "#FFFFFF", - "12px Verdana", - name, - 15, - ); - await drawText( - ctx, - 62, - 49, - "#C9D2F0FF", - "thin 11px Verdana", - presence, - ); + await drawText(ctx, 62, 34, "#FFFFFF", "12px Verdana", name, 15); + await drawText(ctx, 62, 49, "#C9D2F0FF", "thin 11px Verdana", presence); break; case "banner3": if (icon) await drawIcon(ctx, 20, 20, 50, icon); - await drawText( - ctx, - 83, - 44, - "#FFFFFF", - "12px Verdana", - name, - 27, - ); - await drawText( - ctx, - 83, - 58, - "#C9D2F0FF", - "thin 11px Verdana", - presence, - ); + await drawText(ctx, 83, 44, "#FFFFFF", "12px Verdana", name, 27); + await drawText(ctx, 83, 58, "#C9D2F0FF", "thin 11px Verdana", presence); break; case "banner4": if (icon) await drawIcon(ctx, 21, 136, 50, icon); - await drawText( - ctx, - 84, - 156, - "#FFFFFF", - "13px Verdana", - name, - 27, - ); - await drawText( - ctx, - 84, - 171, - "#C9D2F0FF", - "thin 12px Verdana", - presence, - ); + await drawText(ctx, 84, 156, "#FFFFFF", "13px Verdana", name, 27); + await drawText(ctx, 84, 171, "#C9D2F0FF", "thin 12px Verdana", presence); break; default: throw new HTTPError( "Value must be one of ('shield', 'banner1', 'banner2', 'banner3', 'banner4').", - 400, + 400 ); } @@ -201,16 +117,10 @@ router.get( res.set("Content-Type", "image/png"); res.set("Cache-Control", "public, max-age=3600"); return res.send(buffer); - }, + } ); -async function drawIcon( - canvas: any, - x: number, - y: number, - scale: number, - icon: string, -) { +async function drawIcon(canvas: any, x: number, y: number, scale: number, icon: string) { const img = new (require("canvas").Image)(); img.src = icon; @@ -234,12 +144,11 @@ async function drawText( color: string, font: string, text: string, - maxcharacters?: number, + maxcharacters?: number ) { canvas.fillStyle = color; canvas.font = font; - if (text.length > (maxcharacters || 0) && maxcharacters) - text = text.slice(0, maxcharacters) + "..."; + if (text.length > (maxcharacters || 0) && maxcharacters) text = text.slice(0, maxcharacters) + "..."; canvas.fillText(text, x, y); } diff --git a/src/api/routes/guilds/#guild_id/widget.ts b/src/api/routes/guilds/#guild_id/widget.ts index cae0d6be..17667f29 100644 --- a/src/api/routes/guilds/#guild_id/widget.ts +++ b/src/api/routes/guilds/#guild_id/widget.ts @@ -44,7 +44,7 @@ router.get( enabled: guild.widget_enabled || false, channel_id: guild.widget_channel_id || null, }); - }, + } ); // https://discord.com/developers/docs/resources/guild#modify-guild-widget @@ -74,12 +74,12 @@ router.patch( { widget_enabled: body.enabled, widget_channel_id: body.channel_id, - }, + } ); // Widget invite for the widget_channel_id gets created as part of the /guilds/{guild.id}/widget.json request return res.json(body); - }, + } ); export default router; diff --git a/src/api/routes/guilds/index.ts b/src/api/routes/guilds/index.ts index 545beb18..a0d2f33d 100644 --- a/src/api/routes/guilds/index.ts +++ b/src/api/routes/guilds/index.ts @@ -17,14 +17,7 @@ */ import { route } from "@spacebar/api"; -import { - Config, - DiscordApiErrors, - Guild, - GuildCreateSchema, - Member, - getRights, -} from "@spacebar/util"; +import { Config, DiscordApiErrors, Guild, GuildCreateSchema, Member, getRights } from "@spacebar/util"; import { Request, Response, Router } from "express"; const router: Router = Router(); @@ -73,7 +66,7 @@ router.post( await Member.addToGuild(req.user_id, guild.id); res.status(201).json(guild); - }, + } ); export default router; diff --git a/src/api/routes/guilds/templates/index.ts b/src/api/routes/guilds/templates/index.ts index 8f718a21..693c8dcb 100644 --- a/src/api/routes/guilds/templates/index.ts +++ b/src/api/routes/guilds/templates/index.ts @@ -47,13 +47,11 @@ router.get( }, }), async (req: Request, res: Response) => { - const { allowDiscordTemplates, allowRaws, enabled } = - Config.get().templates; + const { allowDiscordTemplates, allowRaws, enabled } = Config.get().templates; if (!enabled) res.json({ code: 403, - message: - "Template creation & usage is disabled on this instance.", + message: "Template creation & usage is disabled on this instance.", }).sendStatus(403); const { code } = req.params; @@ -63,8 +61,7 @@ router.get( return res .json({ code: 403, - message: - "Discord templates cannot be used on this instance.", + message: "Discord templates cannot be used on this instance.", }) .sendStatus(403); const discordTemplateID = code.split("discord:", 2)[1]; @@ -74,7 +71,7 @@ router.get( { method: "get", headers: { "Content-Type": "application/json" }, - }, + } ); return res.json(await discordTemplateData.json()); } @@ -95,75 +92,70 @@ router.get( where: { code: code }, }); res.json(template); - }, + } ); -router.post( - "/:code", - route({ requestBody: "GuildTemplateCreateSchema" }), - async (req: Request, res: Response) => { - const { - enabled, - allowTemplateCreation, - // allowDiscordTemplates, - // allowRaws, - } = Config.get().templates; - if (!enabled) - return res - .json({ - code: 403, - message: - "Template creation & usage is disabled on this instance.", - }) - .sendStatus(403); - if (!allowTemplateCreation) - return res - .json({ - code: 403, - message: "Template creation is disabled on this instance.", - }) - .sendStatus(403); - - const { code } = req.params; - const body = req.body as GuildTemplateCreateSchema; - - const { maxGuilds } = Config.get().limits.user; - - const guild_count = await Member.count({ where: { id: req.user_id } }); - if (guild_count >= maxGuilds) { - throw DiscordApiErrors.MAXIMUM_GUILDS.withParams(maxGuilds); - } - - const template = await Template.findOneOrFail({ - where: { code: code }, - }); - - const guild_id = Snowflake.generate(); - - const [guild] = await Promise.all([ - Guild.create({ - ...body, - ...template.serialized_source_guild, - id: guild_id, - owner_id: req.user_id, - }).save(), - Role.create({ - id: guild_id, - guild_id: guild_id, - color: 0, - hoist: false, - managed: true, - mentionable: true, - name: "@everyone", - permissions: BigInt("2251804225").toString(), // TODO: where did this come from? - position: 0, - }).save(), - ]); - - await Member.addToGuild(req.user_id, guild_id); - - res.status(201).json({ id: guild.id }); - }, -); +router.post("/:code", route({ requestBody: "GuildTemplateCreateSchema" }), async (req: Request, res: Response) => { + const { + enabled, + allowTemplateCreation, + // allowDiscordTemplates, + // allowRaws, + } = Config.get().templates; + if (!enabled) + return res + .json({ + code: 403, + message: "Template creation & usage is disabled on this instance.", + }) + .sendStatus(403); + if (!allowTemplateCreation) + return res + .json({ + code: 403, + message: "Template creation is disabled on this instance.", + }) + .sendStatus(403); + + const { code } = req.params; + const body = req.body as GuildTemplateCreateSchema; + + const { maxGuilds } = Config.get().limits.user; + + const guild_count = await Member.count({ where: { id: req.user_id } }); + if (guild_count >= maxGuilds) { + throw DiscordApiErrors.MAXIMUM_GUILDS.withParams(maxGuilds); + } + + const template = await Template.findOneOrFail({ + where: { code: code }, + }); + + const guild_id = Snowflake.generate(); + + const [guild] = await Promise.all([ + Guild.create({ + ...body, + ...template.serialized_source_guild, + id: guild_id, + owner_id: req.user_id, + }).save(), + Role.create({ + id: guild_id, + guild_id: guild_id, + color: 0, + hoist: false, + managed: true, + mentionable: true, + name: "@everyone", + permissions: BigInt("2251804225").toString(), // TODO: where did this come from? + position: 0, + }).save(), + ]); + + await Member.addToGuild(req.user_id, guild_id); + + res.status(201).json({ id: guild.id }); +}); export default router; |