优化hashtag储存方式,添加违禁词检测功能,注意此commit对数据库结构存在破坏性更改
This commit is contained in:
52
back/main.py
52
back/main.py
@@ -44,7 +44,6 @@ class Submission(db.Model):
|
||||
__tablename__ = 'submissions'
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
content = db.Column(db.Text, nullable=False)
|
||||
hashtopic = db.Column(db.Text) # JSON string
|
||||
identity_token = db.Column(db.String(36), nullable=True)
|
||||
status = db.Column(db.String(20), default='Pending')
|
||||
created_at = db.Column(db.DateTime, default=lambda: datetime.now())
|
||||
@@ -58,13 +57,19 @@ class Comment(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
submission_id = db.Column(db.Integer, db.ForeignKey('submissions.id'), nullable=False)
|
||||
content = db.Column(db.Text, nullable=False)
|
||||
hashtopic = db.Column(db.Text) # JSON string
|
||||
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):
|
||||
__tablename__ = 'hashtags'
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
type = db.Column(db.Integer, nullable=False) # 0: Submission, 1: Comment
|
||||
target_id = db.Column(db.Integer, nullable=False)
|
||||
name = db.Column(db.String(255), nullable=False)
|
||||
|
||||
class DenyWord(db.Model):
|
||||
__tablename__ = 'deny_words'
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
@@ -88,6 +93,17 @@ def load_config():
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to load settings: {e}")
|
||||
|
||||
def load_deny_words():
|
||||
global DENY_WORDS_CACHE
|
||||
with app.app_context():
|
||||
try:
|
||||
words = db.session.query(DenyWord.word).all()
|
||||
# words 是 list of tuples [('word1',), ('word2',)]
|
||||
DENY_WORDS_CACHE = [w[0] for w in words]
|
||||
print(f"Loaded {len(DENY_WORDS_CACHE)} deny words.")
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to load deny words: {e}")
|
||||
|
||||
# --- 用户普通api端点 ---
|
||||
@app.route('/api/settings', methods=['GET'])
|
||||
def get_settings():
|
||||
@@ -185,10 +201,9 @@ def submit_post():
|
||||
|
||||
identity_token = data.get('identity')
|
||||
|
||||
# 违禁词检测
|
||||
deny_words = DenyWord.query.all()
|
||||
for dw in deny_words:
|
||||
if dw.word in content:
|
||||
# 违禁词检测 (使用内存缓存)
|
||||
for word in DENY_WORDS_CACHE:
|
||||
if word in content:
|
||||
return jsonify({"code": 2005, "data": "提交内容包含违禁词"})
|
||||
|
||||
# Identity 验证
|
||||
@@ -201,13 +216,23 @@ def submit_post():
|
||||
# 保存
|
||||
new_post = Submission(
|
||||
content=content,
|
||||
hashtopic=json.dumps(hashtopic) if hashtopic else '[]',
|
||||
identity_token=identity_token,
|
||||
status='Pending' if NEED_AUDIT else 'Pass'
|
||||
)
|
||||
db.session.add(new_post)
|
||||
db.session.commit()
|
||||
|
||||
# 保存 Hashtags
|
||||
if hashtopic:
|
||||
for tag in hashtopic:
|
||||
new_tag = Hashtag(
|
||||
type=0, # 0 for Submission
|
||||
target_id=new_post.id,
|
||||
name=tag
|
||||
)
|
||||
db.session.add(new_tag)
|
||||
db.session.commit()
|
||||
|
||||
code = 1002 if new_post.status == 'Pending' else 1001
|
||||
return jsonify({"code": code, "data": {"id": new_post.id}})
|
||||
except Exception as e:
|
||||
@@ -243,12 +268,22 @@ def submit_comment():
|
||||
new_comment = Comment(
|
||||
submission_id=submission_id,
|
||||
content=content,
|
||||
hashtopic=json.dumps(hashtopic) if hashtopic else '[]',
|
||||
identity_token=identity_token
|
||||
)
|
||||
db.session.add(new_comment)
|
||||
db.session.commit()
|
||||
|
||||
# 保存 Hashtags
|
||||
if hashtopic:
|
||||
for tag in hashtopic:
|
||||
new_tag = Hashtag(
|
||||
type=1, # 1 for Comment
|
||||
target_id=new_comment.id,
|
||||
name=tag
|
||||
)
|
||||
db.session.add(new_tag)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify({"code": 1000, "data": ""})
|
||||
except Exception as e:
|
||||
return jsonify({"code": 2003, "data": f"评论失败: {str(e)}"})
|
||||
@@ -337,4 +372,5 @@ def get_statics():
|
||||
if __name__ == '__main__':
|
||||
init_db()
|
||||
load_config()
|
||||
load_deny_words()
|
||||
app.run(debug=True, port=5000)
|
||||
|
||||
Reference in New Issue
Block a user