fedimovies/src/activitypub/types.rs

159 lines
3.7 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
2021-04-09 00:22:17 +00:00
use chrono::{DateTime, Utc};
2023-02-28 00:20:59 +00:00
use serde::{
Deserialize,
Deserializer,
Serialize,
de::{Error as DeserializerError},
};
use serde_json::Value;
use super::constants::{
AP_CONTEXT,
MITRA_CONTEXT,
W3ID_DATA_INTEGRITY_CONTEXT,
W3ID_SECURITY_CONTEXT,
};
2023-02-28 00:20:59 +00:00
use super::receiver::parse_property_value;
use super::vocabulary::HASHTAG;
2021-04-09 00:22:17 +00:00
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Attachment {
#[serde(rename = "type")]
pub attachment_type: String,
pub name: Option<String>,
pub media_type: Option<String>,
pub url: Option<String>,
2021-04-09 00:22:17 +00:00
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Link {
pub href: String,
}
2022-02-15 23:14:39 +00:00
fn default_tag_type() -> String { HASHTAG.to_string() }
2021-11-11 21:51:47 +00:00
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Tag {
pub name: Option<String>,
2021-11-11 21:51:47 +00:00
2022-02-15 23:14:39 +00:00
#[serde(rename = "type", default = "default_tag_type")]
2021-11-11 21:51:47 +00:00
pub tag_type: String,
pub href: Option<String>,
2021-11-11 21:51:47 +00:00
}
2023-01-15 01:46:54 +00:00
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SimpleTag {
#[serde(rename = "type")]
pub tag_type: String,
pub href: String,
pub name: String,
}
/// https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-e232.md
#[derive(Deserialize, Serialize)]
2023-01-15 01:46:54 +00:00
#[serde(rename_all = "camelCase")]
pub struct LinkTag {
#[serde(rename = "type")]
pub tag_type: String,
pub href: String,
pub media_type: String,
pub name: Option<String>,
}
2023-01-21 00:23:15 +00:00
#[derive(Deserialize, Serialize)]
2023-01-14 21:51:25 +00:00
#[serde(rename_all = "camelCase")]
2023-01-20 01:11:29 +00:00
pub struct EmojiTagImage {
2023-01-14 21:51:25 +00:00
#[serde(rename = "type")]
2023-01-20 01:11:29 +00:00
pub object_type: String,
pub url: String,
pub media_type: Option<String>,
2023-01-14 21:51:25 +00:00
}
2023-01-21 00:23:15 +00:00
#[derive(Deserialize, Serialize)]
2023-01-14 21:51:25 +00:00
#[serde(rename_all = "camelCase")]
pub struct EmojiTag {
#[serde(rename = "type")]
2023-01-20 01:11:29 +00:00
pub tag_type: String,
pub icon: EmojiTagImage,
pub id: String,
pub name: String,
pub updated: DateTime<Utc>,
2023-01-14 21:51:25 +00:00
}
2023-03-03 22:08:38 +00:00
pub fn deserialize_value_array<'de, D>(
2023-02-28 00:20:59 +00:00
deserializer: D,
) -> Result<Vec<Value>, D::Error>
where D: Deserializer<'de>
{
let maybe_value: Option<Value> = Option::deserialize(deserializer)?;
let values = if let Some(value) = maybe_value {
parse_property_value(&value).map_err(DeserializerError::custom)?
} else {
vec![]
};
Ok(values)
}
#[derive(Deserialize)]
#[cfg_attr(test, derive(Default))]
2021-04-09 00:22:17 +00:00
#[serde(rename_all = "camelCase")]
pub struct Object {
// https://www.w3.org/TR/activitypub/#obj-id
// "id" and "type" are required properties
2021-04-09 00:22:17 +00:00
pub id: String,
#[serde(rename = "type")]
pub object_type: String,
pub name: Option<String>,
pub attachment: Option<Value>,
pub cc: Option<Value>,
pub media_type: Option<String>,
2021-04-09 00:22:17 +00:00
pub published: Option<DateTime<Utc>>,
pub attributed_to: Option<Value>,
2021-04-09 00:22:17 +00:00
pub in_reply_to: Option<String>,
pub content: Option<String>,
pub quote_url: Option<String>,
2023-04-15 23:43:19 +00:00
pub sensitive: Option<bool>,
2023-02-28 00:20:59 +00:00
#[serde(
default,
deserialize_with = "deserialize_value_array",
)]
pub tag: Vec<Value>,
2021-04-09 00:22:17 +00:00
pub to: Option<Value>,
2022-05-11 12:50:36 +00:00
pub updated: Option<DateTime<Utc>>,
pub url: Option<Value>,
2021-04-09 00:22:17 +00:00
}
pub type Context = (
&'static str,
&'static str,
&'static str,
HashMap<&'static str, &'static str>,
);
pub fn build_default_context() -> Context {
(
AP_CONTEXT,
W3ID_SECURITY_CONTEXT,
W3ID_DATA_INTEGRITY_CONTEXT,
HashMap::from([
("proofValue", "sec:proofValue"),
("proofPurpose", "sec:proofPurpose"),
("verificationMethod", "sec:verificationMethod"),
("mitra", MITRA_CONTEXT),
("MitraJcsRsaSignature2022", "mitra:MitraJcsRsaSignature2022"),
]),
)
}