2023-06-15 15:43:40 +03:00
|
|
|
from fastapi import APIRouter, Depends, Request
|
2023-11-14 16:31:02 +03:00
|
|
|
from middlewares.auth import AuthBearer, get_current_user
|
2023-12-01 00:29:28 +03:00
|
|
|
from modules.brain.service.brain_user_service import BrainUserService
|
|
|
|
from modules.user.dto.inputs import UserUpdatableProperties
|
2023-11-15 15:17:51 +03:00
|
|
|
from modules.user.entity.user_identity import UserIdentity
|
2023-12-01 00:29:28 +03:00
|
|
|
from modules.user.repository.users import Users
|
2024-04-20 12:22:05 +03:00
|
|
|
from modules.user.service.user_usage import UserUsage
|
2023-06-11 00:59:16 +03:00
|
|
|
|
|
|
|
user_router = APIRouter()
|
2023-12-01 00:29:28 +03:00
|
|
|
brain_user_service = BrainUserService()
|
|
|
|
user_repository = Users()
|
2023-06-12 18:58:05 +03:00
|
|
|
|
2023-06-20 10:54:23 +03:00
|
|
|
|
2023-06-15 15:43:40 +03:00
|
|
|
@user_router.get("/user", dependencies=[Depends(AuthBearer())], tags=["User"])
|
2023-07-04 18:56:54 +03:00
|
|
|
async def get_user_endpoint(
|
2023-08-21 15:05:13 +03:00
|
|
|
request: Request, current_user: UserIdentity = Depends(get_current_user)
|
2023-07-04 18:56:54 +03:00
|
|
|
):
|
2023-06-15 15:43:40 +03:00
|
|
|
"""
|
|
|
|
Get user information and statistics.
|
|
|
|
|
|
|
|
- `current_user`: The current authenticated user.
|
|
|
|
- Returns the user's email, maximum brain size, current brain size, maximum requests number, requests statistics, and the current date.
|
|
|
|
|
|
|
|
This endpoint retrieves information and statistics about the authenticated user. It includes the user's email, maximum brain size,
|
|
|
|
current brain size, maximum requests number, requests statistics, and the current date. The brain size is calculated based on the
|
|
|
|
user's uploaded vectors, and the maximum brain size is obtained from the environment variables. The requests statistics provide
|
|
|
|
information about the user's API usage.
|
|
|
|
"""
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-10-23 19:19:04 +03:00
|
|
|
user_daily_usage = UserUsage(
|
2023-09-13 14:47:12 +03:00
|
|
|
id=current_user.id,
|
|
|
|
email=current_user.email,
|
|
|
|
)
|
2023-10-23 19:19:04 +03:00
|
|
|
user_settings = user_daily_usage.get_user_settings()
|
|
|
|
max_brain_size = user_settings.get("max_brain_size", 1000000000)
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2024-01-22 22:46:26 +03:00
|
|
|
monthly_chat_credit = user_settings.get("monthly_chat_credit", 10)
|
2023-08-21 15:05:13 +03:00
|
|
|
|
2023-10-23 19:19:04 +03:00
|
|
|
user_daily_usage = UserUsage(id=current_user.id)
|
2023-06-11 00:59:16 +03:00
|
|
|
|
2023-07-04 18:56:54 +03:00
|
|
|
return {
|
|
|
|
"email": current_user.email,
|
|
|
|
"max_brain_size": max_brain_size,
|
2024-04-10 12:45:16 +03:00
|
|
|
"current_brain_size": 0,
|
2024-01-22 22:46:26 +03:00
|
|
|
"monthly_chat_credit": monthly_chat_credit,
|
2023-10-23 19:19:04 +03:00
|
|
|
"models": user_settings.get("models", []),
|
2023-08-04 21:25:09 +03:00
|
|
|
"id": current_user.id,
|
2023-10-23 19:19:04 +03:00
|
|
|
"is_premium": user_settings["is_premium"],
|
2023-07-04 18:56:54 +03:00
|
|
|
}
|
2023-08-01 10:24:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
@user_router.put(
|
|
|
|
"/user/identity",
|
|
|
|
dependencies=[Depends(AuthBearer())],
|
|
|
|
tags=["User"],
|
|
|
|
)
|
|
|
|
def update_user_identity_route(
|
2023-08-21 15:05:13 +03:00
|
|
|
user_identity_updatable_properties: UserUpdatableProperties,
|
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-08-01 10:24:57 +03:00
|
|
|
) -> UserIdentity:
|
|
|
|
"""
|
|
|
|
Update user identity.
|
|
|
|
"""
|
2023-12-01 00:29:28 +03:00
|
|
|
return user_repository.update_user_properties(
|
|
|
|
current_user.id, user_identity_updatable_properties
|
|
|
|
)
|
2023-08-01 10:24:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
@user_router.get(
|
|
|
|
"/user/identity",
|
|
|
|
dependencies=[Depends(AuthBearer())],
|
|
|
|
tags=["User"],
|
|
|
|
)
|
|
|
|
def get_user_identity_route(
|
2023-08-21 15:05:13 +03:00
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
2023-08-01 10:24:57 +03:00
|
|
|
) -> UserIdentity:
|
|
|
|
"""
|
|
|
|
Get user identity.
|
|
|
|
"""
|
2023-12-01 00:29:28 +03:00
|
|
|
return user_repository.get_user_identity(current_user.id)
|
2024-04-27 14:44:48 +03:00
|
|
|
|
2024-05-02 12:31:58 +03:00
|
|
|
@user_router.delete(
|
|
|
|
"/user_data",
|
|
|
|
dependencies=[Depends(AuthBearer())],
|
|
|
|
tags=["User"],
|
|
|
|
)
|
|
|
|
async def delete_user_data_route(
|
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Delete a user.
|
|
|
|
|
|
|
|
- `user_id`: The ID of the user to delete.
|
|
|
|
|
|
|
|
This endpoint deletes a user from the system.
|
|
|
|
"""
|
|
|
|
|
|
|
|
user_repository.delete_user_data(current_user.id)
|
|
|
|
|
|
|
|
return {"message": "User deleted successfully"}
|
|
|
|
|
2024-04-27 14:44:48 +03:00
|
|
|
@user_router.get(
|
|
|
|
"/user/credits",
|
|
|
|
dependencies=[Depends(AuthBearer())],
|
|
|
|
tags=["User"],
|
|
|
|
)
|
|
|
|
def get_user_credits(
|
|
|
|
current_user: UserIdentity = Depends(get_current_user),
|
|
|
|
) -> int:
|
|
|
|
"""
|
|
|
|
Get user remaining credits.
|
|
|
|
"""
|
|
|
|
return user_repository.get_user_credits(current_user.id)
|