summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-29 15:26:49 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-29 15:30:11 +1000
commit357ec51f1c28c85ecbb7cf6a5756ef6c1296c4c0 (patch)
treea2ff236e57cfae309894bce08c5d3f42219d6312 /src
parentFix client crash on GUILD_CREATE (diff)
downloadserver-357ec51f1c28c85ecbb7cf6a5756ef6c1296c4c0.tar.xz
Update read state on message create
Diffstat (limited to 'src')
-rw-r--r--src/api/routes/channels/#channel_id/messages/index.ts24
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);
 	},