feat(contacts): add contacts module with backend APIs and web tab
Some checks failed
CI / test (push) Failing after 18s
Some checks failed
CI / test (push) Failing after 18s
- add user_contacts table and migration - expose /users/contacts list/add/remove endpoints - add Contacts tab in chat list with add/remove actions
This commit is contained in:
@@ -6,10 +6,14 @@ from app.database.session import get_db
|
||||
from app.users.models import User
|
||||
from app.users.schemas import UserProfileUpdate, UserRead, UserSearchRead
|
||||
from app.users.service import (
|
||||
add_contact,
|
||||
block_user,
|
||||
get_user_by_id,
|
||||
get_user_by_username,
|
||||
list_blocked_users,
|
||||
list_contacts,
|
||||
has_block_relation_between_users,
|
||||
remove_contact,
|
||||
search_users_by_username,
|
||||
unblock_user,
|
||||
update_user_profile,
|
||||
@@ -72,6 +76,42 @@ async def read_blocked_users(
|
||||
return await list_blocked_users(db, user_id=current_user.id)
|
||||
|
||||
|
||||
@router.get("/contacts", response_model=list[UserSearchRead])
|
||||
async def read_contacts(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> list[UserSearchRead]:
|
||||
return await list_contacts(db, user_id=current_user.id)
|
||||
|
||||
|
||||
@router.post("/{user_id}/contacts", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def add_contact_endpoint(
|
||||
user_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> None:
|
||||
if user_id == current_user.id:
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Cannot add yourself")
|
||||
target = await get_user_by_id(db, user_id)
|
||||
if not target:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
|
||||
is_blocked = await has_block_relation_between_users(db, user_a_id=current_user.id, user_b_id=user_id)
|
||||
if is_blocked:
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Cannot add contact while blocked")
|
||||
await add_contact(db, user_id=current_user.id, contact_user_id=user_id)
|
||||
|
||||
|
||||
@router.delete("/{user_id}/contacts", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def remove_contact_endpoint(
|
||||
user_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> None:
|
||||
if user_id == current_user.id:
|
||||
return
|
||||
await remove_contact(db, user_id=current_user.id, contact_user_id=user_id)
|
||||
|
||||
|
||||
@router.post("/{user_id}/block", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def block_user_endpoint(
|
||||
user_id: int,
|
||||
|
||||
Reference in New Issue
Block a user