33
44from aiohttp import web
55from models_library .conversations import (
6+ ConversationGetDB ,
67 ConversationID ,
78 ConversationMessageGetDB ,
89 ConversationMessageID ,
910 ConversationMessageType ,
11+ ConversationName ,
12+ ConversationType ,
1013)
1114from models_library .groups import GroupID
15+ from models_library .products import ProductName
1216from models_library .projects import ProjectID
1317from models_library .socketio import SocketMessageDict
1418from models_library .users import UserID
2024
2125_MAX_CONCURRENT_SENDS : Final [int ] = 3
2226
27+ SOCKET_IO_CONVERSATION_CREATED_EVENT : Final [str ] = "conversation:created"
28+ SOCKET_IO_CONVERSATION_DELETED_EVENT : Final [str ] = "conversation:deleted"
29+ SOCKET_IO_CONVERSATION_UPDATED_EVENT : Final [str ] = "conversation:updated"
30+
2331SOCKET_IO_CONVERSATION_MESSAGE_CREATED_EVENT : Final [str ] = (
2432 "conversation:message:created"
2533)
3139)
3240
3341
34- class BaseConversationMessage (BaseModel ):
42+ class BaseEvent (BaseModel ):
43+ model_config = ConfigDict (
44+ populate_by_name = True ,
45+ from_attributes = True ,
46+ alias_generator = AliasGenerator (
47+ serialization_alias = to_camel ,
48+ ),
49+ )
50+
51+
52+ class BaseConversationEvent (BaseEvent ):
53+ product_name : ProductName
54+ project_id : ProjectID | None
55+ user_group_id : GroupID
56+ conversation_id : ConversationID
57+ type : ConversationType
58+
59+
60+ class ConversationCreatedOrUpdatedEvent (BaseConversationEvent ):
61+ name : ConversationName
62+ created : datetime .datetime
63+ modified : datetime .datetime
64+
65+
66+ class ConversationDeletedEvent (BaseConversationEvent ): ...
67+
68+
69+ class BaseConversationMessageEvent (BaseEvent ):
3570 conversation_id : ConversationID
3671 message_id : ConversationMessageID
3772 user_group_id : GroupID
@@ -46,13 +81,13 @@ class BaseConversationMessage(BaseModel):
4681 )
4782
4883
49- class ConversationMessageCreatedOrUpdated ( BaseConversationMessage ):
84+ class ConversationMessageCreatedOrUpdatedEvent ( BaseConversationMessageEvent ):
5085 content : str
5186 created : datetime .datetime
5287 modified : datetime .datetime
5388
5489
55- class ConversationMessageDeleted ( BaseConversationMessage ): ...
90+ class ConversationMessageDeletedEvent ( BaseConversationMessageEvent ): ...
5691
5792
5893async def _send_message_to_recipients (
@@ -62,16 +97,79 @@ async def _send_message_to_recipients(
6297):
6398 async for _ in limited_as_completed (
6499 (
65- send_message_to_user (
66- app , recipient , notification_message , ignore_queue = True
67- )
100+ send_message_to_user (app , recipient , notification_message )
68101 for recipient in recipients
69102 ),
70103 limit = _MAX_CONCURRENT_SENDS ,
71104 ):
72105 ...
73106
74107
108+ async def notify_conversation_created (
109+ app : web .Application ,
110+ * ,
111+ recipients : set [UserID ],
112+ project_id : ProjectID ,
113+ conversation : ConversationGetDB ,
114+ ) -> None :
115+ notification_message = SocketMessageDict (
116+ event_type = SOCKET_IO_CONVERSATION_CREATED_EVENT ,
117+ data = {
118+ ** ConversationCreatedOrUpdatedEvent (
119+ project_id = project_id ,
120+ ** conversation .model_dump (),
121+ ).model_dump (mode = "json" , by_alias = True ),
122+ },
123+ )
124+
125+ await _send_message_to_recipients (app , recipients , notification_message )
126+
127+
128+ async def notify_conversation_updated (
129+ app : web .Application ,
130+ * ,
131+ recipients : set [UserID ],
132+ project_id : ProjectID ,
133+ conversation : ConversationGetDB ,
134+ ) -> None :
135+ notification_message = SocketMessageDict (
136+ event_type = SOCKET_IO_CONVERSATION_UPDATED_EVENT ,
137+ data = {
138+ ** ConversationCreatedOrUpdatedEvent (
139+ project_id = project_id ,
140+ ** conversation .model_dump (),
141+ ).model_dump (mode = "json" , by_alias = True ),
142+ },
143+ )
144+
145+ await _send_message_to_recipients (app , recipients , notification_message )
146+
147+
148+ async def notify_conversation_deleted (
149+ app : web .Application ,
150+ * ,
151+ recipients : set [UserID ],
152+ product_name : ProductName ,
153+ user_group_id : GroupID ,
154+ project_id : ProjectID ,
155+ conversation_id : ConversationID ,
156+ ) -> None :
157+ notification_message = SocketMessageDict (
158+ event_type = SOCKET_IO_CONVERSATION_DELETED_EVENT ,
159+ data = {
160+ ** ConversationDeletedEvent (
161+ product_name = product_name ,
162+ project_id = project_id ,
163+ conversation_id = conversation_id ,
164+ user_group_id = user_group_id ,
165+ type = ConversationType .PROJECT_STATIC ,
166+ ).model_dump (mode = "json" , by_alias = True ),
167+ },
168+ )
169+
170+ await _send_message_to_recipients (app , recipients , notification_message )
171+
172+
75173async def notify_conversation_message_created (
76174 app : web .Application ,
77175 * ,
@@ -83,7 +181,7 @@ async def notify_conversation_message_created(
83181 event_type = SOCKET_IO_CONVERSATION_MESSAGE_CREATED_EVENT ,
84182 data = {
85183 "projectId" : project_id ,
86- ** ConversationMessageCreatedOrUpdated (
184+ ** ConversationMessageCreatedOrUpdatedEvent (
87185 ** conversation_message .model_dump ()
88186 ).model_dump (mode = "json" , by_alias = True ),
89187 },
@@ -104,7 +202,7 @@ async def notify_conversation_message_updated(
104202 event_type = SOCKET_IO_CONVERSATION_MESSAGE_UPDATED_EVENT ,
105203 data = {
106204 "projectId" : project_id ,
107- ** ConversationMessageCreatedOrUpdated (
205+ ** ConversationMessageCreatedOrUpdatedEvent (
108206 ** conversation_message .model_dump ()
109207 ).model_dump (mode = "json" , by_alias = True ),
110208 },
@@ -127,7 +225,7 @@ async def notify_conversation_message_deleted(
127225 event_type = SOCKET_IO_CONVERSATION_MESSAGE_DELETED_EVENT ,
128226 data = {
129227 "projectId" : project_id ,
130- ** ConversationMessageDeleted (
228+ ** ConversationMessageDeletedEvent (
131229 conversation_id = conversation_id ,
132230 message_id = message_id ,
133231 user_group_id = user_group_id ,
0 commit comments