From 5bdee5585a6f317fbd22cba372745a6d10c7ca04 Mon Sep 17 00:00:00 2001 From: silverpill Date: Fri, 7 Jan 2022 20:41:46 +0000 Subject: [PATCH] Allow to set visibility of new posts --- docs/openapi.yaml | 49 +++++++++++++++++++++++++++++- src/mastodon_api/statuses/types.rs | 23 +++++++++++--- src/mastodon_api/statuses/views.rs | 4 ++- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 1b229e1..b9a3dba 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -236,6 +236,47 @@ paths: $ref: '#/components/schemas/Relationship' 404: description: Profile not found + /api/v1/statuses: + post: + summary: Create new post. + security: + - tokenAuth: [] + requestBody: + content: + application/json: + schema: + type: object + properties: + status: + description: Text content of the post. + type: string + 'media_ids[]': + description: Array of Attachment ids to be attached as media. + type: array + items: + type: string + format: uuid + in_reply_to_id: + description: ID of the post being replied to, if post is a reply. + type: string + format: uuid + visibility: + description: Visibility of the post. + type: string + enum: + - public + - direct + required: + - status + responses: + 201: + description: Post created + content: + application/json: + schema: + $ref: '#/components/schemas/Status' + 400: + description: Invalid post data /api/v1/statuses/{status_id}: delete: summary: Delete post @@ -426,8 +467,14 @@ components: type: string format: uuid content: - description: HTML-encoded status content. + description: HTML-encoded post content. type: string + visibility: + description: Visibility of this post. + type: string + enum: + - public + - direct tags: description: Hashtags used within the status content. type: array diff --git a/src/mastodon_api/statuses/types.rs b/src/mastodon_api/statuses/types.rs index 6eace68..df1998f 100644 --- a/src/mastodon_api/statuses/types.rs +++ b/src/mastodon_api/statuses/types.rs @@ -1,7 +1,10 @@ +use std::convert::TryFrom; + use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use uuid::Uuid; +use crate::errors::ValidationError; use crate::mastodon_api::accounts::types::Account; use crate::mastodon_api::media::types::Attachment; use crate::models::posts::types::{Post, PostCreateData, Visibility}; @@ -128,22 +131,32 @@ pub struct StatusData { pub media_ids: Option>, pub in_reply_to_id: Option, + pub visibility: Option, } -impl From for PostCreateData { +impl TryFrom for PostCreateData { - fn from(value: StatusData) -> Self { - Self { + type Error = ValidationError; + + fn try_from(value: StatusData) -> Result { + let visibility = match value.visibility.as_deref() { + Some("public") => Visibility::Public, + Some("direct") => Visibility::Direct, + Some(_) => return Err(ValidationError("invalid visibility parameter")), + None => Visibility::Public, + }; + let post_data = Self { content: value.status, in_reply_to_id: value.in_reply_to_id, repost_of_id: None, - visibility: Visibility::Public, + visibility: visibility, attachments: value.media_ids.unwrap_or(vec![]), mentions: vec![], tags: vec![], object_id: None, created_at: None, - } + }; + Ok(post_data) } } diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index d1c64a1..fc0aa51 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -1,4 +1,6 @@ /// https://docs.joinmastodon.org/methods/statuses/ +use std::convert::TryFrom; + use actix_web::{delete, get, post, web, HttpResponse, Scope}; use actix_web_httpauth::extractors::bearer::BearerAuth; use serde::Serialize; @@ -60,7 +62,7 @@ async fn create_status( let db_client = &mut **get_database_client(&db_pool).await?; let current_user = get_current_user(db_client, auth.token()).await?; let instance = config.instance(); - let mut post_data = PostCreateData::from(data.into_inner()); + let mut post_data = PostCreateData::try_from(data.into_inner())?; post_data.clean()?; // Mentions let mention_map = find_mentioned_profiles(