summary refs log tree commit diff
path: root/api/src
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-15 00:35:01 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-15 00:35:01 +0200
commitf38f79b70c3daa2ed45b27803fb01e6bc55adef9 (patch)
treef23dc9bbc5e884dcf3e0e4f03fb782d87c80c2e2 /api/src
parentMerge branch 'fosscord:master' into master (diff)
downloadserver-f38f79b70c3daa2ed45b27803fb01e6bc55adef9.tar.xz
:zap: speed up message query
Diffstat (limited to 'api/src')
-rw-r--r--api/src/routes/channels/#channel_id/messages/index.ts48
1 files changed, 21 insertions, 27 deletions
diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts
index 399fa7bb..b5a2d334 100644
--- a/api/src/routes/channels/#channel_id/messages/index.ts
+++ b/api/src/routes/channels/#channel_id/messages/index.ts
@@ -11,7 +11,7 @@ import {
 	Message,
 	MessageCreateEvent,
 	uploadFile,
-	Member,
+	Member
 } from "@fosscord/util";
 import { HTTPError } from "lambert-server";
 import { handleMessage, postHandleMessage, route } from "@fosscord/api";
@@ -187,40 +187,34 @@ router.post(
 
 		message = await message.save();
 
-		await channel.assign({ last_message_id: message.id }).save();
-
-		//gosh
-		var member = await Member.findOneOrFail({
-			where: { id: req.user_id },
-		});
-		await member.assign({ last_message_id: message.id })
-		await member.save();	//why does member.assign here return void?
-
 		if (channel.isDm()) {
 			const channel_dto = await DmChannelDTO.from(channel);
 
-			for (let recipient of channel.recipients!) {
-				if (recipient.closed) {
-					await emitEvent({
-						event: "CHANNEL_CREATE",
-						data: channel_dto.excludedRecipients([recipient.user_id]),
-						user_id: recipient.user_id
-					});
-				}
-			}
-
 			//Only one recipients should be closed here, since in group DMs the recipient is deleted not closed
+
 			await Promise.all(
-				channel
-					.recipients!.filter((r) => r.closed)
-					.map(async (r) => {
-						r.closed = false;
-						return await r.save();
-					})
+				channel.recipients!.map((recipient) => {
+					if (recipient.closed) {
+						recipient.closed = false;
+						return Promise.all([
+							recipient.save(),
+							emitEvent({
+								event: "CHANNEL_CREATE",
+								data: channel_dto.excludedRecipients([recipient.user_id]),
+								user_id: recipient.user_id
+							})
+						]);
+					}
+				})
 			);
 		}
 
-		await emitEvent({ event: "MESSAGE_CREATE", channel_id: channel_id, data: message } as MessageCreateEvent);
+		await Promise.all([
+			channel.assign({ last_message_id: message.id }).save(),
+			new Member({ id: req.user_id, last_message_id: message.id }).save(),
+			emitEvent({ event: "MESSAGE_CREATE", channel_id: channel_id, data: message } as MessageCreateEvent)
+		]);
+
 		postHandleMessage(message).catch((e) => {}); // no await as it shouldnt block the message send function and silently catch error
 
 		return res.json(message);