diff --git a/src/dispatch/case/flows.py b/src/dispatch/case/flows.py index fe362db9a714..88fc2ab48631 100644 --- a/src/dispatch/case/flows.py +++ b/src/dispatch/case/flows.py @@ -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 @@ -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 diff --git a/src/dispatch/case/messaging.py b/src/dispatch/case/messaging.py index d59f7325c58f..2d21377328d0 100644 --- a/src/dispatch/case/messaging.py +++ b/src/dispatch/case/messaging.py @@ -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 @@ -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}.") diff --git a/src/dispatch/plugins/dispatch_slack/case/interactive.py b/src/dispatch/plugins/dispatch_slack/case/interactive.py index e401784d8717..c8fc8574f5f2 100644 --- a/src/dispatch/plugins/dispatch_slack/case/interactive.py +++ b/src/dispatch/plugins/dispatch_slack/case/interactive.py @@ -5,7 +5,6 @@ from functools import partial import json import pytz -import re from blockkit import ( Actions, @@ -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 @@ -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 @@ -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 @""" - 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