2020-04-17 17:53:01 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-03 11:49:31 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2020-04-17 17:53:01 +02:00
|
|
|
|
|
|
|
"forge.cadoles.com/wpetit/fake-smtp/internal/model"
|
|
|
|
"forge.cadoles.com/wpetit/fake-smtp/internal/storm"
|
2020-11-03 11:49:31 +01:00
|
|
|
stormdb "github.com/asdine/storm/v3"
|
|
|
|
"github.com/asdine/storm/v3/q"
|
2020-04-17 17:53:01 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/cqrs"
|
|
|
|
"gitlab.com/wpetit/goweb/middleware/container"
|
|
|
|
)
|
|
|
|
|
2020-11-03 11:49:31 +01:00
|
|
|
type InboxSearch struct {
|
|
|
|
To string
|
|
|
|
From string
|
|
|
|
Body string
|
|
|
|
Subject string
|
|
|
|
After time.Time
|
|
|
|
Before time.Time
|
|
|
|
}
|
|
|
|
|
2020-04-17 17:53:01 +02:00
|
|
|
type GetInboxRequest struct {
|
|
|
|
OrderBy string
|
|
|
|
Limit int
|
|
|
|
Skip int
|
|
|
|
Reverse bool
|
2020-11-03 11:49:31 +01:00
|
|
|
Search *InboxSearch
|
2020-04-17 17:53:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type InboxData struct {
|
|
|
|
Emails []*model.Email
|
|
|
|
}
|
|
|
|
|
|
|
|
func HandleGetInbox(ctx context.Context, qry cqrs.Query) (interface{}, error) {
|
|
|
|
req, ok := qry.Request().(*GetInboxRequest)
|
|
|
|
if !ok {
|
|
|
|
return nil, cqrs.ErrUnexpectedRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
ctn, err := container.From(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not retrieve service container")
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := storm.From(ctn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not retrieve storm service")
|
|
|
|
}
|
|
|
|
|
|
|
|
emails := make([]*model.Email, 0)
|
|
|
|
|
2020-11-03 11:49:31 +01:00
|
|
|
var query stormdb.Query
|
|
|
|
|
|
|
|
if req.Search != nil {
|
|
|
|
matchers := make([]q.Matcher, 0)
|
|
|
|
|
|
|
|
if req.Search.Body != "" {
|
|
|
|
matchers = append(matchers, q.Or(
|
|
|
|
q.Re("HTML", req.Search.Body),
|
|
|
|
q.Re("Text", req.Search.Body),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Search.Subject != "" {
|
|
|
|
matchers = append(matchers, q.Re("Subject", req.Search.Subject))
|
|
|
|
}
|
|
|
|
|
|
|
|
query = db.Select(matchers...)
|
|
|
|
} else {
|
|
|
|
query = db.Select()
|
|
|
|
}
|
2020-04-17 17:53:01 +02:00
|
|
|
|
|
|
|
if req.OrderBy != "" {
|
|
|
|
query = query.OrderBy(req.OrderBy)
|
|
|
|
} else {
|
|
|
|
query = query.OrderBy("SentAt").Reverse()
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Reverse {
|
|
|
|
query = query.Reverse()
|
|
|
|
}
|
|
|
|
|
2020-11-03 11:49:31 +01:00
|
|
|
if req.Limit != 0 {
|
|
|
|
query = query.Limit(req.Limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Skip != 0 {
|
|
|
|
query = query.Limit(req.Skip)
|
|
|
|
}
|
|
|
|
|
2020-04-17 17:53:01 +02:00
|
|
|
if err := query.Find(&emails); err != nil {
|
|
|
|
if err == storm.ErrNotFound {
|
|
|
|
return &InboxData{emails}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.Wrap(err, "could not retrieve emails")
|
|
|
|
}
|
|
|
|
|
2020-11-03 11:49:31 +01:00
|
|
|
if req.Search == nil {
|
|
|
|
return &InboxData{emails}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filtered := make([]*model.Email, 0, len(emails))
|
|
|
|
|
|
|
|
for _, eml := range emails {
|
|
|
|
match := true
|
|
|
|
|
|
|
|
if req.Search.To != "" {
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, addr := range eml.To {
|
|
|
|
if strings.Contains(addr.Name, req.Search.To) || strings.Contains(addr.Address, req.Search.To) {
|
|
|
|
found = true
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
match = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Search.From != "" {
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, addr := range eml.From {
|
|
|
|
if strings.Contains(addr.Name, req.Search.From) || strings.Contains(addr.Address, req.Search.From) {
|
|
|
|
found = true
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
match = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !req.Search.After.IsZero() && !eml.SentAt.After(req.Search.After) {
|
|
|
|
match = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !req.Search.Before.IsZero() && !eml.SentAt.Before(req.Search.Before) {
|
|
|
|
match = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if match {
|
|
|
|
filtered = append(filtered, eml)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &InboxData{filtered}, nil
|
2020-04-17 17:53:01 +02:00
|
|
|
}
|