Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issue with the handling of slack events. #1338

Merged
merged 2 commits into from
Jun 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/dispatch/plugins/dispatch_slack/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import List
from pydantic import BaseModel

from dispatch.decorators import background_task
from dispatch.event import service as event_service
from dispatch.incident import flows as incident_flows
from dispatch.incident import service as incident_service
Expand All @@ -19,9 +18,7 @@
SLACK_BAN_THREADS,
)
from .service import get_user_email
from .decorators import get_organization_from_channel_id

slack_client = dispatch_slack_service.create_slack_client()
from .decorators import slack_background_task, get_organization_from_channel_id


class EventBodyItem(BaseModel):
Expand Down Expand Up @@ -115,20 +112,25 @@ async def handle_slack_event(*, client, event, background_tasks):
for f in event_functions(event):
background_tasks.add_task(
f,
user_id,
user_email,
channel_id,
conversation.incident_id,
event=event,
)

return {"ok": ""}


@background_task
@slack_background_task
def handle_reaction_added_event(
user_id: str,
user_email: str,
channel_id: str,
incident_id: int,
event: EventEnvelope = None,
db_session=None,
slack_client=None,
):
"""Handles an event where a reaction is added to a message."""
reaction = event.event.reaction
Expand Down Expand Up @@ -168,12 +170,15 @@ def is_business_hours(commander_tz: str):
return now.weekday() not in [5, 6] and 9 <= now.hour < 17


@background_task
@slack_background_task
def after_hours(
user_id: str,
user_email: str,
channel_id: str,
incident_id: int,
event: EventEnvelope = None,
db_session=None,
slack_client=None,
):
"""Notifies the user that this incident is current in after hours mode."""
# we ignore user channel and group join messages
Expand Down Expand Up @@ -210,21 +215,23 @@ def after_hours(
db_session=db_session, incident_id=incident_id, email=user_email
)
if not participant.after_hours_notification:
user_id = dispatch_slack_service.resolve_user(slack_client, user_email)["id"]
dispatch_slack_service.send_ephemeral_message(
slack_client, incident.conversation.channel_id, user_id, "", blocks=blocks
slack_client, channel_id, user_id, "", blocks=blocks
)
participant.after_hours_notification = True
db_session.add(participant)
db_session.commit()


@background_task
@slack_background_task
def member_joined_channel(
user_id: str,
user_email: str,
channel_id: str,
incident_id: int,
event: EventEnvelope,
db_session=None,
slack_client=None,
):
"""Handles the member_joined_channel slack event."""
participant = incident_flows.incident_add_or_reactivate_participant_flow(
Expand Down Expand Up @@ -257,23 +264,29 @@ def member_joined_channel(
db_session.commit()


@background_task
@slack_background_task
def member_left_channel(
user_id: str,
user_email: str,
channel_id: str,
incident_id: int,
event: EventEnvelope,
db_session=None,
slack_client=None,
):
"""Handles the member_left_channel Slack event."""
incident_flows.incident_remove_participant_flow(user_email, incident_id, db_session=db_session)


@background_task
@slack_background_task
def ban_threads_warning(
user_id: str,
user_email: str,
channel_id: str,
incident_id: int,
event: EventEnvelope = None,
db_session=None,
slack_client=None,
):
"""Sends the user an ephemeral message if they use threads."""
if not SLACK_BAN_THREADS:
Expand All @@ -288,9 +301,8 @@ def ban_threads_warning(
# value, when they differ the latter is a reply to the former.
message = "Please refrain from using threads in incident related channels. Threads make it harder for incident participants to maintain context."
dispatch_slack_service.send_ephemeral_message(
slack_client,
event.event.channel,
event.event.user,
channel_id,
user_id,
message,
thread_ts=event.event.thread_ts,
)