fix(data): 删除改成一个事务且真删、配额与内容同事务
四类问题都出在「多次独立写入 + 失败只打日志」这个模式上。
1) 删除投票的级联没有事务(route/user/vote.go)
选项 / 表单字段 / 提交记录 / 投票本体是 4 次独立写入,前 3 步失败只 Warn。
中途失败会留下「选项没了但票还在」这种界面无法修复的状态 —— 重试删除时
投票已不存在,只能报 not exist,脏数据永远留着。
→ db.Votes.DeleteWithCascade:四张表在同一事务里删完。
2) 删除其实是软删除,个人数据没真删掉
模型都嵌了 dbutil.Model(含 DeletedAt),原来所有 Delete 都是软删:
提交记录里参与者填写的文本、voter_ip、voter_token,以及提问里的 FromIP,
只是「界面上看不见」,库里永久留着。用户点删除的预期不是这样。
软删还会占住 link_token 唯一索引,删过的链接永远不能复用。
→ 删除投票与删除提问统一 Unscoped 真删;提问的追问/图片关联/配额回退
原来也是三个独立写入(失败只 Warn),一并收进事务。
3) 配额计数会永久偏低
「创建内容」与「quota_count+1」是两次写入,后者失败只 Warn:
箱主看到的「已收集 7/10」偏小,达到上限后因为计数偏小实际还能继续收,
等于定量收集被静默绕过;重置计数也校准不回来(重置是把计数清零,更脱节)。
→ 抽出 incrementQuotaTx(tx, userID, feature, delta) 作为唯一实现,
提问 Questions.Create、私聊 CreateWithFirstMessage、投票提交
VoteSubmissions.Create(新增 QuotaUserID)都在自己的事务里完成 +1。
注意口径:投票的定量收集是「累计收到的票数」(提交成功时 +1),
不是「创建了几个投票」—— 所以删除投票不回退这个计数。
4) 改写追问回答每次都发通知邮件
箱主改措辞、补一句、修错别字都会重新保存,每次都发的话提问者会连着收到
好几封一模一样的「您的提问有回复」。主回答那边早有 question.Answer == ""
的判定,追问这里补齐:UpdateAnswerByID 在事务内读旧值返回 wasFirst。
顺带:
- isQuestioner 的 token 比较从 == 改为 subtle.ConstantTimeCompare,
与 Pager 中间件里的校验同口径(== 按字节短路,可用响应时间逐字节试探)。
- CSV 导出的表头(字段名)与选项内容列补 sanitizeCSVCell。原来只有数据列走它,
而选项名同样可能是 =HYPERLINK(...) 形式;判断也改成看 trim 后的首字符。
- 写路径的功能开关改 fail-closed:新增 db.FeatureStateOfStrict,读配置失败时
拒绝而不是回退默认配置 —— 提问/投票的默认值是「开启」,fail-open 等于让一次
数据库抖动绕过箱主设的关闭与定量上限。读展示仍用宽松版。
验证:全量单测(带 DB)与 go build ./... 通过;verify_round3 6/6(私聊
message_count 与实际消息数一致)、verify_p1 4/4、verify_round2 19/19、
verify_page_next 6/6、verify_chat_owner 3/3。
This commit is contained in:
@@ -2,6 +2,7 @@ package question
|
||||
|
||||
import (
|
||||
stdctx "context"
|
||||
"crypto/subtle"
|
||||
"fmt"
|
||||
"unicode/utf8"
|
||||
|
||||
@@ -21,7 +22,11 @@ import (
|
||||
// isQuestioner 判断当前请求者是否为该提问的提问者。
|
||||
// 匿名提问者通过 token(URL 或表单隐藏域)验证;登录提问者通过账号 ID 验证。
|
||||
func isQuestioner(ctx context.Context, question *db.Question, token string) bool {
|
||||
if question.Token != "" && question.Token == token {
|
||||
// 常数时间比较:与 Pager 中间件里的校验(route/question/question.go)同一口径。
|
||||
// 用 == 会按字节短路,攻击者能靠响应时间逐字节试探出 token ——
|
||||
// 而 token 就是「私密提问 / 尚未回答的提问」唯一的钥匙。
|
||||
if question.Token != "" && token != "" &&
|
||||
subtle.ConstantTimeCompare([]byte(question.Token), []byte(token)) == 1 {
|
||||
return true
|
||||
}
|
||||
if ctx.IsLogged && question.AskerUserID != 0 && ctx.User.ID == question.AskerUserID {
|
||||
@@ -192,7 +197,8 @@ func AnswerFollowUp(ctx context.Context, pageUser *db.User, question *db.Questio
|
||||
return
|
||||
}
|
||||
|
||||
if err := db.FollowUps.UpdateAnswerByID(ctx.Request().Context(), question.ID, followUpID, f.Answer); err != nil {
|
||||
wasFirst, err := db.FollowUps.UpdateAnswerByID(ctx.Request().Context(), question.ID, followUpID, f.Answer)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrFollowUpNotFound) {
|
||||
// 同请求渲染需用 SetError(SetErrorFlash 要等下一次请求才显示)。
|
||||
ctx.SetError(errors.New(i18n.T(ctx.Lang(), "followup.errNotFound")), f)
|
||||
@@ -205,9 +211,13 @@ func AnswerFollowUp(ctx context.Context, pageUser *db.User, question *db.Questio
|
||||
return
|
||||
}
|
||||
|
||||
// 邮件通知提问者(留下邮箱时):复用"您的提问有了回复"模板,带 token 链接
|
||||
// 邮件通知提问者(留下邮箱时):复用"您的提问有了回复"模板,带 token 链接。
|
||||
//
|
||||
// 只在**首次**回答时发:箱主改一次措辞、补一句、修错别字都会重新保存,
|
||||
// 每次都发的话提问者会连着收到好几封一模一样的「您的提问有回复」。
|
||||
// 主回答那边早就有同样的判定(question.Answer == ""),这里补齐。
|
||||
shared.SafeGo(func() {
|
||||
if question.ReceiveReplyEmail != "" {
|
||||
if wasFirst && question.ReceiveReplyEmail != "" {
|
||||
if err := mail.SendNewAnswerMail(question.ReceiveReplyEmail, pageUser.Domain, question.ID, question.Content, f.Answer, question.Token); err != nil {
|
||||
logrus.WithContext(stdctx.Background()).WithError(err).Error("Failed to send follow up answer mail to questioner")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user