summary refs log tree commit diff
path: root/src/routes/channels
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/channels')
-rw-r--r--src/routes/channels/#channel_id/index.ts3
-rw-r--r--src/routes/channels/#channel_id/invites.ts4
-rw-r--r--src/routes/channels/#channel_id/messages/#message_id/index.ts5
-rw-r--r--src/routes/channels/#channel_id/messages/#message_id/reactions.ts13
-rw-r--r--src/routes/channels/#channel_id/messages/bulk-delete.ts2
-rw-r--r--src/routes/channels/#channel_id/messages/index.ts1
-rw-r--r--src/routes/channels/#channel_id/permissions.ts6
-rw-r--r--src/routes/channels/#channel_id/pins.ts5
-rw-r--r--src/routes/channels/#channel_id/typing.ts40
-rw-r--r--src/routes/channels/#channel_id/webhooks.ts1
10 files changed, 27 insertions, 53 deletions
diff --git a/src/routes/channels/#channel_id/index.ts b/src/routes/channels/#channel_id/index.ts

index dcc093ae..434f61a5 100644 --- a/src/routes/channels/#channel_id/index.ts +++ b/src/routes/channels/#channel_id/index.ts
@@ -12,7 +12,6 @@ router.get("/", async (req: Request, res: Response) => { const { channel_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); @@ -24,7 +23,6 @@ router.delete("/", async (req: Request, res: Response) => { const { channel_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel }); permission.hasThrow("MANAGE_CHANNELS"); @@ -47,7 +45,6 @@ router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response permission.hasThrow("MANAGE_CHANNELS"); const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); const data = toObject(channel); diff --git a/src/routes/channels/#channel_id/invites.ts b/src/routes/channels/#channel_id/invites.ts
index 457e78ca..c9db4dd2 100644 --- a/src/routes/channels/#channel_id/invites.ts +++ b/src/routes/channels/#channel_id/invites.ts
@@ -16,7 +16,7 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) const { channel_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }).exec(); - if (!channel || !channel.guild_id) { + if (!channel.guild_id) { throw new HTTPError("This channel doesn't exist", 404); } const { guild_id } = channel; @@ -50,7 +50,7 @@ router.get("/", async (req: Request, res: Response) => { const { channel_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }).exec(); - if (!channel || !channel.guild_id) { + if (!channel.guild_id) { throw new HTTPError("This channel doesn't exist", 404); } const { guild_id } = channel; diff --git a/src/routes/channels/#channel_id/messages/#message_id/index.ts b/src/routes/channels/#channel_id/messages/#message_id/index.ts
index 90727f60..9cd63d26 100644 --- a/src/routes/channels/#channel_id/messages/#message_id/index.ts +++ b/src/routes/channels/#channel_id/messages/#message_id/index.ts
@@ -13,7 +13,6 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response var body = req.body as MessageCreateSchema; var message = await MessageModel.findOne({ id: message_id, channel_id }, { author_id: true }).exec(); - if (!message) throw new HTTPError("Message not found", 404); const permissions = await getPermission(req.user_id, undefined, channel_id); @@ -30,8 +29,8 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response edited_timestamp: new Date() }); + // @ts-ignore message = await MessageModel.findOneAndUpdate({ id: message_id }, opts).populate("author").exec(); - if (!message) throw new HTTPError("Message not found", 404); await emitEvent({ event: "MESSAGE_UPDATE", @@ -51,9 +50,7 @@ router.delete("/", async (req: Request, res: Response) => { const { message_id, channel_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }); - if (!channel) throw new HTTPError("Channel not found", 404); const message = await MessageModel.findOne({ id: message_id }, { author_id: true }).exec(); - if (!message) throw new HTTPError("Message not found", 404); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); if (message.author_id !== req.user_id) permission.hasThrow("MANAGE_MESSAGES"); diff --git a/src/routes/channels/#channel_id/messages/#message_id/reactions.ts b/src/routes/channels/#channel_id/messages/#message_id/reactions.ts
index c31be435..9f68b5cd 100644 --- a/src/routes/channels/#channel_id/messages/#message_id/reactions.ts +++ b/src/routes/channels/#channel_id/messages/#message_id/reactions.ts
@@ -39,13 +39,11 @@ router.delete("/", async (req: Request, res: Response) => { const { message_id, channel_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); const permissions = await getPermission(req.user_id, undefined, channel_id); permissions.hasThrow("MANAGE_MESSAGES"); - const message = await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }).exec(); - if (!message) throw new HTTPError("Message not found", 404); + await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }).exec(); await emitEvent({ event: "MESSAGE_REACTION_REMOVE_ALL", @@ -66,13 +64,11 @@ router.delete("/:emoji", async (req: Request, res: Response) => { const emoji = getEmoji(req.params.emoji); const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); const permissions = await getPermission(req.user_id, undefined, channel_id); permissions.hasThrow("MANAGE_MESSAGES"); const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); - if (!message) throw new HTTPError("Message not found", 404); const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); if (!already_added) throw new HTTPError("Reaction not found", 404); @@ -118,10 +114,7 @@ router.put("/:emoji/:user_id", async (req: Request, res: Response) => { const emoji = getEmoji(req.params.emoji); const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); - const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); - if (!message) throw new HTTPError("Message not found", 404); const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); const permissions = await getPermission(req.user_id, undefined, channel_id); @@ -130,7 +123,6 @@ router.put("/:emoji/:user_id", async (req: Request, res: Response) => { if (emoji.id) { const external_emoji = await EmojiModel.findOne({ id: emoji.id }).exec(); - if (!external_emoji) throw new HTTPError("Emoji not found", 404); if (!already_added) permissions.hasThrow("USE_EXTERNAL_EMOJIS"); emoji.animated = external_emoji.animated; emoji.name = external_emoji.name; @@ -168,10 +160,7 @@ router.delete("/:emoji/:user_id", async (req: Request, res: Response) => { const emoji = getEmoji(req.params.emoji); const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); - const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); - if (!message) throw new HTTPError("Message not found", 404); const permissions = await getPermission(req.user_id, undefined, channel_id); diff --git a/src/routes/channels/#channel_id/messages/bulk-delete.ts b/src/routes/channels/#channel_id/messages/bulk-delete.ts
index 8cb672d8..e53cd597 100644 --- a/src/routes/channels/#channel_id/messages/bulk-delete.ts +++ b/src/routes/channels/#channel_id/messages/bulk-delete.ts
@@ -14,7 +14,7 @@ export default router; router.post("/", check({ messages: [String] }), async (req: Request, res: Response) => { const { channel_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }, { permission_overwrites: true, guild_id: true }).exec(); - if (!channel?.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400); + if (!channel.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400); const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel }); permission.hasThrow("MANAGE_MESSAGES"); diff --git a/src/routes/channels/#channel_id/messages/index.ts b/src/routes/channels/#channel_id/messages/index.ts
index 5f1f6c54..053a2a02 100644 --- a/src/routes/channels/#channel_id/messages/index.ts +++ b/src/routes/channels/#channel_id/messages/index.ts
@@ -31,7 +31,6 @@ export function isTextChannel(type: ChannelType): boolean { router.get("/", async (req: Request, res: Response) => { const channel_id = req.params.channel_id; const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); isTextChannel(channel.type); diff --git a/src/routes/channels/#channel_id/permissions.ts b/src/routes/channels/#channel_id/permissions.ts
index 43e61821..f3cef53e 100644 --- a/src/routes/channels/#channel_id/permissions.ts +++ b/src/routes/channels/#channel_id/permissions.ts
@@ -12,7 +12,7 @@ router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number, const body = req.body as { allow: bigint; deny: bigint; type: number; id: string }; var channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, permission_overwrites: true }).exec(); - if (!channel || !channel.guild_id) throw new HTTPError("Channel not found", 404); + if (!channel.guild_id) throw new HTTPError("Channel not found", 404); const permissions = await getPermission(req.user_id, channel.guild_id, channel_id); permissions.hasThrow("MANAGE_ROLES"); @@ -38,8 +38,8 @@ router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number, overwrite.allow = body.allow; overwrite.deny = body.deny; + // @ts-ignore channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, channel).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); await emitEvent({ event: "CHANNEL_UPDATE", @@ -59,7 +59,7 @@ router.delete("/:overwrite_id", async (req: Request, res: Response) => { permissions.hasThrow("MANAGE_ROLES"); const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, { $pull: { permission_overwrites: { id: overwrite_id } } }); - if (!channel || !channel.guild_id) throw new HTTPError("Channel not found", 404); + if (!channel.guild_id) throw new HTTPError("Channel not found", 404); await emitEvent({ event: "CHANNEL_UPDATE", diff --git a/src/routes/channels/#channel_id/pins.ts b/src/routes/channels/#channel_id/pins.ts
index 5b61f0d2..f5bd2ef7 100644 --- a/src/routes/channels/#channel_id/pins.ts +++ b/src/routes/channels/#channel_id/pins.ts
@@ -12,12 +12,10 @@ import { HTTPError } from "lambert-server"; import { emitEvent } from "../../../util/Event"; const router: Router = Router(); -// TODO: auto throw error if findOne doesn't find anything router.put("/:message_id", async (req: Request, res: Response) => { const { channel_id, message_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); @@ -30,7 +28,6 @@ router.put("/:message_id", async (req: Request, res: Response) => { await MessageModel.updateOne({ id: message_id }, { pinned: true }).exec(); const message = toObject(await MessageModel.findOne({ id: message_id }).exec()); - if (!message) throw new HTTPError("Message not found", 404); await emitEvent({ event: "MESSAGE_UPDATE", @@ -57,7 +54,6 @@ router.delete("/:message_id", async (req: Request, res: Response) => { const { channel_id, message_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); @@ -90,7 +86,6 @@ router.get("/", async (req: Request, res: Response) => { const { channel_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); diff --git a/src/routes/channels/#channel_id/typing.ts b/src/routes/channels/#channel_id/typing.ts
index f0ca138c..2c2b9bc9 100644 --- a/src/routes/channels/#channel_id/typing.ts +++ b/src/routes/channels/#channel_id/typing.ts
@@ -7,28 +7,26 @@ import { emitEvent } from "../../../util/Event"; const router: Router = Router(); router.post("/", async (req: Request, res: Response) => { - const { channel_id } = req.params; - const user_id = req.user_id; - const timestamp = Date.now() - const channel = await ChannelModel.findOne({ id: channel_id }); - if (!channel) throw new HTTPError("Channel not found", 404) - const member = await MemberModel.findOne({ id: user_id }).exec() - if (!member) throw new HTTPError("Member not found", 404) + const { channel_id } = req.params; + const user_id = req.user_id; + const timestamp = Date.now(); + const channel = await ChannelModel.findOne({ id: channel_id }); + const member = await MemberModel.findOne({ id: user_id }).exec(); - - await emitEvent({ - event: "TYPING_START", - channel_id: channel_id, - guild_id: channel.guild_id, - data: { // this is the paylod - member: toObject(member), - channel_id, - timestamp, - user_id, - guild_id: channel.guild_id - } - } as TypingStartEvent) - res.sendStatus(204) + await emitEvent({ + event: "TYPING_START", + channel_id: channel_id, + guild_id: channel.guild_id, + data: { + // this is the paylod + member: toObject(member), + channel_id, + timestamp, + user_id, + guild_id: channel.guild_id + } + } as TypingStartEvent); + res.sendStatus(204); }); export default router; diff --git a/src/routes/channels/#channel_id/webhooks.ts b/src/routes/channels/#channel_id/webhooks.ts
index b60f4d68..6c1aea2a 100644 --- a/src/routes/channels/#channel_id/webhooks.ts +++ b/src/routes/channels/#channel_id/webhooks.ts
@@ -11,7 +11,6 @@ const router: Router = Router(); router.post("/", check({ name: new Length(String, 1, 80), $avatar: String }), async (req: Request, res: Response) => { const channel_id = req.params.channel_id; const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); isTextChannel(channel.type); if (!channel.guild_id) throw new HTTPError("Not a guild channel", 400);