Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: chat_routes #1512

Merged
merged 9 commits into from
Oct 30, 2023
Empty file added backend/routes/chat/__init_.py
Empty file.
6 changes: 6 additions & 0 deletions backend/routes/chat/brainful_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from routes.chat.interface import ChatInterface


class BrainfulChat(ChatInterface):
def validate_authorization(self, user_id, brain_id):
pass
6 changes: 6 additions & 0 deletions backend/routes/chat/brainless_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from backend.routes.chat.interface import ChatInterface


class BrainlessChat(ChatInterface):
def validate_authorization(self, user_id, brain_id):
pass
11 changes: 11 additions & 0 deletions backend/routes/chat/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from uuid import UUID

from .brainful_chat import BrainfulChat
from .brainless_chat import BrainlessChat


def get_chat_strategy(brain_id: UUID | None = None):
if brain_id:
return BrainfulChat()
else:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Le else n'est pas nécessaire ici

return BrainlessChat()
7 changes: 7 additions & 0 deletions backend/routes/chat/interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from abc import ABC, abstractmethod


class ChatInterface(ABC):
@abstractmethod
def validate_authorization(self, user_id, required_roles):
pass
7 changes: 0 additions & 7 deletions backend/routes/chat_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,7 @@ async def create_question_handler(
"""
Add a new question to the chat.
"""
if brain_id:
validate_brain_authorization(
brain_id=brain_id,
user_id=current_user.id,
required_roles=[RoleEnum.Viewer, RoleEnum.Editor, RoleEnum.Owner],
)

# Retrieve user's OpenAI API key
if brain_id:
validate_brain_authorization(
brain_id=brain_id,
Expand Down