2023-06-28 20:39:27 +03:00
|
|
|
from uuid import UUID
|
|
|
|
|
2023-07-04 18:56:54 +03:00
|
|
|
from auth import AuthBearer, get_current_user
|
2023-06-28 20:39:27 +03:00
|
|
|
from fastapi import APIRouter, Depends, Query
|
2023-08-21 15:05:13 +03:00
|
|
|
from models import Brain, UserIdentity, get_supabase_db
|
2023-07-11 11:00:06 +03:00
|
|
|
from routes.authorizations.brain_authorization import (
|
2023-07-19 18:13:02 +03:00
|
|
|
RoleEnum,
|
|
|
|
has_brain_authorization,
|
|
|
|
validate_brain_authorization,
|
|
|
|
)
|
2023-07-11 11:00:06 +03:00
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
explore_router = APIRouter()
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-06-29 19:26:03 +03:00
|
|
|
@explore_router.get("/explore/", dependencies=[Depends(AuthBearer())], tags=["Explore"])
|
2023-07-04 18:56:54 +03:00
|
|
|
async def explore_endpoint(
|
|
|
|
brain_id: UUID = Query(..., description="The ID of the brain"),
|
|
|
|
):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Retrieve and explore unique user data vectors.
|
|
|
|
"""
|
2023-06-28 20:39:27 +03:00
|
|
|
brain = Brain(id=brain_id)
|
|
|
|
unique_data = brain.get_unique_brain_files()
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
unique_data.sort(key=lambda x: int(x["size"]), reverse=True)
|
2023-06-11 00:59:16 +03:00
|
|
|
return {"documents": unique_data}
|
|
|
|
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
@explore_router.delete(
|
2023-07-11 11:00:06 +03:00
|
|
|
"/explore/{file_name}/",
|
|
|
|
dependencies=[
|
|
|
|
Depends(AuthBearer()),
|
2023-07-19 18:13:02 +03:00
|
|
|
Depends(has_brain_authorization(RoleEnum.Owner)),
|
2023-07-11 11:00:06 +03:00
|
|
|
],
|
|
|
|
tags=["Explore"],
|
2023-06-22 18:50:06 +03:00
|
|
|
)
|
2023-07-04 18:56:54 +03:00
|
|
|
async def delete_endpoint(
|
|
|
|
file_name: str,
|
2023-08-21 15:05:13 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-07-04 18:56:54 +03:00
|
|
|
brain_id: UUID = Query(..., description="The ID of the brain"),
|
|
|
|
):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Delete a specific user file by file name.
|
|
|
|
"""
|
2023-06-28 20:39:27 +03:00
|
|
|
brain = Brain(id=brain_id)
|
|
|
|
brain.delete_file_from_brain(file_name)
|
|
|
|
|
2023-07-04 18:56:54 +03:00
|
|
|
return {
|
|
|
|
"message": f"{file_name} of brain {brain_id} has been deleted by user {current_user.email}."
|
|
|
|
}
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
|
|
|
|
@explore_router.get(
|
2023-06-30 10:08:40 +03:00
|
|
|
"/explore/{file_name}/", dependencies=[Depends(AuthBearer())], tags=["Explore"]
|
2023-06-22 18:50:06 +03:00
|
|
|
)
|
|
|
|
async def download_endpoint(
|
2023-08-21 15:05:13 +03:00
|
|
|
file_name: str, current_user: UserIdentity = Depends(get_current_user)
|
2023-06-22 18:50:06 +03:00
|
|
|
):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Download a specific user file by file name.
|
|
|
|
"""
|
2023-07-04 18:56:54 +03:00
|
|
|
# check if user has the right to get the file: add brain_id to the query
|
2023-06-28 20:39:27 +03:00
|
|
|
|
2023-08-03 21:24:42 +03:00
|
|
|
supabase_db = get_supabase_db()
|
|
|
|
response = supabase_db.get_vectors_by_file_name(file_name)
|
2023-06-11 00:59:16 +03:00
|
|
|
documents = response.data
|
2023-07-11 11:00:06 +03:00
|
|
|
|
|
|
|
if len(documents) == 0:
|
|
|
|
return {"documents": []}
|
|
|
|
|
2023-07-13 12:02:52 +03:00
|
|
|
related_brain_id = (
|
|
|
|
documents[0]["brains_vectors"][0]["brain_id"]
|
|
|
|
if len(documents[0]["brains_vectors"]) != 0
|
|
|
|
else None
|
|
|
|
)
|
2023-07-11 11:00:06 +03:00
|
|
|
if related_brain_id is None:
|
2023-07-13 12:02:52 +03:00
|
|
|
raise Exception(f"File {file_name} has no brain_id associated with it")
|
2023-07-11 11:00:06 +03:00
|
|
|
|
|
|
|
validate_brain_authorization(brain_id=related_brain_id, user_id=current_user.id)
|
|
|
|
|
2023-06-11 00:59:16 +03:00
|
|
|
return {"documents": documents}
|