diff --git a/apps/auth-service/src/emails/mod.rs b/apps/auth-service/src/emails/mod.rs index 1a6058cd3..28aca5e66 100644 --- a/apps/auth-service/src/emails/mod.rs +++ b/apps/auth-service/src/emails/mod.rs @@ -238,6 +238,30 @@ impl EmailApplication { Ok(()) } + /// Records `EmailVerified` for `user_id`'s address `email_id` as + /// `context`'s, then `PrimaryEmailChanged` with no previous address if it + /// became the `primary` one, the account having had none. Shared by + /// `verify_email` and `Added::verified`. + async fn record_verified( + tx: &mut DatabaseTransaction<'_>, + context: &EventContext, + user_id: UserId, + email_id: EmailId, + primary: bool, + ) -> Result<(), DatabaseError> { + let subject = Some(user_id); + tx.record(context, subject, DomainEvent::EmailVerified { email_id }) + .await?; + if primary { + let event = DomainEvent::PrimaryEmailChanged { + email_id, + previous_email_id: None, + }; + tx.record(context, subject, event).await?; + } + Ok(()) + } + /// Refuses another address for an account that holds `count`. const fn room(count: usize) -> Result<(), EmailError> { if count >= MAX_EMAILS { @@ -402,18 +426,7 @@ impl Added<'_> { }) .await?; self.record(tx, id).await?; - let subject = Some(self.user_id); - tx.record( - self.context, - subject, - DomainEvent::EmailVerified { email_id: id }, - ) - .await?; - let primary = DomainEvent::PrimaryEmailChanged { - email_id: id, - previous_email_id: None, - }; - tx.record(self.context, subject, primary).await?; + EmailApplication::record_verified(tx, self.context, self.user_id, id, true).await?; Ok(()) } @@ -536,17 +549,14 @@ impl AccountService { Err(DatabaseError::Conflict(_)) => return Err(EmailError::OwnedElsewhere.into()), Err(error) => return Err(error.into()), }; - let email_id = pending.email_id; - let subject = Some(pending.user_id); - tx.record(&context, subject, DomainEvent::EmailVerified { email_id }) - .await?; - if primary { - let event = DomainEvent::PrimaryEmailChanged { - email_id, - previous_email_id: None, - }; - tx.record(&context, subject, event).await?; - } + EmailApplication::record_verified( + &mut tx, + &context, + pending.user_id, + pending.email_id, + primary, + ) + .await?; tx.commit().await?; Ok(Verified { address: pending.address, diff --git a/apps/auth-service/src/history/mod.rs b/apps/auth-service/src/history/mod.rs index fa3bb94f8..e2e4f9fbd 100644 --- a/apps/auth-service/src/history/mod.rs +++ b/apps/auth-service/src/history/mod.rs @@ -178,16 +178,26 @@ impl HistoryItem { user: UserId, event_id: EventId, ) -> Result, DatabaseError> { + let Some(stored) = Self::stored(database.events(), user, event_id).await? else { + return Ok(None); + }; + let clients = Self::client_names(database.oidc()).await?; + Ok(Self::new(&stored, &clients)) + } + + /// The event `event_id` of `user`'s, as recorded, if the account's history + /// still keeps it: what [`Self::find`] and a report look up. + async fn stored( + mut events: EventRepository<'_>, + user: UserId, + event_id: EventId, + ) -> Result, DatabaseError> { let filter = EventFilter { subject: Some(user), id: Some(event_id), ..EventFilter::default() }; - let Some(stored) = database.events().list(&filter, 1).await?.pop() else { - return Ok(None); - }; - let clients = Self::client_names(database.oidc()).await?; - Ok(Self::new(&stored, &clients)) + Ok(events.list(&filter, 1).await?.pop()) } } @@ -384,19 +394,12 @@ impl AccountService { /// The account `user`'s entry `event_id`, as recorded and as its history shows /// it. async fn entry( - mut events: EventRepository<'_>, + events: EventRepository<'_>, user: UserId, event_id: EventId, ) -> Result<(StoredEvent, HistoryEntry), AccountError> { - let filter = EventFilter { - subject: Some(user), - id: Some(event_id), - ..EventFilter::default() - }; - let stored = events - .list(&filter, 1) + let stored = HistoryItem::stored(events, user, event_id) .await? - .pop() .ok_or(HistoryError::EntryNotFound)?; let entry = stored .event diff --git a/apps/auth-service/src/pictures/mod.rs b/apps/auth-service/src/pictures/mod.rs index daa53e181..30a379f2f 100644 --- a/apps/auth-service/src/pictures/mod.rs +++ b/apps/auth-service/src/pictures/mod.rs @@ -37,7 +37,7 @@ use sourceorigin_common::{ PictureId, UserId, events::{DomainEvent, EventContext}, }; -use sourceorigin_repository::{Database, DatabaseError, StoredPicture}; +use sourceorigin_repository::{Database, DatabaseError, DatabaseTransaction, StoredPicture}; use std::time::Duration; use thiserror::Error; @@ -154,16 +154,9 @@ impl AccountService { crop: Option, ) -> Result { let jpeg = Jpeg::encode(turn, upload, crop).await?; - let id = PictureId::new(); let mut tx = self.database.begin().await?; owner.lock(&mut tx).await?; - let picture = tx.pictures().set(owner.user(), id, jpeg.as_bytes()).await?; - tx.record( - &owner.context(), - Some(owner.user()), - DomainEvent::PictureChanged { picture_id: id }, - ) - .await?; + let picture = Self::store_picture(&mut tx, &owner.context(), owner.user(), &jpeg).await?; tx.commit().await?; Ok(picture.into()) } @@ -202,17 +195,26 @@ impl AccountService { { return Ok(()); } - let id = PictureId::new(); - tx.pictures().set(user, id, jpeg.as_bytes()).await?; - tx.record( - context, - Some(user), - DomainEvent::PictureChanged { picture_id: id }, - ) - .await?; + Self::store_picture(&mut tx, context, user, &jpeg).await?; Ok(tx.commit().await?) } + /// Makes `jpeg` the picture of `user`, whom `tx` locked, under a new ID, and + /// records the change as `context`'s: the step setting and importing a + /// picture share. + async fn store_picture( + tx: &mut DatabaseTransaction<'_>, + context: &EventContext, + user: UserId, + jpeg: &Jpeg, + ) -> Result { + let id = PictureId::new(); + let picture = tx.pictures().set(user, id, jpeg.as_bytes()).await?; + let changed = DomainEvent::PictureChanged { picture_id: id }; + tx.record(context, Some(user), changed).await?; + Ok(picture) + } + /// The account's picture, if it has one. pub(crate) async fn picture( &self,