From 0e73de8704a3c04988599401e43e5c8f3bc1a253 Mon Sep 17 00:00:00 2001 From: khakers <22665282+khakers@users.noreply.github.com> Date: Fri, 18 Aug 2023 06:26:07 -0700 Subject: [PATCH] Synchronize thread NSFW status and title with the database (#18) * Changing the thread title or nsfw status now immediately updates in the database !Changes signature of thread.set_title() adds update_nsfw and update_title methods to the mongodb client * Lint --------- Co-authored-by: Raiden Sakura --- CHANGELOG.md | 8 ++++++++ cogs/modmail.py | 8 +++++--- core/clients.py | 14 ++++++++++++++ core/thread.py | 7 +++++-- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c6f551307..50e0f3693a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,10 +17,18 @@ however, insignificant breaking changes do not guarantee a major version bump, s ### Added - Added `content_type` to attachments stored in the database. +### Changed +- Changing a threads title or NSFW status immediately updates the status in the database. + ### Fixed - Persistent notes have been fixed after the previous discord.py update. - `is_image` now is true only if the image is actually an image. +### Internal +- Add `update_title` and `update_nsfw` methods to `ApiClient` to update thread title and nsfw status in the database. +- `thread.set_title` now requires `channel_id` to be passed as keyword arguments. +- New `thread.set_nsfw_status` method to set nsfw status of a thread. + # v4.1.0 ### Breaking diff --git a/cogs/modmail.py b/cogs/modmail.py index 061640e264..057b307558 100644 --- a/cogs/modmail.py +++ b/cogs/modmail.py @@ -677,7 +677,8 @@ async def unsubscribe(self, ctx, *, user_or_role: Union[discord.Role, User, str. @checks.thread_only() async def nsfw(self, ctx): """Flags a Modmail thread as NSFW (not safe for work).""" - await ctx.channel.edit(nsfw=True) + await ctx.thread.set_nsfw_status(True) + sent_emoji, _ = await self.bot.retrieve_emoji() await self.bot.add_reaction(ctx.message, sent_emoji) @@ -686,7 +687,8 @@ async def nsfw(self, ctx): @checks.thread_only() async def sfw(self, ctx): """Flags a Modmail thread as SFW (safe for work).""" - await ctx.channel.edit(nsfw=False) + await ctx.thread.set_nsfw_status(False) + sent_emoji, _ = await self.bot.retrieve_emoji() await self.bot.add_reaction(ctx.message, sent_emoji) @@ -769,7 +771,7 @@ def format_log_embeds(self, logs, avatar_url): @commands.cooldown(1, 600, BucketType.channel) async def title(self, ctx, *, name: str): """Sets title for a thread""" - await ctx.thread.set_title(name) + await ctx.thread.set_title(name, ctx.channel.id) sent_emoji, _ = await self.bot.retrieve_emoji() await ctx.message.pin() await self.bot.add_reaction(ctx.message, sent_emoji) diff --git a/core/clients.py b/core/clients.py index 6ae313e5c2..e80225d9e9 100644 --- a/core/clients.py +++ b/core/clients.py @@ -429,6 +429,12 @@ async def update_repository(self) -> dict: async def get_user_info(self) -> Optional[dict]: return NotImplemented + async def update_title(self, title: str, channel_id: Union[str, int]): + return NotImplemented + + async def update_nsfw(self, nsfw: bool, channel_id: Union[str, int]): + return NotImplemented + class MongoDBClient(ApiClient): def __init__(self, bot): @@ -759,6 +765,14 @@ async def get_user_info(self) -> Optional[dict]: } } + async def update_title(self, title: str, channel_id: Union[str, int]): + await self.bot.db.logs.find_one_and_update( + {"channel_id": str(channel_id)}, {"$set": {"title": title}} + ) + + async def update_nsfw(self, nsfw: bool, channel_id: Union[str, int]): + await self.bot.db.logs.find_one_and_update({"channel_id": str(channel_id)}, {"$set": {"nsfw": nsfw}}) + class PluginDatabaseClient: def __init__(self, bot): diff --git a/core/thread.py b/core/thread.py index 9ca5180fb6..2518acd6d5 100644 --- a/core/thread.py +++ b/core/thread.py @@ -1167,7 +1167,7 @@ async def get_notifications(self) -> str: return " ".join(set(mentions)) - async def set_title(self, title: str) -> None: + async def set_title(self, title: str, channel_id: int) -> None: topic = f"Title: {title}\n" user_id = match_user_id(self.channel.topic) @@ -1177,7 +1177,10 @@ async def set_title(self, title: str) -> None: ids = ",".join(str(i.id) for i in self._other_recipients) topic += f"\nOther Recipients: {ids}" - await self.channel.edit(topic=topic) + await asyncio.gather(self.channel.edit(topic=topic), self.bot.api.update_title(title, channel_id)) + + async def set_nsfw_status(self, nsfw: bool) -> None: + await asyncio.gather(self.channel.edit(nsfw=nsfw), self.bot.api.update_nsfw(nsfw, self.channel.id)) async def _update_users_genesis(self): genesis_message = await self.get_genesis_message()