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 ):
@@ -143,17 +145,19 @@ async def __process_new_direct_message_event(self, event: Event):
143145 chat_id = chat_id ,
144146 chat_model = DirectChatMessage ,
145147 author = self .user ,
146- text = event .content ["message " ],
148+ text = event .content ["text " ],
147149 reply_to = event .content ["reply_to" ],
148150 )
149151
152+ # get author of the message's via UserSerializer, check for errors
153+ author = await sync_to_async (lambda : (UserDetailSerializer (self .user )).data )()
150154 content = {
151155 "type" : EventType .NEW_MESSAGE ,
152156 "content" : {
153157 "message_id" : msg .id ,
154158 "chat_id" : msg .chat_id ,
155159 "chat_type" : ChatType .PROJECT ,
156- "author_id " : msg . author . pk ,
160+ "author " : author ,
157161 "text" : msg .text ,
158162 "created_at" : msg .created_at .timestamp (),
159163 "is_edited" : msg .is_edited ,
@@ -183,10 +187,11 @@ async def __process_new_project_message_event(self, event: Event, room_name: str
183187 chat_id = chat_id ,
184188 chat_model = ProjectChatMessage ,
185189 author = self .user ,
186- text = event .content ["message " ],
190+ text = event .content ["text " ],
187191 reply_to = event .content ["reply_to" ],
188192 )
189193
194+ author = await sync_to_async (lambda : (UserDetailSerializer (self .user )).data )()
190195 await self .channel_layer .group_send (
191196 room_name ,
192197 {
@@ -195,7 +200,7 @@ async def __process_new_project_message_event(self, event: Event, room_name: str
195200 "message_id" : msg .id ,
196201 "chat_id" : msg .chat_id ,
197202 "chat_type" : ChatType .PROJECT ,
198- "author_id " : msg . author . pk ,
203+ "author " : author ,
199204 "text" : msg .text ,
200205 "created_at" : msg .created_at .timestamp (),
201206 "is_edited" : msg .is_edited ,
@@ -399,15 +404,20 @@ async def __process_edit_direct_message_event(self, event):
399404 msg = await sync_to_async (DirectChatMessage .objects .get )(
400405 pk = event .content ["message_id" ]
401406 )
402- msg .text = event .content ["message" ]
407+ if msg .author != self .user :
408+ raise UserNotMessageAuthorException (
409+ f"User { self .user .id } is not author of message { msg .id } "
410+ )
411+ msg .text = event .content ["text" ]
403412 msg .is_edited = True
404413 await sync_to_async (msg .save )()
414+ author = await sync_to_async (lambda : (UserDetailSerializer (self .user )).data )()
405415 content = {
406416 "type" : EventType .EDIT_MESSAGE ,
407417 "content" : {
408418 "message_id" : msg .id ,
409419 "chat_id" : msg .chat_id ,
410- "author_id " : msg . author_id ,
420+ "author " : author ,
411421 "text" : msg .text ,
412422 "created_at" : msg .created_at .timestamp (),
413423 "is_edited" : msg .is_edited ,
@@ -436,13 +446,18 @@ async def __process_edit_project_message_event(self, event, room_name):
436446 message = await sync_to_async (ProjectChatMessage .objects .get )(
437447 pk = event .content ["message_id" ]
438448 )
449+ if message .author != self .user :
450+ raise UserNotMessageAuthorException (
451+ f"User { self .user .id } is not author of message { message .id } "
452+ )
439453 message .text = event .content ["text" ]
440454 message .is_edited = True
441455 await sync_to_async (message .save )()
456+ author = await sync_to_async (lambda : (UserDetailSerializer (self .user )).data )()
442457 content = {
443458 "message_id" : message .id ,
444459 "chat_id" : message .chat_id ,
445- "author_id " : message . author . pk ,
460+ "author " : author ,
446461 "text" : message .text ,
447462 "created_at" : message .created_at .timestamp (),
448463 "is_edited" : message .is_edited ,
0 commit comments