diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-09-29 15:26:49 +1000 |
---|---|---|
committer | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2022-09-29 15:30:11 +1000 |
commit | dc61a497ffdd7718786f1a48ae8adb1c61f34394 (patch) | |
tree | 1ea9c26ee4ff6999b343ce119b9129ba202c6d67 | |
parent | Fix client crash on GUILD_CREATE (diff) | |
download | server-dc61a497ffdd7718786f1a48ae8adb1c61f34394.tar.xz |
Update read state on message create
-rw-r--r-- | src/api/routes/channels/#channel_id/messages/index.ts | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/api/routes/channels/#channel_id/messages/index.ts b/src/api/routes/channels/#channel_id/messages/index.ts index 631074c6..8598d1f8 100644 --- a/src/api/routes/channels/#channel_id/messages/index.ts +++ b/src/api/routes/channels/#channel_id/messages/index.ts @@ -14,6 +14,7 @@ import { Member, Role, MessageCreateSchema, + ReadState, } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { handleMessage, postHandleMessage, route } from "@fosscord/api"; @@ -74,7 +75,7 @@ router.get("/", async (req: Request, res: Response) => { permissions.hasThrow("VIEW_CHANNEL"); if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json([]); - var query: FindManyOptions<Message> & { where: { id?: any } } = { + var query: FindManyOptions<Message> & { where: { id?: any; }; } = { order: { timestamp: "DESC" }, take: limit, where: { channel_id }, @@ -132,9 +133,8 @@ router.get("/", async (req: Request, res: Response) => { const uri = y.proxy_url.startsWith("http") ? y.proxy_url : `https://example.org${y.proxy_url}`; - y.proxy_url = `${endpoint == null ? "" : endpoint}${ - new URL(uri).pathname - }`; + y.proxy_url = `${endpoint == null ? "" : endpoint}${new URL(uri).pathname + }`; }); /** @@ -267,7 +267,15 @@ router.post( return role.id; }) as any; + 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 }); + read_state.last_message_id = message.id; + await Promise.all([ + read_state.save(), message.save(), emitEvent({ event: "MESSAGE_CREATE", @@ -276,14 +284,14 @@ router.post( } as MessageCreateEvent), message.guild_id ? Member.update( - { id: req.user_id, guild_id: message.guild_id }, - { last_message_id: message.id }, - ) + { id: req.user_id, guild_id: message.guild_id }, + { last_message_id: message.id }, + ) : null, channel.save(), ]); - postHandleMessage(message).catch((e) => {}); // no await as it shouldnt block the message send function and silently catch error + postHandleMessage(message).catch((e) => { }); // no await as it shouldnt block the message send function and silently catch error return res.json(message); }, |