From 1d16fb45a52e8593e1dd8174dacc46f04b8858a7 Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 12 Feb 2023 23:15:28 +0000 Subject: [PATCH] Change /api/v1/{status_id}/context response format to match Mastodon API --- CHANGELOG.md | 1 + docs/openapi.yaml | 25 +++++++++++++++++++++++++ src/mastodon_api/statuses/types.rs | 6 ++++++ src/mastodon_api/statuses/views.rs | 18 +++++++++++++++++- 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c2f0d0..e762ece 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - 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. ## [1.13.1] - 2023-02-09 diff --git a/docs/openapi.yaml b/docs/openapi.yaml index c5c8a04..f97dfe7 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -943,6 +943,31 @@ paths: description: Post does not belong to user 404: description: Post not found + /api/v1/statuses/{status_id}/context: + get: + summary: View statuses above and below this status in the thread. + parameters: + - $ref: '#/components/parameters/status_id' + responses: + 200: + description: Successful operation. + content: + application/json: + schema: + type: object + properties: + ancestors: + description: Parents in the thread. + type: array + items: + $ref: '#/components/schemas/Status' + descendants: + description: Parents in the thread. + type: array + items: + $ref: '#/components/schemas/Status' + 404: + description: Post not found /api/v1/statuses/{status_id}/thread: get: summary: Get thread that contains given post. diff --git a/src/mastodon_api/statuses/types.rs b/src/mastodon_api/statuses/types.rs index 598c50a..fe574e6 100644 --- a/src/mastodon_api/statuses/types.rs +++ b/src/mastodon_api/statuses/types.rs @@ -188,6 +188,12 @@ impl StatusPreview { } } +#[derive(Serialize)] +pub struct Context { + pub ancestors: Vec, + pub descendants: Vec, +} + #[derive(Deserialize)] pub struct TransactionData { pub transaction_id: String, diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index 96001b3..28c395c 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -56,6 +56,7 @@ use super::helpers::{ PostContent, }; use super::types::{ + Context, Status, StatusData, StatusPreview, @@ -302,7 +303,22 @@ async fn get_context( maybe_current_user.as_ref(), posts, ).await?; - Ok(HttpResponse::Ok().json(statuses)) + let mut ancestors = vec![]; + let mut descendants = vec![]; + let mut is_ancestor = true; + for status in statuses { + if is_ancestor { + if status.id == *status_id { + is_ancestor = false; + continue; + }; + ancestors.push(status); + } else { + descendants.push(status); + }; + }; + let context = Context { ancestors, descendants }; + Ok(HttpResponse::Ok().json(context)) } #[get("/{status_id}/thread")]