Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

Adjust the contents of unsubscribe

lotkb93bf70e463b
diff --git a/core/api/batch_mail/v1/unsubscribe.go b/core/api/batch_mail/v1/unsubscribe.go
index 2ba80b40..1c20b3bb 100644
--- a/core/api/batch_mail/v1/unsubscribe.go
+++ b/core/api/batch_mail/v1/unsubscribe.go
@@ -5,8 +5,9 @@ import (
)
type UnsubscribeReq struct {
- g.Meta `path:"/unsubscribe" method:"post" tags:"Unsubscribe" summary:" unsubscribe request"`
- Jwt string `json:"jwt" dc:" unsubscribe jwt"`
+ g.Meta `path:"/unsubscribe" method:"post" tags:"Unsubscribe" summary:" unsubscribe request"`
+ Jwt string `json:"jwt" dc:" unsubscribe jwt"`
+ GroupId []int `json:"group_id" dc:"unsubscribe groupID"`
}
type UnsubscribeRes struct {
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_unsubscribe.go b/core/internal/controller/batch_mail/batch_mail_v1_unsubscribe.go
index f716a52a..46cbd5d5 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_unsubscribe.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_unsubscribe.go
@@ -1,12 +1,11 @@
package batch_mail
import (
+ "billionmail-core/api/batch_mail/v1"
+ "billionmail-core/internal/service/batch_mail"
"context"
"github.com/gogf/gf/v2/frame/g"
"time"
-
- "billionmail-core/api/batch_mail/v1"
- "billionmail-core/internal/service/batch_mail"
)
func (c *ControllerV1) Unsubscribe(ctx context.Context, req *v1.UnsubscribeReq) (res *v1.UnsubscribeRes, err error) {
@@ -18,11 +17,13 @@ func (c *ControllerV1) Unsubscribe(ctx context.Context, req *v1.UnsubscribeReq)
res.Message = "Invalid token"
return res, nil
}
+ // 获取当前用户的组ID
+ GroupId := req.GroupId
// 更新联系人状态
_, err = g.DB().Model("contacts").
Where("email", claims.Email).
- Where("group_id", claims.GroupId).
+ WhereIn("group_id", GroupId).
Data(g.Map{"active": 0}).
Update()
if err != nil {
@@ -31,20 +32,30 @@ func (c *ControllerV1) Unsubscribe(ctx context.Context, req *v1.UnsubscribeReq)
return res, nil
}
- // 记录退订详情
- _, err = g.DB().Model("unsubscribe_records").Insert(g.Map{
- "email": claims.Email,
- "group_id": claims.GroupId,
- "template_id": claims.TemplateId,
- "task_id": claims.TaskId,
- "unsubscribe_time": time.Now().Unix(),
- })
- if err != nil {
- res.Success = false
- res.Message = "Failed to record unsubscribe"
- return res, nil
+ // 记录退订详情 每个组ID都要记录
+ for _, groupId := range GroupId {
+ _, err = g.DB().Model("unsubscribe_records").Insert(g.Map{
+ "email": claims.Email,
+ "group_id": groupId,
+ "template_id": claims.TemplateId,
+ "task_id": claims.TaskId,
+ "unsubscribe_time": time.Now().Unix(),
+ })
+ if err != nil {
+ res.Success = false
+ res.Message = "Failed to record unsubscribe"
+ return res, nil
+ }
}
+ //_, err = g.DB().Model("unsubscribe_records").Insert(g.Map{
+ // "email": claims.Email,
+ // "group_id": req.GroupId,
+ // "template_id": claims.TemplateId,
+ // "task_id": claims.TaskId,
+ // "unsubscribe_time": time.Now().Unix(),
+ //})
+
res.Success = true
res.Message = "Successfully unsubscribed"
return res, nil
diff --git a/core/internal/service/batch_mail/jwt.go b/core/internal/service/batch_mail/jwt.go
index 67ffa1cb..f8ec9c9e 100644
--- a/core/internal/service/batch_mail/jwt.go
+++ b/core/internal/service/batch_mail/jwt.go
@@ -18,7 +18,6 @@ const (
// UnsubscribeClaims 定义退订JWT的声明结构
type UnsubscribeClaims struct {
Email string `json:"email"`
- GroupId int `json:"group_id"`
TemplateId int `json:"template_id"`
TaskId int `json:"task_id"`
jwt.RegisteredClaims
@@ -72,10 +71,20 @@ func getOrGenerateSecret(ctx context.Context) string {
"name": JWT_SECRET_KEY,
"value": newSecret,
}).
- Save()
+ Where("name", JWT_SECRET_KEY).
+ WhereNull("value").
+ Insert()
if err != nil {
g.Log().Warning(ctx, "Failed to persist JWT secret to database:", err)
+
+ val, err := g.DB().Model("bm_options").
+ Where("name", JWT_SECRET_KEY).
+ Value("value")
+
+ if err == nil && val != nil && val.String() != "" {
+ return val.String()
+ }
} else {
g.Log().Info(ctx, "Generated and persisted new JWT unsubscribe secret")
}
@@ -112,7 +121,7 @@ func generateSecret(ctx context.Context) string {
}
// GenerateUnsubscribeJWT 生成退订JWT
-func GenerateUnsubscribeJWT(email string, groupId, templateId, taskId int) (string, error) {
+func GenerateUnsubscribeJWT(email string, templateId, taskId int) (string, error) {
cfg := getConfig()
claims := UnsubscribeClaims{