summary refs log tree commit diff
path: root/api/src
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-04-05 19:58:34 +1000
committerGitHub <noreply@github.com>2022-04-05 12:58:34 +0300
commitd08689b4180c2a66382ca9a6174b1c177076a65f (patch)
tree0f42df759790e39e1824b0989d2ac73998c84692 /api/src
parentRevert "Make member.premium_since ISO8601 timestamp" (#710) (diff)
downloadserver-d08689b4180c2a66382ca9a6174b1c177076a65f.tar.xz
User Notes (#707)
* Notes implementation.
Bug: Client does not save note locally after uploading to server. Client does save after reloading page. Is this due to the response being sent by PUT?

* I don't know why the client doesn't do optimistic UI updates with this, or any updates at all without reloading the page

* Added USER_NOTE_UPDATE event, thanks @TheRealGeoDash2019 !
Diffstat (limited to 'api/src')
-rw-r--r--api/src/routes/users/@me/notes.ts35
1 files changed, 30 insertions, 5 deletions
diff --git a/api/src/routes/users/@me/notes.ts b/api/src/routes/users/@me/notes.ts
index 96067bf5..4887b191 100644
--- a/api/src/routes/users/@me/notes.ts
+++ b/api/src/routes/users/@me/notes.ts
@@ -1,14 +1,39 @@
 import { Request, Response, Router } from "express";
 import { route } from "@fosscord/api";
+import { User, emitEvent } from "@fosscord/util";
 
 const router: Router = Router();
 
+router.get("/:id", route({}), async (req: Request, res: Response) => {
+	const { id } = req.params;
+	const user = await User.findOneOrFail({ where: { id: req.user_id }, select: ["notes"] });
+
+	const note = user.notes[id];
+	return res.json({
+		note: note,
+		note_user_id: id,
+		user_id: user.id,
+	});
+});
+
 router.put("/:id", route({}), async (req: Request, res: Response) => {
-	//TODO
-	res.json({
-		message: "Unknown User",
-		code: 10013
-	}).status(404);
+	const { id } = req.params;
+	const user = await User.findOneOrFail({ where: { id: req.user_id } });
+	const noteUser = await User.findOneOrFail({ where: { id: id }});		//if noted user does not exist throw
+	const { note } = req.body;
+
+	await User.update({ id: req.user_id }, { notes: { ...user.notes, [noteUser.id]: note } });
+
+	await emitEvent({
+		event: "USER_NOTE_UPDATE",
+		data: {
+			note: note,
+			id: noteUser.id
+		},
+		user_id: user.id,
+	})
+
+	return res.status(204);
 });
 
 export default router;