From b6cfb32d5af5c7de1d61e6885ae9ea13c4c5fc5c Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 28 Sep 2025 23:47:32 +0200 Subject: [PATCH] More relations --- .../channels/#channel_id/messages/index.ts | 85 +++++-------------- src/api/util/handlers/Message.ts | 1 + 2 files changed, 20 insertions(+), 66 deletions(-) diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts index 3f297632..46c0cf98 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts @@ -45,13 +45,7 @@ import { import { Request, Response, Router } from "express"; import { HTTPError } from "lambert-server"; import multer from "multer"; -import { - FindManyOptions, - FindOperator, - LessThan, - MoreThan, - MoreThanOrEqual, -} from "typeorm"; +import { FindManyOptions, FindOperator, LessThan, MoreThan, MoreThanOrEqual } from "typeorm"; import { URL } from "url"; import fetch from "node-fetch-commonjs"; import { CloudAttachment } from "../../../../../util/entities/CloudAttachment"; @@ -75,8 +69,7 @@ router.get( }, limit: { type: "number", - description: - "max number of messages to return (1-100). defaults to 50", + description: "max number of messages to return (1-100). defaults to 50", }, }, responses: { @@ -102,14 +95,9 @@ router.get( const before = req.query.before ? `${req.query.before}` : undefined; const after = req.query.after ? `${req.query.after}` : undefined; const limit = Number(req.query.limit) || 50; - if (limit < 1 || limit > 100) - throw new HTTPError("limit must be between 1 and 100", 422); + if (limit < 1 || limit > 100) throw new HTTPError("limit must be between 1 and 100", 422); - const permissions = await getPermission( - req.user_id, - channel.guild_id, - channel_id, - ); + const permissions = await getPermission(req.user_id, channel.guild_id, channel_id); permissions.hasThrow("VIEW_CHANNEL"); if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json([]); @@ -131,6 +119,8 @@ router.get( "referenced_message", "referenced_message.author", "referenced_message.mentions", + "referenced_message.mention_roles", + "referenced_message.mention_channels", ], }; @@ -151,9 +141,7 @@ router.get( }), ]); left.push(...right); - messages = left.sort( - (a, b) => a.timestamp.getTime() - b.timestamp.getTime(), - ); + messages = left.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()); } else { query.take = 1; const message = await Message.findOne({ @@ -164,20 +152,12 @@ router.get( } } else { if (after) { - if (BigInt(after) > BigInt(Snowflake.generate())) - throw new HTTPError( - "after parameter must not be greater than current time", - 422, - ); + if (BigInt(after) > BigInt(Snowflake.generate())) throw new HTTPError("after parameter must not be greater than current time", 422); query.where.id = MoreThan(after); query.order = { timestamp: "ASC" }; } else if (before) { - if (BigInt(before) > BigInt(Snowflake.generate())) - throw new HTTPError( - "before parameter must not be greater than current time", - 422, - ); + if (BigInt(before) > BigInt(Snowflake.generate())) throw new HTTPError("before parameter must not be greater than current time", 422); query.where.id = LessThan(before); } @@ -205,9 +185,7 @@ router.get( }); x.attachments?.forEach((y: Attachment) => { // dynamically set attachment proxy_url in case the endpoint changed - const uri = y.proxy_url.startsWith("http") - ? y.proxy_url - : `https://example.org${y.proxy_url}`; + const uri = y.proxy_url.startsWith("http") ? y.proxy_url : `https://example.org${y.proxy_url}`; const url = new URL(uri); if (endpoint) { @@ -310,10 +288,7 @@ router.post( relations: ["recipients", "recipients.user"], }); if (!channel.isWritable()) { - throw new HTTPError( - `Cannot send messages to channel of type ${channel.type}`, - 400, - ); + throw new HTTPError(`Cannot send messages to channel of type ${channel.type}`, 400); } if (body.nonce) { @@ -335,12 +310,7 @@ router.post( const count = await Message.count({ where: { channel_id, - timestamp: MoreThan( - new Date( - Date.now() - - limits.absoluteRate.sendMessage.window, - ), - ), + timestamp: MoreThan(new Date(Date.now() - limits.absoluteRate.sendMessage.window)), }, }); @@ -357,13 +327,8 @@ router.post( const files = (req.files as Express.Multer.File[]) ?? []; for (const currFile of files) { try { - const file = await uploadFile( - `/attachments/${channel.id}`, - currFile, - ); - attachments.push( - Attachment.create({ ...file, proxy_url: file.url }), - ); + const file = await uploadFile(`/attachments/${channel.id}`, currFile); + attachments.push(Attachment.create({ ...file, proxy_url: file.url })); } catch (error) { return res.status(400).json({ message: error?.toString() }); } @@ -400,9 +365,7 @@ router.post( recipient.save(), emitEvent({ event: "CHANNEL_CREATE", - data: channel_dto.excludedRecipients([ - recipient.user_id, - ]), + data: channel_dto.excludedRecipients([recipient.user_id]), user_id: recipient.user_id, }), ]); @@ -423,16 +386,13 @@ router.post( // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore - message.member.roles = message.member.roles - .filter((x) => x.id != x.guild_id) - .map((x) => x.id); + message.member.roles = message.member.roles.filter((x) => x.id != x.guild_id).map((x) => x.id); } let read_state = await ReadState.findOne({ where: { user_id: req.user_id, channel_id }, }); - if (!read_state) - read_state = ReadState.create({ user_id: req.user_id, channel_id }); + if (!read_state) read_state = ReadState.create({ user_id: req.user_id, channel_id }); read_state.last_message_id = message.id; await Promise.all([ @@ -443,19 +403,12 @@ router.post( channel_id: channel_id, data: message, } as MessageCreateEvent), - message.guild_id - ? Member.update( - { id: req.user_id, guild_id: message.guild_id }, - { last_message_id: message.id }, - ) - : null, + message.guild_id ? Member.update({ id: req.user_id, guild_id: message.guild_id }, { last_message_id: message.id }) : null, channel.save(), ]); // no await as it shouldnt block the message send function and silently catch error - postHandleMessage(message).catch((e) => - console.error("[Message] post-message handler failed", e), - ); + postHandleMessage(message).catch((e) => console.error("[Message] post-message handler failed", e)); return res.json( message.withSignedAttachments( diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts index 90df9a78..16730557 100644 --- a/src/api/util/handlers/Message.ts +++ b/src/api/util/handlers/Message.ts @@ -227,6 +227,7 @@ export async function handleMessage(opts: MessageOptions): Promise { where: { id: opts.message_reference.message_id, }, + relations: ["author", "mentions", "mention_roles", "mention_channels"], }); if (message.referenced_message.channel_id !== opts.message_reference.channel_id) throw new HTTPError("Referenced message not found in the specified channel", 404);