From 3e5f5b80d3a0bd07e62ae77378fdeb4756f248b6 Mon Sep 17 00:00:00 2001 From: silverpill Date: Tue, 4 Jan 2022 21:36:45 +0000 Subject: [PATCH] Set correct primary and secondary audience for outgoing Announce(Note) and Undo(Announce) activities --- src/activitypub/activity.rs | 16 ++++----- src/mastodon_api/statuses/helpers.rs | 10 +++--- src/mastodon_api/statuses/views.rs | 50 +++++++++++++--------------- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/src/activitypub/activity.rs b/src/activitypub/activity.rs index ccb753f..498cfdf 100644 --- a/src/activitypub/activity.rs +++ b/src/activitypub/activity.rs @@ -311,7 +311,7 @@ pub fn create_activity_announce( Some(repost_id), object_id, vec![AP_PUBLIC.to_string(), recipient_id], - vec![], + vec![get_followers_url(instance_url, &actor_profile.username)], ); activity } @@ -320,24 +320,24 @@ pub fn create_activity_undo_announce( instance_url: &str, actor_profile: &DbActorProfile, repost_id: &Uuid, - recipient_id: Option<&String>, + recipient_id: &str, ) -> Activity { let object_id = get_object_url( instance_url, repost_id, ); - let mut recipients = vec![AP_PUBLIC.to_string()]; - if let Some(recipient_id) = recipient_id { - recipients.push(recipient_id.to_string()); - }; + let primary_audience = vec![ + AP_PUBLIC.to_string(), + recipient_id.to_string(), + ]; create_activity( instance_url, &actor_profile.username, UNDO, None, object_id, - recipients, - vec![], + primary_audience, + vec![get_followers_url(instance_url, &actor_profile.username)], ) } diff --git a/src/mastodon_api/statuses/helpers.rs b/src/mastodon_api/statuses/helpers.rs index 5be1efc..ce32e65 100644 --- a/src/mastodon_api/statuses/helpers.rs +++ b/src/mastodon_api/statuses/helpers.rs @@ -30,17 +30,17 @@ pub async fn get_note_audience( pub struct Audience { pub recipients: Vec, - pub primary_recipient: Option, + pub primary_recipient: String, } pub async fn get_like_audience( _db_client: &impl GenericClient, + instance_url: &str, post: &Post, ) -> Result { let mut recipients: Vec = Vec::new(); - let mut primary_recipient = None; + let primary_recipient = post.author.actor_id(instance_url); if let Some(remote_actor) = post.author.actor_json.as_ref() { - primary_recipient = Some(remote_actor.id.clone()); recipients.push(remote_actor.clone()); }; Ok(Audience { recipients, primary_recipient }) @@ -48,6 +48,7 @@ pub async fn get_like_audience( pub async fn get_announce_audience( db_client: &impl GenericClient, + instance_url: &str, current_user: &User, post: &Post, ) -> Result { @@ -58,9 +59,8 @@ pub async fn get_announce_audience( recipients.push(remote_actor); }; }; - let mut primary_recipient = None; + let primary_recipient = post.author.actor_id(instance_url); if let Some(remote_actor) = post.author.actor_json.as_ref() { - primary_recipient = Some(remote_actor.id.clone()); recipients.push(remote_actor.clone()); }; Ok(Audience { recipients, primary_recipient }) diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index 658db08..1889a66 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -215,20 +215,18 @@ async fn favourite( get_actions_for_posts(db_client, ¤t_user.id, vec![&mut post]).await?; if let Some(reaction) = maybe_reaction_created { + // Federate let Audience { recipients, primary_recipient } = - get_like_audience(db_client, &post).await?; - if let Some(remote_actor_id) = primary_recipient { - // Federate - let object_id = post.object_id.as_ref().ok_or(HttpError::InternalError)?; - let activity = create_activity_like( - &config.instance_url(), - ¤t_user.profile, - object_id, - &reaction.id, - &remote_actor_id, - ); - deliver_activity(&config, ¤t_user, activity, recipients); - } + get_like_audience(db_client, &config.instance_url(), &post).await?; + let note_id = post.object_id.as_ref().ok_or(HttpError::InternalError)?; + let activity = create_activity_like( + &config.instance_url(), + ¤t_user.profile, + note_id, + &reaction.id, + &primary_recipient, + ); + deliver_activity(&config, ¤t_user, activity, recipients); } let status = Status::from_post(post, &config.instance_url()); @@ -262,18 +260,16 @@ async fn unfavourite( get_actions_for_posts(db_client, ¤t_user.id, vec![&mut post]).await?; if let Some(reaction_id) = maybe_reaction_deleted { + // Federate let Audience { recipients, primary_recipient } = - get_like_audience(db_client, &post).await?; - if let Some(remote_actor_id) = primary_recipient { - // Federate - let activity = create_activity_undo_like( - &config.instance_url(), - ¤t_user.profile, - &reaction_id, - &remote_actor_id, - ); - deliver_activity(&config, ¤t_user, activity, recipients); - }; + get_like_audience(db_client, &config.instance_url(), &post).await?; + let activity = create_activity_undo_like( + &config.instance_url(), + ¤t_user.profile, + &reaction_id, + &primary_recipient, + ); + deliver_activity(&config, ¤t_user, activity, recipients); }; let status = Status::from_post(post, &config.instance_url()); @@ -300,7 +296,7 @@ async fn reblog( // Federate let Audience { recipients, .. } = - get_announce_audience(db_client, ¤t_user, &post).await?; + get_announce_audience(db_client, &config.instance_url(), ¤t_user, &post).await?; let activity = create_activity_announce( &config.instance_url(), ¤t_user.profile, @@ -332,12 +328,12 @@ async fn unreblog( // Federate let Audience { recipients, primary_recipient } = - get_announce_audience(db_client, ¤t_user, &post).await?; + get_announce_audience(db_client, &config.instance_url(), ¤t_user, &post).await?; let activity = create_activity_undo_announce( &config.instance_url(), ¤t_user.profile, repost_id, - primary_recipient.as_ref(), + &primary_recipient, ); deliver_activity(&config, ¤t_user, activity, recipients);