1
0
Fork 0
mirror of https://codeberg.org/tyy/aspm synced 2024-12-22 15:59:29 -07:00

Fix empty claim imports and support transaction rollback if claims insert fails

This commit is contained in:
Tyler Beckman 2024-03-10 22:22:42 -06:00
parent 00ffbdce8d
commit 6559f676c3
Signed by: Ty
GPG key ID: 2813440C772555A4

View file

@ -6,7 +6,7 @@ use asp::{
utils::jwt::JwtSerialize,
};
use clap::Parser;
use sea_orm::{EntityTrait, IntoActiveValue, SqlErr};
use sea_orm::{EntityTrait, IntoActiveValue, SqlErr, TransactionTrait as _};
use crate::{
commands::AspmSubcommand,
@ -59,6 +59,8 @@ impl AspmSubcommand for ProfilesImportCommand {
let (key, profile) = AriadneSignatureProfile::decode_and_verify(&profile, fingerprint)
.context("The provided or fetched profile was invalid and unable to be imported")?;
let txn = state.db.begin().await?;
match Profiles::insert(profiles::ActiveModel {
alias: self.alias.join(" ").into_active_value(),
key: key.fingerprint.clone().into_active_value(),
@ -77,7 +79,7 @@ impl AspmSubcommand for ProfilesImportCommand {
.map(|color| color.to_string())
.into_active_value(),
})
.exec(&state.db)
.exec(&txn)
.await
{
Ok(_) => {
@ -86,10 +88,13 @@ impl AspmSubcommand for ProfilesImportCommand {
uri: claim.into_active_value(),
..Default::default()
}))
.exec(&state.db)
.on_empty_do_nothing()
.exec(&txn)
.await
// No rollback needed here as an error will cause txn to be dropped, which causes a rollback
.context("Unable to insert claims into database")?;
txn.commit().await.context("Unable to commit transaction")?;
println!(
"Successfully imported profile with fingerprint {}",
key.fingerprint