2023-07-30 17:59:25 +08:00
|
|
|
package email
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-07-20 10:39:17 +08:00
|
|
|
"github.com/Jinnrry/pmail/dto"
|
|
|
|
"github.com/Jinnrry/pmail/dto/response"
|
|
|
|
"github.com/Jinnrry/pmail/services/list"
|
|
|
|
"github.com/Jinnrry/pmail/utils/context"
|
2023-07-30 17:59:25 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type emailListResponse struct {
|
|
|
|
CurrentPage int `json:"current_page"`
|
|
|
|
TotalPage int `json:"total_page"`
|
|
|
|
List []*emilItem `json:"list"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type emilItem struct {
|
2023-09-08 02:11:00 +08:00
|
|
|
ID int `json:"id"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Desc string `json:"desc"`
|
|
|
|
Datetime string `json:"datetime"`
|
|
|
|
IsRead bool `json:"is_read"`
|
|
|
|
Sender User `json:"sender"`
|
2024-07-14 09:42:43 +08:00
|
|
|
To []User `json:"to"`
|
2023-09-08 02:11:00 +08:00
|
|
|
Dangerous bool `json:"dangerous"`
|
2024-07-06 12:54:58 +08:00
|
|
|
Error string `json:"error"`
|
2023-07-30 17:59:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Name string `json:"Name"`
|
|
|
|
EmailAddress string `json:"EmailAddress"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type emailRequest struct {
|
|
|
|
Keyword string `json:"keyword"`
|
|
|
|
Tag string `json:"tag"`
|
|
|
|
CurrentPage int `json:"current_page"`
|
|
|
|
PageSize int `json:"page_size"`
|
|
|
|
}
|
|
|
|
|
2023-09-08 02:11:00 +08:00
|
|
|
func EmailList(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
2023-07-30 17:59:25 +08:00
|
|
|
var lst []*emilItem
|
|
|
|
reqBytes, err := io.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.WithContext(ctx).Errorf("%+v", err)
|
|
|
|
}
|
|
|
|
var retData emailRequest
|
|
|
|
err = json.Unmarshal(reqBytes, &retData)
|
|
|
|
if err != nil {
|
|
|
|
log.WithContext(ctx).Errorf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
offset := 0
|
|
|
|
if retData.CurrentPage >= 1 {
|
|
|
|
offset = (retData.CurrentPage - 1) * retData.PageSize
|
|
|
|
}
|
|
|
|
|
|
|
|
if retData.PageSize == 0 {
|
|
|
|
retData.PageSize = 15
|
|
|
|
}
|
|
|
|
|
2024-07-02 21:02:00 +08:00
|
|
|
var tagInfo dto.SearchTag = dto.SearchTag{
|
|
|
|
Type: -1,
|
|
|
|
Status: -1,
|
|
|
|
GroupId: -1,
|
|
|
|
}
|
|
|
|
_ = json.Unmarshal([]byte(retData.Tag), &tagInfo)
|
|
|
|
|
|
|
|
emailList, total := list.GetEmailList(ctx, tagInfo, retData.Keyword, false, offset, retData.PageSize)
|
2023-07-30 17:59:25 +08:00
|
|
|
|
|
|
|
for _, email := range emailList {
|
|
|
|
var sender User
|
|
|
|
_ = json.Unmarshal([]byte(email.Sender), &sender)
|
|
|
|
|
2024-07-14 09:42:43 +08:00
|
|
|
var tos []User
|
|
|
|
_ = json.Unmarshal([]byte(email.To), &tos)
|
|
|
|
|
2023-07-30 17:59:25 +08:00
|
|
|
lst = append(lst, &emilItem{
|
2023-09-08 02:11:00 +08:00
|
|
|
ID: email.Id,
|
|
|
|
Title: email.Subject,
|
|
|
|
Desc: email.Text.String,
|
|
|
|
Datetime: email.SendDate.Format("2006-01-02 15:04:05"),
|
|
|
|
IsRead: email.IsRead == 1,
|
|
|
|
Sender: sender,
|
2024-07-14 09:42:43 +08:00
|
|
|
To: tos,
|
2023-09-08 02:11:00 +08:00
|
|
|
Dangerous: email.SPFCheck == 0 && email.DKIMCheck == 0,
|
2024-07-06 12:54:58 +08:00
|
|
|
Error: email.Error.String,
|
2023-07-30 17:59:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := emailListResponse{
|
|
|
|
CurrentPage: retData.CurrentPage,
|
|
|
|
TotalPage: cast.ToInt(math.Ceil(cast.ToFloat64(total) / cast.ToFloat64(retData.PageSize))),
|
|
|
|
List: lst,
|
|
|
|
}
|
|
|
|
response.NewSuccessResponse(ret).FPrint(w)
|
|
|
|
}
|