This commit is contained in:
viridian 2024-02-11 18:24:46 +01:00
parent a2ef2a2b31
commit 179264ee90
Signed by: viridian
GPG key ID: DCD4DF95CE23FE8C

View file

@ -1,5 +1,5 @@
use serde_json;
use serde::{Deserialize, Serialize};
use serde_json;
#[derive(Serialize, Deserialize, Debug)]
pub struct Actor {
@ -23,7 +23,6 @@ pub struct Icon {
#[serde(rename = "mediaType")]
media_mime_type: String,
url: String,
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub enum ObjectType {
@ -65,8 +64,15 @@ pub fn parse_activity(input: &String)->Result<Object,String> {
let json_input: Result<Activity, _> = serde_json::from_str(input);
if json_input.is_ok() {
match json_input.as_ref().unwrap().activity_type {
ObjectType::Person => { return Ok(Object::Person(json_to_actor(input)));},
_ => { return Err(format!("Activity type {} not implemented",json_input.unwrap().activity_type)); }
ObjectType::Person => {
return Ok(Object::Person(json_to_actor(input)));
}
_ => {
return Err(format!(
"Activity type {} not implemented",
json_input.unwrap().activity_type
));
}
}
} else {
Err("Failed to parse json data".to_string())
@ -87,13 +93,27 @@ mod tests {
assert_eq!(actor.object_type, ObjectType::Person);
assert_eq!(actor.name, "Sebastian Jambor".to_string());
assert_eq!(actor.preferred_username, "crepels");
assert_eq!(actor.following,"https://mastodon.social/users/crepels/following".to_string());
assert_eq!(actor.followers,"https://mastodon.social/users/crepels/followers".to_string());
assert_eq!(actor.inbox,"https://mastodon.social/users/crepels/inbox".to_string());
assert_eq!(actor.outbox,"https://mastodon.social/users/crepels/outbox".to_string());
assert_eq!(actor.summary,Some("<p>I created a systemd playground to help people learn systemd.</p>".to_string()));
assert_eq!(
actor.following,
"https://mastodon.social/users/crepels/following".to_string()
);
assert_eq!(
actor.followers,
"https://mastodon.social/users/crepels/followers".to_string()
);
assert_eq!(
actor.inbox,
"https://mastodon.social/users/crepels/inbox".to_string()
);
assert_eq!(
actor.outbox,
"https://mastodon.social/users/crepels/outbox".to_string()
);
assert_eq!(
actor.summary,
Some("<p>I created a systemd playground to help people learn systemd.</p>".to_string())
);
assert_eq!(actor.icon,Some(Icon{media_type: "Image".to_string(), media_mime_type: "image/png".to_string(), url: "https://files.mastodon.social/accounts/avatars/108/227/485/389/961/502/original/1213d525278ae01d.png".to_string()}))
}
#[test]