You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
2.8 KiB
Rust

use std::str::FromStr;
use serde::Deserialize;
#[derive(Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Outbox {
pub total_items: u32,
pub ordered_items: Vec<Item>,
}
#[derive(Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Person {
pub id: String,
pub preferred_username: String,
pub name: String,
pub summary: String,
pub url: String,
#[serde(rename = "attachment")]
pub attachments: Option<Vec<Attachment>>,
}
impl Person {
pub fn full_username(&self) -> Option<String> {
let url = &self.url;
let domain_separator = url.find("//")?;
if domain_separator + 2 >= url.len() {
return None;
}
let mut parts = url.get((domain_separator + 2)..)?.split("/");
let domain = parts.next()?;
let user = parts.next()?;
if parts.count() > 0 {
return None;
}
let mut username = String::new();
username.push_str(user);
username.push('@');
username.push_str(domain);
Some(username)
}
}
#[derive(Deserialize, Debug, PartialEq, Clone)]
#[serde(tag = "type")]
pub enum Item {
#[serde(rename = "Create")]
Post {
#[serde(flatten)]
meta: CommonFields,
object: Post,
},
#[serde(rename = "Announce")]
Boost {
#[serde(flatten)]
meta: CommonFields,
object: String,
},
}
#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct CommonFields {
pub id: String,
pub actor: String,
pub published: String,
pub to: Vec<String>,
pub cc: Vec<String>,
}
#[derive(Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Post {
pub summary: Option<String>,
pub in_reply_to: Option<String>,
pub url: String,
pub sensitive: bool,
pub content: String,
#[serde(rename = "attachment")]
pub attachments: Option<Vec<Attachment>>,
pub tag: Option<Vec<Tag>>,
pub replies: Option<Collection>,
pub quote_uri: Option<String>,
}
#[derive(Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all = "camelCase", tag = "type")]
pub enum Attachment {
#[serde(rename = "Document", rename_all = "camelCase")]
File {
media_type: Option<String>,
url: String,
name: Option<String>,
focal_point: Option<[f32; 2]>,
width: Option<u32>,
height: Option<u32>,
},
#[serde(rename = "PropertyValue")]
Property { name: String, value: String },
}
#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct Tag {
href: String,
name: String,
}
#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct Collection {
first: CollectionPage,
}
#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct CollectionPage {
items: Vec<String>,
}