From 057cac1ac700f35c053170d18e06a7b306711006 Mon Sep 17 00:00:00 2001 From: silverpill Date: Fri, 3 Dec 2021 18:13:20 +0000 Subject: [PATCH] Return error if post is already saved to IPFS --- docs/openapi.yaml | 12 ++++++++++++ src/errors.rs | 4 ++++ src/mastodon_api/statuses/views.rs | 9 ++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 33a0f6f..c1e8a5c 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -17,8 +17,14 @@ paths: application/json: schema: $ref: '#/components/schemas/Status' + 403: + description: Post does not belong to user or is not public 404: description: Post not found + 418: + description: IPFS integration is not enabled + 422: + description: Post already saved to IPFS /api/v1/{status_id}/signature: get: summary: Sign post data with instance key @@ -42,8 +48,14 @@ paths: s: type: string example: '6a5cb313907cd3...' + 403: + description: Post does not belong to user or is not public 404: description: Post not found + 418: + description: Ethereum integration is not enabled + 422: + description: Post is not saved to IPFS components: parameters: diff --git a/src/errors.rs b/src/errors.rs index 583e3ab..402e803 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -58,6 +58,9 @@ pub enum HttpError { #[error("operation not supported")] NotSupported, + #[error("{0}")] + OperationError(&'static str), + #[error("internal error")] InternalError, } @@ -94,6 +97,7 @@ impl ResponseError for HttpError { HttpError::PermissionError => StatusCode::FORBIDDEN, HttpError::NotFoundError(_) => StatusCode::NOT_FOUND, HttpError::NotSupported => StatusCode::IM_A_TEAPOT, + HttpError::OperationError(_) => StatusCode::UNPROCESSABLE_ENTITY, _ => StatusCode::INTERNAL_SERVER_ERROR, } } diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index 30009f7..9b75ce3 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -319,9 +319,12 @@ async fn make_permanent( let db_client = &**get_database_client(&db_pool).await?; let current_user = get_current_user(db_client, auth.token()).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() { // 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() .ok_or(HttpError::NotSupported)?; @@ -378,11 +381,11 @@ async fn get_signature( let post = get_post_by_id(db_client, &status_id).await?; if post.author.id != current_user.id || !post.is_public() { // Users can only tokenize their own public posts - return Err(HttpError::NotFoundError("post")); + return Err(HttpError::PermissionError); }; let ipfs_cid = post.ipfs_cid // 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 signature = create_mint_signature( contract_config,