Skip to content

Commit

Permalink
feat(plugin/archive): allow custom mail id
Browse files Browse the repository at this point in the history
  • Loading branch information
bounoable committed May 25, 2021
1 parent c26a540 commit 06a2659
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion plugin/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ func New(s Store, opts ...Option) postdog.Plugin {
errMsg = sendError.Error()
}

id := MailIDFromContext(ctx)
if id == uuid.Nil {
id = uuid.New()
}

m := ExpandMail(pm).
WithID(uuid.New()).
WithID(id).
WithSendError(errMsg).
WithSendTime(sentAt)

Expand Down
36 changes: 36 additions & 0 deletions plugin/archive/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package archive

import (
"context"

"github.com/google/uuid"
)

const (
ctxMailID = ctxKey("mail_id")
)

type ctxKey string

// WithMailID returns a new Context that carries the given UUID. Mails that are
// archived with that Context will use that UUID as their UUID when stored in a
// database.
func WithMailID(ctx context.Context, id uuid.UUID) context.Context {
return context.WithValue(ctx, ctxMailID, id)
}

// MailIDFromContext returns the mail UUID from the given Context, or uuid.Nil
// if the Context has no mail UUID.
func MailIDFromContext(ctx context.Context) uuid.UUID {
val := ctx.Value(ctxMailID)
if val == nil {
return uuid.Nil
}

id, ok := val.(uuid.UUID)
if !ok {
return uuid.Nil
}

return id
}

0 comments on commit 06a2659

Please sign in to comment.