Don't use update_post() to set ipfs_cid, token_id and token_tx_id fields

This commit is contained in:
silverpill 2022-05-10 18:27:26 +00:00
parent b7849c8264
commit 50699b5ab5
4 changed files with 74 additions and 10 deletions

View file

@ -17,8 +17,9 @@ use crate::errors::DatabaseError;
use crate::ipfs::utils::parse_ipfs_url;
use crate::models::posts::queries::{
get_post_by_ipfs_cid,
update_post,
get_token_waitlist,
set_post_token_id,
set_post_token_tx_id,
};
use super::errors::EthereumError;
use super::signatures::{sign_contract_call, CallArgs, SignatureData};
@ -110,7 +111,7 @@ pub async fn process_nft_events(
let tx_id = hex::encode(tx_id_h256.as_bytes());
let ipfs_cid = parse_ipfs_url(&token_uri)
.map_err(|_| EthereumError::TokenUriParsingError)?;
let mut post = match get_post_by_ipfs_cid(db_client, &ipfs_cid).await {
let post = match get_post_by_ipfs_cid(db_client, &ipfs_cid).await {
Ok(post) => post,
Err(DatabaseError::NotFound(_)) => {
// Post was deleted
@ -126,12 +127,11 @@ pub async fn process_nft_events(
log::info!("post {} was tokenized via {}", post.id, tx_id);
let token_id: i32 = token_id_u256.try_into()
.map_err(|_| EthereumError::ConversionError)?;
post.token_id = Some(token_id);
set_post_token_id(db_client, &post.id, token_id).await?;
if post.token_tx_id.as_ref() != Some(&tx_id) {
log::warn!("overwriting incorrect tx id {:?}", post.token_tx_id);
post.token_tx_id = Some(tx_id);
set_post_token_tx_id(db_client, &post.id, &tx_id).await?;
};
update_post(db_client, &post).await?;
token_waitlist_map.remove(&post.id);
};
};

View file

@ -31,7 +31,8 @@ use crate::models::posts::queries::{
get_post_by_id,
get_thread,
find_reposts_by_user,
update_post,
set_post_ipfs_cid,
set_post_token_tx_id,
delete_post,
};
use crate::models::posts::types::{PostCreateData, Visibility};
@ -431,8 +432,8 @@ async fn make_permanent(
.ok_or(HttpError::InternalError)?;
set_attachment_ipfs_cid(db_client, &attachment.id, image_cid).await?;
};
set_post_ipfs_cid(db_client, &post.id, &post_metadata_cid).await?;
post.ipfs_cid = Some(post_metadata_cid);
update_post(db_client, &post).await?;
let status = build_status(
db_client,
@ -490,8 +491,9 @@ async fn token_minted(
if post.author.id != current_user.id || !post.is_public() || post.repost_of_id.is_some() {
return Err(HttpError::PermissionError);
};
post.token_tx_id = Some(transaction_data.into_inner().transaction_id);
update_post(db_client, &post).await?;
let token_tx_id = transaction_data.into_inner().transaction_id;
set_post_token_tx_id(db_client, &post.id, &token_tx_id).await?;
post.token_tx_id = Some(token_tx_id);
let status = build_status(
db_client,

View file

@ -39,7 +39,7 @@ pub async fn set_attachment_ipfs_cid(
"
UPDATE media_attachment
SET ipfs_cid = $1
WHERE id = $2
WHERE id = $2 AND ipfs_cid IS NULL
RETURNING media_attachment
",
&[&ipfs_cid, &attachment_id],

View file

@ -744,6 +744,68 @@ pub async fn update_repost_count(
Ok(())
}
pub async fn set_post_ipfs_cid(
db_client: &impl GenericClient,
post_id: &Uuid,
ipfs_cid: &str,
) -> Result<(), DatabaseError> {
let updated_count = db_client.execute(
"
UPDATE post
SET ipfs_cid = $1
WHERE id = $2
AND repost_of_id IS NULL
AND ipfs_cid IS NULL
",
&[&ipfs_cid, &post_id],
).await?;
if updated_count == 0 {
return Err(DatabaseError::NotFound("post"));
};
Ok(())
}
pub async fn set_post_token_id(
db_client: &impl GenericClient,
post_id: &Uuid,
token_id: i32,
) -> Result<(), DatabaseError> {
let updated_count = db_client.execute(
"
UPDATE post
SET token_id = $1
WHERE id = $2
AND repost_of_id IS NULL
AND token_id IS NULL
",
&[&token_id, &post_id],
).await?;
if updated_count == 0 {
return Err(DatabaseError::NotFound("post"));
};
Ok(())
}
pub async fn set_post_token_tx_id(
db_client: &impl GenericClient,
post_id: &Uuid,
token_tx_id: &str,
) -> Result<(), DatabaseError> {
let updated_count = db_client.execute(
"
UPDATE post
SET token_tx_id = $1
WHERE id = $2
AND repost_of_id IS NULL
",
&[&token_tx_id, &post_id],
).await?;
if updated_count == 0 {
return Err(DatabaseError::NotFound("post"));
};
Ok(())
}
pub async fn get_post_author(
db_client: &impl GenericClient,
post_id: &Uuid,