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

Added access to marketing task details

lotk1fdf0329ab2f
diff --git a/core/api/batch_mail/batch_mail.go b/core/api/batch_mail/batch_mail.go
index 63ea9d49..8643196a 100644
--- a/core/api/batch_mail/batch_mail.go
+++ b/core/api/batch_mail/batch_mail.go
@@ -18,6 +18,7 @@ type IBatchMailV1 interface {
ApiTemplatesDelete(ctx context.Context, req *v1.ApiTemplatesDeleteReq) (res *v1.ApiTemplatesDeleteRes, err error)
ApiMailSend(ctx context.Context, req *v1.ApiMailSendReq) (res *v1.ApiMailSendRes, err error)
ListTasks(ctx context.Context, req *v1.ListTasksReq) (res *v1.ListTasksRes, err error)
+ TaskInfo(ctx context.Context, req *v1.TaskInfoReq) (res *v1.TaskInfoRes, err error)
TaskOverview(ctx context.Context, req *v1.TaskOverviewReq) (res *v1.TaskOverviewRes, err error)
DeleteTask(ctx context.Context, req *v1.DeleteTaskReq) (res *v1.DeleteTaskRes, err error)
CreateTask(ctx context.Context, req *v1.CreateTaskReq) (res *v1.CreateTaskRes, err error)
diff --git a/core/api/batch_mail/v1/batch_mail.go b/core/api/batch_mail/v1/batch_mail.go
index f7af1ed6..37d6f7e7 100644
--- a/core/api/batch_mail/v1/batch_mail.go
+++ b/core/api/batch_mail/v1/batch_mail.go
@@ -1,6 +1,7 @@
package v1
import (
+ "billionmail-core/internal/model/entity"
"billionmail-core/utility/types/api_v1"
"github.com/gogf/gf/v2/frame/g"
)
@@ -70,6 +71,17 @@ type ListTasksRes struct {
} `json:"data"`
}
+type TaskInfoReq struct {
+ g.Meta `path:"/batch_mail/task/find" method:"get" tags:"BatchMail" summary:"get task info by id"`
+ Authorization string `json:"authorization" dc:"Authorization" in:"header"`
+ Id int `json:"id" v:"required" dc:"task id"`
+}
+
+type TaskInfoRes struct {
+ api_v1.StandardRes
+ Data *entity.EmailTask `json:"data"`
+}
+
type TaskSummaryStats struct {
Sends int `json:"sends"`
Delivered int `json:"delivered"`
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_api_mail_send.go b/core/internal/controller/batch_mail/batch_mail_v1_api_mail_send.go
index 93358b87..6641aa02 100644
--- a/core/internal/controller/batch_mail/batch_mail_v1_api_mail_send.go
+++ b/core/internal/controller/batch_mail/batch_mail_v1_api_mail_send.go
@@ -29,17 +29,25 @@ func (c *ControllerV1) ApiMailSend(ctx context.Context, req *v1.ApiMailSendReq)
return res, nil
}
- var expireAt int64
- if apiTemplate.ExpireTime > 0 {
- expireAt = int64(apiTemplate.LastKeyUpdateTime) + int64(apiTemplate.ExpireTime)
- if time.Now().Unix() > expireAt {
- // expired
- res.Code = 1002
- res.SetError(gerror.New(public.LangCtx(ctx, "API key has expired")))
- return res, nil
- }
+ // check client IP
+ err = CheckClientIP(ctx, apiTemplate.Id, clientIP)
+ if err != nil {
+ res.Code = 1002
+ res.SetError(gerror.New(public.LangCtx(ctx, err.Error())))
+ return res, nil
}
+ //var expireAt int64
+ //if apiTemplate.ExpireTime > 0 {
+ // expireAt = int64(apiTemplate.LastKeyUpdateTime) + int64(apiTemplate.ExpireTime)
+ // if time.Now().Unix() > expireAt {
+ // // expired
+ // res.Code = 1002
+ // res.SetError(gerror.New(public.LangCtx(ctx, "API key has expired")))
+ // return res, nil
+ // }
+ //}
+
// 2. check email template
emailTemplate, err := getEmailTemplateById(ctx, apiTemplate.TemplateId)
if err != nil {
@@ -91,23 +99,29 @@ func getApiTemplateByKey(ctx context.Context, apiKey string, clientIP string) (*
return nil, gerror.New(public.LangCtx(ctx, "API key is invalid"))
}
+ return &apiTemplate, nil
+}
+
+// check API template by key and client IP
+func CheckClientIP(ctx context.Context, Id int, clientIP string) error {
+
ipcount, err := g.DB().Model("api_ip_whitelist").
- Where("api_id", apiTemplate.Id).Count()
+ Where("api_id", Id).Count()
if err == nil && ipcount > 0 {
count, err := g.DB().Model("api_ip_whitelist").
- Where("api_id", apiTemplate.Id).
+ Where("api_id", Id).
Where("ip", clientIP).
Count()
if err != nil {
- return nil, err
+ return err
}
if count == 0 {
- return nil, gerror.New(public.LangCtx(ctx, "IP not allowed"))
+ return gerror.New(public.LangCtx(ctx, "IP not allowed"))
}
}
- return &apiTemplate, nil
+ return nil
}
// get email template
diff --git a/core/internal/controller/batch_mail/batch_mail_v1_task_info.go b/core/internal/controller/batch_mail/batch_mail_v1_task_info.go
new file mode 100644
index 00000000..2f8b5ecb
--- /dev/null
+++ b/core/internal/controller/batch_mail/batch_mail_v1_task_info.go
@@ -0,0 +1,33 @@
+package batch_mail
+
+import (
+ "billionmail-core/internal/model/entity"
+ "billionmail-core/internal/service/batch_mail"
+ "billionmail-core/internal/service/public"
+ "context"
+
+ "github.com/gogf/gf/v2/errors/gerror"
+
+ "billionmail-core/api/batch_mail/v1"
+)
+
+func (c *ControllerV1) TaskInfo(ctx context.Context, req *v1.TaskInfoReq) (res *v1.TaskInfoRes, err error) {
+ res = &v1.TaskInfoRes{}
+
+ var taskInfo *entity.EmailTask
+ taskInfo, err = batch_mail.GetTaskInfo(ctx, req.Id)
+ if err != nil {
+ res.Code = 500
+ res.SetError(gerror.New(public.LangCtx(ctx, "Failed to get task info: {}", err.Error())))
+ return
+ }
+
+ if taskInfo == nil || taskInfo.Id == 0 {
+ res.Code = 404
+ res.SetError(gerror.New(public.LangCtx(ctx, "Task not found: {}", req.Id)))
+ return
+ }
+ res.Data = taskInfo
+ res.SetSuccess(public.LangCtx(ctx, "Success"))
+ return
+}