Private
Public Access
Users can set a display name (shown instead of username in the message list, room member list, TopBar, and admin Users tab) and upload a real avatar, replacing the generated color-initial avatars everywhere a user appears. Avatars are square-cropped and downscaled to 512px, reusing app/storage.py's upload primitives from image uploads with a new square option. Two deliberate divergences from message-image handling, documented in backend/README.md: the previous avatar file is deleted on replace/remove (safe since it's strictly one file per user), and avatar serving is not room-gated and uses a short cache (identity-addressed and mutable, unlike a message image's permanent content-addressed URL). Frontend: new ProfileModal reachable from the TopBar account menu; AuthContext gains updateUser() so a profile change reflects instantly everywhere without a refetch.
87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.database import get_db
|
|
from app.dependencies import get_current_user
|
|
from app.models import User
|
|
from app.schemas.invite import InviteRead, MyInviteRead
|
|
from app.schemas.room import RoomMemberRead
|
|
from app.services.invite_service import (
|
|
InviteExpiredError,
|
|
InviteNotFoundError,
|
|
InviteNotPendingError,
|
|
WrongInviteTargetError,
|
|
accept_invite,
|
|
decline_invite,
|
|
list_my_invites,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/invites", tags=["invites"])
|
|
|
|
|
|
@router.get("/mine", response_model=list[MyInviteRead])
|
|
async def list_my_invites_endpoint(
|
|
current_user: User = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
invites = await list_my_invites(db, current_user.id)
|
|
return [
|
|
MyInviteRead(
|
|
id=i.id,
|
|
room_id=i.room_id,
|
|
invited_by=i.invited_by,
|
|
target_user_id=i.target_user_id,
|
|
status=i.status,
|
|
expires_at=i.expires_at,
|
|
created_at=i.created_at,
|
|
room_name=i.room.name,
|
|
invited_by_username=i.inviter.username,
|
|
)
|
|
for i in invites
|
|
]
|
|
|
|
|
|
@router.post("/{invite_id}/accept", response_model=RoomMemberRead)
|
|
async def accept_invite_endpoint(
|
|
invite_id: uuid.UUID,
|
|
current_user: User = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
try:
|
|
membership = await accept_invite(db, invite_id, current_user.id)
|
|
except InviteNotFoundError:
|
|
raise HTTPException(status_code=404, detail="Invite not found")
|
|
except WrongInviteTargetError:
|
|
raise HTTPException(status_code=403, detail="This invite is not addressed to you")
|
|
except InviteNotPendingError:
|
|
raise HTTPException(status_code=400, detail="Invite is no longer pending")
|
|
except InviteExpiredError:
|
|
raise HTTPException(status_code=400, detail="Invite has expired")
|
|
|
|
return RoomMemberRead(
|
|
user_id=membership.user_id,
|
|
username=current_user.username,
|
|
display_name=current_user.display_name,
|
|
avatar_filename=current_user.avatar_filename,
|
|
role=membership.role,
|
|
joined_at=membership.joined_at,
|
|
)
|
|
|
|
|
|
@router.post("/{invite_id}/decline", response_model=InviteRead)
|
|
async def decline_invite_endpoint(
|
|
invite_id: uuid.UUID,
|
|
current_user: User = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
try:
|
|
return await decline_invite(db, invite_id, current_user.id)
|
|
except InviteNotFoundError:
|
|
raise HTTPException(status_code=404, detail="Invite not found")
|
|
except WrongInviteTargetError:
|
|
raise HTTPException(status_code=403, detail="This invite is not addressed to you")
|
|
except InviteNotPendingError:
|
|
raise HTTPException(status_code=400, detail="Invite is no longer pending")
|