feat(invitations): check contact grants on every read, cap pending invitations, and expire them at the deadline

This commit is contained in:
shira committed 2026-10-09 19:06:50 +03:00
1 parent 676b3c6e24
commit 12a7213d4a
67 files changed
+4255 -149

No files matched your search

+29 -11
View File
@@ -18,10 +18,11 @@ const (
)
// GetMine reads one invitation delivered to the session candidate with the
// current terms. Reading has no side effect: it never records a view, so a
// prefetch or a cache refresh never changes what the organization sees. A
// draft, an invitation of another candidate, and an unknown identifier are
// not found alike.
// current terms. Reading never records a view, so a prefetch or a cache refresh
// never changes what the organization sees. The one change a read can cause is
// the due expiry: a delivered or viewed invitation read at its deadline or later
// is expired first, and the response shows it. A draft, an invitation of another
// candidate, and an unknown identifier are not found alike.
func (service *Service) GetMine(ctx context.Context, actorID, invitationID uuid.UUID) (domain.CandidateInvitationView, error) {
if actorID == uuid.Nil {
return domain.CandidateInvitationView{}, fmt.Errorf("%s - actor ID must be set: %w", getMineLocation, ErrForbidden)
@@ -39,6 +40,19 @@ func (service *Service) GetMine(ctx context.Context, actorID, invitationID uuid.
if err != nil {
return err
}
if entry.Invitation.ExpiryDue(now) {
// The expiry writes, so the row is locked, read again, and judged at
// the time that the lock is held.
if entry, err = readCandidateEntry(ctx, unit, actorID, invitationID, true); err != nil {
return err
}
if now, err = service.now(getMineLocation); err != nil {
return err
}
if entry, err = settleCandidateEntry(ctx, unit, entry, now); err != nil {
return fmt.Errorf("%s - expire due invitation: %w", getMineLocation, err)
}
}
view, err = service.candidateView(ctx, unit, entry, now)
return err
})
@@ -51,8 +65,9 @@ func (service *Service) GetMine(ctx context.Context, actorID, invitationID uuid.
// ViewMine records that the session candidate opened the terms. A delivered
// invitation moves to viewed with the next version and keeps its deadline;
// every other state, and a view at the deadline or later, changes nothing and
// returns the invitation as it is. The command is idempotent by its effect,
// so it takes neither a key nor an expected version.
// returns the invitation as it is. A due expiry is applied first. The command
// is idempotent by its effect, so it takes neither a key nor an expected
// version.
//
// Locks: only the invitation row, never the organization. A candidate
// command therefore never holds the row while it waits for an organization
@@ -62,12 +77,8 @@ func (service *Service) ViewMine(ctx context.Context, actorID, invitationID uuid
if actorID == uuid.Nil {
return domain.CandidateInvitationView{}, fmt.Errorf("%s - actor ID must be set: %w", viewMineLocation, ErrForbidden)
}
now, err := service.now(viewMineLocation)
if err != nil {
return domain.CandidateInvitationView{}, err
}
var view domain.CandidateInvitationView
err = service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
if err := authorizeCandidate(ctx, unit, actorID); err != nil {
return err
}
@@ -75,6 +86,13 @@ func (service *Service) ViewMine(ctx context.Context, actorID, invitationID uuid
if err != nil {
return err
}
now, err := service.now(viewMineLocation)
if err != nil {
return err
}
if entry, err = settleCandidateEntry(ctx, unit, entry, now); err != nil {
return fmt.Errorf("%s - expire due invitation: %w", viewMineLocation, err)
}
current := entry.Invitation
viewed, changed, err := current.RecordView(notBefore(now, current.UpdatedAt()))
if err != nil {