@@ -85,29 +85,22 @@ pub struct PendingAddress {
8585}
8686
8787impl EmailMessage {
88- /// The messages `event` sends, each with the ID of the address it goes to:
89- /// its link, if it is one that mails a link, and its notification, `notified`,
90- /// if it gave one. Runs in the event's transaction, before a removed address
91- /// is deleted.
92- async fn for_event (
88+ /// The link `event` mails its subject, `user_id`, if it is one that mails a
89+ /// link, with the ID of the address it goes to.
90+ async fn links (
9391 event : & DomainEvent ,
9492 user_id : UserId ,
95- notified : Option < & Notified > ,
9693 tx : & mut DatabaseTransaction < ' _ > ,
9794 ) -> Result < Vec < ( EmailId , Self ) > , DatabaseError > {
98- // Only the events that mail an address read the addresses.
99- let mails = notified. is_some ( )
100- || matches ! (
101- event,
102- DomainEvent :: EmailAdded { .. } | DomainEvent :: RecoveryStarted { revoke: None , .. }
103- ) ;
104- let emails = if mails {
105- tx. emails ( ) . list ( user_id) . await ?
106- } else {
107- Vec :: new ( )
108- } ;
109- let verified = || emails. iter ( ) . filter ( |email| email. verified_at . is_some ( ) ) ;
110- let mut messages: Vec < _ > = match event {
95+ // Only the events that mail a link read the addresses.
96+ if !matches ! (
97+ event,
98+ DomainEvent :: EmailAdded { .. } | DomainEvent :: RecoveryStarted { revoke: None , .. }
99+ ) {
100+ return Ok ( Vec :: new ( ) ) ;
101+ }
102+ let emails = tx. emails ( ) . list ( user_id) . await ?;
103+ Ok ( match event {
111104 // An address a provider imported verified needs no link.
112105 DomainEvent :: EmailAdded { email_id, .. } => emails
113106 . iter ( )
@@ -124,8 +117,9 @@ impl EmailMessage {
124117 recovery_id,
125118 revoke : None ,
126119 ..
127- } => verified ( )
128- . filter ( |email| email. is_primary )
120+ } => emails
121+ . iter ( )
122+ . filter ( |email| email. verified_at . is_some ( ) && email. is_primary )
129123 . map ( |email| {
130124 let message = Self :: RecoveryLink {
131125 recovery_id : * recovery_id,
@@ -135,10 +129,21 @@ impl EmailMessage {
135129 } )
136130 . collect ( ) ,
137131 _ => Vec :: new ( ) ,
138- } ;
139- let Some ( notified) = notified else {
140- return Ok ( messages) ;
141- } ;
132+ } )
133+ }
134+
135+ /// The emails of `notified`, which `event` wrote, each with the ID of the
136+ /// address it goes to: one to each verified address of its account that its
137+ /// addressees name. Runs in the event's transaction, before a removed address
138+ /// is deleted.
139+ async fn notifying (
140+ event : & DomainEvent ,
141+ notified : & Notified ,
142+ tx : & mut DatabaseTransaction < ' _ > ,
143+ ) -> Result < Vec < ( EmailId , Self ) > , DatabaseError > {
144+ let emails = tx. emails ( ) . list ( notified. user_id ) . await ?;
145+ // The affected address and the recovery are the event's subject's: only
146+ // the subject's own notifications are about them.
142147 let affected = match event {
143148 DomainEvent :: PrimaryEmailChanged {
144149 previous_email_id, ..
@@ -157,24 +162,24 @@ impl EmailMessage {
157162 _ => None ,
158163 } ;
159164 let addressees = notified. notification . addressees ( ) ;
160- messages . extend (
161- verified ( )
162- . filter ( |email| match addressees {
163- Addressees :: Verified => true ,
164- Addressees :: Affected => affected == Some ( email . id ) ,
165- Addressees :: Primary => email. is_primary ,
166- } )
167- . map ( |email| {
168- let message = Self :: Notification {
169- address : email . normalized_email . clone ( ) ,
170- notification_id : notified . id ,
171- notification : notified. notification . clone ( ) ,
172- recovery_id ,
173- } ;
174- ( email . id , message )
175- } ) ,
176- ) ;
177- Ok ( messages )
165+ Ok ( emails
166+ . into_iter ( )
167+ . filter ( |email| email . verified_at . is_some ( ) )
168+ . filter ( |email| match addressees {
169+ Addressees :: Verified => true ,
170+ Addressees :: Affected => affected == Some ( email. id ) ,
171+ Addressees :: Primary => email . is_primary ,
172+ } )
173+ . map ( |email| {
174+ let message = Self :: Notification {
175+ address : email . normalized_email ,
176+ notification_id : notified. id ,
177+ notification : notified . notification . clone ( ) ,
178+ recovery_id ,
179+ } ;
180+ ( email . id , message )
181+ } )
182+ . collect ( ) )
178183 }
179184}
180185
@@ -183,22 +188,29 @@ impl Channel for Email {
183188 type Recipient = UserId ;
184189 type Payload = EmailMessage ;
185190
186- /// One message to each address of the event's subject that its link or
187- /// notification goes to. Its `destination` names the account and the
188- /// address's ID, never the address.
191+ /// The link the event mails its subject, if any, and one message to each
192+ /// address of each notified account that its notification goes to. Its
193+ /// `destination` names the account and the address's ID, never the address.
189194 async fn deliveries (
190195 event : & DomainEvent ,
191196 subject : Option < UserId > ,
192- notified : Option < & Notified > ,
197+ notified : & [ Notified ] ,
193198 tx : & mut DatabaseTransaction < ' _ > ,
194199 ) -> Result < Vec < NewDelivery < Self > > , DatabaseError > {
195- let Some ( user_id) = subject else {
196- return Ok ( vec ! [ ] ) ;
197- } ;
198- let messages = EmailMessage :: for_event ( event, user_id, notified, tx) . await ?;
200+ let mut messages = Vec :: new ( ) ;
201+ if let Some ( user_id) = subject {
202+ for ( email_id, message) in EmailMessage :: links ( event, user_id, tx) . await ? {
203+ messages. push ( ( user_id, email_id, message) ) ;
204+ }
205+ }
206+ for notified in notified {
207+ for ( email_id, message) in EmailMessage :: notifying ( event, notified, tx) . await ? {
208+ messages. push ( ( notified. user_id , email_id, message) ) ;
209+ }
210+ }
199211 Ok ( messages
200212 . into_iter ( )
201- . map ( |( email_id, message) | NewDelivery {
213+ . map ( |( user_id , email_id, message) | NewDelivery {
202214 destination : format ! ( "account {user_id}, address {email_id}" ) ,
203215 recipient : user_id,
204216 payload : message,
0 commit comments