Skip to content

Commit

Permalink
Add Slack watch command to overwrite check percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Jan 7, 2022
1 parent 96a6817 commit 2f53ab2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
46 changes: 46 additions & 0 deletions api/views/slack_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,52 @@ def process_coc_reset(channel: str, message: str) -> None:
client.chat_postMessage(channel=channel, text=msg)


def process_watch_request(channel: str, message: str) -> None:
"""Overwrite the transcription check percentage of a user."""
parsed_message = message.split()

if len(parsed_message) == 1:
# they didn't give a username
msg = i18n["slack"]["errors"]["missing_username"]
elif len(parsed_message) <= 3:
username = clean_links(parsed_message[1])
if user := BlossomUser.objects.filter(username__iexact=username).first():
if len(parsed_message) == 2:
# they didn't give a percentage, default to 100%
decimal_percentage = 1
else:
# parse the provided percentage
percentage = parsed_message[2]

try:
# Try to parse the new check percentage
percentage = int(percentage.rstrip(" %"))
if percentage < 0 or percentage > 100:
raise ValueError

decimal_percentage = percentage / 100
except ValueError:
# The percentage is invalid
msg = i18n["slack"]["watch"]["invalid_percentage"].format(
percentage=percentage
)
client.chat_postMessage(channel=channel, text=msg)
return

# Overwrite the check percentage
user.overwrite_check_percentage = decimal_percentage
msg = i18n["slack"]["watch"]["success"].format(
user=user["username"], percentage=decimal_percentage * 100
)
else:
msg = i18n["slack"]["errors"]["unknown_username"]

else:
msg = i18n["slack"]["errors"]["too_many_params"]

client.chat_postMessage(channel=channel, text=msg)


def dadjoke_target(channel: str, message: str, use_api: bool = True) -> None:
"""Send the pinged user a dad joke. Or just send everybody a joke."""
parsed_message = message.split()
Expand Down
4 changes: 4 additions & 0 deletions blossom/strings/en_US.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ github_sponsor_update="{0} GitHub Sponsors: [{1}] - {2} | {3} {0}"
success="Code of Conduct acceptance revoked for user '{0}'."
success_undo="Code of Conduct acceptance approved for user '{0}'."

[slack.watch]
invalid_percentage="{percentage} is an invalid percentage. Please provide a number between 0 and 100, such as '50%' or '75'."
success="The transcriptions of {user} now have a {percentage}% chance to be checked."

[slack.dadjoke]
message = "Hey {0}! {1}"
fallback_joke = "Did you know that fortune tellers make their tents purple? They believe the color has mythical properties. It allows them to tell the fuschia."

0 comments on commit 2f53ab2

Please sign in to comment.