Skip to content

Commit 3b0b0a1

Browse files
committed
Fixed event data
1 parent af1771d commit 3b0b0a1

1 file changed

Lines changed: 40 additions & 49 deletions

File tree

chats/consumers.py

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
from core.utils import get_user_online_cache_key
4040
from projects.models import Collaborator
4141
from users.models import CustomUser
42-
from users.serializers import UserDetailSerializer
4342

4443

4544
class ChatConsumer(AsyncJsonWebsocketConsumer):
@@ -160,28 +159,23 @@ async def __process_new_direct_message_event(self, event: Event):
160159
reply_to=event.content["reply_to"],
161160
)
162161

163-
# get author of the message's via UserSerializer, check for errors
164-
author = await sync_to_async(lambda: (UserDetailSerializer(self.user)).data)()
162+
message_data = await sync_to_async(
163+
lambda: (DirectChatMessageListSerializer(msg)).data
164+
)()
165165
content = {
166-
"type": EventType.NEW_MESSAGE,
167-
"content": {
168-
"message_id": msg.id,
169-
"chat_id": msg.chat_id,
170-
"chat_type": ChatType.PROJECT,
171-
"author": author,
172-
"text": msg.text,
173-
"created_at": msg.created_at.timestamp(),
174-
"is_edited": msg.is_edited,
175-
},
166+
"chat_id": chat_id,
167+
"message": message_data,
176168
}
169+
177170
# send message to user's channel
178171
other_user_channel = cache.get(get_user_channel_cache_key(other_user), None)
179-
await self.channel_layer.send(self.channel_name, content)
180172

181-
if other_user_channel is None:
182-
return
173+
event_data = {"type": EventType.NEW_MESSAGE, "content": content}
183174

184-
await self.channel_layer.send(other_user_channel, content)
175+
await self.channel_layer.send(self.channel_name, event_data)
176+
177+
if other_user_channel:
178+
await self.channel_layer.send(other_user_channel, event_data)
185179

186180
async def __process_new_project_message_event(self, event: Event, room_name: str):
187181
chat_id = event.content["chat_id"]
@@ -202,22 +196,17 @@ async def __process_new_project_message_event(self, event: Event, room_name: str
202196
reply_to=event.content["reply_to"],
203197
)
204198

205-
author = await sync_to_async(lambda: (UserDetailSerializer(self.user)).data)()
206-
await self.channel_layer.group_send(
207-
room_name,
208-
{
209-
"type": EventType.NEW_MESSAGE,
210-
"content": {
211-
"message_id": msg.id,
212-
"chat_id": msg.chat_id,
213-
"chat_type": ChatType.PROJECT,
214-
"author": author,
215-
"text": msg.text,
216-
"created_at": msg.created_at.timestamp(),
217-
"is_edited": msg.is_edited,
218-
},
219-
},
220-
)
199+
message_data = await sync_to_async(
200+
lambda: (DirectChatMessageListSerializer(msg)).data
201+
)()
202+
content = {
203+
"chat_id": chat_id,
204+
"message": message_data,
205+
}
206+
207+
event_data = {"type": EventType.NEW_MESSAGE, "content": content}
208+
209+
await self.channel_layer.group_send(room_name, event_data)
221210

222211
async def __process_typing_event(self, event: Event, room_name: str):
223212
"""Send typing event to room group."""
@@ -424,22 +413,24 @@ async def __process_edit_direct_message_event(self, event):
424413
msg.text = event.content["text"]
425414
msg.is_edited = True
426415
await sync_to_async(msg.save)()
427-
content = await sync_to_async(
416+
417+
message_data = await sync_to_async(
428418
lambda: (DirectChatMessageListSerializer(msg)).data
429419
)()
430-
content["chat_id"] = chat_id
431-
content["message_id"] = content["id"]
432-
del content["id"]
420+
content = {
421+
"chat_id": chat_id,
422+
"message": message_data,
423+
}
433424

434425
# send message to user's channel
435426
other_user_channel = cache.get(get_user_channel_cache_key(other_user), None)
436427

428+
event_data = {"type": EventType.EDIT_MESSAGE, "content": content}
429+
437430
if other_user_channel:
438-
await self.channel_layer.send(other_user_channel, content)
431+
await self.channel_layer.send(other_user_channel, event_data)
439432

440-
await self.channel_layer.send(
441-
self.channel_name, {"type": EventType.EDIT_MESSAGE, "content": content}
442-
)
433+
await self.channel_layer.send(self.channel_name, event_data)
443434

444435
async def __process_edit_project_message_event(self, event, room_name):
445436
chat_id = event.content["chat_id"]
@@ -465,17 +456,17 @@ async def __process_edit_project_message_event(self, event, room_name):
465456
message.is_edited = True
466457
await sync_to_async(message.save)()
467458

468-
content = await sync_to_async(
459+
message_data = await sync_to_async(
469460
lambda: (ProjectChatMessageListSerializer(message)).data
470461
)()
471-
content["chat_id"] = chat_id
472-
content["message_id"] = content["id"]
473-
del content["id"]
462+
content = {
463+
"chat_id": chat_id,
464+
"message": message_data,
465+
}
474466

475-
await self.channel_layer.group_send(
476-
room_name,
477-
{"type": EventType.EDIT_MESSAGE, "content": content},
478-
)
467+
event_data = {"type": EventType.EDIT_MESSAGE, "content": content}
468+
469+
await self.channel_layer.group_send(room_name, event_data)
479470

480471

481472
class NotificationConsumer(AsyncJsonWebsocketConsumer):

0 commit comments

Comments
 (0)