diff --git a/app/users/repository.py b/app/users/repository.py
index 8381e79..ee9c076 100644
--- a/app/users/repository.py
+++ b/app/users/repository.py
@@ -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)
diff --git a/app/users/router.py b/app/users/router.py
index 7a1f996..ba33576 100644
--- a/app/users/router.py
+++ b/app/users/router.py
@@ -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,
diff --git a/app/users/schemas.py b/app/users/schemas.py
index 8e18a7c..c3ef7c3 100644
--- a/app/users/schemas.py
+++ b/app/users/schemas.py
@@ -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
diff --git a/web/src/api/users.ts b/web/src/api/users.ts
index e102ac0..05414d3 100644
--- a/web/src/api/users.ts
+++ b/web/src/api/users.ts
@@ -48,6 +48,10 @@ export async function addContact(userId: number): Promise {user.name} @{user.username} {user.email || `@${user.username}`}
Add contact (Email)
+{contactEmailError}
: null} +No contacts yet
) : null} @@ -451,7 +490,7 @@ export function ChatList() { type="button" >{user.name}
-@{user.username}
+{user.email || `@${user.username}`}