|
11 | 11 | ConversationMessagePatchDB, |
12 | 12 | ConversationMessageType, |
13 | 13 | ) |
| 14 | +from models_library.groups import GroupID |
| 15 | +from models_library.projects import ProjectID |
14 | 16 | from models_library.rest_ordering import OrderBy, OrderDirection |
15 | 17 | from models_library.rest_pagination import PageTotalCount |
16 | 18 | from models_library.users import UserID |
17 | 19 |
|
| 20 | +from ..projects._groups_repository import list_project_groups |
| 21 | + |
| 22 | +# Import or define SocketMessageDict |
| 23 | +from ..socketio.messages import ( |
| 24 | + SOCKET_IO_PROJECT_CONVERSATION_MESSAGE_CREATED_EVENT, |
| 25 | + SocketMessageDict, |
| 26 | + send_message_to_standard_group, |
| 27 | +) |
18 | 28 | from ..users.api import get_user_primary_group_id |
19 | 29 | from . import _conversation_message_repository |
20 | 30 |
|
21 | 31 | _logger = logging.getLogger(__name__) |
22 | 32 |
|
23 | 33 |
|
| 34 | +async def notify_project_conversation_message_created( |
| 35 | + app: web.Application, |
| 36 | + project_id: ProjectID, |
| 37 | + conversation_message: ConversationMessageGetDB, |
| 38 | + recipients: list[GroupID], |
| 39 | +) -> None: |
| 40 | + message = SocketMessageDict( |
| 41 | + event_type=SOCKET_IO_PROJECT_CONVERSATION_MESSAGE_CREATED_EVENT, |
| 42 | + data={ |
| 43 | + "project_id": project_id, |
| 44 | + "conversation_id": conversation_message.conversation_id, |
| 45 | + "message_id": conversation_message.message_id, |
| 46 | + "user_group_id": conversation_message.user_group_id, |
| 47 | + "content": conversation_message.content, |
| 48 | + "type": conversation_message.type, |
| 49 | + "created": conversation_message.created.isoformat(), |
| 50 | + "modified": conversation_message.modified.isoformat(), |
| 51 | + }, |
| 52 | + ) |
| 53 | + |
| 54 | + for recipient in recipients: |
| 55 | + await send_message_to_standard_group(app, recipient, message) |
| 56 | + |
| 57 | + |
24 | 58 | async def create_message( |
25 | 59 | app: web.Application, |
26 | 60 | *, |
27 | 61 | user_id: UserID, |
| 62 | + project_id: ProjectID, |
28 | 63 | conversation_id: ConversationID, |
29 | 64 | # Creation attributes |
30 | 65 | content: str, |
31 | 66 | type_: ConversationMessageType, |
32 | 67 | ) -> ConversationMessageGetDB: |
33 | 68 | _user_group_id = await get_user_primary_group_id(app, user_id=user_id) |
34 | 69 |
|
35 | | - return await _conversation_message_repository.create( |
| 70 | + created_message = await _conversation_message_repository.create( |
36 | 71 | app, |
37 | 72 | conversation_id=conversation_id, |
38 | 73 | user_group_id=_user_group_id, |
39 | 74 | content=content, |
40 | 75 | type_=type_, |
41 | 76 | ) |
42 | 77 |
|
| 78 | + recipients = [ |
| 79 | + project_to_group.gid |
| 80 | + for project_to_group in await list_project_groups(app, project_id=project_id) |
| 81 | + if project_to_group.read |
| 82 | + ] |
| 83 | + |
| 84 | + await notify_project_conversation_message_created( |
| 85 | + app, project_id, created_message, recipients |
| 86 | + ) |
| 87 | + |
| 88 | + return created_message |
| 89 | + |
43 | 90 |
|
44 | 91 | async def get_message( |
45 | 92 | app: web.Application, |
|
0 commit comments