feat(notifications): send product notifications with a rights check right before each send
This commit is contained in:
1 parent
a856066316
commit
623d41b969
59 files changed
+4125
-1126
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -104,28 +105,31 @@ func (repository deliveryRepository) Create(ctx context.Context, delivery domain
|
||||
content := delivery.Content()
|
||||
hash := content.Hash()
|
||||
inserted, err := repository.queries.InsertDelivery(ctx, sqlc.InsertDeliveryParams{
|
||||
ID: pgUUID(delivery.ID()),
|
||||
SourceEventID: pgOptionalUUID(delivery.SourceEventID()),
|
||||
RecipientUserID: pgUUID(delivery.RecipientUserID()),
|
||||
RecipientEmail: delivery.Recipient().Address(),
|
||||
Kind: string(content.Kind()),
|
||||
TemplateVersion: content.TemplateVersion(),
|
||||
Subject: content.Subject(),
|
||||
TextBody: content.Text(),
|
||||
HtmlBody: content.HTML(),
|
||||
ContentSha256: hash[:],
|
||||
Provider: delivery.ProviderAccount().Provider(),
|
||||
ProviderAccount: delivery.ProviderAccount().Account(),
|
||||
IdempotencyKey: delivery.IdempotencyKey(),
|
||||
Status: string(delivery.Status()),
|
||||
AttemptCount: delivery.AttemptCount(),
|
||||
NextAttemptAt: pgZeroableTime(delivery.NextAttemptAt()),
|
||||
LastAttemptAt: pgZeroableTime(delivery.LastAttemptAt()),
|
||||
ProviderMessageID: pgOptionalText(delivery.ProviderMessageID()),
|
||||
EmailTokenID: pgOptionalUUID(delivery.EmailTokenID()),
|
||||
Version: delivery.Version(),
|
||||
CreatedAt: pgTime(delivery.CreatedAt()),
|
||||
UpdatedAt: pgTime(delivery.UpdatedAt()),
|
||||
ID: pgUUID(delivery.ID()),
|
||||
SourceEventID: pgOptionalUUID(delivery.SourceEventID()),
|
||||
RecipientUserID: pgUUID(delivery.RecipientUserID()),
|
||||
RecipientEmail: delivery.Recipient().Address(),
|
||||
Kind: string(content.Kind()),
|
||||
TemplateVersion: content.TemplateVersion(),
|
||||
Subject: content.Subject(),
|
||||
TextBody: content.Text(),
|
||||
HtmlBody: content.HTML(),
|
||||
ContentSha256: hash[:],
|
||||
Provider: delivery.ProviderAccount().Provider(),
|
||||
ProviderAccount: delivery.ProviderAccount().Account(),
|
||||
IdempotencyKey: delivery.IdempotencyKey(),
|
||||
Status: string(delivery.Status()),
|
||||
AttemptCount: delivery.AttemptCount(),
|
||||
NextAttemptAt: pgZeroableTime(delivery.NextAttemptAt()),
|
||||
LastAttemptAt: pgZeroableTime(delivery.LastAttemptAt()),
|
||||
ProviderMessageID: pgOptionalText(delivery.ProviderMessageID()),
|
||||
EmailTokenID: pgOptionalUUID(delivery.EmailTokenID()),
|
||||
SubjectKind: pgOptionalText(string(delivery.Subject().Kind())),
|
||||
SubjectID: pgOptionalUUID(delivery.Subject().ID()),
|
||||
SubjectOrganizationID: pgOptionalUUID(delivery.Subject().OrganizationID()),
|
||||
Version: delivery.Version(),
|
||||
CreatedAt: pgTime(delivery.CreatedAt()),
|
||||
UpdatedAt: pgTime(delivery.UpdatedAt()),
|
||||
})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("deliveryRepository.Create - insert delivery: %w", mapDeliveryError(err))
|
||||
@@ -142,34 +146,37 @@ func (repository deliveryRepository) Get(ctx context.Context, id uuid.UUID) (dom
|
||||
return domain.Delivery{}, fmt.Errorf("deliveryRepository.Get - select delivery: %w", mapDeliveryError(err))
|
||||
}
|
||||
return deliveryFromRow(sqlc.Delivery{
|
||||
ID: row.ID,
|
||||
SourceEventID: row.SourceEventID,
|
||||
RecipientUserID: row.RecipientUserID,
|
||||
RecipientEmail: row.RecipientEmail,
|
||||
Kind: row.Kind,
|
||||
TemplateVersion: row.TemplateVersion,
|
||||
Subject: row.Subject,
|
||||
TextBody: row.TextBody,
|
||||
HtmlBody: row.HtmlBody,
|
||||
ContentSha256: row.ContentSha256,
|
||||
Provider: row.Provider,
|
||||
ProviderAccount: row.ProviderAccount,
|
||||
IdempotencyKey: row.IdempotencyKey,
|
||||
Status: row.Status,
|
||||
AttemptCount: row.AttemptCount,
|
||||
NextAttemptAt: row.NextAttemptAt,
|
||||
LastAttemptAt: row.LastAttemptAt,
|
||||
ProviderMessageID: row.ProviderMessageID,
|
||||
Version: row.Version,
|
||||
CreatedAt: row.CreatedAt,
|
||||
UpdatedAt: row.UpdatedAt,
|
||||
EmailTokenID: row.EmailTokenID,
|
||||
CancelReason: row.CancelReason,
|
||||
ClaimOwner: row.ClaimOwner,
|
||||
ClaimedUntil: row.ClaimedUntil,
|
||||
FirstSendStartedAt: row.FirstSendStartedAt,
|
||||
FailureCount: row.FailureCount,
|
||||
BodyRedactedAt: row.BodyRedactedAt,
|
||||
ID: row.ID,
|
||||
SourceEventID: row.SourceEventID,
|
||||
RecipientUserID: row.RecipientUserID,
|
||||
RecipientEmail: row.RecipientEmail,
|
||||
Kind: row.Kind,
|
||||
TemplateVersion: row.TemplateVersion,
|
||||
Subject: row.Subject,
|
||||
TextBody: row.TextBody,
|
||||
HtmlBody: row.HtmlBody,
|
||||
ContentSha256: row.ContentSha256,
|
||||
Provider: row.Provider,
|
||||
ProviderAccount: row.ProviderAccount,
|
||||
IdempotencyKey: row.IdempotencyKey,
|
||||
Status: row.Status,
|
||||
AttemptCount: row.AttemptCount,
|
||||
NextAttemptAt: row.NextAttemptAt,
|
||||
LastAttemptAt: row.LastAttemptAt,
|
||||
ProviderMessageID: row.ProviderMessageID,
|
||||
Version: row.Version,
|
||||
CreatedAt: row.CreatedAt,
|
||||
UpdatedAt: row.UpdatedAt,
|
||||
EmailTokenID: row.EmailTokenID,
|
||||
CancelReason: row.CancelReason,
|
||||
ClaimOwner: row.ClaimOwner,
|
||||
ClaimedUntil: row.ClaimedUntil,
|
||||
FirstSendStartedAt: row.FirstSendStartedAt,
|
||||
FailureCount: row.FailureCount,
|
||||
BodyRedactedAt: row.BodyRedactedAt,
|
||||
SubjectKind: row.SubjectKind,
|
||||
SubjectID: row.SubjectID,
|
||||
SubjectOrganizationID: row.SubjectOrganizationID,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -406,6 +413,52 @@ func (repository deliveryRepository) updateState(ctx context.Context, location s
|
||||
return nil
|
||||
}
|
||||
|
||||
// claimKindsBelongToScope reports that every kind is one the lane's claim
|
||||
// statement reads: the product statement has a literal notification predicate,
|
||||
// so it must not be asked for service mail, and the reverse.
|
||||
func claimKindsBelongToScope(scope outbound.ClaimScope, kinds []domain.MailKind) bool {
|
||||
allowed := domain.ServiceMailKinds()
|
||||
if scope == outbound.ClaimScopeNotification {
|
||||
allowed = domain.ProductMailKinds()
|
||||
}
|
||||
for _, kind := range kinds {
|
||||
if !slices.Contains(allowed, kind) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// claimRow runs the claim statement of the claim's lane. The product statement
|
||||
// returns the same columns in the same order, so its row converts to the row of
|
||||
// the service statement; the conversion does not compile if the columns drift.
|
||||
func (repository deliveryRepository) claimRow(ctx context.Context, claim outbound.DeliveryClaim, kinds []string) (sqlc.ClaimDueDeliveryRow, error) {
|
||||
switch claim.Scope {
|
||||
case outbound.ClaimScopeService:
|
||||
return repository.queries.ClaimDueDelivery(ctx, sqlc.ClaimDueDeliveryParams{
|
||||
LeaseOwner: pgUUID(claim.Owner),
|
||||
LeaseSeconds: claim.Lease.Seconds(),
|
||||
Kinds: kinds,
|
||||
Now: pgTime(claim.Now),
|
||||
Provider: claim.Account.Provider(),
|
||||
ProviderAccount: claim.Account.Account(),
|
||||
RebindProvider: claim.RebindProvider,
|
||||
})
|
||||
case outbound.ClaimScopeNotification:
|
||||
product, err := repository.queries.ClaimDueProductDelivery(ctx, sqlc.ClaimDueProductDeliveryParams{
|
||||
LeaseOwner: pgUUID(claim.Owner),
|
||||
LeaseSeconds: claim.Lease.Seconds(),
|
||||
Now: pgTime(claim.Now),
|
||||
Provider: claim.Account.Provider(),
|
||||
ProviderAccount: claim.Account.Account(),
|
||||
RebindProvider: claim.RebindProvider,
|
||||
})
|
||||
return sqlc.ClaimDueDeliveryRow(product), err
|
||||
default:
|
||||
return sqlc.ClaimDueDeliveryRow{}, fmt.Errorf("deliveryRepository.claimRow - unknown claim scope %d: %w", claim.Scope, domain.ErrInvalidDelivery)
|
||||
}
|
||||
}
|
||||
|
||||
func (repository deliveryRepository) ClaimDue(ctx context.Context, claim outbound.DeliveryClaim) (outbound.ClaimedDelivery, error) {
|
||||
const location = "deliveryRepository.ClaimDue"
|
||||
if claim.Owner == uuid.Nil || claim.Account.IsZero() || len(claim.Kinds) == 0 || claim.Now.IsZero() || claim.Lease <= 0 {
|
||||
@@ -418,15 +471,10 @@ func (repository deliveryRepository) ClaimDue(ctx context.Context, claim outboun
|
||||
}
|
||||
kinds = append(kinds, string(kind))
|
||||
}
|
||||
row, err := repository.queries.ClaimDueDelivery(ctx, sqlc.ClaimDueDeliveryParams{
|
||||
LeaseOwner: pgUUID(claim.Owner),
|
||||
LeaseSeconds: claim.Lease.Seconds(),
|
||||
Kinds: kinds,
|
||||
Now: pgTime(claim.Now),
|
||||
Provider: claim.Account.Provider(),
|
||||
ProviderAccount: claim.Account.Account(),
|
||||
RebindProvider: claim.RebindProvider,
|
||||
})
|
||||
if !claimKindsBelongToScope(claim.Scope, claim.Kinds) {
|
||||
return outbound.ClaimedDelivery{}, fmt.Errorf("%s - kinds do not belong to the claim scope: %w", location, domain.ErrInvalidDelivery)
|
||||
}
|
||||
row, err := repository.claimRow(ctx, claim, kinds)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return outbound.ClaimedDelivery{}, outbound.ErrNoDeliveryDue
|
||||
}
|
||||
@@ -434,30 +482,33 @@ func (repository deliveryRepository) ClaimDue(ctx context.Context, claim outboun
|
||||
return outbound.ClaimedDelivery{}, fmt.Errorf("%s - claim due delivery: %w", location, mapDeliveryError(err))
|
||||
}
|
||||
delivery, err := deliveryFromRow(sqlc.Delivery{
|
||||
ID: row.ID,
|
||||
SourceEventID: row.SourceEventID,
|
||||
RecipientUserID: row.RecipientUserID,
|
||||
RecipientEmail: row.RecipientEmail,
|
||||
Kind: row.Kind,
|
||||
TemplateVersion: row.TemplateVersion,
|
||||
Subject: row.Subject,
|
||||
TextBody: row.TextBody,
|
||||
HtmlBody: row.HtmlBody,
|
||||
ContentSha256: row.ContentSha256,
|
||||
Provider: row.Provider,
|
||||
ProviderAccount: row.ProviderAccount,
|
||||
IdempotencyKey: row.IdempotencyKey,
|
||||
Status: row.Status,
|
||||
AttemptCount: row.AttemptCount,
|
||||
NextAttemptAt: row.NextAttemptAt,
|
||||
LastAttemptAt: row.LastAttemptAt,
|
||||
ProviderMessageID: row.ProviderMessageID,
|
||||
EmailTokenID: row.EmailTokenID,
|
||||
CancelReason: row.CancelReason,
|
||||
BodyRedactedAt: row.BodyRedactedAt,
|
||||
Version: row.Version,
|
||||
CreatedAt: row.CreatedAt,
|
||||
UpdatedAt: row.UpdatedAt,
|
||||
ID: row.ID,
|
||||
SourceEventID: row.SourceEventID,
|
||||
RecipientUserID: row.RecipientUserID,
|
||||
RecipientEmail: row.RecipientEmail,
|
||||
Kind: row.Kind,
|
||||
TemplateVersion: row.TemplateVersion,
|
||||
Subject: row.Subject,
|
||||
TextBody: row.TextBody,
|
||||
HtmlBody: row.HtmlBody,
|
||||
ContentSha256: row.ContentSha256,
|
||||
Provider: row.Provider,
|
||||
ProviderAccount: row.ProviderAccount,
|
||||
IdempotencyKey: row.IdempotencyKey,
|
||||
Status: row.Status,
|
||||
AttemptCount: row.AttemptCount,
|
||||
NextAttemptAt: row.NextAttemptAt,
|
||||
LastAttemptAt: row.LastAttemptAt,
|
||||
ProviderMessageID: row.ProviderMessageID,
|
||||
EmailTokenID: row.EmailTokenID,
|
||||
CancelReason: row.CancelReason,
|
||||
BodyRedactedAt: row.BodyRedactedAt,
|
||||
Version: row.Version,
|
||||
CreatedAt: row.CreatedAt,
|
||||
UpdatedAt: row.UpdatedAt,
|
||||
SubjectKind: row.SubjectKind,
|
||||
SubjectID: row.SubjectID,
|
||||
SubjectOrganizationID: row.SubjectOrganizationID,
|
||||
})
|
||||
if err != nil {
|
||||
unreadable := &outbound.UnreadableDeliveryError{DeliveryID: uuid.UUID(row.ID.Bytes), FailureCount: row.FailureCount, Err: err}
|
||||
@@ -575,9 +626,12 @@ func deliveryFromRow(row sqlc.Delivery) (domain.Delivery, error) {
|
||||
return domain.Delivery{}, fmt.Errorf("postgres.deliveryFromRow - decode kind: %w", err)
|
||||
}
|
||||
var content domain.MailContent
|
||||
if row.BodyRedactedAt.Valid {
|
||||
switch {
|
||||
case row.BodyRedactedAt.Valid && kind == domain.MailKindNotification:
|
||||
content, err = domain.RestoreRedactedNotificationContent(row.TemplateVersion, row.Subject, row.ContentSha256)
|
||||
case row.BodyRedactedAt.Valid:
|
||||
content, err = domain.RestoreRedactedMailContent(kind, row.TemplateVersion, row.Subject, row.ContentSha256)
|
||||
} else {
|
||||
default:
|
||||
content, err = domain.RestoreMailContent(kind, row.TemplateVersion, row.Subject, row.TextBody, row.HtmlBody, row.ContentSha256)
|
||||
}
|
||||
if err != nil {
|
||||
@@ -587,12 +641,17 @@ func deliveryFromRow(row sqlc.Delivery) (domain.Delivery, error) {
|
||||
if err != nil {
|
||||
return domain.Delivery{}, fmt.Errorf("postgres.deliveryFromRow - decode provider account: %w", err)
|
||||
}
|
||||
subject, err := domain.RestoreMailSubject(row.SubjectKind.String, optionalUUID(row.SubjectID), optionalUUID(row.SubjectOrganizationID))
|
||||
if err != nil {
|
||||
return domain.Delivery{}, fmt.Errorf("postgres.deliveryFromRow - decode subject: %w", err)
|
||||
}
|
||||
delivery, err := domain.RestoreDelivery(domain.DeliverySnapshot{
|
||||
ID: uuid.UUID(row.ID.Bytes),
|
||||
SourceEventID: optionalUUID(row.SourceEventID),
|
||||
RecipientUserID: uuid.UUID(row.RecipientUserID.Bytes),
|
||||
RecipientAddress: row.RecipientEmail,
|
||||
Content: content,
|
||||
Subject: subject,
|
||||
ProviderAccount: providerAccount,
|
||||
IdempotencyKey: row.IdempotencyKey,
|
||||
Status: domain.DeliveryStatus(row.Status),
|
||||
@@ -654,13 +713,16 @@ func (repository mailRequestRepository) Create(ctx context.Context, request doma
|
||||
return fmt.Errorf("mailRequestRepository.Create - validate request: %w", err)
|
||||
}
|
||||
err := repository.queries.InsertMailRequest(ctx, sqlc.InsertMailRequestParams{
|
||||
ID: pgUUID(request.ID),
|
||||
RecipientUserID: pgUUID(request.RecipientUserID),
|
||||
Kind: string(request.Kind),
|
||||
Subject: request.Notification.Subject,
|
||||
Body: request.Notification.Body,
|
||||
LinkPath: request.Notification.LinkPath,
|
||||
CreatedAt: pgTime(request.CreatedAt),
|
||||
ID: pgUUID(request.ID),
|
||||
RecipientUserID: pgUUID(request.RecipientUserID),
|
||||
Kind: string(request.Kind),
|
||||
Subject: request.Notification.Subject,
|
||||
Body: request.Notification.Body,
|
||||
LinkPath: request.Notification.LinkPath,
|
||||
SubjectKind: pgOptionalText(string(request.Subject.Kind())),
|
||||
SubjectID: pgOptionalUUID(request.Subject.ID()),
|
||||
SubjectOrganizationID: pgOptionalUUID(request.Subject.OrganizationID()),
|
||||
CreatedAt: pgTime(request.CreatedAt),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("mailRequestRepository.Create - insert mail request: %w", mapDeliveryError(err))
|
||||
@@ -680,11 +742,16 @@ func (repository mailRequestRepository) Lock(ctx context.Context, id uuid.UUID)
|
||||
if err != nil {
|
||||
return domain.MailRequest{}, fmt.Errorf("mailRequestRepository.Lock - decode kind: %w", err)
|
||||
}
|
||||
subject, err := domain.RestoreMailSubject(row.SubjectKind.String, optionalUUID(row.SubjectID), optionalUUID(row.SubjectOrganizationID))
|
||||
if err != nil {
|
||||
return domain.MailRequest{}, fmt.Errorf("mailRequestRepository.Lock - decode subject: %w", err)
|
||||
}
|
||||
request, err := domain.NewMailRequest(domain.MailRequest{
|
||||
ID: uuid.UUID(row.ID.Bytes),
|
||||
RecipientUserID: uuid.UUID(row.RecipientUserID.Bytes),
|
||||
Kind: kind,
|
||||
Notification: domain.NotificationText{Subject: row.Subject, Body: row.Body, LinkPath: row.LinkPath},
|
||||
Subject: subject,
|
||||
CreatedAt: row.CreatedAt.Time,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user