Fix audience of private Undo(Like) activity

This commit is contained in:
silverpill 2022-07-28 22:27:50 +00:00
parent ecc4afe568
commit aefed2479e
2 changed files with 43 additions and 24 deletions

View file

@ -15,19 +15,29 @@ use crate::models::posts::types::{Post, Visibility};
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
use crate::models::users::types::User; use crate::models::users::types::User;
pub fn get_like_note_audience(
note_author_id: &str,
note_visibility: &Visibility,
) -> (Vec<String>, Vec<String>) {
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( fn build_like_note(
instance_url: &str, instance_url: &str,
actor_profile: &DbActorProfile, actor_profile: &DbActorProfile,
note_id: &str, note_id: &str,
reaction_id: &Uuid, reaction_id: &Uuid,
recipient_id: &str, note_author_id: &str,
is_public: bool, note_visibility: &Visibility,
) -> Activity { ) -> Activity {
let activity_id = local_object_id(instance_url, reaction_id); let activity_id = local_object_id(instance_url, reaction_id);
let mut primary_audience = vec![recipient_id.to_string()]; let (primary_audience, secondary_audience) =
if is_public { get_like_note_audience(note_author_id, note_visibility);
primary_audience.push(AP_PUBLIC.to_string());
};
let activity = create_activity( let activity = create_activity(
instance_url, instance_url,
&actor_profile.username, &actor_profile.username,
@ -35,22 +45,21 @@ fn build_like_note(
activity_id, activity_id,
note_id, note_id,
primary_audience, primary_audience,
vec![], secondary_audience,
); );
activity activity
} }
pub async fn get_like_note_recipients( pub async fn get_like_note_recipients(
_db_client: &impl GenericClient, _db_client: &impl GenericClient,
instance_url: &str, _instance_url: &str,
post: &Post, post: &Post,
) -> Result<(Vec<Actor>, String), DatabaseError> { ) -> Result<Vec<Actor>, DatabaseError> {
let mut recipients: Vec<Actor> = Vec::new(); let mut recipients: Vec<Actor> = Vec::new();
let primary_recipient = post.author.actor_id(instance_url);
if let Some(remote_actor) = post.author.actor_json.as_ref() { if let Some(remote_actor) = post.author.actor_json.as_ref() {
recipients.push(remote_actor.clone()); recipients.push(remote_actor.clone());
}; };
Ok((recipients, primary_recipient)) Ok(recipients)
} }
pub async fn prepare_like_note( pub async fn prepare_like_note(
@ -60,19 +69,20 @@ pub async fn prepare_like_note(
post: &Post, post: &Post,
reaction_id: &Uuid, reaction_id: &Uuid,
) -> Result<OutgoingActivity<Activity>, DatabaseError> { ) -> Result<OutgoingActivity<Activity>, DatabaseError> {
let (recipients, primary_recipient) = get_like_note_recipients( let recipients = get_like_note_recipients(
db_client, db_client,
&instance.url(), &instance.url(),
post, post,
).await?; ).await?;
let note_id = post.get_object_id(&instance.url()); let note_id = post.get_object_id(&instance.url());
let note_author_id = post.author.actor_id(&instance.url());
let activity = build_like_note( let activity = build_like_note(
&instance.url(), &instance.url(),
&user.profile, &user.profile,
&note_id, &note_id,
reaction_id, reaction_id,
&primary_recipient, &note_author_id,
matches!(post.visibility, Visibility::Public), &post.visibility,
); );
Ok(OutgoingActivity { Ok(OutgoingActivity {
instance, instance,
@ -102,7 +112,7 @@ mod tests {
note_id, note_id,
&reaction_id, &reaction_id,
note_author_id, note_author_id,
true, &Visibility::Public,
); );
assert_eq!( assert_eq!(
activity.id, activity.id,

View file

@ -3,34 +3,39 @@ use uuid::Uuid;
use crate::activitypub::{ use crate::activitypub::{
activity::{create_activity, Activity}, activity::{create_activity, Activity},
constants::AP_PUBLIC,
deliverer::OutgoingActivity, deliverer::OutgoingActivity,
identifiers::local_object_id, identifiers::local_object_id,
vocabulary::UNDO, vocabulary::UNDO,
}; };
use crate::config::Instance; use crate::config::Instance;
use crate::errors::DatabaseError; 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::profiles::types::DbActorProfile;
use crate::models::users::types::User; 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( fn build_undo_like(
instance_url: &str, instance_url: &str,
actor_profile: &DbActorProfile, actor_profile: &DbActorProfile,
reaction_id: &Uuid, reaction_id: &Uuid,
recipient_id: &str, note_author_id: &str,
note_visibility: &Visibility,
) -> Activity { ) -> Activity {
let object_id = local_object_id(instance_url, reaction_id); let object_id = local_object_id(instance_url, reaction_id);
let activity_id = format!("{}/undo", object_id); let activity_id = format!("{}/undo", object_id);
let (primary_audience, secondary_audience) =
get_like_note_audience(note_author_id, note_visibility);
create_activity( create_activity(
instance_url, instance_url,
&actor_profile.username, &actor_profile.username,
UNDO, UNDO,
activity_id, activity_id,
object_id, object_id,
vec![AP_PUBLIC.to_string(), recipient_id.to_string()], primary_audience,
vec![], secondary_audience,
) )
} }
@ -41,16 +46,18 @@ pub async fn prepare_undo_like_note(
post: &Post, post: &Post,
reaction_id: &Uuid, reaction_id: &Uuid,
) -> Result<OutgoingActivity<Activity>, DatabaseError> { ) -> Result<OutgoingActivity<Activity>, DatabaseError> {
let (recipients, primary_recipient) = get_like_note_recipients( let recipients = get_like_note_recipients(
db_client, db_client,
&instance.url(), &instance.url(),
post, post,
).await?; ).await?;
let note_author_id = post.author.actor_id(&instance.url());
let activity = build_undo_like( let activity = build_undo_like(
&instance.url(), &instance.url(),
&user.profile, &user.profile,
reaction_id, reaction_id,
&primary_recipient, &note_author_id,
&post.visibility,
); );
Ok(OutgoingActivity { Ok(OutgoingActivity {
instance, instance,
@ -63,6 +70,7 @@ pub async fn prepare_undo_like_note(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json; use serde_json::json;
use crate::activitypub::constants::AP_PUBLIC;
use crate::utils::id::new_uuid; use crate::utils::id::new_uuid;
use super::*; use super::*;
@ -78,6 +86,7 @@ mod tests {
&author, &author,
&reaction_id, &reaction_id,
note_author_id, note_author_id,
&Visibility::Public,
); );
assert_eq!( assert_eq!(
activity.id, activity.id,
@ -87,6 +96,6 @@ mod tests {
activity.object, activity.object,
format!("{}/objects/{}", INSTANCE_URL, reaction_id), 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]));
} }
} }