Spaces:
Running
Running
| # router_users_social.py | |
| # ========================================== | |
| # 🤝 用户社交路由模块 | |
| # ========================================== | |
| # 作用:处理用户间的社交互动(关注/取关)和隐私设置 | |
| # 关联文件: | |
| # - 数据库连接.py (JSON数据库读写 users.json) | |
| # - notifications.py (发送关注通知) | |
| # - models.py (FollowToggle, PrivacySettings 数据模型) | |
| # - router_users.py (主路由聚合此模块) | |
| # 前端调用: | |
| # - 个人中心视图.js (关注/取关按钮) | |
| # - 个人设置表单组件.js (隐私设置) | |
| # ========================================== | |
| from fastapi import APIRouter, HTTPException | |
| import 数据库连接 as db | |
| from notifications import add_notification | |
| from models import FollowToggle, PrivacySettings | |
| # 创建子路由实例 | |
| router = APIRouter() | |
| # ========================================== | |
| # 👥 关注/取消关注接口 | |
| # ========================================== | |
| # 作用:用户A关注或取消关注用户B | |
| # 关联: | |
| # - users.json 的 followers 和 following 字段 | |
| # - notifications.py 的 add_notification() (发送关注通知) | |
| # 数据结构: | |
| # - 用户A.following: 存储A关注的人的账号列表 | |
| # - 用户B.followers: 存储关注B的人的账号列表 | |
| # 前端调用: | |
| # - 个人中心视图.js 的 handleFollowToggle() | |
| async def toggle_follow(follow: FollowToggle): | |
| """ | |
| 关注/取消关注接口 | |
| 请求参数:(FollowToggle 模型) | |
| - user_id: 当前操作用户的账号(执行关注动作的人) | |
| - target_account: 被关注/取关的目标用户账号 | |
| - is_active: True=关注, False=取消关注 | |
| """ | |
| # 加载用户数据库 | |
| users_db = db.load_data("users.json", default_data={}) | |
| # ========== 用户存在性校验 ========== | |
| if follow.target_account not in users_db or follow.user_id not in users_db: | |
| raise HTTPException(status_code=404, detail="用户不存在") | |
| # 获取双方的关注/粉丝列表 | |
| # setdefault 确保字段存在,不存在则初始化为空列表 | |
| target_followers = users_db[follow.target_account].setdefault("followers", []) | |
| current_following = users_db[follow.user_id].setdefault("following", []) | |
| # ========== 执行关注或取关操作 ========== | |
| if follow.is_active: | |
| # === 关注操作 === | |
| # 将当前用户添加到目标用户的粉丝列表 | |
| if follow.user_id not in target_followers: | |
| target_followers.append(follow.user_id) | |
| # 发送关注通知给目标用户 | |
| # 关联:notifications.py 的 add_notification() | |
| add_notification(follow.target_account, { | |
| "type": "follow", | |
| "from_user": follow.user_id | |
| }) | |
| # 将目标用户添加到当前用户的关注列表 | |
| if follow.target_account not in current_following: | |
| current_following.append(follow.target_account) | |
| else: | |
| # === 取消关注操作 === | |
| # 从目标用户的粉丝列表中移除当前用户 | |
| if follow.user_id in target_followers: | |
| target_followers.remove(follow.user_id) | |
| # 从当前用户的关注列表中移除目标用户 | |
| if follow.target_account in current_following: | |
| current_following.remove(follow.target_account) | |
| # 保存更新后的数据 | |
| db.save_data("users.json", users_db) | |
| return {"status": "success"} | |
| # ========================================== | |
| # 🔒 隐私设置接口 | |
| # ========================================== | |
| # 作用:更新用户的隐私偏好设置 | |
| # 关联: | |
| # - users.json 的 privacy 字段 | |
| # - models.py 的 PrivacySettings 模型 | |
| # 隐私选项说明: | |
| # - follows: 是否公开关注列表 | |
| # - likes: 是否公开点赞记录 | |
| # - favorites: 是否公开收藏记录 | |
| # - downloads: 是否公开下载/使用记录 | |
| # 前端调用: | |
| # - 个人设置表单组件.js 的隐私设置区域 | |
| async def update_privacy(account: str, privacy: PrivacySettings): | |
| """ | |
| 更新隐私设置接口 | |
| 路径参数: | |
| - account: 用户账号 | |
| 请求参数:(PrivacySettings 模型) | |
| - follows: 是否隐藏关注列表 (True=隐藏) | |
| - likes: 是否隐藏点赞记录 (True=隐藏) | |
| - favorites: 是否隐藏收藏记录 (True=隐藏) | |
| - downloads: 是否隐藏下载记录 (True=隐藏) | |
| """ | |
| # 加载用户数据库 | |
| users_db = db.load_data("users.json", default_data={}) | |
| # 检查用户是否存在 | |
| if account not in users_db: | |
| raise HTTPException(status_code=404, detail="用户不存在") | |
| # 更新用户的隐私设置 | |
| users_db[account]["privacy"] = privacy.dict() | |
| # 保存更新后的数据 | |
| db.save_data("users.json", users_db) | |
| return {"status": "success"} | |