Add v2 comments support and align APIs
This commit is contained in:
72
back/main.py
72
back/main.py
@@ -1,6 +1,6 @@
|
||||
# 这里是Sycamore whisper的后端代码喵!
|
||||
# 但愿比V1写的好喵(逃
|
||||
from flask import Flask, jsonify, request
|
||||
from flask import Flask, jsonify, request, abort
|
||||
from flask_cors import CORS
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
import os
|
||||
@@ -56,11 +56,10 @@ class Comment(db.Model):
|
||||
__tablename__ = 'comments'
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
submission_id = db.Column(db.Integer, db.ForeignKey('submissions.id'), nullable=False)
|
||||
nickname = db.Column(db.String(50), default='匿名用户')
|
||||
content = db.Column(db.Text, nullable=False)
|
||||
identity_token = db.Column(db.String(36), nullable=True)
|
||||
created_at = db.Column(db.DateTime, default=lambda: datetime.now())
|
||||
upvotes = db.Column(db.Integer, default=0)
|
||||
downvotes = db.Column(db.Integer, default=0)
|
||||
parent_comment_id = db.Column(db.Integer, db.ForeignKey('comments.id'), nullable=True)
|
||||
|
||||
class Hashtag(db.Model):
|
||||
@@ -192,6 +191,7 @@ def submit_post():
|
||||
return jsonify({"code": 2000, "data": "内容不能为空"})
|
||||
|
||||
content = data['content']
|
||||
nickname = data.get('nickname') or '匿名用户'
|
||||
hashtopic = data.get('hashtopic', [])
|
||||
|
||||
if not isinstance(hashtopic, list):
|
||||
@@ -245,6 +245,12 @@ def submit_comment():
|
||||
|
||||
submission_id = data['submission_id']
|
||||
content = data['content']
|
||||
nickname = data.get('nickname') or '匿名用户'
|
||||
parent_comment_id = data.get('parent_comment_id', 0)
|
||||
try:
|
||||
parent_comment_id = int(parent_comment_id)
|
||||
except Exception:
|
||||
parent_comment_id = 0
|
||||
hashtopic = data.get('hashtopic', [])
|
||||
|
||||
if not isinstance(hashtopic, list):
|
||||
@@ -253,8 +259,8 @@ def submit_comment():
|
||||
identity_token = data.get('identity')
|
||||
|
||||
# 检查 submission 是否存在
|
||||
if not Submission.query.get(submission_id):
|
||||
return jsonify({"code": 2004, "data": "投稿不存在"})
|
||||
if not db.session.get(Submission, submission_id):
|
||||
return jsonify({"code": 2002, "data": "投稿不存在"})
|
||||
|
||||
# 违禁词检测
|
||||
for word in DENY_WORDS_CACHE:
|
||||
@@ -270,8 +276,10 @@ def submit_comment():
|
||||
|
||||
new_comment = Comment(
|
||||
submission_id=submission_id,
|
||||
nickname=nickname,
|
||||
content=content,
|
||||
identity_token=identity_token
|
||||
identity_token=identity_token,
|
||||
parent_comment_id=None if parent_comment_id == 0 else parent_comment_id
|
||||
)
|
||||
db.session.add(new_comment)
|
||||
db.session.commit()
|
||||
@@ -291,6 +299,28 @@ def submit_comment():
|
||||
except Exception as e:
|
||||
return jsonify({"code": 2003, "data": f"评论失败: {str(e)}"})
|
||||
|
||||
@app.route('/api/get/comment', methods=['GET'])
|
||||
def get_comments():
|
||||
try:
|
||||
submission_id = request.args.get("id", type=int)
|
||||
if not submission_id:
|
||||
return jsonify({"code": 2000, "data": "参数错误"})
|
||||
|
||||
submission = db.session.get(Submission, submission_id)
|
||||
if not submission or submission.status != "Pass":
|
||||
return jsonify({"code": 2002, "data": "投稿不存在"})
|
||||
|
||||
data = [{
|
||||
"id": c.id,
|
||||
"nickname": c.nickname,
|
||||
"content": c.content,
|
||||
"parent_comment_id": c.parent_comment_id if c.parent_comment_id is not None else 0
|
||||
} for c in submission.comments]
|
||||
|
||||
return jsonify({"code": 1000, "data": data})
|
||||
except Exception as e:
|
||||
return jsonify({"code": 2003, "data": str(e)})
|
||||
|
||||
@app.route('/api/up', methods=['POST'])
|
||||
def upvote():
|
||||
try:
|
||||
@@ -301,17 +331,14 @@ def upvote():
|
||||
item_id = data['id']
|
||||
item_type = data['type']
|
||||
|
||||
item = None
|
||||
if item_type == 'submission':
|
||||
item = db.session.get(Submission, item_id)
|
||||
elif item_type == 'comment':
|
||||
item = db.session.get(Comment, item_id)
|
||||
|
||||
if not item:
|
||||
return jsonify({"code": 2004, "data": "对象不存在"})
|
||||
|
||||
item.upvotes += 1
|
||||
db.session.commit()
|
||||
if not item:
|
||||
return jsonify({"code": 2002, "data": "对象不存在"})
|
||||
item.upvotes += 1
|
||||
db.session.commit()
|
||||
else:
|
||||
return jsonify({"code": 2000, "data": "参数错误"})
|
||||
|
||||
return jsonify({"code": 1000, "data": ""})
|
||||
except Exception as e:
|
||||
@@ -327,17 +354,14 @@ def downvote():
|
||||
item_id = data['id']
|
||||
item_type = data['type']
|
||||
|
||||
item = None
|
||||
if item_type == 'submission':
|
||||
item = db.session.get(Submission, item_id)
|
||||
elif item_type == 'comment':
|
||||
item = db.session.get(Comment, item_id)
|
||||
|
||||
if not item:
|
||||
return jsonify({"code": 2004, "data": "对象不存在"})
|
||||
|
||||
item.downvotes += 1
|
||||
db.session.commit()
|
||||
if not item:
|
||||
return jsonify({"code": 2002, "data": "对象不存在"})
|
||||
item.downvotes += 1
|
||||
db.session.commit()
|
||||
else:
|
||||
return jsonify({"code": 2000, "data": "参数错误"})
|
||||
|
||||
return jsonify({"code": 1000, "data": ""})
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user