优化hashtag储存方式,添加违禁词检测功能,注意此commit对数据库结构存在破坏性更改
This commit is contained in:
BIN
back/__pycache__/main.cpython-314.pyc
Normal file
BIN
back/__pycache__/main.cpython-314.pyc
Normal file
Binary file not shown.
52
back/main.py
52
back/main.py
@@ -44,7 +44,6 @@ class Submission(db.Model):
|
|||||||
__tablename__ = 'submissions'
|
__tablename__ = 'submissions'
|
||||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
content = db.Column(db.Text, 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)
|
identity_token = db.Column(db.String(36), nullable=True)
|
||||||
status = db.Column(db.String(20), default='Pending')
|
status = db.Column(db.String(20), default='Pending')
|
||||||
created_at = db.Column(db.DateTime, default=lambda: datetime.now())
|
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)
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
submission_id = db.Column(db.Integer, db.ForeignKey('submissions.id'), nullable=False)
|
submission_id = db.Column(db.Integer, db.ForeignKey('submissions.id'), nullable=False)
|
||||||
content = db.Column(db.Text, 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)
|
identity_token = db.Column(db.String(36), nullable=True)
|
||||||
created_at = db.Column(db.DateTime, default=lambda: datetime.now())
|
created_at = db.Column(db.DateTime, default=lambda: datetime.now())
|
||||||
upvotes = db.Column(db.Integer, default=0)
|
upvotes = db.Column(db.Integer, default=0)
|
||||||
downvotes = 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)
|
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):
|
class DenyWord(db.Model):
|
||||||
__tablename__ = 'deny_words'
|
__tablename__ = 'deny_words'
|
||||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||||
@@ -88,6 +93,17 @@ def load_config():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Warning: Failed to load settings: {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端点 ---
|
# --- 用户普通api端点 ---
|
||||||
@app.route('/api/settings', methods=['GET'])
|
@app.route('/api/settings', methods=['GET'])
|
||||||
def get_settings():
|
def get_settings():
|
||||||
@@ -185,10 +201,9 @@ def submit_post():
|
|||||||
|
|
||||||
identity_token = data.get('identity')
|
identity_token = data.get('identity')
|
||||||
|
|
||||||
# 违禁词检测
|
# 违禁词检测 (使用内存缓存)
|
||||||
deny_words = DenyWord.query.all()
|
for word in DENY_WORDS_CACHE:
|
||||||
for dw in deny_words:
|
if word in content:
|
||||||
if dw.word in content:
|
|
||||||
return jsonify({"code": 2005, "data": "提交内容包含违禁词"})
|
return jsonify({"code": 2005, "data": "提交内容包含违禁词"})
|
||||||
|
|
||||||
# Identity 验证
|
# Identity 验证
|
||||||
@@ -201,13 +216,23 @@ def submit_post():
|
|||||||
# 保存
|
# 保存
|
||||||
new_post = Submission(
|
new_post = Submission(
|
||||||
content=content,
|
content=content,
|
||||||
hashtopic=json.dumps(hashtopic) if hashtopic else '[]',
|
|
||||||
identity_token=identity_token,
|
identity_token=identity_token,
|
||||||
status='Pending' if NEED_AUDIT else 'Pass'
|
status='Pending' if NEED_AUDIT else 'Pass'
|
||||||
)
|
)
|
||||||
db.session.add(new_post)
|
db.session.add(new_post)
|
||||||
db.session.commit()
|
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
|
code = 1002 if new_post.status == 'Pending' else 1001
|
||||||
return jsonify({"code": code, "data": {"id": new_post.id}})
|
return jsonify({"code": code, "data": {"id": new_post.id}})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -243,12 +268,22 @@ def submit_comment():
|
|||||||
new_comment = Comment(
|
new_comment = Comment(
|
||||||
submission_id=submission_id,
|
submission_id=submission_id,
|
||||||
content=content,
|
content=content,
|
||||||
hashtopic=json.dumps(hashtopic) if hashtopic else '[]',
|
|
||||||
identity_token=identity_token
|
identity_token=identity_token
|
||||||
)
|
)
|
||||||
db.session.add(new_comment)
|
db.session.add(new_comment)
|
||||||
db.session.commit()
|
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": ""})
|
return jsonify({"code": 1000, "data": ""})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({"code": 2003, "data": f"评论失败: {str(e)}"})
|
return jsonify({"code": 2003, "data": f"评论失败: {str(e)}"})
|
||||||
@@ -337,4 +372,5 @@ def get_statics():
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
init_db()
|
init_db()
|
||||||
load_config()
|
load_config()
|
||||||
|
load_deny_words()
|
||||||
app.run(debug=True, port=5000)
|
app.run(debug=True, port=5000)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// 我草,react-md-editor这么好用,无语了,早知道v1也用这个编辑器了.....
|
// 我草,react-md-editor这么好用,无语了,早知道v1也用这个编辑器了.....
|
||||||
// 不过居然没有汉化...有点可惜
|
// 不过居然没有汉化...有点可惜
|
||||||
// 我撤回刚才那句话,编辑器有提供中文指令集,我是sb。
|
// 我撤回刚才那句话,编辑器有提供中文指令集,我是sb,没发现。
|
||||||
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
||||||
import MDEditor from '@uiw/react-md-editor';
|
import MDEditor from '@uiw/react-md-editor';
|
||||||
// 引入中文指令集
|
// 引入中文指令集
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// v1的时候就连统计信息也5秒获取一次,不妥,所以改成逻辑触发刷新
|
||||||
import React, { useState, useEffect, useCallback } from 'react';
|
import React, { useState, useEffect, useCallback } from 'react';
|
||||||
import {
|
import {
|
||||||
makeStyles,
|
makeStyles,
|
||||||
|
|||||||
Reference in New Issue
Block a user