From a82a54fdd7ccbddfa3fbedad413797c9de1f731c Mon Sep 17 00:00:00 2001 From: MingxuanGame Date: Sat, 16 Aug 2025 16:58:42 +0000 Subject: [PATCH] feat(private-api): add check friend relationship api --- app/router/private/cover.py | 4 +- app/router/private/relationship.py | 65 ++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 app/router/private/relationship.py diff --git a/app/router/private/cover.py b/app/router/private/cover.py index 6ecd83c..08397fe 100644 --- a/app/router/private/cover.py +++ b/app/router/private/cover.py @@ -28,7 +28,7 @@ async def upload_cover( ): """上传用户头图 - 接收图片数据,验证图片格式和大小后存储到存储服务,并更新用户的头像 URL + 接收图片数据,验证图片格式和大小后存储到存储服务,并更新用户的头图 URL 限制条件: - 支持的图片格式: PNG、JPEG、GIF @@ -36,7 +36,7 @@ async def upload_cover( - 最大图片尺寸: 3000x2000 像素 返回: - - 头像 URL 和文件哈希值 + - 头图 URL 和文件哈希值 """ # check file diff --git a/app/router/private/relationship.py b/app/router/private/relationship.py new file mode 100644 index 0000000..15482bf --- /dev/null +++ b/app/router/private/relationship.py @@ -0,0 +1,65 @@ +from __future__ import annotations + +from app.database import Relationship, User +from app.database.relationship import RelationshipType +from app.dependencies.database import get_db +from app.dependencies.user import get_client_user + +from .router import router + +from fastapi import Depends, HTTPException, Path, Security +from pydantic import BaseModel, Field +from sqlmodel import select +from sqlmodel.ext.asyncio.session import AsyncSession + + +class CheckResponse(BaseModel): + is_followed: bool = Field(..., description="对方是否关注了当前用户") + is_following: bool = Field(..., description="当前用户是否关注了对方") + mutual: bool = Field(..., description="当前用户与对方是否互相关注") + + +@router.get( + "/relationship/check/{user_id}", + name="检查关系状态", + description="检查当前用户与指定用户的关系状态", + response_model=CheckResponse, +) +async def check_user_relationship( + user_id: int = Path(..., description="目标用户的 ID"), + current_user: User = Security(get_client_user), + db: AsyncSession = Depends(get_db), +): + if user_id == current_user.id: + raise HTTPException(422, "Cannot check relationship with yourself") + + my_relationship = ( + await db.exec( + select(Relationship).where( + Relationship.user_id == current_user.id, + Relationship.target_id == user_id, + ) + ) + ).first() + + target_relationship = ( + await db.exec( + select(Relationship).where( + Relationship.user_id == user_id, + Relationship.target_id == current_user.id, + ) + ) + ).first() + + is_followed = bool( + target_relationship and target_relationship.type == RelationshipType.FOLLOW + ) + is_following = bool( + my_relationship and my_relationship.type == RelationshipType.FOLLOW + ) + + return CheckResponse( + is_followed=is_followed, + is_following=is_following, + mutual=is_followed and is_following, + )