Skip to content

Commit

Permalink
Merge f0cf28e into 240d8f4
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Feb 28, 2022
2 parents 240d8f4 + f0cf28e commit 6987848
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
4 changes: 3 additions & 1 deletion api/slack/transcription_check/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from django.utils import timezone

from api.models import TranscriptionCheck
from api.slack import client
from api.slack.transcription_check.messages import (
reply_to_action_with_ping,
update_check_message,
)
from api.slack.utils import get_display_name
from authentication.models import BlossomUser

logger = logging.getLogger("api.slack.transcription_check.actions")
Expand Down Expand Up @@ -59,7 +61,7 @@ def process_check_action(data: Dict) -> None:
parts = value.split("_")
action = parts[1]
check_id = parts[2]
mod_username = data["user"]["name"]
mod_username = get_display_name(client, data["user"])

# Retrieve the corresponding objects form the DB
check = TranscriptionCheck.objects.filter(id=check_id).first()
Expand Down
2 changes: 1 addition & 1 deletion api/slack/transcription_check/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def update_check_message(check: TranscriptionCheck) -> None:
def reply_to_action(data: Dict, text: str) -> Dict:
"""Reply to the given action with the given text."""
channel_id = data["channel"]["id"]
message_ts = data["message_ts"]
message_ts = data.get("message", {}).get("ts")

return client.chat_postMessage(channel=channel_id, thread_ts=message_ts, text=text)

Expand Down
9 changes: 9 additions & 0 deletions api/slack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,12 @@ def send_transcription_check(
)
except: # noqa
logger.warning(f"Cannot post message to slack. Msg: {msg}")


def get_display_name(slack_client: WebClient, user: Dict) -> Optional[str]:
"""Get the display name for the given user."""
response = slack_client.users_profile_get(user=user.get("id"))
if response.get("ok"):
return response.get("profile", {}).get("display_name")
else:
return None
25 changes: 20 additions & 5 deletions api/tests/slack/transcription_check/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ def test_process_check_action(client: Client) -> None:

with patch(
"api.slack.transcription_check.actions.update_check_message", return_value=None
) as mock:
) as mock, patch(
"api.slack.transcription_check.actions.get_display_name",
lambda _, us: us["name"],
):
process_check_action(data)

check.refresh_from_db()
Expand Down Expand Up @@ -224,7 +227,10 @@ def test_process_check_action_unknown_check(client: Client) -> None:
) as update_mock, patch(
"api.slack.transcription_check.actions.reply_to_action_with_ping",
return_value={},
) as reply_mock:
) as reply_mock, patch(
"api.slack.transcription_check.actions.get_display_name",
lambda _, us: us["name"],
):
process_check_action(data)

check.refresh_from_db()
Expand Down Expand Up @@ -263,7 +269,10 @@ def test_process_check_action_unknown_mod(client: Client) -> None:
) as update_mock, patch(
"api.slack.transcription_check.actions.reply_to_action_with_ping",
return_value={},
) as reply_mock:
) as reply_mock, patch(
"api.slack.transcription_check.actions.get_display_name",
lambda _, us: us["name"],
):
process_check_action(data)

check.refresh_from_db()
Expand Down Expand Up @@ -308,7 +317,10 @@ def test_process_check_action_no_mod(client: Client) -> None:
) as update_mock, patch(
"api.slack.transcription_check.actions.reply_to_action_with_ping",
return_value={},
) as reply_mock:
) as reply_mock, patch(
"api.slack.transcription_check.actions.get_display_name",
lambda _, us: us["name"],
):
process_check_action(data)

check.refresh_from_db()
Expand Down Expand Up @@ -353,7 +365,10 @@ def test_process_check_action_wrong_mod(client: Client) -> None:
) as update_mock, patch(
"api.slack.transcription_check.actions.reply_to_action_with_ping",
return_value={},
) as reply_mock:
) as reply_mock, patch(
"api.slack.transcription_check.actions.get_display_name",
lambda _, us: us["name"],
):
process_check_action(data)

check.refresh_from_db()
Expand Down

0 comments on commit 6987848

Please sign in to comment.