diff --git a/core/apps/chat/consumers/chat.py b/core/apps/chat/consumers/chat.py index e343c41..d5ecc73 100644 --- a/core/apps/chat/consumers/chat.py +++ b/core/apps/chat/consumers/chat.py @@ -4,6 +4,11 @@ from channels.db import database_sync_to_async from channels.generic.websocket import AsyncWebsocketConsumer from django.contrib.auth.models import AnonymousUser +def get_base_url(scope): + headers = dict(scope["headers"]) + host = headers.get(b"host", b"").decode() + scheme = "https" if scope.get("scheme") == "https" else "http" + return f"{scheme}://{host}" class ChatConsumer(AsyncWebsocketConsumer): """ @@ -91,6 +96,12 @@ class ChatConsumer(AsyncWebsocketConsumer): text=text, ) full_name = user.get_full_name().strip() or str(user.phone) + base_url = get_base_url(self.scope) + + avatar_url = ( + base_url + user.avatar.url + if user.avatar else None + ) return { "id": msg.id, "message_type": msg.message_type, @@ -100,6 +111,8 @@ class ChatConsumer(AsyncWebsocketConsumer): "id": user.id, "full_name": full_name, "role": user.role, + "phone": user.phone, + "avatar": avatar_url, }, "created_at": msg.created_at.isoformat(), } diff --git a/core/apps/chat/serializers/chat/ChatMessage.py b/core/apps/chat/serializers/chat/ChatMessage.py index 22c0918..5026a0a 100644 --- a/core/apps/chat/serializers/chat/ChatMessage.py +++ b/core/apps/chat/serializers/chat/ChatMessage.py @@ -13,10 +13,13 @@ class BaseChatmessageSerializer(serializers.ModelSerializer): full_name = obj.sender.get_full_name().strip() if not full_name: full_name = str(obj.sender.phone) + request = self.context.get("request") return { "id": obj.sender.id, "full_name": full_name, "role": obj.sender.role, + "phone": obj.sender.phone, + "avatar": request.build_absolute_uri(obj.sender.avatar.url) if obj.sender.avatar else None, } def get_file_url(self, obj):