diff options
Diffstat (limited to 'api/src')
-rw-r--r-- | api/src/routes/channels/#channel_id/messages/index.ts | 48 |
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); |