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:
@@ -3,7 +3,7 @@ from datetime import datetime, timezone
|
||||
from sqlalchemy import and_, func, or_, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.users.models import BlockedUser, User
|
||||
from app.users.models import BlockedUser, User, UserContact
|
||||
|
||||
|
||||
async def create_user(db: AsyncSession, *, email: str, name: str, username: str, password_hash: str) -> User:
|
||||
@@ -107,3 +107,40 @@ async def list_blocked_users(db: AsyncSession, *, user_id: int) -> list[User]:
|
||||
)
|
||||
result = await db.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
async def add_contact(db: AsyncSession, *, user_id: int, contact_user_id: int) -> UserContact:
|
||||
existing = await get_contact_relation(db, user_id=user_id, contact_user_id=contact_user_id)
|
||||
if existing:
|
||||
return existing
|
||||
relation = UserContact(user_id=user_id, contact_user_id=contact_user_id)
|
||||
db.add(relation)
|
||||
await db.flush()
|
||||
return relation
|
||||
|
||||
|
||||
async def remove_contact(db: AsyncSession, *, user_id: int, contact_user_id: int) -> None:
|
||||
relation = await get_contact_relation(db, user_id=user_id, contact_user_id=contact_user_id)
|
||||
if relation:
|
||||
await db.delete(relation)
|
||||
|
||||
|
||||
async def get_contact_relation(db: AsyncSession, *, user_id: int, contact_user_id: int) -> UserContact | None:
|
||||
result = await db.execute(
|
||||
select(UserContact).where(
|
||||
UserContact.user_id == user_id,
|
||||
UserContact.contact_user_id == contact_user_id,
|
||||
)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def list_contacts(db: AsyncSession, *, user_id: int) -> list[User]:
|
||||
stmt = (
|
||||
select(User)
|
||||
.join(UserContact, UserContact.contact_user_id == User.id)
|
||||
.where(UserContact.user_id == user_id)
|
||||
.order_by(User.username.asc())
|
||||
)
|
||||
result = await db.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
Reference in New Issue
Block a user