From 51318046a8301eee6d978498ca2c9c118466d94c Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 4 Dec 2022 22:18:39 +0000 Subject: [PATCH] Create dedicated types for building Follow(), Accept(Follow) and Undo(Follow) activities --- src/activitypub/builders/accept_follow.rs | 45 ++++++++++++++-------- src/activitypub/builders/follow.rs | 46 ++++++++++++++--------- src/activitypub/builders/undo_follow.rs | 45 ++++++++++++++-------- 3 files changed, 89 insertions(+), 47 deletions(-) diff --git a/src/activitypub/builders/accept_follow.rs b/src/activitypub/builders/accept_follow.rs index a675128..74f576a 100644 --- a/src/activitypub/builders/accept_follow.rs +++ b/src/activitypub/builders/accept_follow.rs @@ -1,11 +1,12 @@ +use serde::Serialize; use serde_json::json; use crate::activitypub::{ - activity::{create_activity, Activity, Object}, + activity::Object, actors::types::Actor, constants::AP_CONTEXT, deliverer::OutgoingActivity, - identifiers::local_object_id, + identifiers::{local_actor_id, local_object_id}, vocabulary::{ACCEPT, FOLLOW}, }; use crate::config::Instance; @@ -13,12 +14,27 @@ use crate::models::profiles::types::DbActorProfile; use crate::models::users::types::User; use crate::utils::id::new_uuid; +#[derive(Serialize)] +struct AcceptFollow { + #[serde(rename = "@context")] + context: String, + + #[serde(rename = "type")] + activity_type: String, + + id: String, + actor: String, + object: Object, + + to: Vec, +} + fn build_accept_follow( instance_url: &str, actor_profile: &DbActorProfile, source_actor_id: &str, follow_activity_id: &str, -) -> Activity { +) -> AcceptFollow { let object = Object { context: Some(json!(AP_CONTEXT)), id: follow_activity_id.to_string(), @@ -27,16 +43,15 @@ fn build_accept_follow( }; // Accept(Follow) is idempotent so its ID can be random let activity_id = local_object_id(instance_url, &new_uuid()); - let activity = create_activity( - instance_url, - &actor_profile.username, - ACCEPT, - activity_id, - object, - vec![source_actor_id.to_string()], - vec![], - ); - activity + let actor_id = local_actor_id(instance_url, &actor_profile.username); + AcceptFollow { + context: AP_CONTEXT.to_string(), + activity_type: ACCEPT.to_string(), + id: activity_id, + actor: actor_id, + object: object, + to: vec![source_actor_id.to_string()], + } } pub fn prepare_accept_follow( @@ -83,7 +98,7 @@ mod tests { assert_eq!(activity.id.starts_with(INSTANCE_URL), true); assert_eq!(activity.activity_type, "Accept"); - assert_eq!(activity.object["id"], follow_activity_id); - assert_eq!(activity.to.unwrap(), json!([follower_id])); + assert_eq!(activity.object.id, follow_activity_id); + assert_eq!(activity.to, vec![follower_id]); } } diff --git a/src/activitypub/builders/follow.rs b/src/activitypub/builders/follow.rs index 7b2c84a..1f22248 100644 --- a/src/activitypub/builders/follow.rs +++ b/src/activitypub/builders/follow.rs @@ -1,34 +1,48 @@ +use serde::Serialize; use uuid::Uuid; use crate::activitypub::{ - activity::{create_activity, Activity}, actors::types::Actor, + constants::AP_CONTEXT, deliverer::OutgoingActivity, - identifiers::local_object_id, + identifiers::{local_actor_id, local_object_id}, vocabulary::FOLLOW, }; use crate::config::Instance; use crate::models::profiles::types::DbActorProfile; use crate::models::users::types::User; +#[derive(Serialize)] +struct Follow { + #[serde(rename = "@context")] + context: String, + + #[serde(rename = "type")] + activity_type: String, + + id: String, + actor: String, + object: String, + + to: Vec, +} + fn build_follow( instance_url: &str, actor_profile: &DbActorProfile, target_actor_id: &str, follow_request_id: &Uuid, -) -> Activity { - let object = target_actor_id.to_string(); +) -> Follow { let activity_id = local_object_id(instance_url, follow_request_id); - let activity = create_activity( - instance_url, - &actor_profile.username, - FOLLOW, - activity_id, - object, - vec![target_actor_id.to_string()], - vec![], - ); - activity + let actor_id = local_actor_id(instance_url, &actor_profile.username); + Follow { + context: AP_CONTEXT.to_string(), + activity_type: FOLLOW.to_string(), + id: activity_id, + actor: actor_id, + object: target_actor_id.to_string(), + to: vec![target_actor_id.to_string()], + } } pub fn prepare_follow( @@ -54,7 +68,6 @@ pub fn prepare_follow( #[cfg(test)] mod tests { - use serde_json::json; use crate::utils::id::new_uuid; use super::*; @@ -85,7 +98,6 @@ mod tests { format!("{}/users/{}", INSTANCE_URL, follower.username), ); assert_eq!(activity.object, target_actor_id); - assert_eq!(activity.to.unwrap(), json!([target_actor_id])); - assert_eq!(activity.cc.unwrap(), json!([])); + assert_eq!(activity.to, vec![target_actor_id]); } } diff --git a/src/activitypub/builders/undo_follow.rs b/src/activitypub/builders/undo_follow.rs index cba5b21..3e5ab18 100644 --- a/src/activitypub/builders/undo_follow.rs +++ b/src/activitypub/builders/undo_follow.rs @@ -1,8 +1,9 @@ +use serde::Serialize; use serde_json::json; use uuid::Uuid; use crate::activitypub::{ - activity::{create_activity, Activity, Object}, + activity::Object, actors::types::Actor, constants::AP_CONTEXT, deliverer::OutgoingActivity, @@ -13,12 +14,27 @@ use crate::config::Instance; use crate::models::profiles::types::DbActorProfile; use crate::models::users::types::User; +#[derive(Serialize)] +struct UndoFollow { + #[serde(rename = "@context")] + context: String, + + #[serde(rename = "type")] + activity_type: String, + + id: String, + actor: String, + object: Object, + + to: Vec, +} + fn build_undo_follow( instance_url: &str, actor_profile: &DbActorProfile, target_actor_id: &str, follow_request_id: &Uuid, -) -> Activity { +) -> UndoFollow { let follow_activity_id = local_object_id( instance_url, follow_request_id, @@ -36,16 +52,15 @@ fn build_undo_follow( ..Default::default() }; let activity_id = format!("{}/undo", object.id); - let activity = create_activity( - instance_url, - &actor_profile.username, - UNDO, - activity_id, - object, - vec![target_actor_id.to_string()], - vec![], - ); - activity + let actor_id = local_actor_id(instance_url, &actor_profile.username); + UndoFollow { + context: AP_CONTEXT.to_string(), + activity_type: UNDO.to_string(), + id: activity_id, + actor: actor_id, + object: object, + to: vec![target_actor_id.to_string()], + } } pub fn prepare_undo_follow( @@ -97,10 +112,10 @@ mod tests { ); assert_eq!(activity.activity_type, "Undo"); assert_eq!( - activity.object["id"], + activity.object.id, format!("{}/objects/{}", INSTANCE_URL, follow_request_id), ); - assert_eq!(activity.object["object"], target_actor_id); - assert_eq!(activity.to.unwrap(), json!([target_actor_id])); + assert_eq!(activity.object.object.unwrap(), target_actor_id); + assert_eq!(activity.to, vec![target_actor_id]); } }