From b577cb87a50da2b2204c547b475740c8ef75c7a4 Mon Sep 17 00:00:00 2001 From: Ian Eaves Date: Tue, 7 Nov 2023 12:29:04 -0600 Subject: [PATCH 1/2] updated chat_id typing & cache fixes --- grai-server/app/grAI/consumers.py | 9 ++++++--- grai-server/app/grAI/websocket_payloads.py | 8 ++++---- grai-server/app/pyproject.toml | 2 +- grai-server/app/the_guide/__init__.py | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/grai-server/app/grAI/consumers.py b/grai-server/app/grAI/consumers.py index 837e9801f..20859d8b6 100644 --- a/grai-server/app/grAI/consumers.py +++ b/grai-server/app/grAI/consumers.py @@ -15,6 +15,8 @@ from grAI.websocket_payloads import ChatErrorMessages, ChatEvent from users.models import User from workspaces.models import Membership +import logging +from uuid import UUID class ChatConsumer(WebsocketConsumer): @@ -23,8 +25,8 @@ class ChatConsumer(WebsocketConsumer): """ def __init__(self, *args, **kwargs): - self.conversations: dict[str, BaseConversation] = {} - self.active_chats: set = set() + self.conversations: dict[UUID, BaseConversation] = {} + self.active_chats: set[UUID] = set() super().__init__(*args, **kwargs) @cached_property @@ -58,7 +60,7 @@ def disconnect(self, close_code): def receive(self, text_data): data: dict = json.loads(text_data) socket_message_type = data.get("type", None) - + print(data) match socket_message_type: case "chat.message": self.chat_message(data) @@ -77,6 +79,7 @@ def chat_message(self, event: dict): # Insure the conversation exists if payload.chat_id not in self.active_chats: + print("LOOK AT ME NOW") chat, created = UserChat.objects.get_or_create(membership=self.membership, id=payload.chat_id) self.active_chats.add(chat.id) diff --git a/grai-server/app/grAI/websocket_payloads.py b/grai-server/app/grAI/websocket_payloads.py index f50990bae..aee4d7253 100644 --- a/grai-server/app/grAI/websocket_payloads.py +++ b/grai-server/app/grAI/websocket_payloads.py @@ -1,7 +1,7 @@ import uuid from enum import Enum -from typing import Literal - +from typing import Literal, Annotated +from uuid import UUID from pydantic import BaseModel, ValidationError, validator @@ -18,8 +18,8 @@ class ChatErrorMessages(Enum): class ChatEvent(BaseModel): type: Literal["chat.message"] = "chat.message" message: str - chat_id: str + chat_id: UUID @validator("chat_id") def chat_id_is_uuid(cls, v): - return v if v == "" else str(uuid.uuid4()) + return uuid.uuid4() if v == "" else v diff --git a/grai-server/app/pyproject.toml b/grai-server/app/pyproject.toml index a375cbd15..00d0514b0 100644 --- a/grai-server/app/pyproject.toml +++ b/grai-server/app/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "the_guide" -version = "0.1.48" +version = "0.1.49" description = "" authors = ["Ian Eaves ", "Edward Louth "] license = "Elastic-2.0" diff --git a/grai-server/app/the_guide/__init__.py b/grai-server/app/the_guide/__init__.py index a270331d4..dd5d1531d 100755 --- a/grai-server/app/the_guide/__init__.py +++ b/grai-server/app/the_guide/__init__.py @@ -2,5 +2,5 @@ # Django starts so that shared_task will use this app. from .celery import app as celery_app -__version__ = "0.1.48" +__version__ = "0.1.49" __all__ = ("celery_app",) From d9b2c1b460c37b436d6b4324a9667185644ba3f0 Mon Sep 17 00:00:00 2001 From: Ian Eaves Date: Tue, 7 Nov 2023 12:33:32 -0600 Subject: [PATCH 2/2] extraneous print --- grai-server/app/grAI/consumers.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/grai-server/app/grAI/consumers.py b/grai-server/app/grAI/consumers.py index 20859d8b6..8c4eac563 100644 --- a/grai-server/app/grAI/consumers.py +++ b/grai-server/app/grAI/consumers.py @@ -60,7 +60,6 @@ def disconnect(self, close_code): def receive(self, text_data): data: dict = json.loads(text_data) socket_message_type = data.get("type", None) - print(data) match socket_message_type: case "chat.message": self.chat_message(data) @@ -79,7 +78,6 @@ def chat_message(self, event: dict): # Insure the conversation exists if payload.chat_id not in self.active_chats: - print("LOOK AT ME NOW") chat, created = UserChat.objects.get_or_create(membership=self.membership, id=payload.chat_id) self.active_chats.add(chat.id)