diff --git a/src/activitypub/activity.rs b/src/activitypub/activity.rs index 5eefa29..1723ba5 100644 --- a/src/activitypub/activity.rs +++ b/src/activitypub/activity.rs @@ -415,41 +415,6 @@ pub fn create_activity_accept_follow( activity } -pub fn create_activity_undo_follow( - instance_url: &str, - actor_profile: &DbActorProfile, - follow_request_id: &Uuid, - target_actor_id: &str, -) -> Activity { - let follow_activity_id = get_object_url( - instance_url, - follow_request_id, - ); - let follow_actor_id = get_actor_url( - instance_url, - &actor_profile.username, - ); - let object = Object { - context: Some(json!(AP_CONTEXT)), - id: follow_activity_id, - object_type: FOLLOW.to_string(), - actor: Some(follow_actor_id), - object: Some(target_actor_id.to_owned()), - ..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 -} - #[cfg(test)] mod tests { use crate::activitypub::actor::Actor; diff --git a/src/activitypub/builders/mod.rs b/src/activitypub/builders/mod.rs index 036dfdb..e7c2f99 100644 --- a/src/activitypub/builders/mod.rs +++ b/src/activitypub/builders/mod.rs @@ -1,2 +1,3 @@ pub mod delete_note; +pub mod undo_follow; pub mod update_person; diff --git a/src/activitypub/builders/undo_follow.rs b/src/activitypub/builders/undo_follow.rs new file mode 100644 index 0000000..bd5592b --- /dev/null +++ b/src/activitypub/builders/undo_follow.rs @@ -0,0 +1,70 @@ +use serde_json::json; +use uuid::Uuid; + +use crate::activitypub::{ + activity::{create_activity, Activity, Object}, + actor::Actor, + constants::AP_CONTEXT, + deliverer::OutgoingActivity, + views::{get_actor_url, get_object_url}, + vocabulary::{FOLLOW, UNDO}, +}; +use crate::config::Instance; +use crate::models::profiles::types::DbActorProfile; +use crate::models::users::types::User; + +fn build_undo_follow( + instance_url: &str, + actor_profile: &DbActorProfile, + follow_request_id: &Uuid, + target_actor_id: &str, +) -> Activity { + let follow_activity_id = get_object_url( + instance_url, + follow_request_id, + ); + let follow_actor_id = get_actor_url( + instance_url, + &actor_profile.username, + ); + let object = Object { + context: Some(json!(AP_CONTEXT)), + id: follow_activity_id, + object_type: FOLLOW.to_string(), + actor: Some(follow_actor_id), + object: Some(target_actor_id.to_owned()), + ..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 +} + +pub fn prepare_undo_follow( + instance: Instance, + user: &User, + target_actor: &Actor, + follow_request_id: &Uuid, +) -> OutgoingActivity { + let activity = build_undo_follow( + &instance.url(), + &user.profile, + follow_request_id, + &target_actor.id, + ); + let recipients = vec![target_actor.clone()]; + OutgoingActivity { + instance, + sender: user.clone(), + activity, + recipients, + } +} diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index a3ada1f..04557ff 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -2,11 +2,11 @@ use actix_web::{get, post, patch, web, HttpResponse, Scope}; use actix_web_httpauth::extractors::bearer::BearerAuth; use uuid::Uuid; -use crate::activitypub::activity::{ - create_activity_follow, - create_activity_undo_follow, +use crate::activitypub::activity::create_activity_follow; +use crate::activitypub::builders::{ + undo_follow::prepare_undo_follow, + update_person::prepare_update_person, }; -use crate::activitypub::builders::update_person::prepare_update_person; use crate::activitypub::deliverer::deliver_activity; use crate::config::Config; use crate::database::{Pool, get_database_client}; @@ -388,13 +388,12 @@ async fn unfollow_account( &target.id, ).await?; // Federate - let activity = create_activity_undo_follow( - &config.instance_url(), - ¤t_user.profile, + prepare_undo_follow( + config.instance(), + ¤t_user, + &remote_actor, &follow_request.id, - &remote_actor.id, - ); - deliver_activity(&config, ¤t_user, activity, vec![remote_actor]); + ).spawn_deliver(); }, Err(DatabaseError::NotFound(_)) => (), // not following Err(other_error) => return Err(other_error.into()),