Move Like() builder function to builders::like_note module
This commit is contained in:
parent
4f2979f568
commit
7103de2519
4 changed files with 103 additions and 54 deletions
|
@ -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(
|
||||
instance_url: &str,
|
||||
actor_profile: &DbActorProfile,
|
||||
|
@ -227,27 +207,6 @@ mod tests {
|
|||
|
||||
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]
|
||||
fn test_create_activity_undo_like() {
|
||||
let author = DbActorProfile::default();
|
||||
|
|
91
src/activitypub/builders/like_note.rs
Normal file
91
src/activitypub/builders/like_note.rs
Normal 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,
|
||||
¬e_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]));
|
||||
}
|
||||
}
|
|
@ -3,5 +3,6 @@ pub mod create_note;
|
|||
pub mod delete_note;
|
||||
pub mod delete_person;
|
||||
pub mod follow;
|
||||
pub mod like_note;
|
||||
pub mod undo_follow;
|
||||
pub mod update_person;
|
||||
|
|
|
@ -6,13 +6,15 @@ use actix_web_httpauth::extractors::bearer::BearerAuth;
|
|||
use uuid::Uuid;
|
||||
|
||||
use crate::activitypub::activity::{
|
||||
create_activity_like,
|
||||
create_activity_undo_like,
|
||||
create_activity_announce,
|
||||
create_activity_undo_announce,
|
||||
};
|
||||
use crate::activitypub::builders::create_note::prepare_create_note;
|
||||
use crate::activitypub::builders::delete_note::prepare_delete_note;
|
||||
use crate::activitypub::builders::{
|
||||
create_note::prepare_create_note,
|
||||
delete_note::prepare_delete_note,
|
||||
like_note::prepare_like_note,
|
||||
};
|
||||
use crate::activitypub::deliverer::deliver_activity;
|
||||
use crate::config::Config;
|
||||
use crate::database::{Pool, get_database_client};
|
||||
|
@ -227,17 +229,13 @@ async fn favourite(
|
|||
|
||||
if let Some(reaction) = maybe_reaction_created {
|
||||
// Federate
|
||||
let Audience { recipients, primary_recipient } =
|
||||
get_like_recipients(db_client, &config.instance_url(), &post).await?;
|
||||
let object_id = post.get_object_id(&config.instance_url());
|
||||
let activity = create_activity_like(
|
||||
&config.instance_url(),
|
||||
¤t_user.profile,
|
||||
&object_id,
|
||||
prepare_like_note(
|
||||
db_client,
|
||||
config.instance(),
|
||||
¤t_user,
|
||||
&post,
|
||||
&reaction.id,
|
||||
&primary_recipient,
|
||||
);
|
||||
deliver_activity(&config, ¤t_user, activity, recipients);
|
||||
).await?.spawn_deliver();
|
||||
};
|
||||
|
||||
let status = build_status(
|
||||
|
|
Loading…
Reference in a new issue