aboutsummaryrefslogtreecommitdiff
path: root/chat/consumers.py
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-05-17 20:15:08 -0400
committerBobby <[email protected]>2023-05-17 20:15:08 -0400
commit488b7ee1233aefbb7bb70ee5958676f852139025 (patch)
tree0697e181a58c33ff0dc9a4bbcde1e1f0cc1b20d6 /chat/consumers.py
parentdec69b9a6bdd2845d402280ded58fcafa2809c14 (diff)
downloadthatcomputerscientist-488b7ee1233aefbb7bb70ee5958676f852139025.tar.xz
thatcomputerscientist-488b7ee1233aefbb7bb70ee5958676f852139025.zip
Protected usernames + Skippy comes to chat to lonely users + Handle Multiple Chat users
Diffstat (limited to 'chat/consumers.py')
-rw-r--r--chat/consumers.py63
1 files changed, 34 insertions, 29 deletions
diff --git a/chat/consumers.py b/chat/consumers.py
index be1cf265..5b0e7ddf 100644
--- a/chat/consumers.py
+++ b/chat/consumers.py
@@ -1,40 +1,45 @@
import json
-from channels.generic.websocket import WebsocketConsumer
-from asgiref.sync import async_to_sync
+from channels.generic.websocket import AsyncWebsocketConsumer
+from .chat_cache import handle_connect, handle_disconnect, handle_alone_user, discard_user_messages
+from .ai import invokeMFSkippy
-class ChatConsumer(WebsocketConsumer):
- def connect(self):
+class ChatConsumer(AsyncWebsocketConsumer):
+ async def connect(self):
self.room_group_name = 'chat'
- async_to_sync(self.channel_layer.group_add)(
- self.room_group_name,
- self.channel_name
- )
- self.accept()
+ await self.channel_layer.group_add(self.room_group_name, self.channel_name)
+ await self.accept()
+ handle_connect()
+
+ async def disconnect(self, close_code):
+ # Leave room group
+ await self.channel_layer.group_discard(self.room_group_name, self.channel_name)
+ handle_disconnect()
+ discard_user_messages(user_identifier=self.channel_name)
- def receive(self, text_data):
+ async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
username = text_data_json['username']
- async_to_sync(self.channel_layer.group_send)(
- self.room_group_name,
- {
- 'type': 'chat',
- 'message': message,
- 'username': username
- }
+
+ # Send message to room group
+ await self.channel_layer.group_send(
+ self.room_group_name, {"type": "chat", "message": message, "username": username}
)
+ is_alone_user = handle_alone_user()
+ if is_alone_user:
+ bot_response = invokeMFSkippy(message=message, identifier=self.channel_name)
+ if bot_response:
+ await self.channel_layer.group_send(
+ self.room_group_name, {"type": "chat", "message": bot_response, "username": "Skippy"}
+ )
+ else:
+ discard_user_messages(user_identifier=self.channel_name)
- def chat(self, event):
- message = event['message']
- username = event['username']
- self.send(text_data=json.dumps({
- 'message': message,
- 'username': username
- }))
+ # Receive message from room group
+ async def chat(self, event):
+ message = event["message"]
+ username = event["username"]
- def disconnect(self, close_code):
- async_to_sync(self.channel_layer.group_discard)(
- self.room_group_name,
- self.channel_name
- )
+ # Send message to WebSocket
+ await self.send(text_data=json.dumps({"message": message, "username": username}))