fedimovies/src/mastodon_api/statuses/types.rs

126 lines
3.8 KiB
Rust
Raw Normal View History

2021-04-09 00:22:17 +00:00
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::mastodon_api::accounts::types::Account;
use crate::mastodon_api::media::types::Attachment;
use crate::models::posts::types::{Post, PostCreateData, Visibility};
2021-11-11 19:10:28 +00:00
use crate::models::profiles::types::DbActorProfile;
/// https://docs.joinmastodon.org/entities/mention/
#[derive(Serialize)]
pub struct Mention {
id: String,
username: String,
acct: String,
url: String,
}
impl Mention {
fn from_profile(profile: DbActorProfile, instance_url: &str) -> Self {
Mention {
id: profile.id.to_string(),
username: profile.username.clone(),
acct: profile.acct.clone(),
url: profile.actor_id(instance_url).unwrap(),
}
}
}
2021-04-09 00:22:17 +00:00
/// https://docs.joinmastodon.org/entities/status/
#[derive(Serialize)]
pub struct Status {
pub id: Uuid,
2021-11-21 15:08:20 +00:00
pub uri: String,
2021-04-09 00:22:17 +00:00
pub created_at: DateTime<Utc>,
pub account: Account,
pub content: String,
2021-09-22 16:32:44 +00:00
pub in_reply_to_id: Option<Uuid>,
pub reblog: Option<Box<Status>>,
pub visibility: String,
pub replies_count: i32,
pub favourites_count: i32,
pub reblogs_count: i32,
2021-04-09 00:22:17 +00:00
pub media_attachments: Vec<Attachment>,
2021-11-11 19:10:28 +00:00
mentions: Vec<Mention>,
2021-04-09 00:22:17 +00:00
// Authorized user attributes
pub favourited: bool,
pub reblogged: bool,
2021-04-09 00:22:17 +00:00
// Extra fields
pub ipfs_cid: Option<String>,
pub token_id: Option<i32>,
pub token_tx_id: Option<String>,
}
impl Status {
pub fn from_post(post: Post, instance_url: &str) -> Self {
2021-11-21 15:08:20 +00:00
let object_id = post.get_object_id(instance_url);
2021-04-09 00:22:17 +00:00
let attachments: Vec<Attachment> = post.attachments.into_iter()
.map(|item| Attachment::from_db(item, instance_url))
.collect();
2021-11-11 19:10:28 +00:00
let mentions: Vec<Mention> = post.mentions.into_iter()
.map(|item| Mention::from_profile(item, instance_url))
.collect();
2021-04-09 00:22:17 +00:00
let account = Account::from_profile(post.author, instance_url);
let reblog = if let Some(repost_of) = post.repost_of {
let status = Status::from_post(*repost_of, instance_url);
Some(Box::new(status))
} else {
None
};
let visibility = match post.visibility {
Visibility::Public => "public",
Visibility::Direct => "direct",
};
2021-04-09 00:22:17 +00:00
Self {
id: post.id,
2021-11-21 15:08:20 +00:00
uri: object_id,
2021-04-09 00:22:17 +00:00
created_at: post.created_at,
account: account,
content: post.content,
2021-09-22 16:32:44 +00:00
in_reply_to_id: post.in_reply_to_id,
reblog: reblog,
visibility: visibility.to_string(),
replies_count: post.reply_count,
favourites_count: post.reaction_count,
reblogs_count: post.repost_count,
2021-04-09 00:22:17 +00:00
media_attachments: attachments,
2021-11-11 19:10:28 +00:00
mentions: mentions,
favourited: post.actions.as_ref().map_or(false, |actions| actions.favourited),
reblogged: post.actions.as_ref().map_or(false, |actions| actions.reposted),
2021-04-09 00:22:17 +00:00
ipfs_cid: post.ipfs_cid,
token_id: post.token_id,
token_tx_id: post.token_tx_id,
}
}
}
/// https://docs.joinmastodon.org/methods/statuses/
#[derive(Deserialize)]
pub struct StatusData {
pub status: String,
#[serde(rename = "media_ids[]")]
pub media_ids: Option<Vec<Uuid>>,
2021-09-22 16:32:44 +00:00
pub in_reply_to_id: Option<Uuid>,
2021-04-09 00:22:17 +00:00
}
impl From<StatusData> for PostCreateData {
fn from(value: StatusData) -> Self {
Self {
content: value.status,
2021-09-22 16:32:44 +00:00
in_reply_to_id: value.in_reply_to_id,
repost_of_id: None,
visibility: Visibility::Public,
2021-04-09 00:22:17 +00:00
attachments: value.media_ids.unwrap_or(vec![]),
2021-11-11 19:10:28 +00:00
mentions: vec![],
2021-10-08 22:39:40 +00:00
object_id: None,
2021-04-09 00:22:17 +00:00
created_at: None,
}
}
}