Move Announce(Note) builder function to builders::announce_note module
This commit is contained in:
parent
1c4e75b0d8
commit
623e7c9c49
4 changed files with 111 additions and 64 deletions
|
@ -3,7 +3,6 @@ use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::models::posts::types::Post;
|
|
||||||
use crate::models::profiles::types::DbActorProfile;
|
use crate::models::profiles::types::DbActorProfile;
|
||||||
use super::constants::{AP_CONTEXT, AP_PUBLIC};
|
use super::constants::{AP_CONTEXT, AP_PUBLIC};
|
||||||
use super::views::{
|
use super::views::{
|
||||||
|
@ -130,27 +129,6 @@ pub fn create_activity(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_activity_announce(
|
|
||||||
instance_url: &str,
|
|
||||||
actor_profile: &DbActorProfile,
|
|
||||||
post: &Post,
|
|
||||||
repost_id: &Uuid,
|
|
||||||
) -> Activity {
|
|
||||||
let object_id = post.get_object_id(instance_url);
|
|
||||||
let activity_id = get_object_url(instance_url, repost_id);
|
|
||||||
let recipient_id = post.author.actor_id(instance_url);
|
|
||||||
let activity = create_activity(
|
|
||||||
instance_url,
|
|
||||||
&actor_profile.username,
|
|
||||||
ANNOUNCE,
|
|
||||||
activity_id,
|
|
||||||
object_id,
|
|
||||||
vec![AP_PUBLIC.to_string(), recipient_id],
|
|
||||||
vec![get_followers_url(instance_url, &actor_profile.username)],
|
|
||||||
);
|
|
||||||
activity
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_activity_undo_announce(
|
pub fn create_activity_undo_announce(
|
||||||
instance_url: &str,
|
instance_url: &str,
|
||||||
actor_profile: &DbActorProfile,
|
actor_profile: &DbActorProfile,
|
||||||
|
@ -179,45 +157,11 @@ pub fn create_activity_undo_announce(
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::activitypub::actor::Actor;
|
|
||||||
use crate::utils::id::new_uuid;
|
use crate::utils::id::new_uuid;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
const INSTANCE_URL: &str = "https://example.com";
|
const INSTANCE_URL: &str = "https://example.com";
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_create_activity_announce() {
|
|
||||||
let post_author_id = "https://test.net/user/test";
|
|
||||||
let post_author = DbActorProfile {
|
|
||||||
actor_json: Some(Actor {
|
|
||||||
id: post_author_id.to_string(),
|
|
||||||
..Default::default()
|
|
||||||
}),
|
|
||||||
actor_id: Some(post_author_id.to_string()),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let post_id = "https://test.net/obj/123";
|
|
||||||
let post = Post {
|
|
||||||
author: post_author.clone(),
|
|
||||||
object_id: Some(post_id.to_string()),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
let announcer = DbActorProfile::default();
|
|
||||||
let repost_id = new_uuid();
|
|
||||||
let activity = create_activity_announce(
|
|
||||||
INSTANCE_URL,
|
|
||||||
&announcer,
|
|
||||||
&post,
|
|
||||||
&repost_id,
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
activity.id,
|
|
||||||
format!("{}/objects/{}", INSTANCE_URL, repost_id),
|
|
||||||
);
|
|
||||||
assert_eq!(activity.object, post_id);
|
|
||||||
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC, post_author_id]));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_activity_undo_announce() {
|
fn test_create_activity_undo_announce() {
|
||||||
let announcer = DbActorProfile::default();
|
let announcer = DbActorProfile::default();
|
||||||
|
|
104
src/activitypub/builders/announce_note.rs
Normal file
104
src/activitypub/builders/announce_note.rs
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
use tokio_postgres::GenericClient;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::activitypub::{
|
||||||
|
activity::{create_activity, Activity},
|
||||||
|
constants::AP_PUBLIC,
|
||||||
|
deliverer::OutgoingActivity,
|
||||||
|
views::{get_followers_url, get_object_url},
|
||||||
|
vocabulary::ANNOUNCE,
|
||||||
|
};
|
||||||
|
use crate::config::Instance;
|
||||||
|
use crate::errors::DatabaseError;
|
||||||
|
use crate::mastodon_api::statuses::helpers::{get_announce_recipients, Audience};
|
||||||
|
use crate::models::posts::types::Post;
|
||||||
|
use crate::models::profiles::types::DbActorProfile;
|
||||||
|
use crate::models::users::types::User;
|
||||||
|
|
||||||
|
fn build_announce_note(
|
||||||
|
instance_url: &str,
|
||||||
|
actor_profile: &DbActorProfile,
|
||||||
|
post: &Post,
|
||||||
|
repost_id: &Uuid,
|
||||||
|
) -> Activity {
|
||||||
|
let object_id = post.get_object_id(instance_url);
|
||||||
|
let activity_id = get_object_url(instance_url, repost_id);
|
||||||
|
let recipient_id = post.author.actor_id(instance_url);
|
||||||
|
let activity = create_activity(
|
||||||
|
instance_url,
|
||||||
|
&actor_profile.username,
|
||||||
|
ANNOUNCE,
|
||||||
|
activity_id,
|
||||||
|
object_id,
|
||||||
|
vec![AP_PUBLIC.to_string(), recipient_id],
|
||||||
|
vec![get_followers_url(instance_url, &actor_profile.username)],
|
||||||
|
);
|
||||||
|
activity
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn prepare_announce_note(
|
||||||
|
db_client: &impl GenericClient,
|
||||||
|
instance: Instance,
|
||||||
|
user: &User,
|
||||||
|
post: &Post,
|
||||||
|
repost_id: &Uuid,
|
||||||
|
) -> Result<OutgoingActivity, DatabaseError> {
|
||||||
|
assert_ne!(&post.id, repost_id);
|
||||||
|
let Audience { recipients, .. } =
|
||||||
|
get_announce_recipients(db_client, &instance.url(), user, post).await?;
|
||||||
|
let activity = build_announce_note(
|
||||||
|
&instance.url(),
|
||||||
|
&user.profile,
|
||||||
|
post,
|
||||||
|
repost_id,
|
||||||
|
);
|
||||||
|
Ok(OutgoingActivity {
|
||||||
|
instance,
|
||||||
|
sender: user.clone(),
|
||||||
|
activity,
|
||||||
|
recipients,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use serde_json::json;
|
||||||
|
use crate::activitypub::actor::Actor;
|
||||||
|
use crate::utils::id::new_uuid;
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const INSTANCE_URL: &str = "https://example.com";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_build_announce_note() {
|
||||||
|
let post_author_id = "https://test.net/user/test";
|
||||||
|
let post_author = DbActorProfile {
|
||||||
|
actor_json: Some(Actor {
|
||||||
|
id: post_author_id.to_string(),
|
||||||
|
..Default::default()
|
||||||
|
}),
|
||||||
|
actor_id: Some(post_author_id.to_string()),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let post_id = "https://test.net/obj/123";
|
||||||
|
let post = Post {
|
||||||
|
author: post_author.clone(),
|
||||||
|
object_id: Some(post_id.to_string()),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let announcer = DbActorProfile::default();
|
||||||
|
let repost_id = new_uuid();
|
||||||
|
let activity = build_announce_note(
|
||||||
|
INSTANCE_URL,
|
||||||
|
&announcer,
|
||||||
|
&post,
|
||||||
|
&repost_id,
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
activity.id,
|
||||||
|
format!("{}/objects/{}", INSTANCE_URL, repost_id),
|
||||||
|
);
|
||||||
|
assert_eq!(activity.object, post_id);
|
||||||
|
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC, post_author_id]));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod accept_follow;
|
pub mod accept_follow;
|
||||||
|
pub mod announce_note;
|
||||||
pub mod create_note;
|
pub mod create_note;
|
||||||
pub mod delete_note;
|
pub mod delete_note;
|
||||||
pub mod delete_person;
|
pub mod delete_person;
|
||||||
|
|
|
@ -6,10 +6,10 @@ use actix_web_httpauth::extractors::bearer::BearerAuth;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::activitypub::activity::{
|
use crate::activitypub::activity::{
|
||||||
create_activity_announce,
|
|
||||||
create_activity_undo_announce,
|
create_activity_undo_announce,
|
||||||
};
|
};
|
||||||
use crate::activitypub::builders::{
|
use crate::activitypub::builders::{
|
||||||
|
announce_note::prepare_announce_note,
|
||||||
create_note::prepare_create_note,
|
create_note::prepare_create_note,
|
||||||
delete_note::prepare_delete_note,
|
delete_note::prepare_delete_note,
|
||||||
like_note::prepare_like_note,
|
like_note::prepare_like_note,
|
||||||
|
@ -308,15 +308,13 @@ async fn reblog(
|
||||||
post.repost_count += 1;
|
post.repost_count += 1;
|
||||||
|
|
||||||
// Federate
|
// Federate
|
||||||
let Audience { recipients, .. } =
|
prepare_announce_note(
|
||||||
get_announce_recipients(db_client, &config.instance_url(), ¤t_user, &post).await?;
|
db_client,
|
||||||
let activity = create_activity_announce(
|
config.instance(),
|
||||||
&config.instance_url(),
|
¤t_user,
|
||||||
¤t_user.profile,
|
|
||||||
&post,
|
&post,
|
||||||
&repost.id,
|
&repost.id,
|
||||||
);
|
).await?.spawn_deliver();
|
||||||
deliver_activity(&config, ¤t_user, activity, recipients);
|
|
||||||
|
|
||||||
let status = build_status(
|
let status = build_status(
|
||||||
db_client,
|
db_client,
|
||||||
|
|
Loading…
Reference in a new issue