summary refs log tree commit diff
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-07-23 21:06:31 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-07-23 21:06:31 +1000
commit78acb2a013d109e6a5179f8a9fe28f23819c5f3d (patch)
tree06d19c3241eda30d2af59a416533b1a55ef78e98
parentMoved user notes into separate table (diff)
downloadserver-78acb2a013d109e6a5179f8a9fe28f23819c5f3d.tar.xz
Delete Note if no content
-rw-r--r--api/src/routes/users/@me/notes.ts24
1 files changed, 14 insertions, 10 deletions
diff --git a/api/src/routes/users/@me/notes.ts b/api/src/routes/users/@me/notes.ts
index e3406d18..3c503942 100644
--- a/api/src/routes/users/@me/notes.ts
+++ b/api/src/routes/users/@me/notes.ts
@@ -27,18 +27,22 @@ router.put("/:id", route({}), async (req: Request, res: Response) => {
 	const target = 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 } });
-
-	if (await Note.findOne({ owner: { id: owner.id }, target: { id: target.id } })) {
-		Note.update(
-			{ owner: { id: owner.id }, target: { id: target.id } },
-			{ owner, target, content: note }
-		);
+	if (note && note.length) {
+		// upsert a note
+		if (await Note.findOne({ owner: { id: owner.id }, target: { id: target.id } })) {
+			Note.update(
+				{ owner: { id: owner.id }, target: { id: target.id } },
+				{ owner, target, content: note }
+			);
+		}
+		else {
+			Note.insert(
+				{ id: Snowflake.generate(), owner, target, content: note }
+			);
+		}
 	}
 	else {
-		Note.insert(
-			{ id: Snowflake.generate(), owner, target, content: note }
-		);
+		await Note.delete({ owner: { id: owner.id }, target: { id: target.id } });
 	}
 
 	await emitEvent({