summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-01-30 13:34:27 +1100
committerGitHub <noreply@github.com>2023-01-30 13:34:27 +1100
commit71258f64879b43c3bf365c92bcb9d3b61b634f15 (patch)
tree5ea5f1f2cd66d3421d79a3c88c46fd10d8dced5c /src/api
parentImplement WebAuthn (#967) (diff)
downloadserver-71258f64879b43c3bf365c92bcb9d3b61b634f15.tar.xz
read-states/ack-bulk (#969)
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/read-states/ack-bulk.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/api/routes/read-states/ack-bulk.ts b/src/api/routes/read-states/ack-bulk.ts
new file mode 100644
index 00000000..f77ecedf
--- /dev/null
+++ b/src/api/routes/read-states/ack-bulk.ts
@@ -0,0 +1,41 @@
+import { Router, Request, Response } from "express";
+import { route } from "@fosscord/api";
+import { AckBulkSchema, ReadState } from "@fosscord/util";
+const router = Router();
+
+router.post(
+	"/",
+	route({ body: "AckBulkSchema" }),
+	async (req: Request, res: Response) => {
+		const body = req.body as AckBulkSchema;
+
+		// TODO: what is read_state_type ?
+
+		await Promise.all([
+			// for every new state
+			...body.read_states.map(async (x) => {
+				// find an existing one
+				const ret =
+					(await ReadState.findOne({
+						where: {
+							user_id: req.user_id,
+							channel_id: x.channel_id,
+						},
+					})) ??
+					// if it doesn't exist, create it (not a promise)
+					ReadState.create({
+						user_id: req.user_id,
+						channel_id: x.channel_id,
+					});
+
+				ret.last_message_id = x.message_id;
+
+				return ret.save();
+			}),
+		]);
+
+		return res.status(204);
+	},
+);
+
+export default router;