Add optional content_type parameter to /api/v1/status data and set default to text/html
This commit is contained in:
parent
03ebf75da2
commit
33b04c9bdd
2 changed files with 46 additions and 8 deletions
|
@ -504,8 +504,12 @@ paths:
|
||||||
status:
|
status:
|
||||||
description: |
|
description: |
|
||||||
Text content of the post.
|
Text content of the post.
|
||||||
Allowed HTML tags: a, br, pre, code.
|
Allowed HTML tags: a, br, pre, code, strong, em, p.
|
||||||
type: string
|
type: string
|
||||||
|
content_type:
|
||||||
|
description: Media type of the post content.
|
||||||
|
type: string
|
||||||
|
default: text/html
|
||||||
'media_ids[]':
|
'media_ids[]':
|
||||||
description: Array of Attachment ids to be attached as media.
|
description: Array of Attachment ids to be attached as media.
|
||||||
type: array
|
type: array
|
||||||
|
|
|
@ -133,6 +133,8 @@ impl Status {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_post_content_type() -> String { "text/html".to_string() }
|
||||||
|
|
||||||
/// https://docs.joinmastodon.org/methods/statuses/
|
/// https://docs.joinmastodon.org/methods/statuses/
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct StatusData {
|
pub struct StatusData {
|
||||||
|
@ -147,14 +149,17 @@ pub struct StatusData {
|
||||||
// Not supported by Mastodon
|
// Not supported by Mastodon
|
||||||
pub mentions: Option<Vec<Uuid>>,
|
pub mentions: Option<Vec<Uuid>>,
|
||||||
pub links: Option<Vec<Uuid>>,
|
pub links: Option<Vec<Uuid>>,
|
||||||
|
|
||||||
|
#[serde(default = "default_post_content_type")]
|
||||||
|
pub content_type: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<StatusData> for PostCreateData {
|
impl TryFrom<StatusData> for PostCreateData {
|
||||||
|
|
||||||
type Error = ValidationError;
|
type Error = ValidationError;
|
||||||
|
|
||||||
fn try_from(value: StatusData) -> Result<Self, Self::Error> {
|
fn try_from(status_data: StatusData) -> Result<Self, Self::Error> {
|
||||||
let visibility = match value.visibility.as_deref() {
|
let visibility = match status_data.visibility.as_deref() {
|
||||||
Some("public") => Visibility::Public,
|
Some("public") => Visibility::Public,
|
||||||
Some("direct") => Visibility::Direct,
|
Some("direct") => Visibility::Direct,
|
||||||
Some("private") => Visibility::Followers,
|
Some("private") => Visibility::Followers,
|
||||||
|
@ -162,15 +167,19 @@ impl TryFrom<StatusData> for PostCreateData {
|
||||||
Some(_) => return Err(ValidationError("invalid visibility parameter")),
|
Some(_) => return Err(ValidationError("invalid visibility parameter")),
|
||||||
None => Visibility::Public,
|
None => Visibility::Public,
|
||||||
};
|
};
|
||||||
|
let content = match status_data.content_type.as_str() {
|
||||||
|
"text/html" => status_data.status,
|
||||||
|
_ => return Err(ValidationError("unsupported content type")),
|
||||||
|
};
|
||||||
let post_data = Self {
|
let post_data = Self {
|
||||||
content: value.status,
|
content: content,
|
||||||
in_reply_to_id: value.in_reply_to_id,
|
in_reply_to_id: status_data.in_reply_to_id,
|
||||||
repost_of_id: None,
|
repost_of_id: None,
|
||||||
visibility: visibility,
|
visibility: visibility,
|
||||||
attachments: value.media_ids.unwrap_or(vec![]),
|
attachments: status_data.media_ids.unwrap_or(vec![]),
|
||||||
mentions: value.mentions.unwrap_or(vec![]),
|
mentions: status_data.mentions.unwrap_or(vec![]),
|
||||||
tags: vec![],
|
tags: vec![],
|
||||||
links: value.links.unwrap_or(vec![]),
|
links: status_data.links.unwrap_or(vec![]),
|
||||||
object_id: None,
|
object_id: None,
|
||||||
created_at: Utc::now(),
|
created_at: Utc::now(),
|
||||||
};
|
};
|
||||||
|
@ -182,3 +191,28 @@ impl TryFrom<StatusData> for PostCreateData {
|
||||||
pub struct TransactionData {
|
pub struct TransactionData {
|
||||||
pub transaction_id: String,
|
pub transaction_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_status_data_into_post_data() {
|
||||||
|
let status_content = "<p>test</p>";
|
||||||
|
let status_data = StatusData {
|
||||||
|
status: status_content.to_string(),
|
||||||
|
media_ids: None,
|
||||||
|
in_reply_to_id: None,
|
||||||
|
visibility: Some("public".to_string()),
|
||||||
|
mentions: None,
|
||||||
|
links: None,
|
||||||
|
content_type: "text/html".to_string(),
|
||||||
|
};
|
||||||
|
let post_data = PostCreateData::try_from(status_data).unwrap();
|
||||||
|
assert_eq!(post_data.content, status_content);
|
||||||
|
assert_eq!(post_data.visibility, Visibility::Public);
|
||||||
|
assert_eq!(post_data.attachments, vec![]);
|
||||||
|
assert_eq!(post_data.mentions, vec![]);
|
||||||
|
assert_eq!(post_data.links, vec![]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue