summary refs log tree commit diff
path: root/src/routes/guilds
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-07-10 18:05:11 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-07-10 18:05:11 +0200
commitf5b301a1612f4e25711bba2b0a87e38a1f9da28d (patch)
tree1457fdf4e42569bd3e6702c5e8790e8f71f5ac08 /src/routes/guilds
parent:construction: pkg binary bundle (diff)
downloadserver-f5b301a1612f4e25711bba2b0a87e38a1f9da28d.tar.xz
findOne auto throws error if it doesn't exist'
Diffstat (limited to 'src/routes/guilds')
-rw-r--r--src/routes/guilds/#guild_id/bans.ts1
-rw-r--r--src/routes/guilds/#guild_id/channels.ts10
-rw-r--r--src/routes/guilds/#guild_id/delete.ts1
-rw-r--r--src/routes/guilds/#guild_id/index.ts1
-rw-r--r--src/routes/guilds/#guild_id/members/#member_id/index.ts1
-rw-r--r--src/routes/guilds/#guild_id/members/index.ts1
-rw-r--r--src/routes/guilds/#guild_id/roles.ts11
-rw-r--r--src/routes/guilds/#guild_id/templates.ts8
-rw-r--r--src/routes/guilds/#guild_id/vanity-url.ts2
-rw-r--r--src/routes/guilds/#guild_id/welcome_screen.ts13
-rw-r--r--src/routes/guilds/#guild_id/widget.json.ts97
-rw-r--r--src/routes/guilds/#guild_id/widget.png.ts1
-rw-r--r--src/routes/guilds/#guild_id/widget.ts3
-rw-r--r--src/routes/guilds/templates/index.ts2
14 files changed, 57 insertions, 95 deletions
diff --git a/src/routes/guilds/#guild_id/bans.ts b/src/routes/guilds/#guild_id/bans.ts

index cf6a059b..4d9bad37 100644 --- a/src/routes/guilds/#guild_id/bans.ts +++ b/src/routes/guilds/#guild_id/bans.ts
@@ -25,7 +25,6 @@ router.get("/:user", async (req: Request, res: Response) => { const user_id = req.params.ban; var ban = await BanModel.findOne({ guild_id: guild_id, user_id: user_id }).exec(); - if (!ban) throw new HTTPError("Ban not found", 404); return res.json(ban); }); diff --git a/src/routes/guilds/#guild_id/channels.ts b/src/routes/guilds/#guild_id/channels.ts
index a3c6f4fe..90b4473d 100644 --- a/src/routes/guilds/#guild_id/channels.ts +++ b/src/routes/guilds/#guild_id/channels.ts
@@ -37,15 +37,7 @@ router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response const body = req.body as ChannelModifySchema; const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); - if (!guild) throw new HTTPError("Guild not found", 404); - - const channel = { - ...body - }; - const channelm = await ChannelModel.find({ guild_id }).exec(); - if (!channelm) throw new HTTPError("Channel not found", 404); - - await new ChannelModel(channel).save(); + const channel = await ChannelModel.findOneAndUpdate({ guild_id }, body).exec(); await emitEvent({ event: "CHANNEL_UPDATE", data: channel } as ChannelUpdateEvent); diff --git a/src/routes/guilds/#guild_id/delete.ts b/src/routes/guilds/#guild_id/delete.ts
index 5d4db816..c363db25 100644 --- a/src/routes/guilds/#guild_id/delete.ts +++ b/src/routes/guilds/#guild_id/delete.ts
@@ -20,7 +20,6 @@ router.post("/", async (req: Request, res: Response) => { var { guild_id } = req.params; const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec(); - if (!guild) throw new HTTPError("This guild does not exist", 404); if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401); await emitEvent({ diff --git a/src/routes/guilds/#guild_id/index.ts b/src/routes/guilds/#guild_id/index.ts
index 9ef0127a..3af49106 100644 --- a/src/routes/guilds/#guild_id/index.ts +++ b/src/routes/guilds/#guild_id/index.ts
@@ -27,7 +27,6 @@ router.get("/", async (req: Request, res: Response) => { const guild = await GuildModel.findOne({ id: guild_id }) .populate({ path: "joined_at", match: { id: req.user_id } }) .exec(); - if (!guild) throw new HTTPError("Guild does not exist", 404); const member = await MemberModel.exists({ 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); diff --git a/src/routes/guilds/#guild_id/members/#member_id/index.ts b/src/routes/guilds/#guild_id/members/#member_id/index.ts
index 12eedfb2..9a1676e6 100644 --- a/src/routes/guilds/#guild_id/members/#member_id/index.ts +++ b/src/routes/guilds/#guild_id/members/#member_id/index.ts
@@ -23,7 +23,6 @@ router.get("/", async (req: Request, res: Response) => { await isMember(req.user_id, guild_id); const member = await MemberModel.findOne({ id: member_id, guild_id }).exec(); - if (!member) throw new HTTPError("Member not found", 404); return res.json(toObject(member)); }); diff --git a/src/routes/guilds/#guild_id/members/index.ts b/src/routes/guilds/#guild_id/members/index.ts
index 1ec21226..a157d8f5 100644 --- a/src/routes/guilds/#guild_id/members/index.ts +++ b/src/routes/guilds/#guild_id/members/index.ts
@@ -11,7 +11,6 @@ const router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; const guild = await GuildModel.findOne({ id: guild_id }).exec(); - if (!guild) throw new HTTPError("Guild not found", 404); await isMember(req.user_id, guild_id); try { diff --git a/src/routes/guilds/#guild_id/roles.ts b/src/routes/guilds/#guild_id/roles.ts
index e9360847..77206a0f 100644 --- a/src/routes/guilds/#guild_id/roles.ts +++ b/src/routes/guilds/#guild_id/roles.ts
@@ -35,10 +35,7 @@ router.post("/", check(RoleModifySchema), async (req: Request, res: Response) => const body = req.body as RoleModifySchema; const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); - if (!guild) throw new HTTPError("Guild not found", 404); - const user = await UserModel.findOne({ id: req.user_id }).exec(); - if (!user) throw new HTTPError("User not found", 404); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); @@ -71,11 +68,7 @@ router.delete("/:role_id", async (req: Request, res: Response) => { const { role_id } = req.params; const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); - if (!guild) throw new HTTPError("Guild not found", 404); - if (!role_id) throw new HTTPError("Unknown role_id", 404); - const user = await UserModel.findOne({ id: req.user_id }).exec(); - if (!user) throw new HTTPError("User not found", 404); const perms = await getPermission(req.user_id, guild_id); @@ -106,11 +99,7 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res const body = req.body as RoleModifySchema; const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); - if (!guild) throw new HTTPError("Guild not found", 404); - if (!role_id) throw new HTTPError("Unknown template_id", 404); - const user = await UserModel.findOne({ id: req.user_id }).exec(); - if (!user) throw new HTTPError("User not found", 404); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); diff --git a/src/routes/guilds/#guild_id/templates.ts b/src/routes/guilds/#guild_id/templates.ts
index 03435c35..04ac196e 100644 --- a/src/routes/guilds/#guild_id/templates.ts +++ b/src/routes/guilds/#guild_id/templates.ts
@@ -21,7 +21,7 @@ const TemplateGuildProjection = { afk_channel_id: true, system_channel_id: true, system_channel_flags: true, - icon_hash: true, + icon_hash: true }; router.get("/", async (req: Request, res: Response) => { @@ -35,7 +35,6 @@ router.post("/", check(TemplateCreateSchema), async (req: Request, res: Response const guild_id = req.params.guild_id; const guild = await GuildModel.findOne({ id: guild_id }, TemplateGuildProjection).exec(); - if (!guild) throw new HTTPError("Guild not found", 404); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); @@ -47,7 +46,7 @@ router.post("/", check(TemplateCreateSchema), async (req: Request, res: Response created_at: new Date(), updated_at: new Date(), source_guild_id: guild_id, - serialized_source_guild: guild, + serialized_source_guild: guild }).save(); res.json(toObject(template)).send(); @@ -61,7 +60,7 @@ router.delete("/:code", async (req: Request, res: Response) => { perms.hasThrow("MANAGE_GUILD"); const template = await TemplateModel.findOneAndDelete({ - code, + code }).exec(); res.send(toObject(template)); @@ -72,7 +71,6 @@ router.put("/:code", async (req: Request, res: Response) => { const { code } = req.params; const guild = await GuildModel.findOne({ id: guild_id }, TemplateGuildProjection).exec(); - if (!guild) throw new HTTPError("Guild not found", 404); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); diff --git a/src/routes/guilds/#guild_id/vanity-url.ts b/src/routes/guilds/#guild_id/vanity-url.ts
index 052a638f..2d210e5d 100644 --- a/src/routes/guilds/#guild_id/vanity-url.ts +++ b/src/routes/guilds/#guild_id/vanity-url.ts
@@ -8,8 +8,6 @@ router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; const guild = await GuildModel.findOne({ id: guild_id }).exec(); - if (!guild) throw new HTTPError("Guild does not exist", 404); - if (!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204); return res.json({ vanity_ur: guild.vanity_url }); diff --git a/src/routes/guilds/#guild_id/welcome_screen.ts b/src/routes/guilds/#guild_id/welcome_screen.ts
index 8fc5ac39..656a0ee0 100644 --- a/src/routes/guilds/#guild_id/welcome_screen.ts +++ b/src/routes/guilds/#guild_id/welcome_screen.ts
@@ -13,7 +13,6 @@ router.get("/", async (req: Request, res: Response) => { const guild_id = req.params.guild_id; const guild = await GuildModel.findOne({ id: guild_id }); - if (!guild) throw new HTTPError("Guild not found", 404); await isMember(req.user_id, guild_id); @@ -25,22 +24,21 @@ router.post("/", check(GuildAddChannelToWelcomeScreenSchema), async (req: Reques const body = req.body as GuildAddChannelToWelcomeScreenSchema; const guild = await GuildModel.findOne({ id: guild_id }).exec(); - if (!guild) throw new HTTPError("Guild not found", 404); var channelObject = { ...body - } + }; const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - if(!guild.welcome_screen.enabled) throw new HTTPError("Welcome screen disabled", 400); - if(guild.welcome_screen.welcome_channels.some(channel => channel.channel_id === body.channel_id)) throw new Error("Welcome Channel exists") - + if (!guild.welcome_screen.enabled) throw new HTTPError("Welcome screen disabled", 400); + if (guild.welcome_screen.welcome_channels.some((channel) => channel.channel_id === body.channel_id)) + throw new Error("Welcome Channel exists"); await GuildModel.findOneAndUpdate( { - id: guild_id, + id: guild_id }, { $push: { "welcome_screen.welcome_channels": channelObject } } ).exec(); @@ -48,5 +46,4 @@ router.post("/", check(GuildAddChannelToWelcomeScreenSchema), async (req: Reques res.sendStatus(204); }); - export default router; diff --git a/src/routes/guilds/#guild_id/widget.json.ts b/src/routes/guilds/#guild_id/widget.json.ts
index 256a633d..6f777ab4 100644 --- a/src/routes/guilds/#guild_id/widget.json.ts +++ b/src/routes/guilds/#guild_id/widget.json.ts
@@ -18,7 +18,6 @@ router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; const guild = await GuildModel.findOne({ id: guild_id }).exec(); - if (!guild) throw new HTTPError("Guild does not exist", 404); if (!guild.widget_enabled) throw new HTTPError("Widget Disabled", 404); // Fetch existing widget invite for widget channel @@ -43,68 +42,66 @@ router.get("/", async (req: Request, res: Response) => { invite = await new InviteModel(body).save(); } - + // Fetch voice channels, and the @everyone permissions object let channels: any[] = []; - await ChannelModel.find( - { guild_id: guild_id, type: 2 }, - { permission_overwrites: { $elemMatch: { id: guild_id } } } - ).lean() - .select("id name position permission_overwrites") - .sort({ position: 1 }) - .cursor() - .eachAsync(doc => { - // 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) { - channels.push( - { + await ChannelModel.find({ guild_id: guild_id, type: 2 }, { permission_overwrites: { $elemMatch: { id: guild_id } } }) + .lean() + .select("id name position permission_overwrites") + .sort({ position: 1 }) + .cursor() + .eachAsync((doc) => { + // 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 + ) { + channels.push({ id: doc.id, name: doc.name, position: doc.position - } - ) - } - }); + }); + } + }); // Fetch members // TODO: Understand how Discord's max 100 random member sample works, and apply to here (see top of this file) let members: any[] = []; await MemberModel.find({ guild_id: guild_id }) - .lean() - .populate({ path: "user", select: { _id: 0, username: 1, avatar: 1, presence: 1 } }) - .select("id user nick deaf mute") - .cursor() - .eachAsync(doc => { - const status = doc.user?.presence?.status || "offline"; - if (status == "offline")return - - let item = {} - - item = { - ...item, - id: null, // this is updated during the sort outside of the query - username: doc.nick || doc.user?.username, - discriminator: "0000", // intended (https://github.com/discord/discord-api-docs/issues/1287) - avatar: null, // intended, avatar_url below will return a unique guild + user url to the avatar - status: status - } + .lean() + .populate({ path: "user", select: { _id: 0, username: 1, avatar: 1, presence: 1 } }) + .select("id user nick deaf mute") + .cursor() + .eachAsync((doc) => { + const status = doc.user?.presence?.status || "offline"; + if (status == "offline") return; + + let item = {}; - const activity = doc.user?.presence?.activities?.[0]; - if (activity) { item = { ...item, - game: { name: activity.name } + id: null, // this is updated during the sort outside of the query + username: doc.nick || doc.user?.username, + discriminator: "0000", // intended (https://github.com/discord/discord-api-docs/issues/1287) + avatar: null, // intended, avatar_url below will return a unique guild + user url to the avatar + status: status + }; + + const activity = doc.user?.presence?.activities?.[0]; + if (activity) { + item = { + ...item, + game: { name: activity.name } + }; } - } - - // TODO: If the member is in a voice channel, return extra widget details - // Extra fields returned include deaf, mute, self_deaf, self_mute, supress, and channel_id (voice channel connected to) - // Get this from VoiceState + // TODO: If the member is in a voice channel, return extra widget details + // Extra fields returned include deaf, mute, self_deaf, self_mute, supress, and channel_id (voice channel connected to) + // Get this from VoiceState - // TODO: Implement a widget-avatar endpoint on the CDN, and implement logic here to request it - // Get unique avatar url for guild user, cdn to serve the actual avatar image on this url - /* + // TODO: Implement a widget-avatar endpoint on the CDN, and implement logic here to request it + // Get unique avatar url for guild user, cdn to serve the actual avatar image on this url + /* const avatar = doc.user?.avatar; if (avatar) { const CDN_HOST = Config.get().cdn.endpoint || "http://localhost:3003"; @@ -116,8 +113,8 @@ router.get("/", async (req: Request, res: Response) => { } */ - members.push(item); - }); + members.push(item); + }); // Sort members, and update ids (Unable to do under the mongoose query due to https://mongoosejs.com/docs/faq.html#populate_sort_order) members = members.sort((first, second) => 0 - (first.username > second.username ? -1 : 1)); @@ -133,7 +130,7 @@ router.get("/", async (req: Request, res: Response) => { channels: channels, members: members, presence_count: guild.presence_count - } + }; res.set("Cache-Control", "public, max-age=300"); return res.json(data); diff --git a/src/routes/guilds/#guild_id/widget.png.ts b/src/routes/guilds/#guild_id/widget.png.ts
index 839a8129..a0a8c938 100644 --- a/src/routes/guilds/#guild_id/widget.png.ts +++ b/src/routes/guilds/#guild_id/widget.png.ts
@@ -14,7 +14,6 @@ router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; const guild = await GuildModel.findOne({ id: guild_id }).exec(); - if (!guild) throw new HTTPError("Guild does not exist", 404); if (!guild.widget_enabled) throw new HTTPError("Unknown Guild", 404); // Fetch guild information diff --git a/src/routes/guilds/#guild_id/widget.ts b/src/routes/guilds/#guild_id/widget.ts
index 7682bc87..0e6df186 100644 --- a/src/routes/guilds/#guild_id/widget.ts +++ b/src/routes/guilds/#guild_id/widget.ts
@@ -14,9 +14,8 @@ router.get("/", async (req: Request, res: Response) => { perms.hasThrow("MANAGE_GUILD"); const guild = await GuildModel.findOne({ id: guild_id }).exec(); - if (!guild) throw new HTTPError("Guild not found", 404); - return res.json({ enabled: guild.widget_enabled || false, channel_id: guild.widget_channel_id || null}); + return res.json({ enabled: guild.widget_enabled || false, channel_id: guild.widget_channel_id || null }); }); // https://discord.com/developers/docs/resources/guild#modify-guild-widget diff --git a/src/routes/guilds/templates/index.ts b/src/routes/guilds/templates/index.ts
index f23d4fbe..cc95069d 100644 --- a/src/routes/guilds/templates/index.ts +++ b/src/routes/guilds/templates/index.ts
@@ -11,7 +11,6 @@ router.get("/:code", async (req: Request, res: Response) => { const { code } = req.params; const template = await TemplateModel.findOne({ id: code }).exec(); - if (!template) throw new HTTPError("template not found", 404); res.json(toObject(template)).send(); }); @@ -28,7 +27,6 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res } const template = await TemplateModel.findOne({ code: code }).exec(); - if (!template) throw new HTTPError("template not found", 404); const guild_id = Snowflake.generate();