feat(notification): add durable mail delivery state
This commit is contained in:
1 parent
391c8a6cec
commit
2db585fbff
41 files changed
+4903
-108
No files matched your search
@@ -0,0 +1,102 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
// Raw caller text is bounded before the template normalizes it; the
|
||||
// rendered subject and body have tighter template limits.
|
||||
maxMailRequestSubjectRunes = 1000
|
||||
maxMailRequestBodyRunes = 16000
|
||||
maxMailRequestLinkPathSize = 256
|
||||
redactedNotificationText = "[redacted notification text]"
|
||||
)
|
||||
|
||||
// ErrInvalidMailRequest reports a mail request that cannot be stored.
|
||||
// Errors name the rule, never the text.
|
||||
var ErrInvalidMailRequest = errors.New("invalid mail request")
|
||||
|
||||
// NotificationText is the caller text of a notification mail: the subject,
|
||||
// the body, and a path inside the application for the link. It is personal
|
||||
// data addressed to the recipient, so String, GoString, Format, LogValue,
|
||||
// and MarshalJSON redact it.
|
||||
type NotificationText struct {
|
||||
Subject string
|
||||
Body string
|
||||
LinkPath string
|
||||
}
|
||||
|
||||
func (text NotificationText) String() string { return redactedNotificationText }
|
||||
|
||||
func (text NotificationText) GoString() string { return redactedNotificationText }
|
||||
|
||||
// Format redacts every verb; without it %+v would print the fields.
|
||||
func (text NotificationText) Format(state fmt.State, _ rune) {
|
||||
_, _ = state.Write([]byte(redactedNotificationText))
|
||||
}
|
||||
|
||||
func (text NotificationText) LogValue() slog.Value { return slog.StringValue(redactedNotificationText) }
|
||||
|
||||
// MarshalJSON redacts the text, because slog and other encoders reach a
|
||||
// nested struct through encoding/json. Nothing serializes the text as JSON.
|
||||
func (text NotificationText) MarshalJSON() ([]byte, error) {
|
||||
return []byte(`"` + redactedNotificationText + `"`), nil
|
||||
}
|
||||
|
||||
// MailRequest is a stored request for one mail to one account. It lives in
|
||||
// the database only until the delivery consumer turned it into a Delivery,
|
||||
// so the event that announces it carries the request ID and no text.
|
||||
// Deleting the account deletes a request that is still waiting.
|
||||
type MailRequest struct {
|
||||
ID uuid.UUID
|
||||
RecipientUserID uuid.UUID
|
||||
Kind MailKind
|
||||
Notification NotificationText
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// NewMailRequest checks the storage rules of a request. Only notification
|
||||
// mail is requested this way: mail with a one-time token is created in the
|
||||
// transaction that issues the token. Whether the text renders is checked by
|
||||
// the template, not here.
|
||||
func NewMailRequest(request MailRequest) (MailRequest, error) {
|
||||
text := request.Notification
|
||||
switch {
|
||||
case request.ID == uuid.Nil || request.RecipientUserID == uuid.Nil:
|
||||
return MailRequest{}, invalidMailRequest("request and recipient user IDs must not be nil")
|
||||
case request.Kind != MailKindNotification:
|
||||
return MailRequest{}, invalidMailRequest("only notification mail can be requested")
|
||||
case request.CreatedAt.IsZero():
|
||||
return MailRequest{}, invalidMailRequest("creation time must be set")
|
||||
case !utf8.ValidString(text.Subject) || !utf8.ValidString(text.Body) || !utf8.ValidString(text.LinkPath):
|
||||
return MailRequest{}, invalidMailRequest("text must be valid UTF-8")
|
||||
// PostgreSQL text cannot store NUL.
|
||||
case strings.ContainsRune(text.Subject, 0) || strings.ContainsRune(text.Body, 0) || strings.ContainsRune(text.LinkPath, 0):
|
||||
return MailRequest{}, invalidMailRequest("text must not contain NUL characters")
|
||||
case utf8.RuneCountInString(text.Subject) > maxMailRequestSubjectRunes ||
|
||||
utf8.RuneCountInString(text.Body) > maxMailRequestBodyRunes ||
|
||||
len(text.LinkPath) > maxMailRequestLinkPathSize:
|
||||
return MailRequest{}, invalidMailRequest("subject, body, or link path is too long")
|
||||
}
|
||||
return request, nil
|
||||
}
|
||||
|
||||
// LogValue carries the IDs and kind only.
|
||||
func (request MailRequest) LogValue() slog.Value {
|
||||
return slog.GroupValue(
|
||||
slog.String("mail_request_id", request.ID.String()),
|
||||
slog.String("kind", string(request.Kind)),
|
||||
)
|
||||
}
|
||||
|
||||
func invalidMailRequest(reason string) error {
|
||||
return fmt.Errorf("domain.NewMailRequest - %s: %w", reason, ErrInvalidMailRequest)
|
||||
}
|
||||
Reference in new issue
Block a user