email

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EmailTypeInbox represents an inbox email
	EmailTypeInbox = "inbox"
	// EmailTypeInbox represents a sent email
	EmailTypeSent = "sent"
	// EmailTypeInbox represents a draft email
	EmailTypeDraft = "draft"

	// TODO: refactor
	// EmailTypeThread represents a thread, which is a group of emails
	EmailTypeThread = "thread"
)

The constants representing email types

View Source
const (
	ActionRead   = "read"
	ActionUnread = "unread"
)
View Source
const (
	DEFAULT_PAGE_SIZE = 100
)

Variables

View Source
var (
	ErrTooManyRequests = errors.New("too many requests")

	ErrNotFound      = errors.New("email not found")
	ErrInvalidInput  = errors.New("invalid input")
	ErrQueryNotMatch = errors.New("query does not match with next cursor")

	// ErrNotTrashed is returned when trying to delete or untrash an untrashed email
	ErrNotTrashed = errors.New("email is not trashed")
	// ErrAlreadyTrashed is returned when trying to trash an already trashed email
	ErrAlreadyTrashed = errors.New("email is already trashed")
	// ErrReadActionFailed is returned when a read action or unread action fails
	ErrReadActionFailed = errors.New("read action failed")

	// ErrEmailIsNotDraft is returned when expected draft type is not met
	ErrEmailIsNotDraft = errors.New("email type is not draft")
)

Errors

View Source
var (
	ErrInvalidInputToUnmarshal = errors.New("invalid input to unmarshal")
	ErrInvalidInputToDecode    = errors.New("invalid input to decode")
)

Functions

func Delete

func Delete(ctx context.Context, api DeleteItemAPI, messageID string) error

Delete deletes an trashed email from DynamoDB and S3. This action won't be successful if it's not trashed.

func GetContent added in v1.4.0

func GetContent(ctx context.Context, api GetItemContentAPI, messageID, disposition, contentID string) (*storage.GetEmailContentResult, error)

Get returns the email

func Read added in v1.4.0

func Read(ctx context.Context, api UpdateItemAPI, messageID, action string) error

Read marks an email as read or unread

func StoreEmail added in v1.5.0

func StoreEmail(ctx context.Context, api StoreEmailAPI, input *StoreEmailInput)

StoreEmail attempts to store the email. If error occurs, it will be logged and the function will return.

func StoreEmailWithExistingThread added in v1.5.0

func StoreEmailWithExistingThread(ctx context.Context, api TransactWriteItemsAPI, input *StoreEmailWithExistingThreadInput) error

StoreEmailWithExistingThread stores the email and updates the thread.

func StoreEmailWithNewThread added in v1.5.0

func StoreEmailWithNewThread(ctx context.Context, api TransactWriteItemsAPI, input *StoreEmailWithNewThreadInput) error

StoreEmailWithNewThread stores the email, creates a new thread, and add ThreadID to previous email

func Trash

func Trash(ctx context.Context, api UpdateItemAPI, messageID string) error

Trash marks an email as trashed

func Untrash

func Untrash(ctx context.Context, api UpdateItemAPI, messageID string) error

Untrash marks an trashed email as not trashed

Types

type CreateInput

type CreateInput struct {
	EmailInput
	GenerateText string `json:"generateText"` // on, off, or auto (default)
	Send         bool   `json:"send"`         // send email immediately
}

CreateInput represents the input of create method

type CreateResult

type CreateResult struct {
	TimeIndex
	Subject string   `json:"subject"`
	From    []string `json:"from"`
	To      []string `json:"to"`
	Cc      []string `json:"cc"`
	Bcc     []string `json:"bcc"`
	ReplyTo []string `json:"replyTo"`
	Text    string   `json:"text"`
	HTML    string   `json:"html"`
}

CreateResult represents the result of create method

func Create

Create adds an email as draft in DynamoDB

type Cursor added in v1.1.0

type Cursor struct {
	QueryInfo        QueryInfo        `json:"queryInfo"`
	LastEvaluatedKey LastEvaluatedKey `json:"lastEvaluatedKey"`
}

func (*Cursor) Bind added in v1.1.0

func (c *Cursor) Bind(data []byte) error

func (*Cursor) BindString added in v1.1.0

func (c *Cursor) BindString(data string) error

func (Cursor) MarshalJSON added in v1.1.0

func (c Cursor) MarshalJSON() ([]byte, error)

func (*Cursor) UnmarshalJSON added in v1.1.0

func (c *Cursor) UnmarshalJSON(data []byte) error

type DeleteItemAPI

type DeleteItemAPI interface {
	DeleteItem(ctx context.Context, params *dynamodb.DeleteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.DeleteItemOutput, error)
	DeleteObject(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error)
}

DeleteItemAPI defines set of API required to delete an email

type DetermineThreadInput added in v1.5.0

type DetermineThreadInput struct {
	InReplyTo  string
	References string
}

type DetermineThreadOutput added in v1.5.0

type DetermineThreadOutput struct {
	ThreadID          string
	Exists            bool   // If true, the email belongs to an existing thread
	PreviousMessageID string // If Exists is true, the messageID of the last email in the thread

	ShouldCreate    bool   // If true, a new thread should be created
	CreatingEmailID string // If ShouldCreate is true, the messageID of the first email in the thread
	CreatingSubject string // If ShouldCreate is true, the subject of the first email in the thread
	CreatingTime    string // If ShouldCreate is true, the time the first email is received
}

func DetermineThread added in v1.5.0

DetermineThread determines which thread an incoming email belongs to. If a thread already exists, the ThreadID is returned and Exists is true. If a thread does not exist and a new thread should be created, the ThreadID is randomly generated and ShouldCreate is true.

type EmailInput

type EmailInput struct {
	MessageID string   `json:"messageID"`
	Subject   string   `json:"subject"`
	From      []string `json:"from"`
	To        []string `json:"to"`
	Cc        []string `json:"cc"`
	Bcc       []string `json:"bcc"`
	ReplyTo   []string `json:"replyTo"`
	Text      string   `json:"text"`
	HTML      string   `json:"html"`
}

func (EmailInput) GenerateAttributes

func (e EmailInput) GenerateAttributes(typeYearMonth, dateTime string) map[string]types.AttributeValue

GenerateAttributes generates DynamoDB AttributeValues

type EmailItem added in v1.3.0

type EmailItem struct {
	TimeIndex
	Subject        string   `json:"subject"`
	From           []string `json:"from"`
	To             []string `json:"to"`
	Unread         *bool    `json:"unread,omitempty"`
	ThreadID       string   `json:"threadID,omitempty"`
	IsThreadLatest bool     `json:"isThreadLatest,omitempty"`
}

type EmailVerdict added in v1.3.0

type EmailVerdict struct {
	Spam  bool `json:"spam"`
	DKIM  bool `json:"dkim"`
	DMARC bool `json:"dmarc"`
	SPF   bool `json:"spf"`
	Virus bool `json:"virus"`
}

type GSIIndex

type GSIIndex struct {
	MessageID     string `dynamodbav:"MessageID"`
	TypeYearMonth string `dynamodbav:"TypeYearMonth"`
	DateTime      string `dynamodbav:"DateTime"`
}

GSIIndex represents Global Secondary Index of an email

func (GSIIndex) ToTimeIndex

func (gsi GSIIndex) ToTimeIndex() (*TimeIndex, error)

ToTimeIndex returns TimeIndex

type GetAndSendEmailAPI added in v1.2.0

type GetAndSendEmailAPI interface {
	GetItemAPI
	SendEmailAPI
}

GetAndSendEmailAPI defines set of API required to get and send a email

type GetItemAPI

type GetItemAPI interface {
	GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
}

GetItemAPI defines set of API required to get an email

type GetItemContentAPI added in v1.4.0

type GetItemContentAPI interface {
	GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
}

GetItemContentAPI defines set of API required to get attachments or inlines of an email

type GetResult

type GetResult struct {
	MessageID      string   `json:"messageID"`
	Type           string   `json:"type"`
	Subject        string   `json:"subject"`
	From           []string `json:"from"`
	To             []string `json:"to"`
	Text           string   `json:"text"`
	HTML           string   `json:"html"`
	ThreadID       string   `json:"threadID,omitempty"`
	IsThreadLatest bool     `json:"isThreadLatest,omitempty"`

	// Inbox email attributes
	TimeReceived string        `json:"timeReceived,omitempty"`
	DateSent     string        `json:"dateSent,omitempty"`
	Source       string        `json:"source,omitempty"`
	Destination  []string      `json:"destination,omitempty"`
	ReturnPath   string        `json:"returnPath,omitempty"`
	Verdict      *EmailVerdict `json:"verdict,omitempty"`
	Unread       *bool         `json:"unread,omitempty"`

	// Draft email attributes
	TimeUpdated string   `json:"timeUpdated,omitempty"`
	Cc          []string `json:"cc,omitempty"`
	Bcc         []string `json:"bcc,omitempty"`
	ReplyTo     []string `json:"replyTo,omitempty"`

	// Attachment attributes, currently only support
	Attachments *types.Files `json:"attachments,omitempty"`
	Inlines     *types.Files `json:"inlines,omitempty"`
}

GetResult represents the result of get method

func Get

func Get(ctx context.Context, api GetItemAPI, messageID string) (*GetResult, error)

Get returns the email

type GetThreadWithEmailsAPI added in v1.5.0

type GetThreadWithEmailsAPI interface {
	GetItemAPI
	BatchGetItem(ctx context.Context, params *dynamodb.BatchGetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.BatchGetItemOutput, error)
}

GetThreadAPI defines set of API required to get a thread and its emails

type LastEvaluatedKey added in v1.1.0

type LastEvaluatedKey map[string]types.AttributeValue

func (*LastEvaluatedKey) Decode added in v1.3.0

func (k *LastEvaluatedKey) Decode(data []byte) error

func (LastEvaluatedKey) Encode added in v1.3.0

func (k LastEvaluatedKey) Encode() ([]byte, error)

type ListInput added in v1.1.0

type ListInput struct {
	Type       string  `json:"type"`
	Year       string  `json:"year"`
	Month      string  `json:"month"`
	Order      string  `json:"order"`     // asc or desc (default)
	ShowTrash  string  `json:"showTrash"` // 'include', 'exclude' or 'only' (default is 'exclude')
	PageSize   int     `json:"pageSize"`  // 0 means no limit, default is 100
	NextCursor *Cursor `json:"nextCursor"`
}

ListInput represents the input of list method

type ListResult

type ListResult struct {
	Count      int         `json:"count"`
	Items      []EmailItem `json:"items"`
	NextCursor *Cursor     `json:"nextCursor"`
	HasMore    bool        `json:"hasMore"`
}

ListResult represents the result of list method

func List

func List(ctx context.Context, api QueryAPI, input ListInput) (*ListResult, error)

List lists emails in DynamoDB

type OriginalMessageIDIndex added in v1.5.0

type OriginalMessageIDIndex struct {
	MessageID         string
	OriginalMessageID string
}

type PutItemAPI

type PutItemAPI interface {
	PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
}

PutItemAPI defines set of API required to create an new email or replaces an existing email

type QueryAPI

type QueryAPI interface {
	Query(ctx context.Context, params *dynamodb.QueryInput, optFns ...func(*dynamodb.Options)) (*dynamodb.QueryOutput, error)
}

QueryAPI defines set of API required to query for emails

type QueryAndGetItemAPI added in v1.5.0

type QueryAndGetItemAPI interface {
	QueryAPI
	GetItemAPI
}

type QueryInfo added in v1.1.0

type QueryInfo struct {
	Type  string `json:"type"`
	Year  string `json:"year"`
	Month string `json:"month"`
	Order string `json:"order"`
}

type RawEmailItem added in v1.3.0

type RawEmailItem struct {
	GSIIndex
	Subject        string
	From           []string `json:"from"`
	To             []string `json:"to"`
	Unread         *bool    `json:"unread,omitempty"`
	ThreadID       string   `json:"threadID,omitempty"`
	IsThreadLatest bool     `json:"isThreadLatest,omitempty"`
}

func (RawEmailItem) ToEmailItem added in v1.3.0

func (raw RawEmailItem) ToEmailItem() (*EmailItem, error)

type SaveAndSendEmailAPI added in v1.2.0

type SaveAndSendEmailAPI interface {
	PutItemAPI
	SendEmailAPI
}

SaveAndSendEmailAPI defines set of API required to save an email and send it

type SaveInput

type SaveInput struct {
	EmailInput
	GenerateText string `json:"generateText"` // on, off, or auto (default)
	Send         bool   `json:"send"`         // send email immediately
}

SaveInput represents the input of save method

type SaveResult

type SaveResult struct {
	TimeIndex
	Subject string   `json:"subject"`
	From    []string `json:"from"`
	To      []string `json:"to"`
	Cc      []string `json:"cc"`
	Bcc     []string `json:"bcc"`
	ReplyTo []string `json:"replyTo"`
	Text    string   `json:"text"`
	HTML    string   `json:"html"`
}

SaveResult represents the result of save method

func Save

func Save(ctx context.Context, api SaveAndSendEmailAPI, input SaveInput) (*SaveResult, error)

Save puts an email as draft in DynamoDB

type SendEmailAPI

type SendEmailAPI interface {
	BatchWriteItem(ctx context.Context, params *dynamodb.BatchWriteItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.BatchWriteItemOutput, error)
	SendEmail(ctx context.Context, params *sesv2.SendEmailInput, optFns ...func(*sesv2.Options)) (*sesv2.SendEmailOutput, error)
}

SendEmailAPI defines et of API required to send a email

type SendResult added in v1.1.1

type SendResult struct {
	MessageID string
}

func Send

func Send(ctx context.Context, api GetAndSendEmailAPI, messageID string) (*SendResult, error)

Send sends a draft email

type StoreEmailAPI added in v1.5.0

type StoreEmailAPI interface {
	QueryAPI
	GetItemAPI
	PutItemAPI
	TransactWriteItemsAPI
}

type StoreEmailInput added in v1.5.0

type StoreEmailInput struct {
	InReplyTo    string
	References   string
	Item         map[string]dynamodbTypes.AttributeValue
	TimeReceived string // RFC3339
}

type StoreEmailWithExistingThreadInput added in v1.5.0

type StoreEmailWithExistingThreadInput struct {
	ThreadID          string
	Email             map[string]dynamodbTypes.AttributeValue
	TimeReceived      string
	PreviousMessageID string
}

type StoreEmailWithNewThreadInput added in v1.5.0

type StoreEmailWithNewThreadInput struct {
	ThreadID        string
	Email           map[string]dynamodbTypes.AttributeValue
	TimeReceived    string
	CreatingEmailID string
	CreatingSubject string
	CreatingTime    string
}

type Thread added in v1.5.0

type Thread struct {
	MessageID   string   `json:"messageID"`
	Type        string   `json:"type"`    // always "thread"
	Subject     string   `json:"subject"` // The subject of the first email in the thread
	EmailIDs    []string `json:"emailIDs"`
	TimeUpdated string   `json:"timeUpdated"` // The time the last email is received

	Emails []GetResult `json:"emails,omitempty"`
}

func GetThread added in v1.5.0

func GetThread(ctx context.Context, api GetItemAPI, messageID string) (*Thread, error)

func GetThreadWithEmails added in v1.5.0

func GetThreadWithEmails(ctx context.Context, api GetThreadWithEmailsAPI, messageID string) (*Thread, error)

type TimeIndex

type TimeIndex struct {
	MessageID string `json:"messageID"`
	Type      string `json:"type"`

	// TimeReceived is used by inbox emails
	TimeReceived string `json:"timeReceived,omitempty"`

	// TimeUpdated is used by draft emails
	TimeUpdated string `json:"timeUpdated,omitempty"`

	// TimeSent is used by sent emails
	TimeSent string `json:"timeSent,omitempty"`
}

TimeIndex represents the index attributes of an email

type TransactWriteItemsAPI added in v1.5.0

type TransactWriteItemsAPI interface {
	TransactWriteItems(ctx context.Context, params *dynamodb.TransactWriteItemsInput, optFns ...func(*dynamodb.Options)) (*dynamodb.TransactWriteItemsOutput, error)
}

type UpdateItemAPI

type UpdateItemAPI interface {
	UpdateItem(ctx context.Context, params *dynamodb.UpdateItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.UpdateItemOutput, error)
}

UpdateItemAPI defines set of API required to update an email

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL