Spaces:
Running
Running
Upload 2 files
Browse files- router_comments.py +66 -10
- router_items.py +34 -2
router_comments.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
# router_comments.py
|
| 2 |
-
from fastapi import APIRouter, HTTPException
|
| 3 |
import time
|
| 4 |
import uuid
|
| 5 |
import 数据库连接 as db
|
| 6 |
from notifications import add_notification
|
| 7 |
from models import InteractionToggle, CommentCreate
|
|
|
|
| 8 |
|
| 9 |
router = APIRouter()
|
| 10 |
|
|
@@ -67,17 +68,72 @@ async def post_comment(comment: CommentCreate):
|
|
| 67 |
return {"status": "success", "data": new_comment}
|
| 68 |
|
| 69 |
@router.delete("/api/comments/{item_id}/{comment_id}")
|
| 70 |
-
async def soft_delete_comment(item_id: str, comment_id: str,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
comments_db = db.load_data("comments.json", default_data={})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
item_comments = comments_db.get(item_id, [])
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
for c in comments_list:
|
| 75 |
if c["id"] == comment_id:
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
if "replies" in c and
|
|
|
|
| 79 |
return False
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# router_comments.py
|
| 2 |
+
from fastapi import APIRouter, HTTPException, Depends
|
| 3 |
import time
|
| 4 |
import uuid
|
| 5 |
import 数据库连接 as db
|
| 6 |
from notifications import add_notification
|
| 7 |
from models import InteractionToggle, CommentCreate
|
| 8 |
+
from 安全认证 import require_auth, is_admin
|
| 9 |
|
| 10 |
router = APIRouter()
|
| 11 |
|
|
|
|
| 68 |
return {"status": "success", "data": new_comment}
|
| 69 |
|
| 70 |
@router.delete("/api/comments/{item_id}/{comment_id}")
|
| 71 |
+
async def soft_delete_comment(item_id: str, comment_id: str, account: str = Depends(require_auth)):
|
| 72 |
+
"""
|
| 73 |
+
软删除评论
|
| 74 |
+
权限规则:评论作者可删除自己的评论,帖子/内容作者可删除其帖子下的评论,管理员可删除任何评论
|
| 75 |
+
"""
|
| 76 |
comments_db = db.load_data("comments.json", default_data={})
|
| 77 |
+
items_db = db.load_data("items.json", default_data=[])
|
| 78 |
+
posts_db = db.load_data("posts.json", default_data=[])
|
| 79 |
+
users_db = db.load_data("users.json", default_data={})
|
| 80 |
+
|
| 81 |
item_comments = comments_db.get(item_id, [])
|
| 82 |
+
target_comment = None
|
| 83 |
+
|
| 84 |
+
def find_comment(comments_list):
|
| 85 |
+
"""查找目标评论"""
|
| 86 |
+
nonlocal target_comment
|
| 87 |
for c in comments_list:
|
| 88 |
if c["id"] == comment_id:
|
| 89 |
+
target_comment = c
|
| 90 |
+
return True
|
| 91 |
+
if "replies" in c and find_comment(c["replies"]):
|
| 92 |
+
return True
|
| 93 |
return False
|
| 94 |
+
|
| 95 |
+
if not find_comment(item_comments):
|
| 96 |
+
raise HTTPException(status_code=404, detail="找不到该评论")
|
| 97 |
+
|
| 98 |
+
# 权限检查
|
| 99 |
+
is_comment_author = target_comment["author"] == account
|
| 100 |
+
is_admin_user = is_admin(account)
|
| 101 |
+
|
| 102 |
+
# 检查是否为内容作者(帖子作者或资源作者)
|
| 103 |
+
is_content_author = False
|
| 104 |
+
# 检查是否为帖子作者
|
| 105 |
+
for post in posts_db:
|
| 106 |
+
if post["id"] == item_id and post.get("author") == account:
|
| 107 |
+
is_content_author = True
|
| 108 |
+
break
|
| 109 |
+
# 检查是否为资源作者
|
| 110 |
+
if not is_content_author:
|
| 111 |
+
for item in items_db:
|
| 112 |
+
if item["id"] == item_id and item.get("author") == account:
|
| 113 |
+
is_content_author = True
|
| 114 |
+
break
|
| 115 |
+
# 检查是否为个人主页留言板作者
|
| 116 |
+
if not is_content_author:
|
| 117 |
+
if item_id in users_db and item_id == account:
|
| 118 |
+
is_content_author = True
|
| 119 |
+
|
| 120 |
+
# 权限判断:评论作者、内容作者或管理员可删除
|
| 121 |
+
if not (is_comment_author or is_content_author or is_admin_user):
|
| 122 |
+
raise HTTPException(status_code=403, detail="无权删除此评论")
|
| 123 |
+
|
| 124 |
+
# 执行软删除
|
| 125 |
+
def mark_deleted(comments_list):
|
| 126 |
+
for c in comments_list:
|
| 127 |
+
if c["id"] == comment_id:
|
| 128 |
+
c["isDeleted"] = True
|
| 129 |
+
c["content"] = ""
|
| 130 |
+
return True
|
| 131 |
+
if "replies" in c and mark_deleted(c["replies"]):
|
| 132 |
+
return True
|
| 133 |
+
return False
|
| 134 |
+
|
| 135 |
+
mark_deleted(item_comments)
|
| 136 |
+
comments_db[item_id] = item_comments
|
| 137 |
+
db.save_data("comments.json", comments_db)
|
| 138 |
+
|
| 139 |
+
return {"status": "success"}
|
router_items.py
CHANGED
|
@@ -9,7 +9,8 @@ import urllib.error
|
|
| 9 |
import json
|
| 10 |
import 数据库连接 as db
|
| 11 |
from models import ItemCreate, ItemUpdate
|
| 12 |
-
from 安全认证 import require_auth
|
|
|
|
| 13 |
|
| 14 |
router = APIRouter()
|
| 15 |
|
|
@@ -284,4 +285,35 @@ async def check_github_updates():
|
|
| 284 |
async def get_item_version(item_id: str):
|
| 285 |
"""获取单个资源的最新版本号"""
|
| 286 |
versions_db = db.load_data("versions.json", default_data={})
|
| 287 |
-
return {"status": "success", "version": versions_db.get(item_id, "")}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import json
|
| 10 |
import 数据库连接 as db
|
| 11 |
from models import ItemCreate, ItemUpdate
|
| 12 |
+
from 安全认证 import require_auth, check_ownership
|
| 13 |
+
from 数据库连接 import invalidate_cache
|
| 14 |
|
| 15 |
router = APIRouter()
|
| 16 |
|
|
|
|
| 285 |
async def get_item_version(item_id: str):
|
| 286 |
"""获取单个资源的最新版本号"""
|
| 287 |
versions_db = db.load_data("versions.json", default_data={})
|
| 288 |
+
return {"status": "success", "version": versions_db.get(item_id, "")}
|
| 289 |
+
|
| 290 |
+
@router.delete("/api/items/{item_id}")
|
| 291 |
+
async def delete_item(item_id: str, current_user: str = Depends(require_auth)):
|
| 292 |
+
"""
|
| 293 |
+
删除内容(仅作者或管理员可操作)
|
| 294 |
+
"""
|
| 295 |
+
items_db = db.load_data("items.json", default_data=[])
|
| 296 |
+
comments_db = db.load_data("comments.json", default_data={})
|
| 297 |
+
|
| 298 |
+
for i, item in enumerate(items_db):
|
| 299 |
+
if item["id"] == item_id:
|
| 300 |
+
# 🔒 权限检查:仅作者或管理员可删除
|
| 301 |
+
if not check_ownership(item, current_user, owner_field="author", allow_admin=True):
|
| 302 |
+
raise HTTPException(status_code=403, detail="无权删除他人发布的内容")
|
| 303 |
+
|
| 304 |
+
# 1. 从 items.json 中删除该条目
|
| 305 |
+
items_db.pop(i)
|
| 306 |
+
db.save_data("items.json", items_db)
|
| 307 |
+
|
| 308 |
+
# 2. 清理关联评论:从 comments.json 中删除该内容的所有评论
|
| 309 |
+
if item_id in comments_db:
|
| 310 |
+
del comments_db[item_id]
|
| 311 |
+
db.save_data("comments.json", comments_db)
|
| 312 |
+
|
| 313 |
+
# 3. 清理缓存:使 items.json 和 comments.json 的缓存失效
|
| 314 |
+
invalidate_cache("items.json")
|
| 315 |
+
invalidate_cache("comments.json")
|
| 316 |
+
|
| 317 |
+
return {"status": "success", "message": "内容已删除"}
|
| 318 |
+
|
| 319 |
+
raise HTTPException(status_code=404, detail="找不到该内容记录")
|