use holo_hash::{ActionHash, EntryHash};
use sqlx::SqliteConnection;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RemoveCountersigningSessionOutcome {
Removed,
AlreadyPublished,
}
pub(crate) async fn remove_countersigning_session(
conn: &mut SqliteConnection,
action_hash: &ActionHash,
entry_hash: &EntryHash,
) -> sqlx::Result<RemoveCountersigningSessionOutcome> {
let published: i64 = sqlx::query_scalar(
"SELECT count(*) FROM ChainOp
JOIN ChainOpPublish ON ChainOpPublish.op_hash = ChainOp.hash
WHERE ChainOp.action_hash = ?1 AND ChainOpPublish.withhold_publish IS NULL",
)
.bind(action_hash.get_raw_36())
.fetch_one(&mut *conn)
.await?;
if published != 0 {
return Ok(RemoveCountersigningSessionOutcome::AlreadyPublished);
}
let author: Option<Vec<u8>> = sqlx::query_scalar("SELECT author FROM Action WHERE hash = ?1")
.bind(action_hash.get_raw_36())
.fetch_optional(&mut *conn)
.await?;
sqlx::query(
"DELETE FROM ChainOpPublish
WHERE op_hash IN (SELECT hash FROM ChainOp WHERE action_hash = ?1)",
)
.bind(action_hash.get_raw_36())
.execute(&mut *conn)
.await?;
sqlx::query("DELETE FROM ChainOp WHERE action_hash = ?1")
.bind(action_hash.get_raw_36())
.execute(&mut *conn)
.await?;
sqlx::query("DELETE FROM Action WHERE hash = ?1")
.bind(action_hash.get_raw_36())
.execute(&mut *conn)
.await?;
sqlx::query("DELETE FROM Entry WHERE hash = ?1")
.bind(entry_hash.get_raw_36())
.execute(&mut *conn)
.await?;
sqlx::query("DELETE FROM PrivateEntry WHERE hash = ?1 AND author = ?2")
.bind(entry_hash.get_raw_36())
.bind(author)
.execute(&mut *conn)
.await?;
Ok(RemoveCountersigningSessionOutcome::Removed)
}