feat(private-api): add check friend relationship api
This commit is contained in:
@@ -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
|
||||
|
||||
65
app/router/private/relationship.py
Normal file
65
app/router/private/relationship.py
Normal file
@@ -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,
|
||||
)
|
||||
Reference in New Issue
Block a user