Allow filtering emails via headers

This commit is contained in:
wpetit 2022-02-18 10:14:16 +01:00
parent 450a9de82d
commit 6a52595fa7
3 changed files with 53 additions and 0 deletions

View File

@ -19,6 +19,7 @@ type InboxSearch struct {
From string
Body string
Subject string
Headers map[string]string
After time.Time
Before time.Time
}
@ -149,6 +150,38 @@ func HandleGetInbox(ctx context.Context, qry cqrs.Query) (interface{}, error) {
match = false
}
if req.Search.Headers != nil {
found := false
for searchKey, searchValue := range req.Search.Headers {
for headerKey, headerValues := range eml.Headers {
if searchKey != headerKey {
continue
}
for _, hv := range headerValues {
if strings.Contains(hv, searchValue) {
found = true
break
}
}
if found {
break
}
}
if found {
break
}
}
if !found {
match = false
}
}
if match {
filtered = append(filtered, eml)
}

View File

@ -1,6 +1,7 @@
package route
import (
"encoding/json"
"net/http"
"strconv"
"time"
@ -75,6 +76,16 @@ func createInboxQueryFromRequest(r *http.Request) (*query.GetInboxRequest, error
}
}
var headers map[string]string
rawHeaders := r.URL.Query().Get("headers")
if rawHeaders != "" {
headers = make(map[string]string)
if err := json.Unmarshal([]byte(rawHeaders), &headers); err != nil {
return nil, errors.WithStack(err)
}
}
search := &query.InboxSearch{}
if to != "" {
search.To = to
@ -96,6 +107,10 @@ func createInboxQueryFromRequest(r *http.Request) (*query.GetInboxRequest, error
search.Before = before
}
if rawHeaders != "" {
search.Headers = headers
}
inboxRequest := &query.GetInboxRequest{
OrderBy: orderBy,
Reverse: reverse == "y",

5
misc/api.http Normal file
View File

@ -0,0 +1,5 @@
@baseURL = http://localhost:8080
### Filter emails via headers
GET {{ baseURL }}/api/v1/emails?headers={"Mime-Version":"1.0"}