package domain import ( "time" "github.com/google/uuid" ) // InvitationNoticeStanding is what the rights check of an invitation notice // reads about its recipient and its source at the time of the send. It holds // no address and no contact: the recipient address enters only as the // normalized key that the delivery is compared with. type InvitationNoticeStanding struct { RecipientFound bool RecipientEmailVerified bool // RecipientEmailKey is the normalized address of the account now. RecipientEmailKey string InvitationFound bool InvitationOrganizationID uuid.UUID InvitationState InvitationState // ResponseDeadlineAt is zero when the invitation was never delivered. ResponseDeadlineAt time.Time // OrganizationHidden reports that the recipient hid the sending // organization. OrganizationHidden bool // PublicationPublished reports that the recipient's publication (C2) is // current, which is the state an invitation was sent under. PublicationPublished bool } // CheckInvitationNotice decides whether the notice about an invitation may still // be sent at the given time, and returns the cancel reason when it may not. An // empty reason means that the notice is sendable. The checks run in this order // and the first failure decides: // // 1. the delivery names an invitation subject; // 2. the recipient account exists and its address is verified; // 3. the address of the delivery is still the address of the account; // 4. the invitation exists and belongs to the organization of the subject; // 5. the invitation still awaits an answer (delivered or viewed) and its // response deadline has not passed; // 6. the recipient has not hidden the sending organization, and the // recipient's publication (C2) is current. // // The notice declares no allowed fields. Its body is fixed text that names // neither the organization nor the terms, and it carries no contact, so no // field consent (C4) applies. A future notice that carries any candidate field // or contact must add that field's consent check here, and must not put a // contact into the mail at all: contacts are read on request, and every read // re-checks the grant. // // The check is a snapshot taken in the sending transaction. A change after it // and before the provider call still lets the notice go, which is the same // rule as for service mail. func (delivery Delivery) CheckInvitationNotice(standing InvitationNoticeStanding, at time.Time) DeliveryCancelReason { subject := delivery.Subject() awaitsAnswer := standing.InvitationState == InvitationDelivered || standing.InvitationState == InvitationViewed switch { case subject.Kind() != MailSubjectInvitation: return DeliveryCancelSourceClosed case !standing.RecipientFound || !standing.RecipientEmailVerified: return DeliveryCancelRecipientIneligible case standing.RecipientEmailKey != delivery.Recipient().Normalized(): return DeliveryCancelRecipientMismatch case !standing.InvitationFound || standing.InvitationOrganizationID != subject.OrganizationID(): return DeliveryCancelSourceClosed case !awaitsAnswer || !at.Before(standing.ResponseDeadlineAt): return DeliveryCancelRightsRevoked case standing.OrganizationHidden || !standing.PublicationPublished: return DeliveryCancelRightsRevoked default: return "" } }