refactor(submission): 重构发件入库的去重流程。

- 将发件消息插入与附件保存改为事务内执行,避免去重键存在但消息缺失时产生脏数据。
- 抽出可注入的数据库执行器,便于在同一事务中复用消息与附件写入逻辑。
- 补充异常场景测试,覆盖“去重键存在但未命中消息”的失败行为。
This commit is contained in:
LanQin_
2026-06-24 13:26:53 +08:00
parent d1876691dd
commit d3bee62acd
3 changed files with 109 additions and 15 deletions
+20 -3
View File
@@ -1338,6 +1338,18 @@ func (a *App) messageByID(ctx context.Context, id string, includeBody bool) (*Ma
}
func (a *App) insertMessage(ctx context.Context, msg storedMessage, attachments []AttachmentInput) (string, error) {
return a.insertMessageWithDB(ctx, a.db, msg, attachments)
}
type dbExecutor interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}
type dbQueryer interface {
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}
func (a *App) insertMessageWithDB(ctx context.Context, db dbExecutor, msg storedMessage, attachments []AttachmentInput) (string, error) {
id := newID("mail")
now := a.now().UTC().Format(time.RFC3339Nano)
hasAttachments := len(attachments) > 0
@@ -1355,13 +1367,14 @@ func (a *App) insertMessage(ctx context.Context, msg storedMessage, attachments
folderID = msg.FolderID
}
recipientAddr := normalizeEmail(msg.RecipientAddr)
_, err := a.db.ExecContext(ctx, `INSERT INTO messages(id,mailbox_id,folder_id,recipient_addr,message_uid,message_id,subject,from_addr,from_name,to_addrs,cc_addrs,bcc_addrs,sent_at,received_at,snippet,body_text,body_html,is_read,is_starred,has_attachments,size_bytes,raw_path,created_at,updated_at)
_, err := db.ExecContext(ctx, `INSERT INTO messages(id,mailbox_id,folder_id,recipient_addr,message_uid,message_id,subject,from_addr,from_name,to_addrs,cc_addrs,bcc_addrs,sent_at,received_at,snippet,body_text,body_html,is_read,is_starred,has_attachments,size_bytes,raw_path,created_at,updated_at)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`, id, mailboxID, folderID, recipientAddr, msg.MessageUID, msg.MessageID, msg.Subject, msg.From, msg.FromName, jsonEncode(msg.To), jsonEncode(msg.CC), jsonEncode(msg.BCC), msg.SentAt.Format(time.RFC3339Nano), msg.ReceivedAt.Format(time.RFC3339Nano), msg.Snippet, msg.BodyText, msg.BodyHTML, boolInt(msg.IsRead), boolInt(msg.IsStarred), boolInt(hasAttachments), size, msg.RawPath, now, now)
if err != nil {
return "", err
}
for _, att := range attachments {
if err := a.storeAttachment(ctx, id, att); err != nil {
if err := a.storeAttachmentWithDB(ctx, db, id, att); err != nil {
a.deleteMessageFiles(ctx, id)
return "", err
}
}
@@ -1369,6 +1382,10 @@ func (a *App) insertMessage(ctx context.Context, msg storedMessage, attachments
}
func (a *App) storeAttachment(ctx context.Context, messageID string, input AttachmentInput) error {
return a.storeAttachmentWithDB(ctx, a.db, messageID, input)
}
func (a *App) storeAttachmentWithDB(ctx context.Context, db dbExecutor, messageID string, input AttachmentInput) error {
filename := filepath.Base(strings.TrimSpace(input.Filename))
if filename == "." || filename == "" {
filename = "attachment.bin"
@@ -1390,7 +1407,7 @@ func (a *App) storeAttachment(ctx context.Context, messageID string, input Attac
if err := os.WriteFile(path, data, 0o600); err != nil {
return err
}
_, err = a.db.ExecContext(ctx, `INSERT INTO attachments(id,message_id,filename,content_type,size_bytes,storage_path,created_at) VALUES(?,?,?,?,?,?,?)`, id, messageID, filename, contentType, len(data), path, a.now().UTC().Format(time.RFC3339Nano))
_, err = db.ExecContext(ctx, `INSERT INTO attachments(id,message_id,filename,content_type,size_bytes,storage_path,created_at) VALUES(?,?,?,?,?,?,?)`, id, messageID, filename, contentType, len(data), path, a.now().UTC().Format(time.RFC3339Nano))
return err
}