Implemented comprehensive models for chat functionality, including Chat, Message, MessageHistory, MessageReaction, and MessageFile. Updated ChatConsumer to enforce authentication and improve message handling. Added initial scaffolding for 'pages' and 'posts' Django apps with basic files.
42 lines
984 B
Python
42 lines
984 B
Python
# chat/consumers.py
|
|
import json
|
|
|
|
from account.models import UserProfile
|
|
|
|
from channels.db import database_sync_to_async
|
|
from channels.generic.websocket import AsyncWebsocketConsumer
|
|
|
|
|
|
class ChatConsumer(AsyncWebsocketConsumer):
|
|
async def connect(self):
|
|
user = self.scope["user"]
|
|
|
|
if not user.is_authenticated:
|
|
await self.close(code=4401) # unauthorized
|
|
return
|
|
|
|
await self.accept()
|
|
|
|
async def disconnect(self, close_code):
|
|
self.disconnect()
|
|
pass
|
|
|
|
async def receive(self, data):
|
|
if data["type"] == "chat_message":
|
|
pass
|
|
else:
|
|
self.close(reason="Unsupported message type")
|
|
|
|
|
|
# -- CUSTOM METHODS --
|
|
|
|
async def send_message_to_chat_group(self, event):
|
|
message = event["message"]
|
|
await create_new_message()
|
|
await self.send(text_data=json.dumps({"message": message}))
|
|
|
|
|
|
|
|
@database_sync_to_async
|
|
def create_new_message(user_id):
|
|
return None |