11import logging
22
3- from typing import TypeVar , List
3+ from sqlalchemy . orm import Session
44
55from dispatch .case .models import Case
66from dispatch .conference .models import Conference
7- from dispatch .database .core import SessionLocal , resolve_attr
7+ from dispatch .database .core import SessionLocal
88from dispatch .document .models import Document
99from dispatch .event import service as event_service
1010from dispatch .incident .models import Incident
1111from dispatch .plugin import service as plugin_service
1212from dispatch .storage .models import Storage
1313from dispatch .ticket .models import Ticket
1414from dispatch .utils import deslug_and_capitalize_resource_type
15- from dispatch .config import DISPATCH_UI_URL
15+ from dispatch .types import Subject
1616
1717from .models import Conversation , ConversationCreate
1818from .service import create
1919
2020log = logging .getLogger (__name__ )
2121
2222
23- Resource = TypeVar ( "Resource" , Document , Conference , Storage , Ticket )
23+ Resource = Document | Conference | Storage | Ticket
2424
2525
26- def create_case_conversation (case : Case , conversation_target : str , db_session : SessionLocal ):
26+ def create_case_conversation (
27+ case : Case ,
28+ conversation_target : str ,
29+ db_session : Session ,
30+ ):
2731 """Create external communication conversation."""
2832
2933 plugin = plugin_service .get_active_instance (
@@ -38,10 +42,24 @@ def create_case_conversation(case: Case, conversation_target: str, db_session: S
3842
3943 conversation = None
4044
41- if conversation_target :
45+ # This case is a thread version, we send a new messaged (threaded) to the conversation target
46+ # for the configured case type
47+ if conversation_target and not case .dedicated_channel :
4248 try :
4349 conversation = plugin .instance .create_threaded (
44- case = case , conversation_id = conversation_target , db_session = db_session
50+ case = case ,
51+ conversation_id = conversation_target ,
52+ db_session = db_session ,
53+ )
54+ except Exception as e :
55+ # TODO: consistency across exceptions
56+ log .exception (e )
57+
58+ # otherwise, it must be a channel based case.
59+ if case .dedicated_channel :
60+ try :
61+ conversation = plugin .instance .create (
62+ name = f"case-{ case .name } " ,
4563 )
4664 except Exception as e :
4765 # TODO: consistency across exceptions
@@ -53,11 +71,12 @@ def create_case_conversation(case: Case, conversation_target: str, db_session: S
5371
5472 conversation .update ({"resource_type" : plugin .plugin .slug , "resource_id" : conversation ["id" ]})
5573
74+ print (f"got convo: { conversation } " )
5675 conversation_in = ConversationCreate (
5776 resource_id = conversation ["resource_id" ],
5877 resource_type = conversation ["resource_type" ],
5978 weblink = conversation ["weblink" ],
60- thread_id = conversation [ "timestamp" ] ,
79+ thread_id = conversation . get ( "timestamp" ) ,
6180 channel_id = conversation ["id" ],
6281 )
6382 case .conversation = create (db_session = db_session , conversation_in = conversation_in )
@@ -210,16 +229,23 @@ def set_conversation_topic(incident: Incident, db_session: SessionLocal):
210229 log .exception (e )
211230
212231
213- def add_conversation_bookmark (incident : Incident , resource : Resource , db_session : SessionLocal ):
232+ def add_conversation_bookmark (
233+ db_session : Session ,
234+ subject : Subject ,
235+ resource : Resource ,
236+ title : str | None = None ,
237+ ):
214238 """Adds a conversation bookmark."""
215- if not incident .conversation :
239+ if not subject .conversation :
216240 log .warning (
217- f"Conversation bookmark { resource .name .lower ()} not added. No conversation available for this incident ."
241+ f"Conversation bookmark { resource .name .lower ()} not added. No conversation available."
218242 )
219243 return
220244
221245 plugin = plugin_service .get_active_instance (
222- db_session = db_session , project_id = incident .project .id , plugin_type = "conversation"
246+ db_session = db_session ,
247+ project_id = subject .project .id ,
248+ plugin_type = "conversation" ,
223249 )
224250 if not plugin :
225251 log .warning (
@@ -228,109 +254,34 @@ def add_conversation_bookmark(incident: Incident, resource: Resource, db_session
228254 return
229255
230256 try :
231- title = deslug_and_capitalize_resource_type (resource .resource_type )
257+ if not title :
258+ title = deslug_and_capitalize_resource_type (resource .resource_type )
232259 (
233260 plugin .instance .add_bookmark (
234- incident .conversation .channel_id ,
261+ subject .conversation .channel_id ,
235262 resource .weblink ,
236263 title = title ,
237264 )
238265 if resource
239266 else log .warning (
240- f"{ resource .name } bookmark not added. No { resource .name .lower ()} available for this incident ."
267+ f"{ resource .name } bookmark not added. No { resource .name .lower ()} available for subject. ."
241268 )
242269 )
243270 except Exception as e :
244271 event_service .log_incident_event (
245272 db_session = db_session ,
246273 source = "Dispatch Core App" ,
247274 description = f"Adding the { resource .name .lower ()} bookmark failed. Reason: { e } " ,
248- incident_id = incident .id ,
275+ incident_id = subject .id ,
249276 )
250277 log .exception (e )
251278
252279
253- def add_conversation_bookmarks (incident : Incident , db_session : SessionLocal ):
254- """Adds the conversation bookmarks."""
255- if not incident .conversation :
256- log .warning (
257- "Conversation bookmarks not added. No conversation available for this incident."
258- )
259- return
260-
261- plugin = plugin_service .get_active_instance (
262- db_session = db_session , project_id = incident .project .id , plugin_type = "conversation"
263- )
264- if not plugin :
265- log .warning ("Conversation bookmarks not added. No conversation plugin enabled." )
266- return
267-
268- try :
269- (
270- plugin .instance .add_bookmark (
271- incident .conversation .channel_id ,
272- resolve_attr (incident , "incident_document.weblink" ),
273- title = "Incident Document" ,
274- )
275- if incident .incident_document
276- else log .warning (
277- "Incident document bookmark not added. No document available for this incident."
278- )
279- )
280-
281- (
282- plugin .instance .add_bookmark (
283- incident .conversation .channel_id ,
284- resolve_attr (incident , "conference.weblink" ),
285- title = "Video Conference" ,
286- )
287- if incident .conference
288- else log .warning (
289- "Conference bookmark not added. No conference available for this incident."
290- )
291- )
292-
293- (
294- plugin .instance .add_bookmark (
295- incident .conversation .channel_id ,
296- resolve_attr (incident , "storage.weblink" ),
297- title = "Storage Folder" ,
298- )
299- if incident .storage
300- else log .warning ("Storage bookmark not added. No storage available for this incident." )
301- )
302-
303- ticket_weblink = resolve_attr (incident , "ticket.weblink" )
304- (
305- plugin .instance .add_bookmark (
306- incident .conversation .channel_id ,
307- ticket_weblink ,
308- title = "Ticket" ,
309- )
310- if incident .ticket
311- else log .warning ("Ticket bookmark not added. No ticket available for this incident." )
312- )
313-
314- dispatch_weblink = f"{ DISPATCH_UI_URL } /{ incident .project .organization .name } /incidents/{ incident .name } ?project={ incident .project .name } "
315-
316- # only add Dispatch UI ticket if not using Dispatch ticket plugin
317- if ticket_weblink != dispatch_weblink :
318- plugin .instance .add_bookmark (
319- incident .conversation .channel_id ,
320- dispatch_weblink ,
321- title = "Dispatch UI" ,
322- )
323- except Exception as e :
324- event_service .log_incident_event (
325- db_session = db_session ,
326- source = "Dispatch Core App" ,
327- description = f"Adding the incident conversation bookmarks failed. Reason: { e } " ,
328- incident_id = incident .id ,
329- )
330- log .exception (e )
331-
332-
333- def add_case_participants (case : Case , participant_emails : List [str ], db_session : SessionLocal ):
280+ def add_case_participants (
281+ case : Case ,
282+ participant_emails : list [str ],
283+ db_session : Session ,
284+ ):
334285 """Adds one or more participants to the case conversation."""
335286 if not case .conversation :
336287 log .warning (
@@ -364,7 +315,9 @@ def add_case_participants(case: Case, participant_emails: List[str], db_session:
364315
365316
366317def add_incident_participants (
367- incident : Incident , participant_emails : List [str ], db_session : SessionLocal
318+ incident : Incident ,
319+ participant_emails : list [str ],
320+ db_session : Session ,
368321):
369322 """Adds one or more participants to the incident conversation."""
370323 if not incident .conversation :
0 commit comments