Allow to set visibility of new posts

This commit is contained in:
silverpill 2022-01-07 20:41:46 +00:00
parent bce194ebda
commit 5bdee5585a
3 changed files with 69 additions and 7 deletions

View file

@ -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

View file

@ -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<Vec<Uuid>>,
pub in_reply_to_id: Option<Uuid>,
pub visibility: Option<String>,
}
impl From<StatusData> for PostCreateData {
impl TryFrom<StatusData> for PostCreateData {
fn from(value: StatusData) -> Self {
Self {
type Error = ValidationError;
fn try_from(value: StatusData) -> Result<Self, Self::Error> {
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)
}
}

View file

@ -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(