diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts index a0834be0..d3c0a409 100644 --- a/api/src/routes/channels/#channel_id/messages/index.ts +++ b/api/src/routes/channels/#channel_id/messages/index.ts @@ -186,14 +186,13 @@ router.post( timestamp: new Date() }); - message = await message.save(); + channel.last_message_id = message.id; if (channel.isDm()) { const channel_dto = await DmChannelDTO.from(channel); - //Only one recipients should be closed here, since in group DMs the recipient is deleted not closed - - await Promise.all( + // Only one recipients should be closed here, since in group DMs the recipient is deleted not closed + Promise.all( channel.recipients!.map((recipient) => { if (recipient.closed) { recipient.closed = false; @@ -211,9 +210,10 @@ router.post( } await Promise.all([ - channel.assign({ last_message_id: message.id }).save(), + message.save(), + emitEvent({ event: "MESSAGE_CREATE", channel_id: channel_id, data: message } as MessageCreateEvent), message.guild_id ? Member.update({ id: req.user_id, guild_id: message.guild_id }, { last_message_id: message.id }) : null, - emitEvent({ event: "MESSAGE_CREATE", channel_id: channel_id, data: message } as MessageCreateEvent) + channel.save() ]); postHandleMessage(message).catch((e) => {}); // no await as it shouldnt block the message send function and silently catch error diff --git a/api/src/util/Message.ts b/api/src/util/Message.ts index d14d3aa2..789178b6 100644 --- a/api/src/util/Message.ts +++ b/api/src/util/Message.ts @@ -184,7 +184,7 @@ export async function sendMessage(opts: MessageOptions) { const message = await handleMessage({ ...opts, timestamp: new Date() }); await Promise.all([ - message.save(), + Message.insert(message), emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data: message.toJSON() } as MessageCreateEvent) ]);