diff --git a/src/activitypub/activity.rs b/src/activitypub/activity.rs index 3ab3599..1cb064d 100644 --- a/src/activitypub/activity.rs +++ b/src/activitypub/activity.rs @@ -5,7 +5,9 @@ use uuid::Uuid; use crate::models::posts::types::Post; use crate::models::profiles::types::DbActorProfile; +use crate::models::users::types::User; use crate::utils::files::get_file_url; +use super::actor::{get_local_actor, ActorKeyError}; use super::constants::{AP_CONTEXT, AP_PUBLIC}; use super::views::{get_actor_url, get_object_url}; use super::vocabulary::*; @@ -92,7 +94,7 @@ fn create_activity( actor_name: &str, activity_type: &str, activity_uuid: Option, - object: Value, + object: impl Serialize, ) -> Activity { let actor_id = get_actor_url( instance_url, @@ -107,7 +109,7 @@ fn create_activity( id: activity_id, activity_type: activity_type.to_string(), actor: actor_id, - object: object, + object: serde_json::to_value(object).unwrap(), }; activity } @@ -193,7 +195,7 @@ pub fn create_activity_note( &post.author.username, CREATE, None, - serde_json::to_value(object).unwrap(), + object, ); activity } @@ -214,7 +216,7 @@ pub fn create_activity_like( &actor_profile.username, LIKE, None, - serde_json::to_value(object).unwrap(), + object, ); activity } @@ -236,7 +238,7 @@ pub fn create_activity_follow( &actor_profile.username, FOLLOW, Some(*follow_request_id), - serde_json::to_value(object).unwrap(), + object, ); activity } @@ -258,7 +260,7 @@ pub fn create_activity_accept_follow( &actor_profile.username, ACCEPT, None, - serde_json::to_value(object).unwrap(), + object, ); activity } @@ -291,11 +293,26 @@ pub fn create_activity_undo_follow( &actor_profile.username, UNDO, None, - serde_json::to_value(object).unwrap(), + object, ); activity } +pub fn create_activity_update_person( + user: &User, + instance_url: &str, +) -> Result { + let actor = get_local_actor(user, instance_url)?; + let activity = create_activity( + instance_url, + &user.profile.username, + UPDATE, + None, + actor, + ); + Ok(activity) +} + #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct OrderedCollection { diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index b5955f7..0addd1f 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -6,7 +6,9 @@ use uuid::Uuid; use crate::activitypub::activity::{ create_activity_follow, create_activity_undo_follow, + create_activity_update_person, }; +use crate::activitypub::actor::Actor; use crate::activitypub::deliverer::deliver_activity; use crate::config::Config; use crate::database::{Pool, get_database_client}; @@ -17,6 +19,7 @@ use crate::mastodon_api::oauth::auth::get_current_user; use crate::models::posts::helpers::get_actions_for_posts; use crate::models::posts::queries::get_posts_by_author; use crate::models::profiles::queries::{ + get_followers, get_profile_by_id, update_profile, }; @@ -133,6 +136,21 @@ async fn update_credentials( ¤t_user.id, profile_data, ).await?; + + // Federate + let activity = create_activity_update_person(¤t_user, &config.instance_url()) + .map_err(|_| HttpError::InternalError)?; + let followers = get_followers(db_client, ¤t_user.id).await?; + let mut recipients: Vec = Vec::new(); + for follower in followers { + let maybe_remote_actor = follower.remote_actor() + .map_err(|_| HttpError::InternalError)?; + if let Some(remote_actor) = maybe_remote_actor { + recipients.push(remote_actor); + }; + }; + deliver_activity(&config, ¤t_user, activity, recipients); + let account = Account::from_user(current_user, &config.instance_url()); Ok(HttpResponse::Ok().json(account)) }