feat(email): 实现邮件详情免配置一键翻译与原文切换功能并发布V2.14.0
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-10-18 11:02:23 +08:00
1 parent 2cedb02bcc
commit 8cf7c55a0b
10 files changed
+568 -17

No files matched your search

+116
View File
@@ -0,0 +1,116 @@
package email
import (
"context"
"fmt"
"net/http"
"strings"
"git.pcux.cn/xuans/Smail/dto/response"
"git.pcux.cn/xuans/Smail/services/detail"
"git.pcux.cn/xuans/Smail/services/translate"
smailContext "git.pcux.cn/xuans/Smail/utils/context"
log "github.com/sirupsen/logrus"
)
type emailTranslateRequest struct {
ID int `json:"id"`
Subject string `json:"subject"`
Text string `json:"text"`
TargetLang string `json:"target_lang"`
}
type emailTranslateResponse struct {
SubjectTranslated string `json:"subject_translated"`
ContentTranslated string `json:"content_translated"`
DetectedLang string `json:"detected_lang"`
TargetLang string `json:"target_lang"`
}
// TranslateEmail 邮件免配置一键翻译接口
func TranslateEmail(ctx *smailContext.Context, w http.ResponseWriter, req *http.Request) {
var retData emailTranslateRequest
if err := decodeEmailJSONRequest(w, req, &retData, maxEmailJSONRequestBytes); err != nil {
log.WithContext(ctx).WithError(err).Info("invalid email translate request")
response.NewErrorResponse(response.ParamsError, "参数错误", "").FPrint(w)
return
}
targetLang := strings.TrimSpace(retData.TargetLang)
if targetLang == "" {
targetLang = "zh-CN"
}
var subjectToTranslate string
var contentToTranslate string
if retData.ID > 0 {
emailData, err := detail.GetEmailDetail(ctx, retData.ID, false)
if err != nil {
log.WithContext(ctx).WithError(err).Warnf("translate email: load email detail failed for id=%d", retData.ID)
response.NewErrorResponse(response.ServerError, "获取邮件失败", "").FPrint(w)
return
}
subjectToTranslate = emailData.Subject
if emailData.Text.Valid && strings.TrimSpace(emailData.Text.String) != "" {
contentToTranslate = emailData.Text.String
} else if emailData.Html.Valid {
contentToTranslate = emailData.Html.String
}
} else {
subjectToTranslate = retData.Subject
contentToTranslate = retData.Text
}
if strings.TrimSpace(subjectToTranslate) == "" && strings.TrimSpace(contentToTranslate) == "" {
response.NewSuccessResponse(emailTranslateResponse{
SubjectTranslated: "",
ContentTranslated: "",
DetectedLang: "auto",
TargetLang: targetLang,
}).FPrint(w)
return
}
callCtx := req.Context()
if callCtx == nil {
callCtx = context.Background()
}
var translatedSubject string
var detectedLang = "auto"
if strings.TrimSpace(subjectToTranslate) != "" {
subRes, err := translate.TranslateText(callCtx, subjectToTranslate, targetLang)
if err != nil {
log.WithContext(ctx).WithError(err).Warn("translate subject failed")
response.NewErrorResponse(response.ServerError, fmt.Sprintf("翻译主题失败: %v", err), "").FPrint(w)
return
}
translatedSubject = subRes.TranslatedText
if subRes.SourceLang != "" && subRes.SourceLang != "auto" {
detectedLang = subRes.SourceLang
}
}
var translatedContent string
if strings.TrimSpace(contentToTranslate) != "" {
contentRes, err := translate.TranslateText(callCtx, contentToTranslate, targetLang)
if err != nil {
log.WithContext(ctx).WithError(err).Warn("translate content failed")
response.NewErrorResponse(response.ServerError, fmt.Sprintf("翻译内容失败: %v", err), "").FPrint(w)
return
}
translatedContent = contentRes.TranslatedText
if detectedLang == "auto" && contentRes.SourceLang != "" && contentRes.SourceLang != "auto" {
detectedLang = contentRes.SourceLang
}
}
response.NewSuccessResponse(emailTranslateResponse{
SubjectTranslated: translatedSubject,
ContentTranslated: translatedContent,
DetectedLang: detectedLang,
TargetLang: targetLang,
}).FPrint(w)
}
@@ -0,0 +1,55 @@
package email
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"git.pcux.cn/xuans/Smail/services/translate"
"git.pcux.cn/xuans/Smail/utils/context"
)
func TestTranslateEmailDirectText(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`[[["你好","Hello",null,null,10]],null,"en"]`))
}))
defer ts.Close()
translate.SetApiBaseURL(ts.URL)
translate.SetDefaultClient(ts.Client())
reqBody := emailTranslateRequest{
Subject: "Hello",
Text: "Hello",
TargetLang: "zh-CN",
}
bodyBytes, _ := json.Marshal(reqBody)
r := httptest.NewRequest(http.MethodPost, "/api/v1/email/translate", bytes.NewReader(bodyBytes))
w := httptest.NewRecorder()
ctx := &context.Context{UserID: 1}
TranslateEmail(ctx, w, r)
if w.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d, body: %s", w.Code, w.Body.String())
}
var resp struct {
Code int `json:"code"`
Data emailTranslateResponse `json:"data"`
}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("unmarshal response failed: %v", err)
}
if resp.Data.SubjectTranslated != "你好" {
t.Errorf("expected SubjectTranslated='你好', got %q", resp.Data.SubjectTranslated)
}
if resp.Data.ContentTranslated != "你好" {
t.Errorf("expected ContentTranslated='你好', got %q", resp.Data.ContentTranslated)
}
}