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

+37 -6
View File
@@ -170,6 +170,19 @@ const (
// again and again for a reason of its own, so it stopped trying and left
// the row to an operator.
DeliveryCancelProcessingFailed DeliveryCancelReason = "processing_failed"
// DeliveryCancelRightsRevoked: a notification's basis no longer holds
// when it is about to be sent. The organization is hidden from the
// candidate, the candidate withdrew publication (C2), or the invitation
// was answered, expired, or passed its deadline (see
// Delivery.CheckInvitationNotice).
DeliveryCancelRightsRevoked DeliveryCancelReason = "rights_revoked"
// DeliveryCancelRecipientIneligible: the recipient account is gone or its
// address is not verified, so a notification may not go to it.
DeliveryCancelRecipientIneligible DeliveryCancelReason = "recipient_ineligible"
// DeliveryCancelSourceClosed: the record a notification is about does not
// exist, belongs to another organization, or the notification names no
// record at all, so there is no right to check.
DeliveryCancelSourceClosed DeliveryCancelReason = "source_closed"
)
// DeliveryCancelReasons lists every cancel reason, for metrics that expose
@@ -179,7 +192,8 @@ func DeliveryCancelReasons() []DeliveryCancelReason {
DeliveryCancelTokenMissing, DeliveryCancelTokenPurposeMismatch, DeliveryCancelTokenOwnerMismatch,
DeliveryCancelTokenConsumed, DeliveryCancelTokenRevoked, DeliveryCancelTokenExpired,
DeliveryCancelAccountNotCurrent, DeliveryCancelRecipientMismatch, DeliveryCancelContentInvalid,
DeliveryCancelProcessingFailed,
DeliveryCancelProcessingFailed, DeliveryCancelRightsRevoked, DeliveryCancelRecipientIneligible,
DeliveryCancelSourceClosed,
}
}
@@ -189,7 +203,8 @@ func ParseDeliveryCancelReason(value string) (DeliveryCancelReason, error) {
case DeliveryCancelTokenMissing, DeliveryCancelTokenPurposeMismatch, DeliveryCancelTokenOwnerMismatch,
DeliveryCancelTokenConsumed, DeliveryCancelTokenRevoked, DeliveryCancelTokenExpired,
DeliveryCancelAccountNotCurrent, DeliveryCancelRecipientMismatch, DeliveryCancelContentInvalid,
DeliveryCancelProcessingFailed:
DeliveryCancelProcessingFailed, DeliveryCancelRightsRevoked, DeliveryCancelRecipientIneligible,
DeliveryCancelSourceClosed:
return reason, nil
default:
return "", invalidDelivery("domain.ParseDeliveryCancelReason", "unknown cancel reason")
@@ -239,6 +254,7 @@ type Delivery struct {
recipientUserID uuid.UUID
recipient EmailAddress
content MailContent
subject MailSubject
providerAccount MailProviderAccount
idempotencyKey string
status DeliveryStatus
@@ -271,7 +287,11 @@ type NewDeliveryInput struct {
// the worker with the reason token_missing. It must be uuid.Nil for a
// notification.
EmailTokenID uuid.UUID
CreatedAt time.Time
// Subject names the record a notification is about; the sending worker
// checks its rights before the send. Only a notification may name one, and
// the zero value names none.
Subject MailSubject
CreatedAt time.Time
}
// NewDelivery validates input and returns a pending delivery that is due at
@@ -293,6 +313,8 @@ func NewDelivery(input NewDeliveryInput) (Delivery, error) {
return Delivery{}, invalidDelivery(location, "creation time must be set")
case input.EmailTokenID != uuid.Nil && !input.Content.Kind().CarriesBearerToken():
return Delivery{}, invalidDelivery(location, "only a delivery of a token kind is bound to a token")
case !input.Subject.IsZero() && input.Content.Kind() != MailKindNotification:
return Delivery{}, invalidDelivery(location, "only a notification names a subject")
}
return Delivery{
id: input.ID,
@@ -300,6 +322,7 @@ func NewDelivery(input NewDeliveryInput) (Delivery, error) {
recipientUserID: input.RecipientUserID,
recipient: input.Recipient,
content: input.Content,
subject: input.Subject,
providerAccount: input.ProviderAccount,
emailTokenID: input.EmailTokenID,
idempotencyKey: deliveryIdempotencyKeyPrefix + input.ID.String(),
@@ -329,9 +352,11 @@ type DeliverySnapshot struct {
EmailTokenID uuid.UUID
// CancelReason is set exactly for a cancelled delivery.
CancelReason DeliveryCancelReason
Version int64
CreatedAt time.Time
UpdatedAt time.Time
// Subject is the zero value for a delivery without a subject.
Subject MailSubject
Version int64
CreatedAt time.Time
UpdatedAt time.Time
}
// RestoreDelivery rebuilds a stored delivery and checks the invariants that
@@ -362,6 +387,7 @@ func RestoreDelivery(snapshot DeliverySnapshot) (Delivery, error) {
recipientUserID: snapshot.RecipientUserID,
recipient: recipient,
content: snapshot.Content,
subject: snapshot.Subject,
providerAccount: snapshot.ProviderAccount,
idempotencyKey: snapshot.IdempotencyKey,
status: snapshot.Status,
@@ -408,6 +434,8 @@ func checkCancellationAndBinding(location string, snapshot DeliverySnapshot) err
return invalidDelivery(location, "cancel reason must be set exactly for a cancelled delivery")
case snapshot.EmailTokenID != uuid.Nil && !snapshot.Content.Kind().CarriesBearerToken():
return invalidDelivery(location, "only a delivery of a token kind is bound to a token")
case !snapshot.Subject.IsZero() && snapshot.Content.Kind() != MailKindNotification:
return invalidDelivery(location, "only a notification names a subject")
}
if snapshot.CancelReason != "" {
if _, err := ParseDeliveryCancelReason(string(snapshot.CancelReason)); err != nil {
@@ -440,6 +468,9 @@ func (delivery Delivery) RecipientUserID() uuid.UUID { return delivery.recipient
// only to send the message; never log it.
func (delivery Delivery) Recipient() EmailAddress { return delivery.recipient }
// Subject is the record a notification is about; zero for other deliveries.
func (delivery Delivery) Subject() MailSubject { return delivery.subject }
func (delivery Delivery) Kind() MailKind { return delivery.content.Kind() }
func (delivery Delivery) Content() MailContent { return delivery.content }