Move Like() builder function to builders::like_note module

This commit is contained in:
silverpill 2022-07-14 08:11:21 +00:00
parent 4f2979f568
commit 7103de2519
4 changed files with 103 additions and 54 deletions

View file

@ -130,26 +130,6 @@ pub fn create_activity(
} }
} }
pub fn create_activity_like(
instance_url: &str,
actor_profile: &DbActorProfile,
object_id: &str,
reaction_id: &Uuid,
recipient_id: &str,
) -> Activity {
let activity_id = get_object_url(instance_url, reaction_id);
let activity = create_activity(
instance_url,
&actor_profile.username,
LIKE,
activity_id,
object_id,
vec![AP_PUBLIC.to_string(), recipient_id.to_string()],
vec![],
);
activity
}
pub fn create_activity_undo_like( pub fn create_activity_undo_like(
instance_url: &str, instance_url: &str,
actor_profile: &DbActorProfile, actor_profile: &DbActorProfile,
@ -227,27 +207,6 @@ mod tests {
const INSTANCE_URL: &str = "https://example.com"; const INSTANCE_URL: &str = "https://example.com";
#[test]
fn test_create_activity_like() {
let author = DbActorProfile::default();
let note_id = "https://example.com/objects/123";
let note_author_id = "https://example.com/users/test";
let reaction_id = new_uuid();
let activity = create_activity_like(
INSTANCE_URL,
&author,
note_id,
&reaction_id,
note_author_id,
);
assert_eq!(
activity.id,
format!("{}/objects/{}", INSTANCE_URL, reaction_id),
);
assert_eq!(activity.object, json!(note_id));
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC, note_author_id]));
}
#[test] #[test]
fn test_create_activity_undo_like() { fn test_create_activity_undo_like() {
let author = DbActorProfile::default(); let author = DbActorProfile::default();

View file

@ -0,0 +1,91 @@
use tokio_postgres::GenericClient;
use uuid::Uuid;
use crate::activitypub::{
activity::{create_activity, Activity},
constants::AP_PUBLIC,
deliverer::OutgoingActivity,
views::get_object_url,
vocabulary::LIKE,
};
use crate::config::Instance;
use crate::errors::DatabaseError;
use crate::mastodon_api::statuses::helpers::{get_like_recipients, Audience};
use crate::models::posts::types::Post;
use crate::models::profiles::types::DbActorProfile;
use crate::models::users::types::User;
fn build_like_note(
instance_url: &str,
actor_profile: &DbActorProfile,
note_id: &str,
reaction_id: &Uuid,
recipient_id: &str,
) -> Activity {
let activity_id = get_object_url(instance_url, reaction_id);
let activity = create_activity(
instance_url,
&actor_profile.username,
LIKE,
activity_id,
note_id,
vec![AP_PUBLIC.to_string(), recipient_id.to_string()],
vec![],
);
activity
}
pub async fn prepare_like_note(
db_client: &impl GenericClient,
instance: Instance,
user: &User,
post: &Post,
reaction_id: &Uuid,
) -> Result<OutgoingActivity, DatabaseError> {
let Audience { recipients, primary_recipient } =
get_like_recipients(db_client, &instance.url(), post).await?;
let note_id = post.get_object_id(&instance.url());
let activity = build_like_note(
&instance.url(),
&user.profile,
&note_id,
reaction_id,
&primary_recipient,
);
Ok(OutgoingActivity {
instance,
sender: user.clone(),
activity,
recipients,
})
}
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::utils::id::new_uuid;
use super::*;
const INSTANCE_URL: &str = "https://example.com";
#[test]
fn test_build_like_note() {
let author = DbActorProfile::default();
let note_id = "https://example.com/objects/123";
let note_author_id = "https://example.com/users/test";
let reaction_id = new_uuid();
let activity = build_like_note(
INSTANCE_URL,
&author,
note_id,
&reaction_id,
note_author_id,
);
assert_eq!(
activity.id,
format!("{}/objects/{}", INSTANCE_URL, reaction_id),
);
assert_eq!(activity.object, json!(note_id));
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC, note_author_id]));
}
}

View file

@ -3,5 +3,6 @@ pub mod create_note;
pub mod delete_note; pub mod delete_note;
pub mod delete_person; pub mod delete_person;
pub mod follow; pub mod follow;
pub mod like_note;
pub mod undo_follow; pub mod undo_follow;
pub mod update_person; pub mod update_person;

View file

@ -6,13 +6,15 @@ use actix_web_httpauth::extractors::bearer::BearerAuth;
use uuid::Uuid; use uuid::Uuid;
use crate::activitypub::activity::{ use crate::activitypub::activity::{
create_activity_like,
create_activity_undo_like, create_activity_undo_like,
create_activity_announce, create_activity_announce,
create_activity_undo_announce, create_activity_undo_announce,
}; };
use crate::activitypub::builders::create_note::prepare_create_note; use crate::activitypub::builders::{
use crate::activitypub::builders::delete_note::prepare_delete_note; create_note::prepare_create_note,
delete_note::prepare_delete_note,
like_note::prepare_like_note,
};
use crate::activitypub::deliverer::deliver_activity; use crate::activitypub::deliverer::deliver_activity;
use crate::config::Config; use crate::config::Config;
use crate::database::{Pool, get_database_client}; use crate::database::{Pool, get_database_client};
@ -227,17 +229,13 @@ async fn favourite(
if let Some(reaction) = maybe_reaction_created { if let Some(reaction) = maybe_reaction_created {
// Federate // Federate
let Audience { recipients, primary_recipient } = prepare_like_note(
get_like_recipients(db_client, &config.instance_url(), &post).await?; db_client,
let object_id = post.get_object_id(&config.instance_url()); config.instance(),
let activity = create_activity_like( &current_user,
&config.instance_url(), &post,
&current_user.profile,
&object_id,
&reaction.id, &reaction.id,
&primary_recipient, ).await?.spawn_deliver();
);
deliver_activity(&config, &current_user, activity, recipients);
}; };
let status = build_status( let status = build_status(