Add tag posts view and comment counts

This commit is contained in:
LeonspaceX
2026-01-30 21:59:27 +08:00
parent 43ec347f86
commit 8f6ae3fdfc
7 changed files with 366 additions and 9 deletions

View File

@@ -607,6 +607,67 @@ def get_posts_info():
except Exception as e:
return jsonify({"code": 2003, "data": str(e)})
@app.route('/api/get_posts_by_tag', methods=['GET'])
def get_posts_by_tag():
try:
tag = request.args.get("tag")
if not tag:
return jsonify({"code": 2000, "data": "参数错误"})
tag = str(tag).strip().strip('"').strip("'")
if tag.startswith('#'):
tag = tag[1:]
if not tag:
return jsonify({"code": 2000, "data": "参数错误"})
page = request.args.get("page", 1, type=int)
if page < 1:
page = 1
per_page = 10
submission_ids_query = db.session.query(
Hashtag.target_id.label('submission_id')
).filter(
Hashtag.type == 0,
Hashtag.name == tag
)
comment_submission_ids_query = db.session.query(
Comment.submission_id.label('submission_id')
).join(
Hashtag,
db.and_(Hashtag.type == 1, Hashtag.target_id == Comment.id)
).filter(
Hashtag.name == tag
)
union_subq = submission_ids_query.union(comment_submission_ids_query).subquery()
query = Submission.query.filter(
Submission.id.in_(db.select(union_subq.c.submission_id)),
Submission.status == 'Pass'
).order_by(Submission.id.desc())
pagination = query.paginate(page=page, per_page=per_page, error_out=False)
data = []
for s in pagination.items:
data.append({
"id": s.id,
"content": s.content,
"upvotes": s.upvotes,
"downvotes": s.downvotes,
"created_at": s.created_at.isoformat() if s.created_at else None,
"time": s.created_at.isoformat() if s.created_at else None,
"modified": 0 if (not s.updated_at or not s.created_at or s.updated_at == s.created_at) else 1,
"comment_count": len(s.comments),
"total_pages": pagination.total,
})
return jsonify({"code": 1000, "data": data})
except Exception as e:
return jsonify({"code": 2003, "data": str(e)})
@app.route('/api/post_info', methods=['GET'])
def get_post_info():
try: