Skip to content

Commit 4cc9fab

Browse files
feat: add events
1 parent 0743bd8 commit 4cc9fab

File tree

4 files changed

+101
-15
lines changed

4 files changed

+101
-15
lines changed

packages/models-library/src/models_library/conversations.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
from datetime import datetime
22
from enum import auto
3-
from typing import TypeAlias
3+
from typing import Annotated, TypeAlias
44
from uuid import UUID
55

66
from models_library.groups import GroupID
77
from models_library.projects import ProjectID
8-
from pydantic import BaseModel, ConfigDict
8+
from pydantic import BaseModel, ConfigDict, StringConstraints
99

1010
from .products import ProductName
1111
from .utils.enums import StrAutoEnum
1212

1313
ConversationID: TypeAlias = UUID
14+
ConversationName: TypeAlias = Annotated[
15+
str, StringConstraints(strip_whitespace=True, min_length=1, max_length=255)
16+
]
17+
1418
ConversationMessageID: TypeAlias = UUID
1519

1620

@@ -36,7 +40,7 @@ class ConversationMessageType(StrAutoEnum):
3640
class ConversationGetDB(BaseModel):
3741
conversation_id: ConversationID
3842
product_name: ProductName
39-
name: str
43+
name: ConversationName
4044
project_uuid: ProjectID | None
4145
user_group_id: GroupID
4246
type: ConversationType
@@ -63,7 +67,7 @@ class ConversationMessageGetDB(BaseModel):
6367

6468

6569
class ConversationPatchDB(BaseModel):
66-
name: str | None = None
70+
name: ConversationName | None = None
6771

6872

6973
class ConversationMessagePatchDB(BaseModel):

services/web/server/src/simcore_service_webserver/conversations/_conversation_service.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
from models_library.rest_pagination import PageTotalCount
1717
from models_library.users import UserID
1818

19-
from ..conversations._socketio import notify_conversation_created
19+
from ..conversations._socketio import (
20+
notify_conversation_created,
21+
notify_conversation_deleted,
22+
notify_conversation_updated,
23+
)
2024
from ..projects._groups_repository import list_project_groups
2125
from ..users._users_service import get_users_in_group
2226
from ..users.api import get_user_primary_group_id
@@ -83,27 +87,51 @@ async def get_conversation(
8387
async def update_conversation(
8488
app: web.Application,
8589
*,
90+
project_id: ProjectID,
8691
conversation_id: ConversationID,
8792
# Update attributes
8893
updates: ConversationPatchDB,
8994
) -> ConversationGetDB:
90-
return await _conversation_repository.update(
95+
updated_conversation = await _conversation_repository.update(
9196
app,
9297
conversation_id=conversation_id,
9398
updates=updates,
9499
)
95100

101+
await notify_conversation_updated(
102+
app,
103+
recipients=await _get_recipients(app, project_id),
104+
project_id=project_id,
105+
conversation=updated_conversation,
106+
)
107+
108+
return updated_conversation
109+
96110

97111
async def delete_conversation(
98112
app: web.Application,
99113
*,
114+
product_name: ProductName,
115+
project_id: ProjectID,
116+
user_id: UserID,
100117
conversation_id: ConversationID,
101118
) -> None:
102119
await _conversation_repository.delete(
103120
app,
104121
conversation_id=conversation_id,
105122
)
106123

124+
_user_group_id = await get_user_primary_group_id(app, user_id=user_id)
125+
126+
await notify_conversation_deleted(
127+
app,
128+
recipients=await _get_recipients(app, project_id),
129+
product_name=product_name,
130+
user_group_id=_user_group_id,
131+
project_id=project_id,
132+
conversation_id=conversation_id,
133+
)
134+
107135

108136
async def list_conversations_for_project(
109137
app: web.Application,

services/web/server/src/simcore_service_webserver/conversations/_socketio.py

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
ConversationMessageGetDB,
99
ConversationMessageID,
1010
ConversationMessageType,
11+
ConversationName,
1112
ConversationType,
1213
)
1314
from models_library.groups import GroupID
@@ -57,10 +58,14 @@ class BaseConversationEvent(BaseEvent):
5758

5859

5960
class ConversationCreatedOrUpdatedEvent(BaseConversationEvent):
61+
name: ConversationName
6062
created: datetime.datetime
6163
modified: datetime.datetime
6264

6365

66+
class ConversationDeletedEvent(BaseConversationEvent): ...
67+
68+
6469
class BaseConversationMessageEvent(BaseEvent):
6570
conversation_id: ConversationID
6671
message_id: ConversationMessageID
@@ -92,9 +97,7 @@ async def _send_message_to_recipients(
9297
):
9398
async for _ in limited_as_completed(
9499
(
95-
send_message_to_user(
96-
app, recipient, notification_message, ignore_queue=True
97-
)
100+
send_message_to_user(app, recipient, notification_message)
98101
for recipient in recipients
99102
),
100103
limit=_MAX_CONCURRENT_SENDS,
@@ -112,10 +115,55 @@ async def notify_conversation_created(
112115
notification_message = SocketMessageDict(
113116
event_type=SOCKET_IO_CONVERSATION_CREATED_EVENT,
114117
data={
115-
"projectId": project_id,
116-
**ConversationCreatedOrUpdatedEvent(**conversation.model_dump()).model_dump(
117-
mode="json", by_alias=True
118-
),
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+
project_id: ProjectID,
154+
user_group_id: GroupID,
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),
119167
},
120168
)
121169

services/web/server/src/simcore_service_webserver/projects/_conversations_service.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
ConversationMessageID,
99
ConversationMessagePatchDB,
1010
ConversationMessageType,
11+
ConversationName,
1112
ConversationPatchDB,
1213
ConversationType,
1314
)
@@ -87,7 +88,7 @@ async def update_project_conversation(
8788
project_uuid: ProjectID,
8889
conversation_id: ConversationID,
8990
# attributes
90-
name: str,
91+
name: ConversationName,
9192
) -> ConversationGetDB:
9293
await check_user_project_permission(
9394
app,
@@ -98,6 +99,7 @@ async def update_project_conversation(
9899
)
99100
return await conversations_service.update_conversation(
100101
app,
102+
project_id=project_uuid,
101103
conversation_id=conversation_id,
102104
updates=ConversationPatchDB(name=name),
103105
)
@@ -119,7 +121,11 @@ async def delete_project_conversation(
119121
permission="read",
120122
)
121123
await conversations_service.delete_conversation(
122-
app, conversation_id=conversation_id
124+
app,
125+
product_name=product_name,
126+
project_id=project_uuid,
127+
user_id=user_id,
128+
conversation_id=conversation_id,
123129
)
124130

125131

0 commit comments

Comments
 (0)