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
Enhance batch mail functionality with tag and group management, including new TagInfo struct and batch loading methods
lotk70ea219b5391
diff --git a/core/api/batch_mail/v1/batch_mail.go b/core/api/batch_mail/v1/batch_mail.goindex 53dde4be..36e533d7 100644--- a/core/api/batch_mail/v1/batch_mail.go+++ b/core/api/batch_mail/v1/batch_mail.go@@ -6,6 +6,12 @@ import ("github.com/gogf/gf/v2/frame/g")+type TagInfo struct {+ Id int `json:"id" dc:"Tag ID"`+ Name string `json:"name" dc:"Tag Name"`+ CreateTime int `json:"create_time" dc:"Create Time"`+}+type EmailTask struct {Id int `json:"id" dc:"task id"`TaskName string `json:"task_name" dc:"task name"`@@ -20,24 +26,26 @@ type EmailTask struct {Unsubscribe int `json:"unsubscribe" dc:"unsubscribe"`Threads int `json:"threads" dc:"threads"`//Etypes string `json:"etypes" dc:"etypes"`- TrackOpen int `json:"track_open" dc:"track open"`- TrackClick int `json:"track_click" dc:"track click"`- StartTime int `json:"start_time" dc:"start time"`- CreateTime int `json:"create_time" dc:"create time"`- UpdateTime int `json:"update_time" dc:"update time"`- Remark string `json:"remark" dc:"remark"`- Active int `json:"active" dc:"status"`- AddType int `json:"add_type" dc:"add type"`- EstimatedTimeWithWarmup int64 `json:"estimated_time_with_warmup" dc:"estimated time for warmup (if applicable)"`- SendsCount int `json:"sendsCount" description:""`- DeliveredCount int `json:"deliveredCount" description:""`- BouncedCount int `json:"bouncedCount" description:""`- DeferredCount int `json:"deferredCount" description:""`- StatsUpdateTime int `json:"statsUpdateTime" description:""`- GroupId int `json:"group_id" dc:"Group ID"`- TagIds []int `json:"tag_ids" dc:"Tag IDs"`- UseTagFilter int `json:"use_tag_filter" dc:"Whether to use tag filter (1: yes, 0: no)"`- TagLogic string `json:"tag_logic" dc:"Tag logic (AND: must have all tags, OR: have any tag)"`+ TrackOpen int `json:"track_open" dc:"track open"`+ TrackClick int `json:"track_click" dc:"track click"`+ StartTime int `json:"start_time" dc:"start time"`+ CreateTime int `json:"create_time" dc:"create time"`+ UpdateTime int `json:"update_time" dc:"update time"`+ Remark string `json:"remark" dc:"remark"`+ Active int `json:"active" dc:"status"`+ AddType int `json:"add_type" dc:"add type"`+ EstimatedTimeWithWarmup int64 `json:"estimated_time_with_warmup" dc:"estimated time for warmup (if applicable)"`+ SendsCount int `json:"sendsCount" description:""`+ DeliveredCount int `json:"deliveredCount" description:""`+ BouncedCount int `json:"bouncedCount" description:""`+ DeferredCount int `json:"deferredCount" description:""`+ StatsUpdateTime int `json:"statsUpdateTime" description:""`+ GroupId int `json:"group_id" dc:"Group ID"`+ GroupName string `json:"group_name" dc:"Group Name"`+ Tags []TagInfo `json:"tags" dc:"Task Tags"`+ UseTagFilter int `json:"use_tag_filter" dc:"Whether to use tag filter (1: yes, 0: no)"`+ TagLogic string `json:"tag_logic" dc:"Tag logic (AND: must have all tags, OR: have any tag)"`+ TagIdsRaw string `json:"-" dc:"Tag IDs raw data for internal processing"`}type GroupInfo struct {@@ -144,9 +152,8 @@ type CreateTaskReq struct {Warmup int `json:"warmup" v:"in:0,1" dc:"warmup" default:"0"`Remark string `json:"remark" dc:"remark"`- 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)" default:"AND"`-+ 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)" default:"AND"`}type CreateTaskRes struct {diff --git a/core/internal/controller/batch_mail/batch_mail_v1_list_tasks.go b/core/internal/controller/batch_mail/batch_mail_v1_list_tasks.goindex 01afc448..2917ccd0 100644--- a/core/internal/controller/batch_mail/batch_mail_v1_list_tasks.go+++ b/core/internal/controller/batch_mail/batch_mail_v1_list_tasks.go@@ -21,11 +21,88 @@ func (c *ControllerV1) ListTasks(ctx context.Context, req *v1.ListTasksReq) (res}task_list := make([]*v1.TaskDetail, len(tasks))++ // Collect all tag IDs and group IDs from all tasks for batch loading+ allTagIds := make(map[int]bool)+ allGroupIds := make(map[int]bool)++ for _, task := range tasks {+ // Collect group IDs+ if task.GroupId > 0 {+ allGroupIds[task.GroupId] = true+ }++ // Collect tag IDs from TagIdsRaw field+ if task.TagIdsRaw != "" {+ tagIds := batch_mail.GetTaskTagIds(task.TagIdsRaw)+ for _, tagId := range tagIds {+ allTagIds[tagId] = true+ }+ }+ }++ // Convert map keys to slices for batch loading+ var tagIdsToFetch []int+ for tagId := range allTagIds {+ tagIdsToFetch = append(tagIdsToFetch, tagId)+ }++ var groupIdsToFetch []int+ for groupId := range allGroupIds {+ groupIdsToFetch = append(groupIdsToFetch, groupId)+ }++ // Batch load all tags+ var allTags []v1.TagInfo+ if len(tagIdsToFetch) > 0 {+ allTags, err = batch_mail.GetTagsByIds(ctx, tagIdsToFetch)+ if err != nil {+ g.Log().Warningf(ctx, "Failed to load tags: %v", err)+ allTags = []v1.TagInfo{}+ }+ }++ // Batch load all group names+ var groupNameMap map[int]string+ if len(groupIdsToFetch) > 0 {+ groupNameMap, err = batch_mail.GetGroupsByIds(ctx, groupIdsToFetch)+ if err != nil {+ g.Log().Warningf(ctx, "Failed to load group names: %v", err)+ groupNameMap = make(map[int]string)+ }+ } else {+ groupNameMap = make(map[int]string)+ }++ // Create tag lookup map+ tagMap := make(map[int]v1.TagInfo)+ for _, tag := range allTags {+ tagMap[tag.Id] = tag+ }+for i, task := range tasks {detail := &v1.TaskDetail{EmailTask: *task,}+ // Set group name+ if groupName, exists := groupNameMap[task.GroupId]; exists {+ detail.GroupName = groupName+ }++ // Load and populate tags for this task+ if task.TagIdsRaw != "" {+ tagIds := batch_mail.GetTaskTagIds(task.TagIdsRaw)+ detail.Tags = make([]v1.TagInfo, 0, len(tagIds))+ for _, tagId := range tagIds {+ if tag, exists := tagMap[tagId]; exists {+ detail.Tags = append(detail.Tags, tag)+ }+ }+ } else {+ detail.Tags = []v1.TagInfo{}+ }+detail.SentCount = task.SendsCountdetail.SuccessCount = task.DeliveredCountdetail.Deferred = task.DeferredCountdiff --git a/core/internal/service/batch_mail/batch_mail.go b/core/internal/service/batch_mail/batch_mail.goindex d2ad9c15..8b0464d2 100644--- a/core/internal/service/batch_mail/batch_mail.go+++ b/core/internal/service/batch_mail/batch_mail.go@@ -72,9 +72,10 @@ func GetTasksWithPage(ctx context.Context, page, pageSize int, keyword string, sreturn 0, nil, err}- // pagination query+ // pagination query - include tag_ids field and map it to TagIdsRawlist = make([]*v1.EmailTask, 0)err = model.Page(page, pageSize).+ Fields("*, tag_ids as TagIdsRaw").Order("create_time DESC").Scan(&list)@@ -794,3 +795,66 @@ func getSingleTaskStats(ctx context.Context, taskId int64) map[string]interface{return stats}++// GetTagsByIds get tags by IDs+func GetTagsByIds(ctx context.Context, tagIds []int) ([]v1.TagInfo, error) {+ if len(tagIds) == 0 {+ return []v1.TagInfo{}, nil+ }++ var tags []v1.TagInfo+ err := g.DB().Model("bm_tags").+ WhereIn("id", tagIds).+ Fields("id, name, create_time").+ Scan(&tags)++ if err != nil {+ return nil, err+ }++ return tags, nil+}++// GetTaskTagIds get tag IDs from task tag_ids field (JSON array)+func GetTaskTagIds(tagIdsStr string) []int {+ if tagIdsStr == "" {+ return []int{}+ }++ var tagIds []int+ if err := gconv.Scan(tagIdsStr, &tagIds); err != nil {+ g.Log().Warningf(context.Background(), "Failed to parse tag_ids: %s, error: %v", tagIdsStr, err)+ return []int{}+ }++ return tagIds+}++// GetGroupsByIds get groups by IDs+func GetGroupsByIds(ctx context.Context, groupIds []int) (map[int]string, error) {+ if len(groupIds) == 0 {+ return make(map[int]string), nil+ }++ var groups []struct {+ Id int `json:"id"`+ Name string `json:"name"`+ }++ err := g.DB().Model("bm_contact_groups").+ WhereIn("id", groupIds).+ Fields("id, name").+ Scan(&groups)++ if err != nil {+ return nil, err+ }++ // Convert to map for quick lookup+ groupMap := make(map[int]string)+ for _, group := range groups {+ groupMap[group.Id] = group.Name+ }++ return groupMap, nil+}