-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor storage and webhook code (#346)
- Loading branch information
Showing
11 changed files
with
176 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/sqs" | ||
) | ||
|
||
// SQSSendMessageAPI defines set of API required by SendEmailReceipt and SendEmailNotification functions | ||
type SQSSendMessageAPI interface { | ||
GetQueueUrl(ctx context.Context, params *sqs.GetQueueUrlInput, optFns ...func(*sqs.Options)) (*sqs.GetQueueUrlOutput, error) | ||
SendMessage(ctx context.Context, params *sqs.SendMessageInput, optFns ...func(*sqs.Options)) (*sqs.SendMessageOutput, error) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package hook | ||
|
||
const ( | ||
EventEmail = "email" | ||
ActionReceived = "received" | ||
) | ||
|
||
// EmailReceipt contains information needed for an email receipt | ||
type EmailReceipt struct { | ||
MessageID string | ||
Timestamp string | ||
} | ||
|
||
// EmailNotification contains information needed for an email state change notification | ||
type EmailNotification struct { | ||
Event string `json:"event"` | ||
MessageID string `json:"messageID"` | ||
Timestamp string `json:"timestamp"` | ||
} | ||
|
||
type Webhook struct { | ||
Event string `json:"event"` | ||
Action string `json:"action"` | ||
Email Email | ||
} | ||
|
||
type Email struct { | ||
ID string `json:"id"` // message id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package hook | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/sqs" | ||
"github.com/aws/aws-sdk-go-v2/service/sqs/types" | ||
"github.com/harryzcy/mailbox/internal/api" | ||
"github.com/harryzcy/mailbox/internal/env" | ||
) | ||
|
||
// sqsEnabled returns true if SQS is enabled | ||
func sqsEnabled() bool { | ||
return env.QueueName != "" | ||
} | ||
|
||
// SendEmailHandle sends an email receipt to SQS, if SQS is enabled. | ||
// Otherwise, it does nothing. | ||
func SendSQS(ctx context.Context, api api.SQSSendMessageAPI, input EmailReceipt) error { | ||
if !sqsEnabled() { | ||
return nil | ||
} | ||
|
||
fmt.Printf("Sending email receipt (MessageID: %s)\n", input.MessageID) | ||
return sendSQSEmailNotification(ctx, api, EmailNotification{ | ||
Event: "receive", | ||
MessageID: input.MessageID, | ||
Timestamp: input.Timestamp, | ||
}) | ||
} | ||
|
||
// sendSQSEmailNotification notifies about a change of state of an email, categorized by event. | ||
func sendSQSEmailNotification(ctx context.Context, api api.SQSSendMessageAPI, input EmailNotification) error { | ||
result, err := api.GetQueueUrl(ctx, &sqs.GetQueueUrlInput{ | ||
QueueName: &env.QueueName, | ||
}) | ||
if err != nil { | ||
fmt.Println("Failed to get queue url") | ||
return err | ||
} | ||
|
||
body, _ := json.Marshal(input) | ||
|
||
resp, err := api.SendMessage(ctx, &sqs.SendMessageInput{ | ||
MessageAttributes: map[string]types.MessageAttributeValue{ | ||
"Event": { | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(input.Event), | ||
}, | ||
"Timestamp": { | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(input.Timestamp), | ||
}, | ||
}, | ||
MessageBody: aws.String(string(body)), | ||
QueueUrl: result.QueueUrl, | ||
}) | ||
if err != nil { | ||
fmt.Println("Failed to send message to SQS") | ||
return err | ||
} | ||
|
||
fmt.Println("Sent message with ID: " + *resp.MessageId) | ||
return nil | ||
} |
Oops, something went wrong.