feat(notifications): send product notifications with a rights check right before each send

This commit is contained in:
shira committed 2026-10-09 20:38:39 +03:00
1 parent a856066316
commit 623d41b969
59 files changed
+4125 -1126

No files matched your search

+22 -6
View File
@@ -60,7 +60,11 @@ type MailRequest struct {
RecipientUserID uuid.UUID
Kind MailKind
Notification NotificationText
CreatedAt time.Time
// 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
@@ -68,7 +72,6 @@ type MailRequest struct {
// 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")
@@ -76,17 +79,30 @@ func NewMailRequest(request MailRequest) (MailRequest, error) {
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 MailRequest{}, invalidMailRequest("text must be valid UTF-8")
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 MailRequest{}, invalidMailRequest("text must not contain NUL characters")
return 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 invalidMailRequest("subject, body, or link path is too long")
}
return request, nil
return nil
}
// LogValue carries the IDs and kind only.