Skip to content

Commit

Permalink
Merge db50f09 into 295a3d5
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Feb 22, 2022
2 parents 295a3d5 + db50f09 commit bb238d8
Show file tree
Hide file tree
Showing 11 changed files with 485 additions and 421 deletions.
22 changes: 15 additions & 7 deletions api/slack/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,21 @@ def watch_cmd(channel: str, message: str) -> None:
client.chat_postMessage(channel=channel, text=msg)
return

# Overwrite the check percentage
user.overwrite_check_percentage = decimal_percentage
user.save()
# Only allow to set the percentage if it's higher than the default
if decimal_percentage < user.auto_check_percentage:
msg = i18n["slack"]["watch"]["percentage_too_low"].format(
auto_percentage=user.auto_check_percentage
)
else:
# Remember the percentage before the overwrite
previous = user.transcription_check_reason(ignore_low_activity=True)
# Overwrite the check percentage
user.overwrite_check_percentage = decimal_percentage
user.save()

msg = i18n["slack"]["watch"]["success"].format(
user=user.username, percentage=decimal_percentage
)
msg = i18n["slack"]["watch"]["success"].format(
user=user.username, percentage=decimal_percentage, previous=previous
)
else:
msg = i18n["slack"]["errors"]["unknown_username"]

Expand Down Expand Up @@ -206,7 +214,7 @@ def watchlist_cmd(channel: str, message: str) -> None:
parsed_message = message.split()
sorting = parsed_message[1] if len(parsed_message) > 1 else "percentage"

response_msg = "**List of all watched users:**\n\n"
response_msg = "*List of all watched users:*\n\n"

watched_users: List[BlossomUser] = list(
BlossomUser.objects.filter(overwrite_check_percentage__isnull=False)
Expand Down
52 changes: 24 additions & 28 deletions api/slack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,43 +82,39 @@ def get_message(data: Dict) -> Optional[str]:
return None


def _send_transcription_to_slack(
def send_transcription_check(
transcription: Transcription,
submission: Submission,
user: BlossomUser,
slack_client: WebClient,
reason: str,
) -> None:
"""Notify slack for the transcription check."""
url = None
# it's possible that we either won't pull a transcription object OR that
# a transcription object won't have a URL. If either fails, then we default
# to the submission's URL.
if transcription:
url = transcription.url
if not url:
url = submission.tor_url

url = "https://reddit.com" + url if submission.source == "reddit" else url

msg = f"Please check the following transcription of " f"u/{user.username}: {url}."

if user.overwrite_check_percentage is not None:
# Let the mods know that the user is being watched
percentage = user.overwrite_check_percentage
msg += (
f"\n\nThis user is being watched with a chance of {percentage:.0%}.\n"
+ f"Undo this using the `unwatch {user.username}` command."
)

# the `done` process is still going here, so they technically don't have
# a transcription yet. It's about to get assigned, but for right now the
# value is still zero.
if user.gamma == 0:
msg = ":rotating_light: First transcription! :rotating_light: " + msg
gamma = user.gamma
msg = f"*Transcription check* for u/{user.username} ({user.gamma:,d}):\n"

# Add relevant links
tor_url = (
"<{}|ToR Post>".format(submission.tor_url) if submission.tor_url else "[N/A]"
)
post_url = "<{}|Partner Post>".format(submission.url) if submission.url else "[N/A]"
transcription_url = (
"<{}|Transcription>".format(transcription.url)
if transcription.url and not transcription.removed_from_reddit
else "[Removed]"
)
msg += " | ".join([tor_url, post_url, transcription_url]) + "\n"

# Add check reason
msg += f"Reason: {reason}\n"

# Is it the first transcription? Extra care has to be taken
if gamma == 1:
msg += ":rotating_light: First transcription! :rotating_light:"

try:
slack_client.chat_postMessage(
channel=settings.SLACK_TRANSCRIPTION_CHECK_CHANNEL, text=msg,
channel=settings.SLACK_TRANSCRIPTION_CHECK_CHANNEL, text=msg.strip(),
)
except: # noqa
logger.warning(f"Cannot post message to slack. Msg: {msg}")
26 changes: 12 additions & 14 deletions api/tests/submissions/test_submission_approve.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import json
from unittest.mock import MagicMock
from unittest.mock import patch

from django.test import Client
from django.urls import reverse
from rest_framework import status

from api.slack import client as slack_client
from utils.test_helpers import create_submission, setup_user_client


Expand Down Expand Up @@ -121,8 +120,6 @@ def test_approve_reverting_removal(self, client: Client) -> None:

def test_approve_update_report_message(self, client: Client) -> None:
"""Verify that approving updates the report message, if available."""
mock = MagicMock()
slack_client.chat_update = mock
client, headers, user = setup_user_client(client)

submission = create_submission(
Expand All @@ -136,15 +133,16 @@ def test_approve_update_report_message(self, client: Client) -> None:

data = {}

result = client.patch(
reverse("submission-approve", args=[submission.id]),
json.dumps(data),
content_type="application/json",
**headers
)
with patch("api.slack.client.chat_update") as mock:
result = client.patch(
reverse("submission-approve", args=[submission.id]),
json.dumps(data),
content_type="application/json",
**headers
)

submission.refresh_from_db()
submission.refresh_from_db()

assert result.status_code == status.HTTP_200_OK
assert submission.approved
assert mock.call_count == 1
assert result.status_code == status.HTTP_200_OK
assert submission.approved
assert mock.call_count == 1
Loading

0 comments on commit bb238d8

Please sign in to comment.