2023-06-14 22:21:13 +03:00
|
|
|
from auth.auth_bearer import AuthBearer, get_current_user
|
2023-06-11 00:59:16 +03:00
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from models.users import User
|
|
|
|
from utils.vectors import CommonsDep
|
|
|
|
|
|
|
|
explore_router = APIRouter()
|
|
|
|
|
2023-06-12 18:58:05 +03:00
|
|
|
def get_unique_user_data(commons, user):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Retrieve unique user data vectors.
|
|
|
|
"""
|
2023-06-11 00:59:16 +03:00
|
|
|
response = commons['supabase'].table("vectors").select(
|
|
|
|
"name:metadata->>file_name, size:metadata->>file_size", count="exact").filter("user_id", "eq", user.email).execute()
|
|
|
|
documents = response.data # Access the data from the response
|
|
|
|
# Convert each dictionary to a tuple of items, then to a set to remove duplicates, and then back to a dictionary
|
|
|
|
unique_data = [dict(t) for t in set(tuple(d.items()) for d in documents)]
|
2023-06-12 18:58:05 +03:00
|
|
|
return unique_data
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-06-15 15:43:40 +03:00
|
|
|
@explore_router.get("/explore", dependencies=[Depends(AuthBearer())], tags=["Explore"])
|
2023-06-12 18:58:05 +03:00
|
|
|
async def explore_endpoint(commons: CommonsDep, current_user: User = Depends(get_current_user)):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Retrieve and explore unique user data vectors.
|
|
|
|
"""
|
2023-06-12 18:58:05 +03:00
|
|
|
unique_data = get_unique_user_data(commons, current_user)
|
|
|
|
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-15 15:43:40 +03:00
|
|
|
@explore_router.delete("/explore/{file_name}", dependencies=[Depends(AuthBearer())], tags=["Explore"])
|
2023-06-14 22:21:13 +03:00
|
|
|
async def delete_endpoint(commons: CommonsDep, file_name: str, credentials: dict = Depends(AuthBearer())):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Delete a specific user file by file name.
|
|
|
|
"""
|
2023-06-11 00:59:16 +03:00
|
|
|
user = User(email=credentials.get('email', 'none'))
|
|
|
|
# Cascade delete the summary from the database first, because it has a foreign key constraint
|
|
|
|
commons['supabase'].table("summaries").delete().match(
|
|
|
|
{"metadata->>file_name": file_name}).execute()
|
|
|
|
commons['supabase'].table("vectors").delete().match(
|
|
|
|
{"metadata->>file_name": file_name, "user_id": user.email}).execute()
|
|
|
|
return {"message": f"{file_name} of user {user.email} has been deleted."}
|
|
|
|
|
2023-06-15 15:43:40 +03:00
|
|
|
@explore_router.get("/explore/{file_name}", dependencies=[Depends(AuthBearer())], tags=["Explore"])
|
2023-06-12 18:58:05 +03:00
|
|
|
async def download_endpoint(commons: CommonsDep, file_name: str, current_user: User = Depends(get_current_user)):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Download a specific user file by file name.
|
|
|
|
"""
|
2023-06-11 00:59:16 +03:00
|
|
|
response = commons['supabase'].table("vectors").select(
|
2023-06-12 18:58:05 +03:00
|
|
|
"metadata->>file_name, metadata->>file_size, metadata->>file_extension, metadata->>file_url", "content").match({"metadata->>file_name": file_name, "user_id": current_user.email}).execute()
|
2023-06-11 00:59:16 +03:00
|
|
|
documents = response.data
|
|
|
|
return {"documents": documents}
|