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

Refactor tag filter logic and remove unused field in task management

lotk802c68e5fc54
diff --git a/core/api/batch_mail/v1/batch_mail.go b/core/api/batch_mail/v1/batch_mail.go
index bf193482..24147e5c 100644
--- a/core/api/batch_mail/v1/batch_mail.go
+++ b/core/api/batch_mail/v1/batch_mail.go
@@ -273,7 +273,6 @@ type UpdateTaskInfoReq struct {
StartTime int `json:"start_time" dc:"start time"`
TagIds []int `json:"tag_ids" dc:"tag ids for filtering contacts"`
TagLogic string `json:"tag_logic" v:"in:AND,OR" dc:"tag logic (AND: must have all tags, OR: have any tag)"`
- UseTagFilter int `json:"use_tag_filter" dc:"whether to use tag filter (1: yes, 0: no)" v:"in:0,1"`
}
type UpdateTaskInfoRes struct {
api_v1.StandardRes
diff --git a/core/api/contact/contact.go b/core/api/contact/contact.go
index f515080c..900f6a34 100644
--- a/core/api/contact/contact.go
+++ b/core/api/contact/contact.go
@@ -25,6 +25,7 @@ type IContactV1 interface {
UpdateContactsGroup(ctx context.Context, req *v1.UpdateContactsGroupReq) (res *v1.UpdateContactsGroupRes, err error)
GetContactsTrend(ctx context.Context, req *v1.GetContactsTrendReq) (res *v1.GetContactsTrendRes, err error)
GetGroupContactCount(ctx context.Context, req *v1.GetGroupContactCountReq) (res *v1.GetGroupContactCountRes, err error)
+ GetSingleGroupTagContactCount(ctx context.Context, req *v1.GetSingleGroupTagContactCountReq) (res *v1.GetSingleGroupTagContactCountRes, err error)
GetGroupInfo(ctx context.Context, req *v1.GetGroupInfoReq) (res *v1.GetGroupInfoRes, err error)
EditContacts(ctx context.Context, req *v1.EditContactsReq) (res *v1.EditContactsRes, err error)
ListContactsNDP(ctx context.Context, req *v1.ListContactsNDPReq) (res *v1.ListContactsNDPRes, err error)
diff --git a/core/api/contact/v1/contact.go b/core/api/contact/v1/contact.go
index 26f18b4c..7c465e62 100644
--- a/core/api/contact/v1/contact.go
+++ b/core/api/contact/v1/contact.go
@@ -298,7 +298,7 @@ type GetContactsTrendRes struct {
}
type MonthlyCount struct {
- Date string `json:"date" dc:"Month Format: YYYY-MM"`
+ Date string `json:"date" dc:"Month Format: YYYY-MM"`
Count int `json:"count" dc:"Count"`
}
@@ -320,6 +320,21 @@ type GetGroupContactCountRes struct {
} `json:"data" dc:"Data"`
}
+type GetSingleGroupTagContactCountReq struct {
+ g.Meta `path:"/contact/group/tag_contact_count" method:"post" tags:"Contact" summary:"Get single group contact count with tag ids"`
+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`
+ GroupId int `json:"group_id" v:"required" dc:"Group ID"`
+ TagIds []int `json:"tag_ids" dc:"Tag IDs"`
+ TagLogic string `json:"tag_logic" v:"required|in:AND,OR" dc:"Tag Logic (AND/OR)"`
+}
+
+type GetSingleGroupTagContactCountRes struct {
+ api_v1.StandardRes
+ Data struct {
+ Total int `json:"total" dc:"Total contact count with given tag ids in the group"`
+ } `json:"data" dc:"Data"`
+}
+
type GetGroupInfoReq struct {
g.Meta `path:"/contact/group/info" method:"get" tags:"Contact" summary:"Get group info"`
Authorization string `json:"authorization" dc:"Authorization" in:"header"`
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_create_task.go b/core/internal/controller/batch_mail/batch_mail_v1_create_task.go
index 86d96e93..89f47190 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_create_task.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_create_task.go
@@ -56,16 +56,14 @@ func validateCreateTaskRequest(req *v1.CreateTaskReq) error {
return gerror.New("Must select a contact group")
}
- if req.UseTagFilter==1 && len(req.TagIds) == 0 {
- return gerror.New("Must select at least one tag when tag filter is enabled")
- }
-
- if req.UseTagFilter==1 && req.TagLogic != "" && req.TagLogic != "AND" && req.TagLogic != "OR" {
- return gerror.New("Tag logic must be AND or OR")
- }
+ if len(req.TagIds) > 0 {
+ if req.TagLogic != "" && req.TagLogic != "AND" && req.TagLogic != "OR" {
+ return gerror.New("Tag logic must be AND or OR")
+ }
- if req.UseTagFilter==1 && req.TagLogic == "" {
- req.TagLogic = "AND"
+ if req.TagLogic == "" {
+ req.TagLogic = "AND"
+ }
}
return nil
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_update_task_info.go b/core/internal/controller/batch_mail/batch_mail_v1_update_task_info.go
index e2a93d6e..58526508 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_update_task_info.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_update_task_info.go
@@ -49,9 +49,6 @@ func (c *ControllerV1) UpdateTaskInfo(ctx context.Context, req *v1.UpdateTaskInf
if req.Warmup == 0 || req.Warmup == 1 {
updateData["warmup"] = req.Warmup
}
- if req.UseTagFilter == 0 || req.UseTagFilter == 1 {
- updateData["use_tag_filter"] = req.UseTagFilter
- }
if req.Addresser != "" {
updateData["addresser"] = req.Addresser
diff --git a/core/internal/controller/contact/contact_v1_get_single_group_tag_contact_count.go b/core/internal/controller/contact/contact_v1_get_single_group_tag_contact_count.go
new file mode 100644
index 00000000..d0ef33c5
--- /dev/null
+++ b/core/internal/controller/contact/contact_v1_get_single_group_tag_contact_count.go
@@ -0,0 +1,76 @@
+package contact
+
+import (
+ "billionmail-core/api/contact/v1"
+ "billionmail-core/internal/service/contact"
+ "billionmail-core/internal/service/public"
+ "context"
+ "fmt"
+ "github.com/gogf/gf/v2/frame/g"
+ "strconv"
+ "strings"
+
+ "github.com/gogf/gf/v2/errors/gerror"
+)
+
+func (c *ControllerV1) GetSingleGroupTagContactCount(ctx context.Context, req *v1.GetSingleGroupTagContactCountReq) (res *v1.GetSingleGroupTagContactCountRes, err error) {
+ res = &v1.GetSingleGroupTagContactCountRes{}
+
+ if len(req.TagIds) == 0 {
+ GroupIds := []int{req.GroupId}
+ total, err := contact.GetGroupContactCount(ctx, GroupIds)
+ if err != nil {
+ res.Code = 500
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to get contact count: {}", err.Error())))
+ return res, nil
+ }
+ // Set response data
+ res.Data.Total = total
+ res.SetSuccess(public.LangCtx(ctx, "Successfully retrieved contact count"))
+ return res, nil
+ }
+
+ model := g.DB().Model("bm_contacts c").
+ Where("c.active", 1).
+ Where("c.status", 1)
+
+ if req.GroupId > 0 {
+ model = model.Where("c.group_id", req.GroupId)
+ }
+
+ if len(req.TagIds) > 0 {
+ if req.TagLogic == "AND" {
+ // AND logic: contact must have all tags
+ for i, tagId := range req.TagIds {
+ alias := "ct" + strconv.Itoa(i)
+ model = model.InnerJoin(
+ "bm_contact_tags "+alias,
+ fmt.Sprintf("c.id = %s.contact_id AND %s.tag_id = %d", alias, alias, tagId),
+ )
+ }
+ } else {
+ // OR logic: contact must have at least one of the tags
+ var inValues []string
+ for _, tagId := range req.TagIds {
+ inValues = append(inValues, strconv.Itoa(tagId))
+ }
+ subQuery := fmt.Sprintf(
+ "(SELECT DISTINCT contact_id FROM bm_contact_tags WHERE tag_id IN (%s)) ct",
+ strings.Join(inValues, ","),
+ )
+ model = model.InnerJoin(subQuery, "c.id = ct.contact_id")
+ }
+ }
+
+ total, err := model.Fields("DISTINCT c.id").Count()
+
+ if err != nil {
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to get contact count: {}", err.Error())))
+ return
+ }
+
+ // Set response data
+ res.Data.Total = total
+ res.SetSuccess(public.LangCtx(ctx, "Successfully retrieved contact count"))
+ return
+}
diff --git a/core/internal/service/batch_mail/batch_mail.go b/core/internal/service/batch_mail/batch_mail.go
index 7dc4c06b..d2ad9c15 100644
--- a/core/internal/service/batch_mail/batch_mail.go
+++ b/core/internal/service/batch_mail/batch_mail.go
@@ -34,14 +34,13 @@ type CreateTaskArgs struct {
TrackOpen int `json:"track_open"` // Track email opens
TrackClick int `json:"track_click"` // Track email clicks
//Etypes string `json:"etypes"` // Email types (e.g., group IDs)
- Remark string `json:"remark"` // Task remark
- StartTime int `json:"start_time"` // Scheduled start time
- Warmup int `json:"warmup"` // Warmup campaign association
- AddType int `json:"add_type"` // Add type (0: normal)
- GroupId int `json:"group_id"` // Groups to unsubscribe from
- TagIds []int `json:"tag_ids"` // Tag IDs for filtering contacts
- TagLogic string `json:"tag_logic"` // Tag logic (AND/OR)
- UseTagFilter int `json:"use_tag_filter"` // Whether to use tag filter
+ Remark string `json:"remark"` // Task remark
+ StartTime int `json:"start_time"` // Scheduled start time
+ Warmup int `json:"warmup"` // Warmup campaign association
+ AddType int `json:"add_type"` // Add type (0: normal)
+ GroupId int `json:"group_id"` // Groups to unsubscribe from
+ TagIds []int `json:"tag_ids"` // Tag IDs for filtering contacts
+ TagLogic string `json:"tag_logic"` // Tag logic (AND/OR)
}
// ============= task related operations =============
@@ -142,18 +141,17 @@ func CreateTask(ctx context.Context, args CreateTaskArgs) (int, error) {
"unsubscribe": args.Unsubscribe,
"threads": args.Threads,
//"etypes": args.Etypes,
- "track_open": args.TrackOpen,
- "track_click": args.TrackClick,
- "start_time": args.StartTime,
- "create_time": now,
- "update_time": now,
- "active": 1,
- "remark": args.Remark,
- "add_type": args.AddType,
- "group_id": args.GroupId,
- "tag_ids": tagIdsJson,
- "tag_logic": args.TagLogic,
- "use_tag_filter": args.UseTagFilter,
+ "track_open": args.TrackOpen,
+ "track_click": args.TrackClick,
+ "start_time": args.StartTime,
+ "create_time": now,
+ "update_time": now,
+ "active": 1,
+ "remark": args.Remark,
+ "add_type": args.AddType,
+ "group_id": args.GroupId,
+ "tag_ids": tagIdsJson,
+ "tag_logic": args.TagLogic,
})
if err != nil {
g.Log().Debug(ctx, "Failed to create campaign:", err.Error())
@@ -300,10 +298,9 @@ func GetActiveContacts(ctx context.Context, groupId int) ([]*entity.Contact, err
}
type ContactFilter struct {
- GroupId int
- TagIds []int
- TagLogic string // "AND" or "OR"
- UseTagFilter int
+ GroupId int
+ TagIds []int
+ TagLogic string // "AND" or "OR"
}
func GetFilteredContacts(ctx context.Context, filter ContactFilter) ([]*entity.Contact, error) {
@@ -318,7 +315,7 @@ func GetFilteredContacts(ctx context.Context, filter ContactFilter) ([]*entity.C
model = model.Where("c.group_id", filter.GroupId)
}
- if filter.UseTagFilter == 1 && len(filter.TagIds) > 0 {
+ if len(filter.TagIds) > 0 {
if filter.TagLogic == "AND" {
// AND
for i, tagId := range filter.TagIds {
@@ -495,14 +492,13 @@ func CreateTaskWithRecipients(ctx context.Context, req *v1.CreateTaskReq, addTyp
TrackOpen: req.TrackOpen,
TrackClick: req.TrackClick,
//Etypes: etype,
- Remark: req.Remark,
- StartTime: req.StartTime,
- Warmup: req.Warmup,
- AddType: addType,
- GroupId: req.GroupId,
- TagIds: req.TagIds,
- TagLogic: req.TagLogic,
- UseTagFilter: req.UseTagFilter,
+ Remark: req.Remark,
+ StartTime: req.StartTime,
+ Warmup: req.Warmup,
+ AddType: addType,
+ GroupId: req.GroupId,
+ TagIds: req.TagIds,
+ TagLogic: req.TagLogic,
})
if err != nil {
return gerror.New(public.LangCtx(ctx, "Failed to create task {}", err.Error()))
@@ -530,14 +526,13 @@ func CreateTaskWithRecipients(ctx context.Context, req *v1.CreateTaskReq, addTyp
}
filter := ContactFilter{
- GroupId: req.GroupId,
- TagIds: req.TagIds,
- TagLogic: req.TagLogic,
- UseTagFilter: req.UseTagFilter,
+ GroupId: req.GroupId,
+ TagIds: req.TagIds,
+ TagLogic: req.TagLogic,
}
var contacts []*entity.Contact
- if req.UseTagFilter == 1 {
+ if len(req.TagIds) > 0 {
contacts, err = GetFilteredContacts(ctx, filter)
if err != nil {
@@ -555,7 +550,7 @@ func CreateTaskWithRecipients(ctx context.Context, req *v1.CreateTaskReq, addTyp
}
if len(contacts) == 0 {
- if req.UseTagFilter == 1 {
+ if len(req.TagIds) > 0 {
return gerror.New(public.LangCtx(ctx, "No contacts found matching the tag filter criteria"))
} else {
return gerror.New(public.LangCtx(ctx, "No contacts found in group {}", req.GroupId))
diff --git a/core/internal/service/database_initialization/batch_mail.go b/core/internal/service/database_initialization/batch_mail.go
index 3988b704..21c6db89 100644
--- a/core/internal/service/database_initialization/batch_mail.go
+++ b/core/internal/service/database_initialization/batch_mail.go
@@ -92,8 +92,7 @@ func init() {
group_id INTEGER NOT NULL DEFAULT 0,
stats_update_time INTEGER NOT NULL DEFAULT 0,
tag_ids TEXT DEFAULT '', -- JSON array of tag ids for filtering contacts
- tag_logic VARCHAR(10) DEFAULT 'AND', -- Tag logic (AND: must have all tags, OR: have any tag)
- use_tag_filter SMALLINT NOT NULL DEFAULT 0 -- Whether to use tag filter (0: no, 1: yes)
+ tag_logic VARCHAR(10) DEFAULT 'AND' -- Tag logic (AND: must have all tags, OR: have any tag)
)`,
@@ -257,7 +256,6 @@ func init() {
_ = AddColumnIfNotExists("email_tasks", "group_id", "INTEGER", "0", true)
_ = AddColumnIfNotExists("email_tasks", "tag_ids", "TEXT", "''", false)
_ = AddColumnIfNotExists("email_tasks", "tag_logic", "VARCHAR(10)", "'AND'", false)
- _ = AddColumnIfNotExists("email_tasks", "use_tag_filter", "SMALLINT", "0", true)
// unsubscribe_records
_ = DropForeignKeyIfExists("unsubscribe_records", "group_id")