feat(contacts): switch contacts UX to email-first flow
Some checks failed
CI / test (push) Failing after 18s

- expose email in user search/contact responses

- add add-contact-by-email API endpoint

- show email in contacts/search and add contact form by email in Contacts tab
This commit is contained in:
2026-03-08 11:51:02 +03:00
parent 897defc39d
commit cbd1b008bb
6 changed files with 77 additions and 5 deletions

View File

@@ -43,7 +43,12 @@ async def search_users_by_username(
exclude_user_id: int | None = None,
) -> list[User]:
normalized = query.lower().strip().lstrip("@")
stmt = select(User).where(func.lower(User.username).like(f"%{normalized}%"))
stmt = select(User).where(
or_(
func.lower(User.username).like(f"%{normalized}%"),
func.lower(User.email).like(f"%{normalized}%"),
)
)
if exclude_user_id is not None:
stmt = stmt.where(User.id != exclude_user_id)
stmt = stmt.order_by(User.username.asc()).limit(limit)

View File

@@ -4,11 +4,12 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.auth.service import get_current_user
from app.database.session import get_db
from app.users.models import User
from app.users.schemas import UserProfileUpdate, UserRead, UserSearchRead
from app.users.schemas import ContactByEmailRequest, UserProfileUpdate, UserRead, UserSearchRead
from app.users.service import (
add_contact,
block_user,
get_user_by_id,
get_user_by_email,
get_user_by_username,
list_blocked_users,
list_contacts,
@@ -101,6 +102,23 @@ async def add_contact_endpoint(
await add_contact(db, user_id=current_user.id, contact_user_id=user_id)
@router.post("/contacts/by-email", status_code=status.HTTP_204_NO_CONTENT)
async def add_contact_by_email_endpoint(
payload: ContactByEmailRequest,
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user),
) -> None:
target = await get_user_by_email(db, payload.email)
if not target:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
if target.id == current_user.id:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Cannot add yourself")
is_blocked = await has_block_relation_between_users(db, user_a_id=current_user.id, user_b_id=target.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=target.id)
@router.delete("/{user_id}/contacts", status_code=status.HTTP_204_NO_CONTENT)
async def remove_contact_endpoint(
user_id: int,

View File

@@ -40,4 +40,9 @@ class UserSearchRead(BaseModel):
id: int
name: str
username: str
email: EmailStr
avatar_url: str | None = None
class ContactByEmailRequest(BaseModel):
email: EmailStr