From 25243d01f95ad5f80eb429cc1dcc2f65bde96ffc Mon Sep 17 00:00:00 2001 From: harryzcy Date: Thu, 25 May 2023 20:20:15 +0800 Subject: [PATCH] Mark email as read when viewed (#268) --- internal/email/common.go | 5 +++++ internal/email/create.go | 2 +- internal/email/get.go | 23 +++++++++++++++++++++-- internal/email/get_test.go | 4 ++-- internal/email/send.go | 2 +- internal/email/thread.go | 4 ++-- 6 files changed, 32 insertions(+), 8 deletions(-) diff --git a/internal/email/common.go b/internal/email/common.go index 1e201ada..bccbb983 100644 --- a/internal/email/common.go +++ b/internal/email/common.go @@ -18,6 +18,11 @@ type GetItemAPI interface { GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error) } +type GetEmailAPI interface { + GetItemAPI + UpdateItemAPI +} + // GetItemContentAPI defines set of API required to get attachments or inlines of an email type GetItemContentAPI interface { GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error) diff --git a/internal/email/create.go b/internal/email/create.go index fde92816..b8468e85 100644 --- a/internal/email/create.go +++ b/internal/email/create.go @@ -278,7 +278,7 @@ type ThreadInfo struct { func getThreadInfo(ctx context.Context, api CreateAndSendEmailAPI, replyEmailID string) (*ThreadInfo, error) { fmt.Println("getting email to reply to") - email, err := Get(ctx, api, replyEmailID) + email, err := get(ctx, api, replyEmailID) if err != nil { return nil, err } diff --git a/internal/email/get.go b/internal/email/get.go index c6a25fb1..b87c7779 100644 --- a/internal/email/get.go +++ b/internal/email/get.go @@ -58,8 +58,27 @@ type EmailVerdict struct { Virus bool `json:"virus"` } -// Get returns the email -func Get(ctx context.Context, api GetItemAPI, messageID string) (*GetResult, error) { +// Get returns the email and marks it as read +func Get(ctx context.Context, api GetEmailAPI, messageID string) (*GetResult, error) { + result, err := get(ctx, api, messageID) + if err != nil { + return nil, err + } + + // mark email as read + if result.Type == EmailTypeInbox && result.Unread != nil && *result.Unread { + err = Read(ctx, api, messageID, ActionRead) + if err != nil { + return nil, err + } + fmt.Println("email marked as read") + } + + return result, nil +} + +// get returns the email +func get(ctx context.Context, api GetItemAPI, messageID string) (*GetResult, error) { resp, err := api.GetItem(ctx, &dynamodb.GetItemInput{ TableName: aws.String(tableName), Key: map[string]dynamodbTypes.AttributeValue{ diff --git a/internal/email/get_test.go b/internal/email/get_test.go index 79903167..9a13f990 100644 --- a/internal/email/get_test.go +++ b/internal/email/get_test.go @@ -17,7 +17,7 @@ func (m mockGetItemAPI) GetItem(ctx context.Context, params *dynamodb.GetItemInp return m(ctx, params, optFns...) } -func TestGet(t *testing.T) { +func Test_get(t *testing.T) { tableName = "table-for-get" tests := []struct { client func(t *testing.T) GetItemAPI @@ -143,7 +143,7 @@ func TestGet(t *testing.T) { for i, test := range tests { t.Run(strconv.Itoa(i), func(t *testing.T) { ctx := context.TODO() - result, err := Get(ctx, test.client(t), test.messageID) + result, err := get(ctx, test.client(t), test.messageID) assert.Equal(t, test.expected, result) assert.Equal(t, test.expectedErr, err) }) diff --git a/internal/email/send.go b/internal/email/send.go index 5a003a42..5ef853af 100644 --- a/internal/email/send.go +++ b/internal/email/send.go @@ -27,7 +27,7 @@ func Send(ctx context.Context, api GetAndSendEmailAPI, messageID string) (*SendR return nil, ErrEmailIsNotDraft } - resp, err := Get(ctx, api, messageID) + resp, err := get(ctx, api, messageID) if err != nil { return nil, err } diff --git a/internal/email/thread.go b/internal/email/thread.go index 1d69a518..6bef969a 100644 --- a/internal/email/thread.go +++ b/internal/email/thread.go @@ -174,7 +174,7 @@ func DetermineThread(ctx context.Context, api QueryAndGetItemAPI, input *Determi if possibleSentID != "" { // Check if the messageID is a sent email first fmt.Println("checking possible sent email") - previousEmail, err = Get(ctx, api, possibleSentID) + previousEmail, err = get(ctx, api, possibleSentID) if err != nil && !errors.Is(err, ErrNotFound) { return nil, err } @@ -205,7 +205,7 @@ func DetermineThread(ctx context.Context, api QueryAndGetItemAPI, input *Determi } searchMessageID := resp.Items[0]["MessageID"].(*dynamodbTypes.AttributeValueMemberS).Value - previousEmail, err = Get(ctx, api, searchMessageID) + previousEmail, err = get(ctx, api, searchMessageID) if err != nil { if errors.Is(err, ErrNotFound) { return &DetermineThreadOutput{}, nil