Return error if post is already saved to IPFS

This commit is contained in:
silverpill 2021-12-03 18:13:20 +00:00
parent d668ee78d6
commit 057cac1ac7
3 changed files with 22 additions and 3 deletions

View file

@ -17,8 +17,14 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/Status' $ref: '#/components/schemas/Status'
403:
description: Post does not belong to user or is not public
404: 404:
description: Post not found description: Post not found
418:
description: IPFS integration is not enabled
422:
description: Post already saved to IPFS
/api/v1/{status_id}/signature: /api/v1/{status_id}/signature:
get: get:
summary: Sign post data with instance key summary: Sign post data with instance key
@ -42,8 +48,14 @@ paths:
s: s:
type: string type: string
example: '6a5cb313907cd3...' example: '6a5cb313907cd3...'
403:
description: Post does not belong to user or is not public
404: 404:
description: Post not found description: Post not found
418:
description: Ethereum integration is not enabled
422:
description: Post is not saved to IPFS
components: components:
parameters: parameters:

View file

@ -58,6 +58,9 @@ pub enum HttpError {
#[error("operation not supported")] #[error("operation not supported")]
NotSupported, NotSupported,
#[error("{0}")]
OperationError(&'static str),
#[error("internal error")] #[error("internal error")]
InternalError, InternalError,
} }
@ -94,6 +97,7 @@ impl ResponseError for HttpError {
HttpError::PermissionError => StatusCode::FORBIDDEN, HttpError::PermissionError => StatusCode::FORBIDDEN,
HttpError::NotFoundError(_) => StatusCode::NOT_FOUND, HttpError::NotFoundError(_) => StatusCode::NOT_FOUND,
HttpError::NotSupported => StatusCode::IM_A_TEAPOT, HttpError::NotSupported => StatusCode::IM_A_TEAPOT,
HttpError::OperationError(_) => StatusCode::UNPROCESSABLE_ENTITY,
_ => StatusCode::INTERNAL_SERVER_ERROR, _ => StatusCode::INTERNAL_SERVER_ERROR,
} }
} }

View file

@ -319,9 +319,12 @@ async fn make_permanent(
let db_client = &**get_database_client(&db_pool).await?; let db_client = &**get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?; let current_user = get_current_user(db_client, auth.token()).await?;
let mut post = get_post_by_id(db_client, &status_id).await?; let mut post = get_post_by_id(db_client, &status_id).await?;
if post.ipfs_cid.is_some() {
return Err(HttpError::OperationError("post already saved to IPFS"));
};
if post.author.id != current_user.id || !post.is_public() { if post.author.id != current_user.id || !post.is_public() {
// Users can only archive their own public posts // Users can only archive their own public posts
return Err(HttpError::NotFoundError("post")); return Err(HttpError::PermissionError);
}; };
let ipfs_api_url = config.ipfs_api_url.as_ref() let ipfs_api_url = config.ipfs_api_url.as_ref()
.ok_or(HttpError::NotSupported)?; .ok_or(HttpError::NotSupported)?;
@ -378,11 +381,11 @@ async fn get_signature(
let post = get_post_by_id(db_client, &status_id).await?; let post = get_post_by_id(db_client, &status_id).await?;
if post.author.id != current_user.id || !post.is_public() { if post.author.id != current_user.id || !post.is_public() {
// Users can only tokenize their own public posts // Users can only tokenize their own public posts
return Err(HttpError::NotFoundError("post")); return Err(HttpError::PermissionError);
}; };
let ipfs_cid = post.ipfs_cid let ipfs_cid = post.ipfs_cid
// Post metadata is not immutable // Post metadata is not immutable
.ok_or(HttpError::ValidationError("post is not immutable".into()))?; .ok_or(HttpError::OperationError("post is not immutable"))?;
let token_uri = get_ipfs_url(&ipfs_cid); let token_uri = get_ipfs_url(&ipfs_cid);
let signature = create_mint_signature( let signature = create_mint_signature(
contract_config, contract_config,