1
0
Fork 0
mirror of https://codeberg.org/tyy/aspm synced 2024-12-23 01:19:28 -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, utils::jwt::JwtSerialize,
}; };
use clap::Parser; use clap::Parser;
use sea_orm::{EntityTrait, IntoActiveValue, SqlErr}; use sea_orm::{EntityTrait, IntoActiveValue, SqlErr, TransactionTrait as _};
use crate::{ use crate::{
commands::AspmSubcommand, commands::AspmSubcommand,
@ -59,6 +59,8 @@ impl AspmSubcommand for ProfilesImportCommand {
let (key, profile) = AriadneSignatureProfile::decode_and_verify(&profile, fingerprint) let (key, profile) = AriadneSignatureProfile::decode_and_verify(&profile, fingerprint)
.context("The provided or fetched profile was invalid and unable to be imported")?; .context("The provided or fetched profile was invalid and unable to be imported")?;
let txn = state.db.begin().await?;
match Profiles::insert(profiles::ActiveModel { match Profiles::insert(profiles::ActiveModel {
alias: self.alias.join(" ").into_active_value(), alias: self.alias.join(" ").into_active_value(),
key: key.fingerprint.clone().into_active_value(), key: key.fingerprint.clone().into_active_value(),
@ -77,7 +79,7 @@ impl AspmSubcommand for ProfilesImportCommand {
.map(|color| color.to_string()) .map(|color| color.to_string())
.into_active_value(), .into_active_value(),
}) })
.exec(&state.db) .exec(&txn)
.await .await
{ {
Ok(_) => { Ok(_) => {
@ -86,10 +88,13 @@ impl AspmSubcommand for ProfilesImportCommand {
uri: claim.into_active_value(), uri: claim.into_active_value(),
..Default::default() ..Default::default()
})) }))
.exec(&state.db) .on_empty_do_nothing()
.exec(&txn)
.await .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")?; .context("Unable to insert claims into database")?;
txn.commit().await.context("Unable to commit transaction")?;
println!( println!(
"Successfully imported profile with fingerprint {}", "Successfully imported profile with fingerprint {}",
key.fingerprint key.fingerprint