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 // Subject names the record the notification is about. It is copied to the // delivery, whose sending worker checks its rights; the zero value names // none. Subject MailSubject 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) { 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 !request.Subject.IsZero() && request.Subject.Kind() != MailSubjectInvitation: return MailRequest{}, invalidMailRequest("the subject kind is unknown") } if err := checkMailRequestText(request.Notification); err != nil { return MailRequest{}, err } return request, nil } // checkMailRequestText bounds and checks the raw text of a request, before the // template normalizes it. func checkMailRequestText(text NotificationText) error { switch { case !utf8.ValidString(text.Subject) || !utf8.ValidString(text.Body) || !utf8.ValidString(text.LinkPath): return 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 invalidMailRequest("text must not contain NUL characters") case utf8.RuneCountInString(text.Subject) > maxMailRequestSubjectRunes || utf8.RuneCountInString(text.Body) > maxMailRequestBodyRunes || len(text.LinkPath) > maxMailRequestLinkPathSize: return invalidMailRequest("subject, body, or link path is too long") } return 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) }