Make /api/v1/statuses endpoint compatible with Mastodon clients

This commit is contained in:
silverpill 2023-02-19 16:50:02 +00:00
parent e8ea52adba
commit 21054de712
5 changed files with 15 additions and 14 deletions

View file

@ -12,13 +12,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Added OAuth authorization page.
- Support `authorization_code` OAuth grant type.
- Documented `http_cors_allowlist` configuration parameter.
- Added `/api/v1/{status_id}/thread` API endpoint (replaces `/api/v1/{status_id}/context`).
- Added `/api/v1/statuses/{status_id}/thread` API endpoint (replaces `/api/v1/statuses/{status_id}/context`).
### Changed
- Allow `instance_uri` configuration value to contain URI scheme.
- Changed `Content-Security-Policy` header value in nginx config examples.
- Changed `/api/v1/{status_id}/context` response format to match Mastodon API.
- Changed `/api/v1/statuses/{status_id}/context` response format to match Mastodon API.
- Changed status code of `/api/v1/statuses` response to 200 to match Mastodon API.
## [1.13.1] - 2023-02-09

View file

@ -886,7 +886,7 @@ paths:
required:
- status
responses:
201:
200:
description: Post created
content:
application/json:

View file

@ -4,12 +4,16 @@ use actix_web::{
error::{Error, JsonPayloadError},
http::StatusCode,
middleware::{ErrorHandlerResponse, ErrorHandlers},
web::{Form, Json},
Either,
HttpRequest,
};
use serde_json::json;
use crate::errors::HttpError;
pub type FormOrJson<T> = Either<Form<T>, Json<T>>;
/// Error handler for 401 Unauthorized
pub fn create_auth_error_handler<B: MessageBody + 'static>() -> ErrorHandlers<B> {
// Creates and returns actix middleware
@ -30,6 +34,7 @@ pub fn create_auth_error_handler<B: MessageBody + 'static>() -> ErrorHandlers<B>
})
}
/// Convert JSON payload deserialization errors into validation errors
pub fn json_error_handler(
error: JsonPayloadError,
_: &HttpRequest,

View file

@ -3,7 +3,6 @@ use actix_web::{
http::header as http_header,
post,
web,
Either,
HttpResponse,
Scope as ActixScope,
};
@ -19,6 +18,7 @@ use crate::ethereum::{
eip4361::verify_eip4361_signature,
utils::validate_ethereum_address,
};
use crate::http::FormOrJson;
use crate::models::{
oauth::queries::{
create_oauth_authorization,
@ -115,15 +115,9 @@ const ACCESS_TOKEN_EXPIRES_IN: i64 = 86400 * 7;
async fn token_view(
config: web::Data<Config>,
db_pool: web::Data<DbPool>,
request_data: Either<
web::Json<TokenRequest>,
web::Form<TokenRequest>,
>,
request_data: FormOrJson<TokenRequest>,
) -> Result<HttpResponse, HttpError> {
let request_data = match request_data {
Either::Left(json) => json.into_inner(),
Either::Right(form) => form.into_inner(),
};
let request_data = request_data.into_inner();
let db_client = &**get_database_client(&db_pool).await?;
let user = match request_data.grant_type.as_str() {
"authorization_code" => {

View file

@ -21,6 +21,7 @@ use crate::activitypub::builders::{
use crate::database::{get_database_client, DatabaseError, DbPool};
use crate::errors::{HttpError, ValidationError};
use crate::ethereum::nft::create_mint_signature;
use crate::http::FormOrJson;
use crate::ipfs::{
store as ipfs_store,
posts::PostMetadata,
@ -70,7 +71,7 @@ async fn create_status(
auth: BearerAuth,
config: web::Data<Config>,
db_pool: web::Data<DbPool>,
status_data: web::Json<StatusData>,
status_data: FormOrJson<StatusData>,
) -> Result<HttpResponse, HttpError> {
let db_client = &mut **get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?;
@ -190,7 +191,7 @@ async fn create_status(
.await?.enqueue(db_client).await?;
let status = Status::from_post(post, &instance.url());
Ok(HttpResponse::Created().json(status))
Ok(HttpResponse::Ok().json(status))
}
#[post("/preview")]