From aefed2479e8678f1e299c2e5e345a1d1dc7b39f7 Mon Sep 17 00:00:00 2001 From: silverpill Date: Thu, 28 Jul 2022 22:27:50 +0000 Subject: [PATCH] Fix audience of private Undo(Like) activity --- src/activitypub/builders/like_note.rs | 40 ++++++++++++++-------- src/activitypub/builders/undo_like_note.rs | 27 ++++++++++----- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/activitypub/builders/like_note.rs b/src/activitypub/builders/like_note.rs index 552e996..892dfda 100644 --- a/src/activitypub/builders/like_note.rs +++ b/src/activitypub/builders/like_note.rs @@ -15,19 +15,29 @@ use crate::models::posts::types::{Post, Visibility}; use crate::models::profiles::types::DbActorProfile; use crate::models::users::types::User; +pub fn get_like_note_audience( + note_author_id: &str, + note_visibility: &Visibility, +) -> (Vec, Vec) { + let mut primary_audience = vec![note_author_id.to_string()]; + if matches!(note_visibility, Visibility::Public) { + primary_audience.push(AP_PUBLIC.to_string()); + }; + let secondary_audience = vec![]; + (primary_audience, secondary_audience) +} + fn build_like_note( instance_url: &str, actor_profile: &DbActorProfile, note_id: &str, reaction_id: &Uuid, - recipient_id: &str, - is_public: bool, + note_author_id: &str, + note_visibility: &Visibility, ) -> Activity { let activity_id = local_object_id(instance_url, reaction_id); - let mut primary_audience = vec![recipient_id.to_string()]; - if is_public { - primary_audience.push(AP_PUBLIC.to_string()); - }; + let (primary_audience, secondary_audience) = + get_like_note_audience(note_author_id, note_visibility); let activity = create_activity( instance_url, &actor_profile.username, @@ -35,22 +45,21 @@ fn build_like_note( activity_id, note_id, primary_audience, - vec![], + secondary_audience, ); activity } pub async fn get_like_note_recipients( _db_client: &impl GenericClient, - instance_url: &str, + _instance_url: &str, post: &Post, -) -> Result<(Vec, String), DatabaseError> { +) -> Result, DatabaseError> { let mut recipients: Vec = Vec::new(); - let primary_recipient = post.author.actor_id(instance_url); if let Some(remote_actor) = post.author.actor_json.as_ref() { recipients.push(remote_actor.clone()); }; - Ok((recipients, primary_recipient)) + Ok(recipients) } pub async fn prepare_like_note( @@ -60,19 +69,20 @@ pub async fn prepare_like_note( post: &Post, reaction_id: &Uuid, ) -> Result, DatabaseError> { - let (recipients, primary_recipient) = get_like_note_recipients( + let recipients = get_like_note_recipients( db_client, &instance.url(), post, ).await?; let note_id = post.get_object_id(&instance.url()); + let note_author_id = post.author.actor_id(&instance.url()); let activity = build_like_note( &instance.url(), &user.profile, ¬e_id, reaction_id, - &primary_recipient, - matches!(post.visibility, Visibility::Public), + ¬e_author_id, + &post.visibility, ); Ok(OutgoingActivity { instance, @@ -102,7 +112,7 @@ mod tests { note_id, &reaction_id, note_author_id, - true, + &Visibility::Public, ); assert_eq!( activity.id, diff --git a/src/activitypub/builders/undo_like_note.rs b/src/activitypub/builders/undo_like_note.rs index 098d89d..b01c821 100644 --- a/src/activitypub/builders/undo_like_note.rs +++ b/src/activitypub/builders/undo_like_note.rs @@ -3,34 +3,39 @@ use uuid::Uuid; use crate::activitypub::{ activity::{create_activity, Activity}, - constants::AP_PUBLIC, deliverer::OutgoingActivity, identifiers::local_object_id, vocabulary::UNDO, }; use crate::config::Instance; use crate::errors::DatabaseError; -use crate::models::posts::types::Post; +use crate::models::posts::types::{Post, Visibility}; use crate::models::profiles::types::DbActorProfile; use crate::models::users::types::User; -use super::like_note::get_like_note_recipients; +use super::like_note::{ + get_like_note_audience, + get_like_note_recipients, +}; fn build_undo_like( instance_url: &str, actor_profile: &DbActorProfile, reaction_id: &Uuid, - recipient_id: &str, + note_author_id: &str, + note_visibility: &Visibility, ) -> Activity { let object_id = local_object_id(instance_url, reaction_id); let activity_id = format!("{}/undo", object_id); + let (primary_audience, secondary_audience) = + get_like_note_audience(note_author_id, note_visibility); create_activity( instance_url, &actor_profile.username, UNDO, activity_id, object_id, - vec![AP_PUBLIC.to_string(), recipient_id.to_string()], - vec![], + primary_audience, + secondary_audience, ) } @@ -41,16 +46,18 @@ pub async fn prepare_undo_like_note( post: &Post, reaction_id: &Uuid, ) -> Result, DatabaseError> { - let (recipients, primary_recipient) = get_like_note_recipients( + let recipients = get_like_note_recipients( db_client, &instance.url(), post, ).await?; + let note_author_id = post.author.actor_id(&instance.url()); let activity = build_undo_like( &instance.url(), &user.profile, reaction_id, - &primary_recipient, + ¬e_author_id, + &post.visibility, ); Ok(OutgoingActivity { instance, @@ -63,6 +70,7 @@ pub async fn prepare_undo_like_note( #[cfg(test)] mod tests { use serde_json::json; + use crate::activitypub::constants::AP_PUBLIC; use crate::utils::id::new_uuid; use super::*; @@ -78,6 +86,7 @@ mod tests { &author, &reaction_id, note_author_id, + &Visibility::Public, ); assert_eq!( activity.id, @@ -87,6 +96,6 @@ mod tests { activity.object, format!("{}/objects/{}", INSTANCE_URL, reaction_id), ); - assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC, note_author_id])); + assert_eq!(activity.to.unwrap(), json!([note_author_id, AP_PUBLIC])); } }