Skip to content

Commit

Permalink
Synchronize thread NSFW status and title with the database (raidensak…
Browse files Browse the repository at this point in the history
…ura#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 <raiden@project-mei.xyz>
  • Loading branch information
khakers and raidensakura committed Aug 18, 2023
1 parent 2e9e07b commit 0e73de8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions cogs/modmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions core/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 5 additions & 2 deletions core/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 0e73de8

Please sign in to comment.