feat(notification): add durable mail delivery state

This commit is contained in:
shira committed 2026-10-06 10:52:47 +03:00
1 parent 391c8a6cec
commit 2db585fbff
41 files changed
+4903 -108

No files matched your search

@@ -1,7 +1,6 @@
package mailtemplate
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"log/slog"
@@ -15,7 +14,7 @@ import (
// the same digest, and any change to any part gives another one. The delivery
// layer can store it with the idempotency key to pin the body of one logical
// delivery and to detect a different body offered under the same key.
type ContentHash [sha256.Size]byte
type ContentHash [domain.MailContentHashSize]byte
// String returns the lowercase hex digest. It is not secret: SHA-256 over a
// body that holds a 256-bit token cannot be reversed.
@@ -50,7 +49,7 @@ func newRenderedMail(kind Kind, version TemplateVersion, subject, text, html str
subject: subject,
text: text,
html: html,
contentHash: contentHash(string(version), subject, text, html),
contentHash: ContentHash(domain.MailContentHash(string(version), subject, text, html)),
}}
}
@@ -78,6 +77,18 @@ func (mail RenderedMail) read() renderedContent {
return *mail.content
}
// Content returns the rendering as domain.MailContent, the form a delivery
// stores. The kinds and the hash are the same: both use
// domain.MailContentHash.
func (mail RenderedMail) Content() (domain.MailContent, error) {
content := mail.read()
stored, err := domain.NewMailContent(domain.MailKind(content.kind), string(content.version), content.subject, content.text, content.html)
if err != nil {
return domain.MailContent{}, fmt.Errorf("RenderedMail.Content - %w", err)
}
return stored, nil
}
// ToOutgoingMail addresses the rendering for the MailSender port. The
// recipient and the idempotency key come from the delivery, never from the
// template data.
@@ -13,7 +13,6 @@ package mailtemplate
import (
"bytes"
"crypto/sha256"
"embed"
"errors"
"fmt"
@@ -172,21 +171,6 @@ func finishPart(part string) string {
return strings.TrimSpace(part) + "\n"
}
// contentHash is SHA-256 over the parts encoded as netstrings ("<len>:<part>,"),
// so moving bytes from one part to the next changes the digest.
func contentHash(parts ...string) ContentHash {
digest := sha256.New()
for _, part := range parts {
// hash.Hash.Write never returns an error.
_, _ = fmt.Fprintf(digest, "%d:", len(part))
_, _ = io.WriteString(digest, part)
_, _ = io.WriteString(digest, ",")
}
var sum ContentHash
copy(sum[:], digest.Sum(nil))
return sum
}
// parseTemplates reads every file in the template directory. Each version
// must provide the subject, text, and HTML parts, and no other file may be
// present, so a misnamed file cannot be silently ignored.
@@ -306,9 +306,20 @@ func TestContentHashPinsTheBody(t *testing.T) {
if mail.ContentHash() != second[kind].ContentHash() {
t.Fatalf("%s content hash differs for the same version and data", kind)
}
if mail.ContentHash() != contentHash(string(mail.TemplateVersion()), mail.Subject(), mail.Text(), mail.HTML()) {
if mail.ContentHash() != ContentHash(domain.MailContentHash(string(mail.TemplateVersion()), mail.Subject(), mail.Text(), mail.HTML())) {
t.Fatalf("%s content hash does not cover the version, subject, and parts", kind)
}
// A delivery stores the rendering as domain.MailContent; both forms
// must name the same kind, version, parts, and hash.
content, err := mail.Content()
if err != nil {
t.Fatalf("%s Content() error = %v", kind, err)
}
if string(content.Kind()) != string(kind) || content.TemplateVersion() != string(mail.TemplateVersion()) ||
content.Subject() != mail.Subject() || content.Text() != mail.Text() || content.HTML() != mail.HTML() ||
ContentHash(content.Hash()) != mail.ContentHash() {
t.Fatalf("%s Content() does not carry the rendering", kind)
}
seen[mail.ContentHash()] = kind
}
if len(seen) != len(first) {
@@ -350,19 +361,6 @@ func TestContentHashPinsTheBody(t *testing.T) {
t.Fatalf("content hash did not change with the %s", change.name)
}
}
base := contentHash("verification.v1", "subject", "text", "html")
for name, other := range map[string]ContentHash{
"version": contentHash("verification.v2", "subject", "text", "html"),
"subject": contentHash("verification.v1", "subject!", "text", "html"),
"text": contentHash("verification.v1", "subject", "text!", "html"),
"html": contentHash("verification.v1", "subject", "text", "html!"),
"shifted border": contentHash("verification.v1", "subjec", "ttext", "html"),
} {
if other == base {
t.Fatalf("content hash ignores a change of the %s", name)
}
}
}
// TestRenderingNeverReachesLogs plays the render harness: it prints every