feat(feedback): 提交接口支持携带图片资产并校验引用
This commit is contained in:
1 parent
b31e887b2a
commit
29315cab20
6 files changed
+244
-3
No files matched your search
@@ -7,9 +7,10 @@ backend/app/api/user/handlers.py
|
||||
"""
|
||||
from flask import Blueprint, request, jsonify, current_app
|
||||
from sqlalchemy import and_, or_
|
||||
from sqlalchemy.exc import DatabaseError, OperationalError
|
||||
from sqlalchemy.exc import DatabaseError, IntegrityError, OperationalError
|
||||
from app import db
|
||||
from app.models import User, Agent, QuickQuestion, UserFavoriteAgent, UserDefaultAgent, UserHomepageConfig, GeneralFeedback, ChatLog
|
||||
from app.models import User, Agent, QuickQuestion, UserFavoriteAgent, UserDefaultAgent, UserHomepageConfig, GeneralFeedback, GeneralFeedbackImage, ChatLog
|
||||
from app.services.file_assets.service import get_asset_for_user, mark_asset_referenced
|
||||
from app.services.feedback_notify import send_feedback_group_notification
|
||||
from app.services.agent_category import serialize_agent_list_item, sort_agent_rows
|
||||
from app.services.agent_registry.serialization import serialize_registry_fields
|
||||
@@ -32,6 +33,11 @@ from .auth import (
|
||||
)
|
||||
user_bp = Blueprint('user', __name__)
|
||||
|
||||
# [CONST] 反馈附件图片上限与允许的图片扩展名(POST /feedback 的 image_asset_nos 校验用)
|
||||
_FEEDBACK_MAX_IMAGES = 5
|
||||
_FEEDBACK_IMAGE_EXTENSIONS = {'png', 'jpg', 'jpeg', 'webp'}
|
||||
|
||||
|
||||
@user_bp.route('/wecom/deeplink-opened', methods=['POST'])
|
||||
def mark_wecom_deeplink_opened():
|
||||
user = _get_current_user()
|
||||
@@ -161,9 +167,46 @@ def submit_general_feedback():
|
||||
raise ValidationError('chat_log_id not found')
|
||||
if chat_log.user_id != user.id:
|
||||
raise ForbiddenError('chat_log_id is not owned by current user')
|
||||
# [STEP 1] 解析并规整 image_asset_nos(可选参数):非 list 直接拒绝;元素转 str 去空白、保序去重;超上限整单拒绝
|
||||
raw_asset_nos = data.get('image_asset_nos')
|
||||
normalized_asset_nos: list[str] = []
|
||||
if raw_asset_nos is not None:
|
||||
if not isinstance(raw_asset_nos, list):
|
||||
raise ValidationError(code='FEEDBACK.IMAGE_INVALID', message='image_asset_nos must be a list')
|
||||
seen_asset_nos: set[str] = set()
|
||||
for item in raw_asset_nos:
|
||||
asset_no = str(item or '').strip()
|
||||
if not asset_no or asset_no in seen_asset_nos:
|
||||
continue
|
||||
seen_asset_nos.add(asset_no)
|
||||
normalized_asset_nos.append(asset_no)
|
||||
if len(normalized_asset_nos) > _FEEDBACK_MAX_IMAGES:
|
||||
raise ValidationError(code='FEEDBACK.IMAGE_TOO_MANY', message='最多附加 5 张图片')
|
||||
# [STEP 2] 逐个校验资产归属与图片语义(任一失败整单拒绝,反馈与图片关联都不落库)
|
||||
feedback_assets = []
|
||||
for asset_no in normalized_asset_nos:
|
||||
asset = get_asset_for_user(asset_no, user.id)
|
||||
if asset.extension not in _FEEDBACK_IMAGE_EXTENSIONS:
|
||||
raise ValidationError(code='FEEDBACK.IMAGE_INVALID', message='仅支持 png/jpg/jpeg/webp 图片')
|
||||
if asset.upload_status != 'ready':
|
||||
raise ValidationError(code='FEEDBACK.IMAGE_INVALID', message='图片尚未上传完成')
|
||||
if asset.reference_count != 0 or asset.lifecycle_status != 'temporary':
|
||||
raise ValidationError(code='FEEDBACK.IMAGE_INVALID', message='该文件已被使用')
|
||||
feedback_assets.append(asset)
|
||||
row = GeneralFeedback(user_id=user.id, username=user.username, feedback_type=feedback_type, feedback_module=feedback_module, content=content, source_page=source_page, chat_log_id=chat_log_id)
|
||||
# [STEP 3] 落库:flush 拿反馈 id → 按枚举序写图片关联(sort_order=下标)→ 逐个标记资产被引用 → commit;
|
||||
# IntegrityError 兜底并发双挂(asset_id 唯一约束),回滚整单并转业务错误
|
||||
db.session.add(row)
|
||||
db.session.commit()
|
||||
db.session.flush()
|
||||
for idx, asset in enumerate(feedback_assets):
|
||||
db.session.add(GeneralFeedbackImage(feedback_id=row.id, asset_id=asset.id, sort_order=idx))
|
||||
mark_asset_referenced(asset)
|
||||
try:
|
||||
db.session.commit()
|
||||
except IntegrityError:
|
||||
db.session.rollback()
|
||||
raise ValidationError(code='FEEDBACK.IMAGE_INVALID', message='图片已被使用,请重新添加')
|
||||
# [STEP 4] 通知与返回不变:提交成功后推送企微群通知(失败不影响提交结果),返回旧契约 JSON
|
||||
notify_result = send_feedback_group_notification(row, user)
|
||||
if notify_result.get('ok'):
|
||||
current_app.logger.info('[feedback] group notification sent feedback_id=%s', row.id)
|
||||
|
||||
Reference in new issue
Block a user