Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 33 additions & 23 deletions apps/auth-service/src/emails/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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,
Expand Down
31 changes: 17 additions & 14 deletions apps/auth-service/src/history/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,26 @@ impl HistoryItem {
user: UserId,
event_id: EventId,
) -> Result<Option<Self>, 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<Option<StoredEvent>, 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())
}
}

Expand Down Expand Up @@ -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
Expand Down
36 changes: 19 additions & 17 deletions apps/auth-service/src/pictures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -154,16 +154,9 @@ impl AccountService {
crop: Option<Crop>,
) -> Result<PictureView, AccountError> {
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())
}
Expand Down Expand Up @@ -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<StoredPicture, DatabaseError> {
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,
Expand Down