Skip to content

Commit

Permalink
fix: allow users to delete brains
Browse files Browse the repository at this point in the history
  • Loading branch information
mamadoudicko committed Nov 6, 2023
1 parent 2041152 commit 4dc9b95
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
4 changes: 4 additions & 0 deletions backend/models/databases/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ def insert_knowledge(self, brain_id: UUID):
def remove_knowledge_by_id(self, knowledge_id: UUID):
pass

@abstractmethod
def remove_brain_all_knowledge(self, brain_id: UUID):
pass

@abstractmethod
def get_knowledge_by_id(self, knowledge_id: UUID):
pass
Expand Down
10 changes: 10 additions & 0 deletions backend/models/databases/supabase/knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,13 @@ def get_all_knowledge_in_brain(self, brain_id: UUID) -> List[Knowledge]:
).data

return all_knowledge

def remove_brain_all_knowledge(self, brain_id: UUID) -> None:
"""
Remove all knowledge in a brain
Args:
brain_id (UUID): The id of the brain
"""
self.db.from_("knowledge").delete().filter(
"brain_id", "eq", str(brain_id)
).execute()
14 changes: 14 additions & 0 deletions backend/repository/knowledge/remove_brain_all_knowledge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from uuid import UUID

from logger import get_logger
from models.settings import get_supabase_db

logger = get_logger(__name__)


def remove_brain_all_knowledge(brain_id: UUID) -> None:
supabase_db = get_supabase_db()

supabase_db.remove_brain_all_knowledge(brain_id)

logger.info(f"All knowledge in brain {brain_id} removed successfully from table")
29 changes: 20 additions & 9 deletions backend/routes/subscription_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
SubscriptionInvitationService,
resend_invitation_email,
)
from repository.knowledge.remove_brain_all_knowledge import (
remove_brain_all_knowledge,
)
from repository.prompt import delete_prompt_by_id, get_prompt_by_id
from repository.user import get_user_email_by_user_id, get_user_id_by_user_email

Expand Down Expand Up @@ -128,6 +131,14 @@ async def remove_user_subscription(
"""
Remove a user's subscription to a brain
"""
targeted_brain = get_brain_by_id(brain_id)

if targeted_brain is None:
raise HTTPException(
status_code=404,
detail="Brain not found while trying to delete",
)

brain = Brain(
id=brain_id,
)
Expand All @@ -151,15 +162,15 @@ async def remove_user_subscription(

if len(brain_other_owners) == 0:
# Delete its prompt if it's private
brain_to_delete = get_brain_by_id(brain_id)
if brain_to_delete:
brain.delete_brain(current_user.id)
if brain_to_delete.prompt_id:
brain_to_delete_prompt = get_prompt_by_id(brain_to_delete.prompt_id)
if brain_to_delete_prompt is not None and (
brain_to_delete_prompt.status == PromptStatusEnum.private
):
delete_prompt_by_id(brain_to_delete.prompt_id)

remove_brain_all_knowledge(brain_id)
brain.delete_brain(current_user.id)
if targeted_brain.prompt_id:
brain_to_delete_prompt = get_prompt_by_id(targeted_brain.prompt_id)
if brain_to_delete_prompt is not None and (
brain_to_delete_prompt.status == PromptStatusEnum.private
):
delete_prompt_by_id(targeted_brain.prompt_id)

else:
brain.delete_user_from_brain(current_user.id)
Expand Down

0 comments on commit 4dc9b95

Please sign in to comment.