import json from channels.db import database_sync_to_async from channels.generic.websocket import AsyncWebsocketConsumer from django.contrib.auth.models import AnonymousUser class ChatConsumer(AsyncWebsocketConsumer): """ Xona asosidagi chat consumer. Ulanish: ws://host/ws/chat/room/{room_id}/?token= Matn xabari yuborish: { "message_type": "text", "text": "Salom" } Fayl xabari (oldin REST orqali yuklab, keyin URL yuborish): { "message_type": "image", "file_url": "https://...", "text": "caption (ixtiyoriy)" } """ async def connect(self): user = self.scope.get("user") if not user or isinstance(user, AnonymousUser): await self.close(code=4001) return self.room_id = self.scope["url_route"]["kwargs"]["room_id"] self.room_group = f"chat_room_{self.room_id}" await self.channel_layer.group_add(self.room_group, self.channel_name) await self.accept() async def disconnect(self, close_code): if hasattr(self, "room_group"): await self.channel_layer.group_discard(self.room_group, self.channel_name) async def receive(self, text_data): user = self.scope.get("user") if not user or isinstance(user, AnonymousUser): await self.close(code=4001) return try: data = json.loads(text_data) except (json.JSONDecodeError, ValueError): await self.send(text_data=json.dumps({"error": "Noto'g'ri format."})) return message_type = data.get("message_type", "text") text = (data.get("text") or "").strip() # Matn xabari uchun text majburiy if message_type == "text" and not text: await self.send(text_data=json.dumps({"error": "Matn bo'sh bo'lishi mumkin emas."})) return # WS orqali faqat matn + caption saqlanadi. # Fayl yuklash uchun REST /chat/messages/ POST ishlatiladi. if message_type != "text": await self.send( text_data=json.dumps( {"error": "Fayl xabarlarni yuklash uchun REST API dan foydalaning."} ) ) return # DB ga saqlash — post_save signal WS ga broadcast qiladi await self._save_message(user, text) async def chat_message(self, event): await self.send( text_data=json.dumps( { "id": event["id"], "message_type": event["message_type"], "text": event["text"], "file_url": event["file_url"], "sender": event["sender"], "created_at": event["created_at"], } ) ) @database_sync_to_async def _save_message(self, user, text): from core.apps.chat.models import ChatmessageModel msg = ChatmessageModel.objects.create( room_id=self.room_id, sender=user, message_type="text", text=text, ) full_name = user.get_full_name().strip() or str(user.phone) return { "id": msg.id, "message_type": msg.message_type, "text": msg.text, "file_url": None, "sender": { "id": user.id, "full_name": full_name, "role": user.role, }, "created_at": msg.created_at.isoformat(), }