Skip to content

Commit

Permalink
Merge 15bfd9e into bd410a6
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Jan 7, 2022
2 parents bd410a6 + 15bfd9e commit 1662748
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
30 changes: 29 additions & 1 deletion api/tests/test_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
is_valid_github_request,
process_blacklist,
process_coc_reset,
process_watch,
)
from blossom.strings import translation
from utils.test_helpers import create_user
Expand Down Expand Up @@ -293,7 +294,7 @@ def test_process_blacklist_errors(message: str, response: str) -> None:


def test_process_coc_reset() -> None:
"""Test blacklist functionality and ensure that it works in reverse."""
"""Test reset functionality and ensure that it works in reverse."""
slack_client.chat_postMessage = MagicMock()

test_user = create_user()
Expand Down Expand Up @@ -348,6 +349,33 @@ def test_process_coc_reset_errors(message: str, response: str) -> None:
assert slack_client.chat_postMessage.call_args[1]["text"] == response


@pytest.mark.parametrize(
"message,percentage",
[
("watch u123", 1),
("watch u123 50", 0.5),
("watch u123 75%", 0.75),
("watch <https://reddit.com/u/u123|u123> 10", 0.1),
],
)
def test_process_watch(message: str, percentage: float) -> None:
"""Test watch functionality."""
slack_client.chat_postMessage = MagicMock()

test_user = create_user(username="u123")
assert test_user.overwrite_check_percentage is None
# process the message
process_watch("", message)
slack_client.chat_postMessage.assert_called_once()
test_user.refresh_from_db()
expected_message = i18n["slack"]["watch"]["success"].format(
user=test_user.username, percentage=percentage * 100
)

assert test_user.overwrite_check_percentage == percentage
assert slack_client.chat_postMessage.call_args[1]["text"] == expected_message


@pytest.mark.parametrize(
"message", [("dadjoke"), ("dadjoke <@asdf>"), ("dadjoke a b c")],
)
Expand Down
49 changes: 49 additions & 0 deletions api/views/slack_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,54 @@ def process_coc_reset(channel: str, message: str) -> None:
client.chat_postMessage(channel=channel, text=msg)


def process_watch(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
user.save()

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 Expand Up @@ -323,6 +371,7 @@ def process_message(data: Dict) -> None:
"reset": process_coc_reset,
"info": send_info,
"blacklist": process_blacklist,
"watch": process_watch,
"dadjoke": dadjoke_target,
}

Expand Down
9 changes: 9 additions & 0 deletions api/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,18 @@ def _should_check_transcription(volunteer: BlossomUser) -> bool:
is to verify that they are continuing to do a good job, not to constantly
be looking over their shoulder.
This can also be overwritten manually by a mod through the
overwrite_check_percentage field.
:param volunteer: the volunteer for which the post should be checked
:return: whether the post should be checked
"""
if percentage := volunteer.overwrite_check_percentage:
if random.random() < percentage:
return True
else:
return False

probabilities = [
(5, 1),
(50, 0.4),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.10 on 2022-01-06 20:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("authentication", "0005_blossomuser_is_bot"),
]

operations = [
migrations.AddField(
model_name="blossomuser",
name="overwrite_check_percentage",
field=models.FloatField(blank=True, default=None, null=True),
),
]
6 changes: 6 additions & 0 deletions authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class Meta:
APIKey, on_delete=models.CASCADE, null=True, blank=True
)

# The percentage used to determine if a transcription should be checked by the mods.
# If this is set, it will overwrite the automatically determined percentage
# based on the user's gamma.
# This must be a number between 0 and 1 (inclusive).
overwrite_check_percentage = models.FloatField(null=True, blank=True, default=None)

# The time that this record was last updated.
last_update_time = models.DateTimeField(default=timezone.now)
# Whether this particular user has accepted our Code of Conduct.
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 1662748

Please sign in to comment.