Skip to content

Commit

Permalink
pass a Filter struct to email query to allow for providing before and…
Browse files Browse the repository at this point in the history
… after date fields
  • Loading branch information
cwinters8 committed Jan 12, 2023
1 parent 655b90a commit 889fde9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
14 changes: 9 additions & 5 deletions objects/emails/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package emails

import (
"fmt"
"time"

"github.com/cwinters8/gomap/client"
"github.com/cwinters8/gomap/parse"
Expand All @@ -10,15 +11,18 @@ import (
"github.com/google/uuid"
)

func Query(c *client.Client, text string, inMailboxID string) (emailIDs []string, err error) {
type Filter struct {
InMailboxID string `json:"inMailbox,omitempty"`
Text string `json:"text,omitempty"` // searches From, To, Cc, Bcc, and Subject header fields and any text/* body parts
Before *time.Time `json:"before,omitempty"` // UTC timestamp the email's receivedAt must be before
After *time.Time `json:"after,omitempty"` // UTC timestamp the email's receivedAt must match or be after
}

func Query(c *client.Client, filter *Filter) (emailIDs []string, err error) {
id, err := uuid.NewRandom()
if err != nil {
return nil, fmt.Errorf("failed to generate new uuid: %w", err)
}
filter := map[string]string{"text": text}
if len(inMailboxID) > 0 {
filter["inMailbox"] = inMailboxID
}
call := requests.Call{
ID: id,
AccountID: c.Session.PrimaryAccounts.Mail,
Expand Down
50 changes: 50 additions & 0 deletions objects/emails/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package emails_test

import (
"encoding/json"
"testing"
"time"

"github.com/cwinters8/gomap/objects/emails"
"github.com/cwinters8/gomap/utils"
)

func TestFilterJSON(t *testing.T) {
timestamp := time.Now().UTC()
filter := emails.Filter{
InMailboxID: "xyz",
Text: "hello world",
After: &timestamp,
}
b, err := json.Marshal(filter)
if err != nil {
t.Fatalf("failed to marshal filter to json: %s", err.Error())
}
var m map[string]string
if err := json.Unmarshal(b, &m); err != nil {
t.Fatalf("failed to unmarshal filter to map: %s", err.Error())
}
if _, ok := m["before"]; ok {
t.Error("wanted `before` field to be omitted")
}
after, err := time.Parse("2006-01-02T15:04:05.999999Z", m["after"])
if err != nil {
t.Fatalf("failed to parse `after` filter to time.Time: %s", err.Error())
}
cases := utils.Cases{utils.NewCase(
m["inMailbox"] != filter.InMailboxID,
"wanted mailbox id %s; got %s",
filter.InMailboxID, m["inMailbox"],
), utils.NewCase(
m["text"] != filter.Text,
"wanted text %s; got %s",
filter.Text, m["text"],
), utils.NewCase(
after != *filter.After,
"wanted after timestamp %v; got %v",
*filter.After, after,
)}
cases.Iterator(func(c *utils.Case) {
t.Error(c.Message)
})
}
11 changes: 9 additions & 2 deletions objects/emails/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/cwinters8/gomap/client"
"github.com/cwinters8/gomap/objects/emails"
Expand Down Expand Up @@ -70,10 +71,16 @@ func TestSubmit(t *testing.T) {
if err != nil {
t.Fatalf("failed to retrieve mailbox `%s`: %s", boxName, err.Error())
}
attempts := 30
attempts := 60
gotID := ""
strID := testEmailID.String()
filter := emails.Filter{
InMailboxID: box.ID,
Text: strID,
}
time.Sleep(5 * time.Second)
for attempts > 0 {
emailIDs, err := emails.Query(c, testEmailID.String(), box.ID)
emailIDs, err := emails.Query(c, &filter)
if err != nil {
t.Fatalf("email query failed: %s", err.Error())
}
Expand Down

0 comments on commit 889fde9

Please sign in to comment.