1111 WrongChatIdException ,
1212 ChatException ,
1313 UserNotInChatException ,
14+ UserNotMessageAuthorException ,
1415)
1516from chats .models import (
1617 BaseChat ,
3435from core .utils import get_user_online_cache_key
3536from projects .models import Collaborator
3637from users .models import CustomUser
38+ from users .serializers import UserDetailSerializer
3739
3840
3941class ChatConsumer (AsyncJsonWebsocketConsumer ):
@@ -97,6 +99,13 @@ async def receive_json(self, content, **kwargs):
9799 await self .__process_chat_related_event (event , room_name )
98100 except ChatException as e :
99101 await self .send_json ({"error" : str (e .get_error ())})
102+ except KeyError as e :
103+ await self .send_json (
104+ {
105+ "error" : f"Missing key (might be backend's fault,"
106+ f" but most likely you are missing this field): { e } "
107+ }
108+ )
100109
101110 elif event .type in [EventType .SET_ONLINE , EventType .SET_OFFLINE ]:
102111 room_name = EventGroupType .GENERAL_EVENTS
@@ -143,17 +152,19 @@ async def __process_new_direct_message_event(self, event: Event):
143152 chat_id = chat_id ,
144153 chat_model = DirectChatMessage ,
145154 author = self .user ,
146- text = event .content ["message " ],
155+ text = event .content ["text " ],
147156 reply_to = event .content ["reply_to" ],
148157 )
149158
159+ # get author of the message's via UserSerializer, check for errors
160+ author = await sync_to_async (lambda : (UserDetailSerializer (self .user )).data )()
150161 content = {
151162 "type" : EventType .NEW_MESSAGE ,
152163 "content" : {
153164 "message_id" : msg .id ,
154165 "chat_id" : msg .chat_id ,
155166 "chat_type" : ChatType .PROJECT ,
156- "author_id " : msg . author . pk ,
167+ "author " : author ,
157168 "text" : msg .text ,
158169 "created_at" : msg .created_at .timestamp (),
159170 "is_edited" : msg .is_edited ,
@@ -183,10 +194,11 @@ async def __process_new_project_message_event(self, event: Event, room_name: str
183194 chat_id = chat_id ,
184195 chat_model = ProjectChatMessage ,
185196 author = self .user ,
186- text = event .content ["message " ],
197+ text = event .content ["text " ],
187198 reply_to = event .content ["reply_to" ],
188199 )
189200
201+ author = await sync_to_async (lambda : (UserDetailSerializer (self .user )).data )()
190202 await self .channel_layer .group_send (
191203 room_name ,
192204 {
@@ -195,7 +207,7 @@ async def __process_new_project_message_event(self, event: Event, room_name: str
195207 "message_id" : msg .id ,
196208 "chat_id" : msg .chat_id ,
197209 "chat_type" : ChatType .PROJECT ,
198- "author_id " : msg . author . pk ,
210+ "author " : author ,
199211 "text" : msg .text ,
200212 "created_at" : msg .created_at .timestamp (),
201213 "is_edited" : msg .is_edited ,
@@ -399,15 +411,20 @@ async def __process_edit_direct_message_event(self, event):
399411 msg = await sync_to_async (DirectChatMessage .objects .get )(
400412 pk = event .content ["message_id" ]
401413 )
402- msg .text = event .content ["message" ]
414+ if msg .author != self .user :
415+ raise UserNotMessageAuthorException (
416+ f"User { self .user .id } is not author of message { msg .id } "
417+ )
418+ msg .text = event .content ["text" ]
403419 msg .is_edited = True
404420 await sync_to_async (msg .save )()
421+ author = await sync_to_async (lambda : (UserDetailSerializer (self .user )).data )()
405422 content = {
406423 "type" : EventType .EDIT_MESSAGE ,
407424 "content" : {
408425 "message_id" : msg .id ,
409426 "chat_id" : msg .chat_id ,
410- "author_id " : msg . author_id ,
427+ "author " : author ,
411428 "text" : msg .text ,
412429 "created_at" : msg .created_at .timestamp (),
413430 "is_edited" : msg .is_edited ,
@@ -436,13 +453,18 @@ async def __process_edit_project_message_event(self, event, room_name):
436453 message = await sync_to_async (ProjectChatMessage .objects .get )(
437454 pk = event .content ["message_id" ]
438455 )
456+ if message .author != self .user :
457+ raise UserNotMessageAuthorException (
458+ f"User { self .user .id } is not author of message { message .id } "
459+ )
439460 message .text = event .content ["text" ]
440461 message .is_edited = True
441462 await sync_to_async (message .save )()
463+ author = await sync_to_async (lambda : (UserDetailSerializer (self .user )).data )()
442464 content = {
443465 "message_id" : message .id ,
444466 "chat_id" : message .chat_id ,
445- "author_id " : message . author . pk ,
467+ "author " : author ,
446468 "text" : message .text ,
447469 "created_at" : message .created_at .timestamp (),
448470 "is_edited" : message .is_edited ,
0 commit comments