Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
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
22 changes: 16 additions & 6 deletions src/dispatch/conversation/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ def get(*, db_session, conversation_id: int) -> Optional[Conversation]:
return db_session.query(Conversation).filter(Conversation.id == conversation_id).one_or_none()


def get_by_channel_id_ignoring_channel_type(db_session, channel_id: str) -> Optional[Conversation]:
def get_by_channel_id_ignoring_channel_type(
db_session, channel_id: str, thread_id: str = None
) -> Optional[Conversation]:
"""
Gets a conversation by its id ignoring the channel type, and updates the
channel id in the database if the channel type has changed.
"""
channel_id_without_type = channel_id[1:]
conversation = (
db_session.query(Conversation)
.filter(Conversation.channel_id.contains(channel_id_without_type))
.one_or_none()
)
if thread_id:
conversation = (
db_session.query(Conversation)
.filter(Conversation.channel_id.contains(channel_id_without_type))
.filter(Conversation.thread_id == thread_id)
.one_or_none()
)
else:
conversation = (
db_session.query(Conversation)
.filter(Conversation.channel_id.contains(channel_id_without_type))
.one_or_none()
)

if conversation:
if channel_id[0] != conversation.channel_id[0]:
Expand Down
1 change: 1 addition & 0 deletions src/dispatch/plugins/dispatch_slack/case/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class CaseNotificationActions(DispatchEnum):
join_incident = "case-notification-join-incident"
reopen = "case-notification-reopen"
resolve = "case-notification-resolve"
triage = "case-notification-triage"


class CasePaginateActions(DispatchEnum):
Expand Down
17 changes: 17 additions & 0 deletions src/dispatch/plugins/dispatch_slack/case/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,13 @@ def handle_case_participant_role_activity(
case_id=context["subject"].id, user_email=user.email, db_session=db_session
)
participant.user_conversation_id = context["user_id"]

# if a participant is active mark the case as being in the triaged state
case = case_service.get(db_session=db_session, case_id=context["subject"].id)
if case.status == CaseStatus.new:
case.status = CaseStatus.triage
db_session.commit()
case_flows.update_conversation(case, db_session)


@message_dispatcher.add(
Expand Down Expand Up @@ -1207,6 +1213,17 @@ def resolve_button_click(
client.views_open(trigger_id=body["trigger_id"], view=modal)


@app.action(CaseNotificationActions.triage, middleware=[button_context_middleware, db_middleware])
def triage_button_click(
ack: Ack, body: dict, db_session: Session, context: BoltContext, client: WebClient
):
ack()
case = case_service.get(db_session=db_session, case_id=context["subject"].id)
case.status = CaseStatus.triage
db_session.commit()
case_flows.update_conversation(case, db_session)


@app.view(
CaseResolveActions.submit,
middleware=[action_context_middleware, db_middleware, user_middleware, modal_submit_middleware],
Expand Down
57 changes: 31 additions & 26 deletions src/dispatch/plugins/dispatch_slack/case/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,37 @@ def create_case_message(case: Case, channel_id: str) -> list[Block]:
]
)
else:
blocks.extend(
[
Actions(
elements=[
Button(
text="Edit",
action_id=CaseNotificationActions.edit,
style="primary",
value=button_metadata,
),
Button(
text="Resolve",
action_id=CaseNotificationActions.resolve,
style="primary",
value=button_metadata,
),
Button(
text="Escalate",
action_id=CaseNotificationActions.escalate,
style="danger",
value=button_metadata,
),
]
)
]
)
action_buttons = [
Button(
text="Resolve",
action_id=CaseNotificationActions.resolve,
style="primary",
value=button_metadata,
),
Button(
text="Edit",
action_id=CaseNotificationActions.edit,
style="primary",
value=button_metadata,
),
Button(
text="Escalate",
action_id=CaseNotificationActions.escalate,
style="danger",
value=button_metadata,
),
]
if case.status == CaseStatus.new:
action_buttons.insert(
0,
Button(
text="Triage",
action_id=CaseNotificationActions.triage,
style="primary",
value=button_metadata,
),
)
blocks.extend([Actions(elements=action_buttons)])

return Message(blocks=blocks).build()["blocks"]

Expand Down
11 changes: 7 additions & 4 deletions src/dispatch/plugins/dispatch_slack/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@


@timer
def resolve_context_from_conversation(channel_id: str) -> Optional[Subject]:
"""Attempts to resolve a conversation based on the channel id."""
def resolve_context_from_conversation(channel_id: str, thread_id: str = None) -> Optional[Subject]:
"""Attempts to resolve a conversation based on the channel id and thread_id."""
db_session = SessionLocal()
organization_slugs = [o.slug for o in organization_service.get_all(db_session=db_session)]
db_session.close()
Expand All @@ -37,7 +37,7 @@ def resolve_context_from_conversation(channel_id: str) -> Optional[Subject]:
scoped_db_session = refetch_db_session(slug)

conversation = conversation_service.get_by_channel_id_ignoring_channel_type(
db_session=scoped_db_session, channel_id=channel_id
db_session=scoped_db_session, channel_id=channel_id, thread_id=thread_id
)

if conversation:
Expand Down Expand Up @@ -146,14 +146,17 @@ def message_context_middleware(
if is_bot(request):
return context.ack()

if subject := resolve_context_from_conversation(channel_id=context.channel_id):
if subject := resolve_context_from_conversation(
channel_id=context.channel_id, thread_id=payload.get("thread_ts")
):
context.update(subject._asdict())
else:
raise ContextError("Unable to determine context for message.")

next()


# TODO should we support reactions for cases?
def reaction_context_middleware(context: BoltContext, next: Callable) -> None:
"""Attemps to determine the current context of a reaction event."""
if subject := resolve_context_from_conversation(channel_id=context.channel_id):
Expand Down