Skip to content

Commit bcd411d

Browse files
committed
Fixed some bugs
1 parent 264d27e commit bcd411d

2 files changed

Lines changed: 25 additions & 26 deletions

File tree

chats/consumers.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
ProjectChat,
2121
ProjectChatMessage,
2222
)
23+
from chats.serializers import (
24+
ProjectChatMessageListSerializer,
25+
DirectChatMessageListSerializer,
26+
)
2327
from chats.utils import (
2428
get_user_channel_cache_key,
2529
create_message,
@@ -350,7 +354,7 @@ async def __process_delete_project_message_event(self, event: Event, room_name:
350354
await self.channel_layer.group_send(
351355
room_name,
352356
{
353-
"type": EventType.NEW_MESSAGE,
357+
"type": EventType.DELETE_MESSAGE,
354358
"content": {"message_id": event.content["message_id"]},
355359
},
356360
)
@@ -420,26 +424,19 @@ async def __process_edit_direct_message_event(self, event):
420424
msg.text = event.content["text"]
421425
msg.is_edited = True
422426
await sync_to_async(msg.save)()
423-
author = await sync_to_async(lambda: (UserDetailSerializer(self.user)).data)()
424-
content = {
425-
"type": EventType.EDIT_MESSAGE,
426-
"content": {
427-
"message_id": msg.id,
428-
"chat_id": msg.chat_id,
429-
"author": author,
430-
"text": msg.text,
431-
"created_at": msg.created_at.timestamp(),
432-
"is_edited": msg.is_edited,
433-
},
434-
}
427+
content = await sync_to_async(
428+
lambda: (DirectChatMessageListSerializer(msg)).data
429+
)()
430+
435431
# send message to user's channel
436432
other_user_channel = cache.get(get_user_channel_cache_key(other_user), None)
437-
await self.channel_layer.send(self.channel_name, content)
438433

439-
if other_user_channel is None:
440-
return
434+
if other_user_channel:
435+
await self.channel_layer.send(other_user_channel, content)
441436

442-
await self.channel_layer.send(other_user_channel, content)
437+
await self.channel_layer.send(
438+
self.channel_name, {"type": EventType.EDIT_MESSAGE, "content": content}
439+
)
443440

444441
async def __process_edit_project_message_event(self, event, room_name):
445442
chat_id = event.content["chat_id"]
@@ -455,6 +452,7 @@ async def __process_edit_project_message_event(self, event, room_name):
455452
message = await sync_to_async(ProjectChatMessage.objects.get)(
456453
pk=event.content["message_id"]
457454
)
455+
458456
message_author = await sync_to_async(lambda: message.author)()
459457
if message_author != self.user:
460458
raise UserNotMessageAuthorException(
@@ -463,15 +461,10 @@ async def __process_edit_project_message_event(self, event, room_name):
463461
message.text = event.content["text"]
464462
message.is_edited = True
465463
await sync_to_async(message.save)()
466-
author = await sync_to_async(lambda: (UserDetailSerializer(self.user)).data)()
467-
content = {
468-
"message_id": message.id,
469-
"chat_id": message.chat_id,
470-
"author": author,
471-
"text": message.text,
472-
"created_at": message.created_at.timestamp(),
473-
"is_edited": message.is_edited,
474-
}
464+
465+
content = await sync_to_async(
466+
lambda: (ProjectChatMessageListSerializer(message)).data
467+
)()
475468
await self.channel_layer.group_send(
476469
room_name,
477470
{"type": EventType.EDIT_MESSAGE, "content": content},

chats/serializers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class Meta:
9191
"author",
9292
"text",
9393
"reply_to",
94+
"is_edited",
95+
"is_read",
96+
"is_deleted",
9497
"created_at",
9598
]
9699

@@ -105,5 +108,8 @@ class Meta:
105108
"author",
106109
"text",
107110
"reply_to",
111+
"is_edited",
112+
"is_read",
113+
"is_deleted",
108114
"created_at",
109115
]

0 commit comments

Comments
 (0)