Skip to content

Commit 4386a4c

Browse files
committed
refactor(database): an event's channels see every notification it wrote, each with its account
1 parent f06c87a commit 4386a4c

10 files changed

Lines changed: 90 additions & 70 deletions

File tree

‎apps/auth-service/src/admin/tests/deliveries.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Channel for Unsent {
1616
fn deliveries(
1717
_: &DomainEvent,
1818
_: Option<UserId>,
19-
_: Option<&piquel_repository::Notified>,
19+
_: &[piquel_repository::Notified],
2020
_: &mut DatabaseTransaction<'_>,
2121
) -> impl Future<Output = Result<Vec<NewDelivery<Self>>, DatabaseError>> + Send {
2222
std::future::ready(Ok(vec![]))

‎apps/auth-service/src/deliveries/tests.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ macro_rules! worker_channels {
268268
fn deliveries(
269269
_: &DomainEvent,
270270
_: Option<UserId>,
271-
_: Option<&piquel_repository::Notified>,
271+
_: &[piquel_repository::Notified],
272272
_: &mut DatabaseTransaction<'_>,
273273
) -> impl Future<Output = Result<Vec<NewDelivery<Self>>, DatabaseError>> + Send {
274274
std::future::ready(Ok(vec![]))

‎apps/auth-service/src/web/admin/tests.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ impl Channel for Unsent {
13921392
fn deliveries(
13931393
_: &DomainEvent,
13941394
_: Option<UserId>,
1395-
_: Option<&piquel_repository::Notified>,
1395+
_: &[piquel_repository::Notified],
13961396
_: &mut DatabaseTransaction<'_>,
13971397
) -> impl Future<Output = Result<Vec<NewDelivery<Self>>, DatabaseError>> + Send {
13981398
std::future::ready(Ok(vec![]))

‎crates/database/src/repositories/deliveries/announcement.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Channel for Announcement {
4646
async fn deliveries(
4747
event: &DomainEvent,
4848
_: Option<UserId>,
49-
_: Option<&Notified>,
49+
_: &[Notified],
5050
tx: &mut DatabaseTransaction<'_>,
5151
) -> Result<Vec<NewDelivery<Self>>, DatabaseError> {
5252
let DomainEvent::AnnouncementSent {

‎crates/database/src/repositories/deliveries/backchannel.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl Channel for BackchannelLogout {
3434
async fn deliveries(
3535
event: &DomainEvent,
3636
_: Option<UserId>,
37-
_: Option<&Notified>,
37+
_: &[Notified],
3838
tx: &mut DatabaseTransaction<'_>,
3939
) -> Result<Vec<NewDelivery<Self>>, DatabaseError> {
4040
let DomainEvent::SessionRevoked { session_id, .. } = event else {

‎crates/database/src/repositories/deliveries/email.rs‎

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,22 @@ pub struct PendingAddress {
8585
}
8686

8787
impl 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,

‎crates/database/src/repositories/deliveries/mod.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ pub trait Channel: Sized + Send + Sync + 'static {
3434
type Payload: Serialize + DeserializeOwned + Send + Sync;
3535

3636
/// The deliveries `event`, concerning `subject`, causes, with the
37-
/// notification it just gave `subject`, if any. Runs inside the event's
37+
/// notifications it just wrote, `notified`. Runs inside the event's
3838
/// transaction, which sees the rows as the change left them, or as they were
3939
/// for an event recorded before its change, such as `EmailRemoved`. A
4040
/// committed event has all its deliveries and a rolled-back one has none.
4141
fn deliveries(
4242
event: &DomainEvent,
4343
subject: Option<UserId>,
44-
notified: Option<&Notified>,
44+
notified: &[Notified],
4545
tx: &mut DatabaseTransaction<'_>,
4646
) -> impl Future<Output = Result<Vec<NewDelivery<Self>>, DatabaseError>> + Send;
4747
}
@@ -56,7 +56,7 @@ pub(crate) trait Channels {
5656
event_id: EventId,
5757
subject: Option<UserId>,
5858
event: &DomainEvent,
59-
notified: Option<&Notified>,
59+
notified: &[Notified],
6060
) -> impl Future<Output = Result<(), DatabaseError>> + Send;
6161
}
6262

@@ -70,7 +70,7 @@ impl Channels for () {
7070
_: EventId,
7171
_: Option<UserId>,
7272
_: &DomainEvent,
73-
_: Option<&Notified>,
73+
_: &[Notified],
7474
) -> impl Future<Output = Result<(), DatabaseError>> + Send {
7575
std::future::ready(Ok(()))
7676
}
@@ -83,7 +83,7 @@ impl<C: Channel, Rest: Channels> Channels for (C, Rest) {
8383
event_id: EventId,
8484
subject: Option<UserId>,
8585
event: &DomainEvent,
86-
notified: Option<&Notified>,
86+
notified: &[Notified],
8787
) -> Result<(), DatabaseError> {
8888
for delivery in C::deliveries(event, subject, notified, tx).await? {
8989
tx.deliveries().insert(Some(event_id), delivery).await?;

‎crates/database/src/repositories/deliveries/tests.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ macro_rules! test_channels {
3131
fn deliveries(
3232
_: &DomainEvent,
3333
_: Option<UserId>,
34-
_: Option<&Notified>,
34+
_: &[Notified],
3535
_: &mut DatabaseTransaction<'_>,
3636
) -> impl Future<Output = Result<Vec<NewDelivery<Self>>, DatabaseError>> + Send {
3737
std::future::ready(Ok(vec![]))
@@ -68,7 +68,7 @@ impl Channel for Recorder {
6868
async fn deliveries(
6969
_: &DomainEvent,
7070
subject: Option<UserId>,
71-
_: Option<&Notified>,
71+
_: &[Notified],
7272
tx: &mut DatabaseTransaction<'_>,
7373
) -> Result<Vec<NewDelivery<Self>>, DatabaseError> {
7474
let Some(subject) = subject else {

‎crates/database/src/repositories/notifications/mod.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub struct StoredNotification {
3232
/// see: the `email` channel sends it.
3333
#[derive(Clone, Debug, PartialEq, Eq)]
3434
pub struct Notified {
35+
/// The account whose inbox it is in.
36+
pub user_id: UserId,
3537
/// The notification's ID, which its page's path names.
3638
pub id: NotificationId,
3739
/// What it says.

‎crates/database/src/transaction.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,21 @@ impl<'a> DatabaseTransaction<'a> {
6363
event: DomainEvent,
6464
) -> Result<EventId, DatabaseError> {
6565
let id = self.events().insert(context, subject, &event).await?;
66-
let notified = match subject.zip(event.notification(&context.actor)) {
67-
Some((user, notification)) => self
66+
let notified: Vec<_> = match subject.zip(event.notification(&context.actor)) {
67+
Some((user_id, notification)) => self
6868
.notifications()
69-
.insert(user, Some(id), &notification)
69+
.insert(user_id, Some(id), &notification)
7070
.await?
71-
.map(|id| Notified { id, notification }),
72-
None => None,
71+
.map(|id| Notified {
72+
user_id,
73+
id,
74+
notification,
75+
})
76+
.into_iter()
77+
.collect(),
78+
None => Vec::new(),
7379
};
74-
C::fan_out(self, id, subject, &event, notified.as_ref()).await?;
80+
C::fan_out(self, id, subject, &event, &notified).await?;
7581
self.recorded.push(Committed {
7682
id,
7783
actor: HistoryActor::from(&context.actor),

0 commit comments

Comments
 (0)