mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-14 17:03:29 +03:00
fix: allow users to delete brains (#1596)
Issue: https://github.com/StanGirard/quivr/issues/1362 Demo: https://github.com/StanGirard/quivr/assets/63923024/aa5e41f8-a738-41f2-b037-975788ad8611
This commit is contained in:
parent
6abba91a8f
commit
3c0819de09
@ -252,6 +252,10 @@ class Repository(ABC):
|
||||
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
|
||||
|
@ -92,4 +92,23 @@ class Knowledges(Repository):
|
||||
.execute()
|
||||
).data
|
||||
|
||||
return all_knowledge
|
||||
return [Knowledge(**knowledge) for knowledge in 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
|
||||
"""
|
||||
all_knowledge = self.get_all_knowledge_in_brain(brain_id)
|
||||
knowledge_to_delete_list = []
|
||||
|
||||
for knowledge in all_knowledge:
|
||||
if knowledge.file_name:
|
||||
knowledge_to_delete_list.append(f"{brain_id}/{knowledge.file_name}")
|
||||
|
||||
self.db.storage.from_("quivr").remove(knowledge_to_delete_list)
|
||||
|
||||
self.db.from_("knowledge").delete().filter(
|
||||
"brain_id", "eq", str(brain_id)
|
||||
).execute()
|
||||
|
14
backend/repository/knowledge/remove_brain_all_knowledge.py
Normal file
14
backend/repository/knowledge/remove_brain_all_knowledge.py
Normal file
@ -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")
|
@ -9,6 +9,7 @@ from repository.files.generate_file_signed_url import generate_file_signed_url
|
||||
from repository.knowledge.get_all_knowledge import get_all_knowledge
|
||||
from repository.knowledge.get_knowledge import get_knowledge
|
||||
from repository.knowledge.remove_knowledge import remove_knowledge
|
||||
|
||||
from routes.authorizations.brain_authorization import (
|
||||
RoleEnum,
|
||||
has_brain_authorization,
|
||||
|
@ -17,6 +17,9 @@ from repository.brain_subscription import (
|
||||
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
|
||||
|
||||
@ -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,
|
||||
)
|
||||
@ -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)
|
||||
|
@ -62,6 +62,10 @@ export const usePrompt = (props: UsePromptProps) => {
|
||||
promptId
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
setCurrentPromptId(promptId);
|
||||
}, [promptId]);
|
||||
|
||||
const fetchPrompt = async () => {
|
||||
if (currentPromptId === "" || currentPromptId === undefined) {
|
||||
return;
|
||||
|
Loading…
Reference in New Issue
Block a user