Remember CIDs of media attachments after adding them to IPFS

This commit is contained in:
silverpill 2021-09-28 21:44:31 +00:00
parent def4ac69dc
commit f87284b07c
5 changed files with 24 additions and 0 deletions

View file

@ -0,0 +1 @@
ALTER TABLE media_attachment ADD COLUMN ipfs_cid VARCHAR(200);

View file

@ -60,6 +60,7 @@ CREATE TABLE media_attachment (
owner_id UUID NOT NULL REFERENCES actor_profile (id) ON DELETE CASCADE,
media_type VARCHAR(50),
file_name VARCHAR(200) NOT NULL,
ipfs_cid VARCHAR(200),
post_id UUID REFERENCES post (id) ON DELETE CASCADE,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
)

View file

@ -13,6 +13,7 @@ use crate::ethereum::nft::create_mint_signature;
use crate::ipfs::store as ipfs_store;
use crate::ipfs::utils::{IPFS_LOGO, get_ipfs_url};
use crate::mastodon_api::users::auth::get_current_user;
use crate::models::attachments::queries::set_attachment_ipfs_cid;
use crate::models::profiles::queries::get_followers;
use crate::models::posts::queries::{
create_post,
@ -114,6 +115,7 @@ async fn make_permanent(
.map_err(|_| HttpError::InternalError)?;
let image_cid = ipfs_store::add(&ipfs_api_url, image_data).await
.map_err(|_| HttpError::InternalError)?;
set_attachment_ipfs_cid(db_client, &attachment.id, &image_cid).await?;
image_cid
} else {
// Use IPFS logo if there's no image

View file

@ -47,3 +47,22 @@ pub async fn find_orphaned_files(
.collect::<Result<_, _>>()?;
Ok(orphaned_files)
}
pub async fn set_attachment_ipfs_cid(
db_client: &impl GenericClient,
attachment_id: &Uuid,
ipfs_cid: &str,
) -> Result<DbMediaAttachment, DatabaseError> {
let maybe_row = db_client.query_opt(
"
UPDATE media_attachment
SET ipfs_cid = $1
WHERE id = $2
RETURNING media_attachment
",
&[&ipfs_cid, &attachment_id],
).await?;
let row = maybe_row.ok_or(DatabaseError::NotFound("attachment"))?;
let db_attachment = row.try_get("media_attachment")?;
Ok(db_attachment)
}

View file

@ -9,6 +9,7 @@ pub struct DbMediaAttachment {
pub owner_id: Uuid,
pub media_type: Option<String>,
pub file_name: String,
pub ipfs_cid: Option<String>,
pub post_id: Option<Uuid>,
pub created_at: DateTime<Utc>,
}