From 60bfddf5d313d5bc560313e647e307d89521b994 Mon Sep 17 00:00:00 2001 From: Kevin Glisson Date: Tue, 7 Feb 2023 13:33:28 -0800 Subject: [PATCH 1/2] Direct add participants --- src/dispatch/case/flows.py | 10 ++++++++++ src/dispatch/participant/service.py | 12 ++++++++++++ .../plugins/dispatch_slack/case/interactive.py | 17 +++++++++++++++++ .../plugins/dispatch_slack/middleware.py | 12 +++++++++--- 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/dispatch/case/flows.py b/src/dispatch/case/flows.py index 5f41a0ac18ce..46913574c491 100644 --- a/src/dispatch/case/flows.py +++ b/src/dispatch/case/flows.py @@ -282,6 +282,16 @@ def case_new_create_flow(*, case_id: int, organization_slug: OrganizationSlug, d ) # wait until all resources are created before adding suggested participants individual_participants = [x.email for x, _ in individual_participants] + + for email in individual_participants: + # we don't rely on on this flow to add folks to the conversation because in this case + # we want to do it in bulk + case_add_or_reactive_participant_flow( + db_session=db_session, + user_email=email, + case_id=case.id, + add_to_conversation=False, + ) conversation_plugin.instance.add_to_thread( case.conversation.channel_id, case.conversation.thread_id, diff --git a/src/dispatch/participant/service.py b/src/dispatch/participant/service.py index 9fd60947b644..deb633cdcb99 100644 --- a/src/dispatch/participant/service.py +++ b/src/dispatch/participant/service.py @@ -79,6 +79,18 @@ def get_by_incident_id_and_conversation_id( ) +def get_by_case_id_and_conversation_id( + *, db_session, case_id: int, user_conversation_id: str +) -> Optional[Participant]: + """Get participant by case and user_conversation id.""" + return ( + db_session.query(Participant) + .filter(Participant.case_id == case_id) + .filter(Participant.user_conversation_id == user_conversation_id) + .one_or_none() + ) + + def get_all(*, db_session) -> List[Optional[Participant]]: """Get all participants.""" return db_session.query(Participant) diff --git a/src/dispatch/plugins/dispatch_slack/case/interactive.py b/src/dispatch/plugins/dispatch_slack/case/interactive.py index 7213072f21b6..eabe6436dbf9 100644 --- a/src/dispatch/plugins/dispatch_slack/case/interactive.py +++ b/src/dispatch/plugins/dispatch_slack/case/interactive.py @@ -73,6 +73,23 @@ def assignee_select( ) +@message_dispatcher.add( + subject="case", exclude={"subtype": ["channel_join", "channel_leave"]} +) # we ignore channel join and leave messages +def handle_new_participant_message( + ack: Ack, user: DispatchUser, context: BoltContext, db_session: Session, client: WebClient +) -> None: + """Looks for new participants that have starting chatting for the first time.""" + ack() + participant = case_flows.case_add_or_reactive_participant_flow( + case_id=context["subject"].id, + user_email=user.email, + db_session=db_session, + add_to_conversation=False, + ) + participant.user_conversation_id = context["user_id"] + + @message_dispatcher.add( subject="case", exclude={"subtype": ["channel_join", "channel_leave"]} ) # we ignore channel join and leave messages diff --git a/src/dispatch/plugins/dispatch_slack/middleware.py b/src/dispatch/plugins/dispatch_slack/middleware.py index c4ccb0da322b..65f4ad0dc229 100644 --- a/src/dispatch/plugins/dispatch_slack/middleware.py +++ b/src/dispatch/plugins/dispatch_slack/middleware.py @@ -198,9 +198,15 @@ def user_middleware( ) db_session = refetch_db_session(slug) - participant = participant_service.get_by_incident_id_and_conversation_id( - db_session=db_session, incident_id=context["subject"].id, user_conversation_id=user_id - ) + if context["subject"].type == "incident": + participant = participant_service.get_by_incident_id_and_conversation_id( + db_session=db_session, incident_id=context["subject"].id, user_conversation_id=user_id + ) + else: + participant = participant_service.get_by_case_id_and_conversation_id( + db_session=db_session, case_id=context["subject"].id, user_conversation_id=user_id + ) + if participant: context["user"] = user_service.get_or_create( db_session=db_session, From 02e53ce0fa62ecedb41f35627a7d16ad115a6fb3 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Tue, 7 Feb 2023 13:48:18 -0800 Subject: [PATCH 2/2] Update src/dispatch/case/flows.py Co-authored-by: Will Sheldon <114631109+wssheldon@users.noreply.github.com> --- src/dispatch/case/flows.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dispatch/case/flows.py b/src/dispatch/case/flows.py index 46913574c491..912e9199abfa 100644 --- a/src/dispatch/case/flows.py +++ b/src/dispatch/case/flows.py @@ -286,7 +286,7 @@ def case_new_create_flow(*, case_id: int, organization_slug: OrganizationSlug, d for email in individual_participants: # we don't rely on on this flow to add folks to the conversation because in this case # we want to do it in bulk - case_add_or_reactive_participant_flow( + case_add_or_reactivate_participant_flow( db_session=db_session, user_email=email, case_id=case.id,