Skip to content

Commit

Permalink
Mark email as read when viewed (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
harryzcy authored May 25, 2023
1 parent 0acfbb9 commit 25243d0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
5 changes: 5 additions & 0 deletions internal/email/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/email/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 21 additions & 2 deletions internal/email/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions internal/email/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion internal/email/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions internal/email/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 25243d0

Please sign in to comment.