yepcord / server

Unofficial discord backend implementation in python.
GNU Affero General Public License v3.0
2 stars 1 forks source link

update read state of mentioned users #235

Open github-actions[bot] opened 3 days ago

github-actions[bot] commented 3 days ago

https://github.com/yepcord/server/blob/7dfe87e5c92d527d77109fc89663993144526033/yepcord/yepcord/models/readstate.py#L73


            "last_message_id": str(self.last_read_id),
            "id": str(self.channel.id),
        }

    @classmethod
    async def create_or_add(
            cls, user: models.User, channel: models.Channel, mentions: int = 1, last_read_id: Optional[int] = None,
    ) -> ReadState:
        state, created = await cls.get_or_create(user=user, channel=channel, defaults={
            "count": mentions,
            "last_read_id": last_read_id or 0,
        })
        if not created:
            state.count += mentions
            state.last_read_id = last_read_id or state.last_read_id
            await state.save(update_fields=["count", "last_read_id"])

        return state

    @classmethod
    async def update_from_message(cls, message: models.Message) -> None:
        if message.channel.type in (ChannelType.DM, ChannelType.GROUP_DM):
            for user in await message.channel.recipients.filter(id__not=message.author.id):
                await models.ReadState.create_or_add(user, message.channel)
        elif message.channel.type in GUILD_CHANNELS:
            ...  # TODO: update read state of mentioned users