Spaces:
Running
Running
| # router_comments.py | |
| from fastapi import APIRouter, HTTPException | |
| import time | |
| import uuid | |
| import 数据库连接 as db | |
| from notifications import add_notification | |
| from models import InteractionToggle, CommentCreate | |
| router = APIRouter() | |
| async def toggle_interaction(interaction: InteractionToggle): | |
| items_db = db.load_data("items.json", default_data=[]) | |
| target_item = next((item for item in items_db if item["id"] == interaction.item_id), None) | |
| if not target_item: raise HTTPException(status_code=404, detail="目标存在问题") | |
| list_key = f"{interaction.action_type}d_by" | |
| count_key = "likes" if interaction.action_type == "like" else "favorites" | |
| if list_key not in target_item: target_item[list_key] = []; target_item[count_key] = 0 | |
| user_list = target_item[list_key] | |
| if interaction.is_active: | |
| if interaction.user_id not in user_list: | |
| user_list.append(interaction.user_id) | |
| target_item[count_key] += 1 | |
| add_notification(target_item.get("author", ""), {"type": interaction.action_type, "from_user": interaction.user_id, "target_item_id": target_item["id"], "target_item_title": target_item["title"]}) | |
| else: | |
| if interaction.user_id in user_list: | |
| user_list.remove(interaction.user_id) | |
| target_item[count_key] = max(0, target_item[count_key] - 1) | |
| db.save_data("items.json", items_db) | |
| return {"status": "success", "new_count": target_item[count_key]} | |
| async def post_comment(comment: CommentCreate): | |
| comments_db = db.load_data("comments.json", default_data={}) | |
| users_db = db.load_data("users.json", default_data={}) | |
| author_info = users_db.get(comment.author, {}) | |
| reply_name = users_db.get(comment.reply_to_user, {}).get("name", comment.reply_to_user) if comment.reply_to_user else None | |
| item_comments = comments_db.get(comment.item_id, []) | |
| new_comment = { | |
| "id": f"c_{int(time.time())}_{uuid.uuid4().hex[:6]}", "author": comment.author, | |
| "authorName": author_info.get("name", comment.author), "avatar": author_info.get("avatarDataUrl", "https://via.placeholder.com/150"), | |
| "content": comment.content, "replyToUser": comment.reply_to_user, "replyToUserName": reply_name, | |
| "isDeleted": False, "replies": [], "created_at": int(time.time()) | |
| } | |
| if comment.parent_id: | |
| parent = next((c for c in item_comments if c["id"] == comment.parent_id), None) | |
| if parent: | |
| parent["replies"].append(new_comment) | |
| if comment.reply_to_user: | |
| add_notification(comment.reply_to_user, {"type": "reply", "from_user": comment.author, "target_item_id": comment.item_id, "target_item_title": "收到新的回复", "content": comment.content}) | |
| else: raise HTTPException(status_code=404, detail="找不到要回复的评论") | |
| else: | |
| item_comments.append(new_comment) | |
| items_db = db.load_data("items.json", default_data=[]) | |
| target_item = next((item for item in items_db if item["id"] == comment.item_id), None) | |
| if target_item: | |
| add_notification(target_item.get("author", ""), {"type": "comment", "from_user": comment.author, "target_item_id": comment.item_id, "target_item_title": target_item["title"], "content": comment.content}) | |
| elif comment.item_id in users_db: | |
| add_notification(comment.item_id, {"type": "comment", "from_user": comment.author, "target_item_id": comment.item_id, "target_item_title": "您的个人留言板", "content": comment.content}) | |
| comments_db[comment.item_id] = item_comments | |
| db.save_data("comments.json", comments_db) | |
| return {"status": "success", "data": new_comment} | |
| async def soft_delete_comment(item_id: str, comment_id: str, author: str): | |
| comments_db = db.load_data("comments.json", default_data={}) | |
| item_comments = comments_db.get(item_id, []) | |
| def find_and_delete(comments_list): | |
| for c in comments_list: | |
| if c["id"] == comment_id: | |
| if c["author"] != author: raise HTTPException(status_code=403, detail="无权删除他人的评论") | |
| c["isDeleted"] = True; c["content"] = ""; return True | |
| if "replies" in c and find_and_delete(c["replies"]): return True | |
| return False | |
| if find_and_delete(item_comments): | |
| db.save_data("comments.json", comments_db) | |
| return {"status": "success"} | |
| raise HTTPException(status_code=404, detail="找不到该评论") |