diff options
-rw-r--r-- | bot/exts/recruitment/talentpool/_api.py | 17 | ||||
-rw-r--r-- | bot/exts/recruitment/talentpool/_cog.py | 55 | ||||
-rw-r--r-- | bot/exts/recruitment/talentpool/_review.py | 33 |
3 files changed, 41 insertions, 64 deletions
diff --git a/bot/exts/recruitment/talentpool/_api.py b/bot/exts/recruitment/talentpool/_api.py index f968b639b..85dadb0f6 100644 --- a/bot/exts/recruitment/talentpool/_api.py +++ b/bot/exts/recruitment/talentpool/_api.py @@ -89,5 +89,20 @@ class NominationAPI: reason: str, ) -> None: """Edit a nomination entry.""" - data = {"actor_id": str(actor_id), "reason": reason} + data = {"actor": str(actor_id), "reason": reason} await self.site_api.patch(f"bot/nominations/{nomination_id}", json=data) + + async def post_nomination( + self, + user_id: int, + actor_id: int, + reason: str, + ) -> Nomination: + """Post a nomination to site.""" + data = { + "actor": actor_id, + "reason": reason, + "user": user_id, + } + result = await self.site_api.post("bot/nominations", json=data) + return Nomination.parse_obj(result) diff --git a/bot/exts/recruitment/talentpool/_cog.py b/bot/exts/recruitment/talentpool/_cog.py index 43a9a28ff..b81411c29 100644 --- a/bot/exts/recruitment/talentpool/_cog.py +++ b/bot/exts/recruitment/talentpool/_cog.py @@ -234,29 +234,17 @@ class TalentPool(Cog, name="Talentpool"): await ctx.send(f":x: The reason's length must not exceed {REASON_MAX_CHARS} characters.") return - # Manual request with `raise_for_status` as False because we want the actual response - session = self.bot.api_client.session - url = self.bot.api_client._url_for('bot/nominations') - kwargs = { - 'json': { - 'actor': ctx.author.id, - 'reason': reason, - 'user': user.id - }, - 'raise_for_status': False, - } - async with session.post(url, **kwargs) as resp: - response_data = await resp.json() - - if resp.status == 400: - if response_data.get('user', False): + try: + await self.api.post_nomination(user.id, ctx.author.id, reason) + except ResponseCodeError as err: + if err.status == 400: + if err.response_json.get("user", False): await ctx.send(f":x: {user.mention} can't be found in the database tables.") - elif response_data.get('actor', False): + return + elif err.response_json.get('actor', False): await ctx.send(f":x: You have already nominated {user.mention}.") - - return - else: - resp.raise_for_status() + return + raise await ctx.send(f"✅ The nomination for {user.mention} has been added to the talent pool.") @@ -264,7 +252,7 @@ class TalentPool(Cog, name="Talentpool"): @has_any_role(*MODERATION_ROLES) async def history_command(self, ctx: Context, user: MemberOrUser) -> None: """Shows the specified user's nomination history.""" - result = await self.api.get_nominations(user_id=user.id, ordering="-active,-inserted_at") + result = await self.api.get_nominations(user.id, ordering="-active,-inserted_at") if not result: await ctx.send(f":warning: {user.mention} has never been nominated.") @@ -425,21 +413,14 @@ class TalentPool(Cog, name="Talentpool"): await self.api.edit_nomination(nomination_id, end_reason=reason) await ctx.send(f":white_check_mark: Updated the nomination end reason for <@{nomination.user_id}>.") - @nomination_group.command(aliases=('mr',)) - @has_any_role(*MODERATION_ROLES) - async def mark_reviewed(self, ctx: Context, user_id: int) -> None: - """Mark a user's nomination as reviewed and cancel the review task.""" - if not await self.reviewer.mark_reviewed(ctx, user_id): - return - await ctx.send(f"{Emojis.check_mark} The user with ID `{user_id}` was marked as reviewed.") - @nomination_group.command(aliases=('gr',)) @has_any_role(*MODERATION_ROLES) async def get_review(self, ctx: Context, user_id: int) -> None: """Get the user's review as a markdown file.""" - nominations = await self.api.get_nominations(user_id=user_id) + nominations = await self.api.get_nominations(user_id, active=True) if not nominations: await ctx.send(f"There doesn't appear to be an active nomination for {user_id}") + return review, _, _ = await self.reviewer.make_review(nominations[0]) file = discord.File(StringIO(review), f"{user_id}_review.md") @@ -449,10 +430,16 @@ class TalentPool(Cog, name="Talentpool"): @has_any_role(*MODERATION_ROLES) async def post_review(self, ctx: Context, user_id: int) -> None: """Post the automatic review for the user ahead of time.""" - if not await self.reviewer.mark_reviewed(ctx, user_id): + nominations = await self.api.get_nominations(user_id, active=True) + if not nominations: + await ctx.send(f"There doesn't appear to be an active nomination for {user_id}") return - await self.reviewer.post_review(user_id, update_database=False) + nomination = nominations[0] + if nomination.reviewed: + await ctx.send(":x: This nomination was already reviewed, but here's a cookie :cookie:") + + await self.reviewer.post_review(nomination) await ctx.message.add_reaction(Emojis.check_mark) @Cog.listener() @@ -485,7 +472,7 @@ class TalentPool(Cog, name="Talentpool"): async def end_nomination(self, user_id: int, reason: str) -> bool: """End the active nomination of a user with the given reason and return True on success.""" - active_nominations = await self.api.get_nominations(user_id=user_id) + active_nominations = await self.api.get_nominations(user_id, active=True) if not active_nominations: log.debug(f"No active nominate exists for {user_id=}") diff --git a/bot/exts/recruitment/talentpool/_review.py b/bot/exts/recruitment/talentpool/_review.py index c238028fe..1ea46d158 100644 --- a/bot/exts/recruitment/talentpool/_review.py +++ b/bot/exts/recruitment/talentpool/_review.py @@ -10,7 +10,6 @@ from typing import List, Optional, Union from botcore.site_api import ResponseCodeError from discord import Embed, Emoji, Member, Message, NotFound, PartialMessage, TextChannel -from discord.ext.commands import Context from bot.bot import Bot from bot.constants import Channels, Colours, Emojis, Guild, Roles @@ -68,7 +67,7 @@ class Reviewer: if not nomination: return False - await self.post_review(nomination, True) + await self.post_review(nomination) return True async def is_ready_for_review(self) -> bool: @@ -121,7 +120,7 @@ class Reviewer: now = datetime.now(timezone.utc) possible: list[Nomination] = [] - nominations = await self.api.get_nominations() + nominations = await self.api.get_nominations(active=True) for nomination in nominations: time_since_nomination = now - nomination.inserted_at if ( @@ -157,7 +156,7 @@ class Reviewer: return entries_score * REVIEW_SCORE_WEIGHT + age_score - async def post_review(self, nomination: Nomination, update_database: bool) -> None: + async def post_review(self, nomination: Nomination) -> None: """Format the review of a user and post it to the nomination voting channel.""" review, reviewed_emoji, nominee = await self.make_review(nomination) if not nominee: @@ -181,8 +180,7 @@ class Reviewer: ) message = await thread.send(f"<@&{Roles.mod_team}> <@&{Roles.admins}>") - if update_database: - await self.api.edit_nomination(nomination.id, reviewed=True) + await self.api.edit_nomination(nomination.id, reviewed=True) bump_cog: ThreadBumper = self.bot.get_cog("ThreadBumper") if bump_cog: @@ -469,26 +467,3 @@ class Reviewer: results.append(await channel.send(message)) return results - - async def mark_reviewed(self, ctx: Context, user_id: int) -> bool: - """ - Mark an active nomination as reviewed, updating the database and canceling the review task. - - Returns True if the user was successfully marked as reviewed, False otherwise. - """ - # TODO: Remove this function or update it - log.trace(f"Updating user {user_id} as reviewed") - await self._pool.refresh_cache() - if user_id not in self._pool.cache: - log.trace(f"Can't find a nominated user with id {user_id}") - await ctx.send(f":x: Can't find a currently nominated user with id `{user_id}`") - return False - - nomination = self._pool.cache.get(user_id) - if nomination["reviewed"]: - await ctx.send(":x: This nomination was already reviewed, but here's a cookie :cookie:") - return False - - await self.bot.api_client.patch(f"bot/nominations/{nomination['id']}", json={"reviewed": True}) - - return True |