diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts index 70bbc479..433d9468 100644 --- a/src/api/util/handlers/Message.ts +++ b/src/api/util/handlers/Message.ts @@ -324,8 +324,31 @@ export async function handleMessage(opts: MessageOptions): Promise { ]; message.mention_everyone = mention_everyone; - - if (mention_everyone || channel.type === ChannelType.DM || channel.type === ChannelType.GROUP_DM) { + async function fillInMissingIDs(ids: string[]) { + const states = await ReadState.findBy({ + user_id: Or(...ids.map((id) => Equal(id))), + channel_id: channel.id, + }); + const users = new Set(ids); + states.forEach((state) => users.delete(state.user_id)); + if (!users.size) { + return; + } + return Promise.all( + [...users].map((user_id) => { + return ReadState.create({ user_id, channel_id: channel.id }).save(); + }), + ); + } + if (!!message.content?.match(EVERYONE_MENTION) || channel.type === ChannelType.DM || channel.type === ChannelType.GROUP_DM) { + if (channel.type === ChannelType.DM || channel.type === ChannelType.GROUP_DM) { + if (channel.recipients) { + await fillInMissingIDs(channel.recipients.map(({ user_id }) => user_id)); + } + } else { + console.log(channel.guild_id); + await fillInMissingIDs((await Member.find({ where: { guild_id: channel.guild_id } })).map(({ id }) => id)); + } const repository = ReadState.getRepository(); const condition = { channel_id: channel.id }; repository.update({ ...condition, mention_count: IsNull() }, { mention_count: 0 }); @@ -344,11 +367,15 @@ export async function handleMessage(opts: MessageOptions): Promise { ).map((member) => member.id), ...message.mentions.map((user) => user.id), ]); + if (users.size) { + const repository = ReadState.getRepository(); + const condition = { user_id: Or(...[...users].map((id) => Equal(id))), channel_id: channel.id }; - const repository = ReadState.getRepository(); - const condition = { user_id: Or(...[...users].map((id) => Equal(id))), channel_id: channel.id }; - repository.update({ ...condition, mention_count: IsNull() }, { mention_count: 0 }); - repository.increment(condition, "mention_count", 1); + await fillInMissingIDs([...users]); + + await repository.update({ ...condition, mention_count: IsNull() }, { mention_count: 0 }); + await repository.increment(condition, "mention_count", 1); + } } // TODO: check and put it all in the body