Skip to content

Commit

Permalink
Merge e659596 into fd2c35b
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Jan 28, 2022
2 parents fd2c35b + e659596 commit ad66aee
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 23 deletions.
12 changes: 12 additions & 0 deletions api/slack/events.py → api/slack/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ class ReportMessageStatus(Enum):
APPROVED = "approved"


def process_action(data: Dict) -> None:
"""Process a Slack action, e.g. a button press."""
value = data["actions"][0].get("value")
if "approve" in value or "remove" in value:
process_submission_report_update(data)
else:
client.chat_postMessage(
channel=data["channel"]["id"],
text=i18n["slack"]["errors"]["unknown_payload"].format(value),
)


def send_github_sponsors_message(data: Dict, action: str) -> None:
"""
Process the POST request from GitHub Sponsors.
Expand Down
18 changes: 2 additions & 16 deletions api/slack/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

import requests

from api.helpers import fire_and_forget
from api.serializers import VolunteerSerializer
from api.slack import client
from api.slack.events import process_submission_report_update
from api.slack.utils import clean_links, dict_to_table, get_message
from api.views.misc import Summary
from authentication.models import BlossomUser
Expand All @@ -17,20 +15,8 @@
i18n = translation()


@fire_and_forget
def process_message(data: Dict) -> None:
"""Identify the purpose of a slack message and route accordingly."""
if data.get("type") == "block_actions":
value = data["actions"][0].get("value")
if "keep" in value or "remove" in value:
process_submission_report_update(data)
else:
client.chat_postMessage(
channel=data["channel"]["id"],
text=i18n["slack"]["errors"]["unknown_payload"].format(value),
)
return

def process_command(data: Dict) -> None:
"""Process a Slack command."""
e = data.get("event") # noqa: VNE001
channel = e.get("channel")

Expand Down
2 changes: 1 addition & 1 deletion api/tests/test_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pytest_django.fixtures import SettingsWrapper

from api.slack import client as slack_client
from api.slack.actions import is_valid_github_request
from api.slack.commands import (
blacklist_cmd,
dadjoke_cmd,
Expand All @@ -21,7 +22,6 @@
watch_cmd,
watchlist_cmd,
)
from api.slack.events import is_valid_github_request
from api.slack.utils import dict_to_table
from api.views.slack import github_sponsors_endpoint
from blossom.strings import translation
Expand Down
21 changes: 18 additions & 3 deletions api/views/slack.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
"""Views that specifically relate to communication with Slack."""
import json
from typing import Dict

from django.http import HttpRequest, HttpResponse
from django.views.decorators.csrf import csrf_exempt

from api.slack.commands import process_message
from api.slack.events import (
from api.helpers import fire_and_forget
from api.slack.actions import (
is_valid_github_request,
is_valid_slack_request,
process_action,
send_github_sponsors_message,
)
from api.slack.commands import process_command


@fire_and_forget
def _process_slack_message(data: Dict) -> None:
"""Process a Slack message and route it accordingly."""
if data.get("type") == "block_actions":
# It's an action, e.g. a button press
process_action(data)
else:
# It's a normal command
process_command(data)


@csrf_exempt
Expand Down Expand Up @@ -65,7 +79,8 @@ def slack_endpoint(request: HttpRequest) -> HttpResponse:
return HttpResponse(json_data["challenge"])
# It's not a challenge, so just hand off data processing to the
# thread and give Slack the result it craves.
process_message(json_data)
_process_slack_message(json_data)

return HttpResponse(status=200)


Expand Down
2 changes: 1 addition & 1 deletion api/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from api.pagination import StandardResultsSetPagination
from api.serializers import SubmissionSerializer
from api.slack import client as slack
from api.slack.events import ask_about_removing_post
from api.slack.actions import ask_about_removing_post
from api.slack.utils import _send_transcription_to_slack
from api.views.volunteer import VolunteerViewSet
from authentication.models import BlossomUser
Expand Down
2 changes: 1 addition & 1 deletion app/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from api.models import Source
from api.slack import client as slack_client
from api.slack.events import ask_about_removing_post
from api.slack.actions import ask_about_removing_post
from app.views import get_blossom_app_source
from utils.test_helpers import create_submission

Expand Down
2 changes: 1 addition & 1 deletion app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from rest_framework import status

from api.models import Source, Submission, Transcription
from api.slack.events import ask_about_removing_post
from api.slack.actions import ask_about_removing_post
from api.views.submission import SubmissionViewSet
from app.permissions import RequireCoCMixin, require_coc, require_reddit_auth
from app.reddit_actions import (
Expand Down

0 comments on commit ad66aee

Please sign in to comment.