From 6a52595fa7193962b56dc50daf431741c51e1273 Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 18 Feb 2022 10:14:16 +0100 Subject: [PATCH 1/2] Allow filtering emails via headers --- internal/query/get_inbox.go | 33 +++++++++++++++++++++++++++++++++ internal/route/helper.go | 15 +++++++++++++++ misc/api.http | 5 +++++ 3 files changed, 53 insertions(+) create mode 100644 misc/api.http diff --git a/internal/query/get_inbox.go b/internal/query/get_inbox.go index cdd94ff..9bb508c 100644 --- a/internal/query/get_inbox.go +++ b/internal/query/get_inbox.go @@ -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) } diff --git a/internal/route/helper.go b/internal/route/helper.go index 950a11b..0308995 100644 --- a/internal/route/helper.go +++ b/internal/route/helper.go @@ -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", diff --git a/misc/api.http b/misc/api.http new file mode 100644 index 0000000..ca63531 --- /dev/null +++ b/misc/api.http @@ -0,0 +1,5 @@ +@baseURL = http://localhost:8080 + +### Filter emails via headers + +GET {{ baseURL }}/api/v1/emails?headers={"Mime-Version":"1.0"} \ No newline at end of file From f959fdb93f37e4d8a3bc49b61aec47bf4c53f090 Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 18 Feb 2022 10:24:46 +0100 Subject: [PATCH 2/2] Update Dockerfile --- misc/docker/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/misc/docker/Dockerfile b/misc/docker/Dockerfile index 404da39..f342298 100644 --- a/misc/docker/Dockerfile +++ b/misc/docker/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.15 AS build +FROM golang:1.17 AS build ARG HTTP_PROXY= ARG HTTPS_PROXY= @@ -7,7 +7,7 @@ ARG https_proxy= RUN apt-get update && apt-get install -y build-essential git bash curl -RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - \ +RUN curl -sL https://deb.nodesource.com/setup_14.x | bash - \ && apt-get install -y nodejs COPY . /src