Skip to content

Commit

Permalink
Rewrite case welcome into add_or_reactivate flow (#4967)
Browse files Browse the repository at this point in the history
  • Loading branch information
whitdog47 authored Jul 17, 2024
1 parent cbd5695 commit f5ba3c7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 50 deletions.
8 changes: 8 additions & 0 deletions src/dispatch/case/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from sqlalchemy.orm import Session

from dispatch.case import service as case_service
from dispatch.case.messaging import send_case_welcome_participant_message
from dispatch.case.models import CaseRead
from dispatch.conversation import flows as conversation_flows
from dispatch.database.core import SessionLocal
Expand Down Expand Up @@ -129,6 +130,13 @@ def case_add_or_reactivate_participant_flow(
case, [participant.individual.email], db_session
)

# we send the welcome messages to the participant
send_case_welcome_participant_message(
participant_email=user_email,
case=case,
db_session=db_session
)

return participant


Expand Down
31 changes: 31 additions & 0 deletions src/dispatch/case/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from dispatch.plugin import service as plugin_service
from dispatch.event import service as event_service
from dispatch.notification import service as notification_service
from dispatch.plugins.dispatch_slack.case.messages import create_welcome_ephemeral_message_to_participant

from .enums import CaseStatus

Expand Down Expand Up @@ -302,3 +303,33 @@ def send_case_update_notifications(
)

log.debug("Case updated notifications sent.")


def send_case_welcome_participant_message(
*,
participant_email: str,
case: Case,
db_session: Session,
):
if not case.dedicated_channel:
return

plugin = plugin_service.get_active_instance(
db_session=db_session, project_id=case.project.id, plugin_type="conversation"
)

if not plugin:
log.warning(
"Case participant welcome message not sent. No conversation plugin enabled."
)
return

welcome_message = create_welcome_ephemeral_message_to_participant(case=case)
plugin.instance.send_ephemeral(
conversation_id=case.conversation.channel_id,
user=participant_email,
text=f"Welcome to {case.name}",
blocks=welcome_message,
)

log.debug(f"Welcome ephemeral message sent to {participant_email}.")
50 changes: 0 additions & 50 deletions src/dispatch/plugins/dispatch_slack/case/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from functools import partial
import json
import pytz
import re

from blockkit import (
Actions,
Expand Down Expand Up @@ -58,7 +57,6 @@
from dispatch.plugins.dispatch_slack.case.messages import (
create_case_message,
create_signal_engagement_message,
create_welcome_ephemeral_message_to_participant,
)
from dispatch.plugins.dispatch_slack.config import SlackConversationConfiguration
from dispatch.plugins.dispatch_slack.decorators import message_dispatcher
Expand Down Expand Up @@ -97,7 +95,6 @@
SignalSubjects,
SubjectMetadata,
)
from dispatch.plugins.dispatch_slack.service import get_user_email
from dispatch.project import service as project_service
from dispatch.search.utils import create_filter_expression
from dispatch.signal import service as signal_service
Expand Down Expand Up @@ -939,53 +936,6 @@ def handle_new_participant_message(
participant.user_conversation_id = context["user_id"]


@message_dispatcher.add(
subject=CaseSubjects.case, exclude={"subtype": ["channel_join", "channel_leave"]}
) # we ignore channel join and leave messages
def handle_new_participant_added(
ack: Ack,
payload: dict,
context: BoltContext,
db_session: Session,
client: WebClient,
) -> None:
"""Looks for new participants being added to conversation via @<user-name>"""
ack()
participants = re.findall(r"\<\@([a-zA-Z0-9]*)\>", payload["text"])
for user_id in participants:
try:
case: Case = context["subject"]
user_email = get_user_email(client=client, user_id=user_id)

# check if user is already a participant
participant = participant_service.get_by_case_id_and_email(
db_session=db_session, case_id=case.id, email=user_email
)

if participant and participant.active_roles:
continue

participant = case_flows.case_add_or_reactivate_participant_flow(
case_id=case.id,
user_email=user_email,
db_session=db_session,
add_to_conversation=False,
)
participant.user_conversation_id = user_id

case = case_service.get(db_session=db_session, case_id=case.id)
if case.dedicated_channel:
welcome_message = create_welcome_ephemeral_message_to_participant(case=case)
client.chat_postEphemeral(
blocks=welcome_message,
channel=payload["channel"],
user=user_id,
)
except Exception as e:
log.warn(f"Error adding participant {user_id} to Case {context['subject'].id}: {e}")
continue


@message_dispatcher.add(
subject=CaseSubjects.case, exclude={"subtype": ["channel_join", "channel_leave"]}
) # we ignore channel join and leave messages
Expand Down

0 comments on commit f5ba3c7

Please sign in to comment.