Move audience helpers to activitypub::builders

This commit is contained in:
silverpill 2022-07-14 12:02:01 +00:00
parent c9bbf7020e
commit 9aa9130d05
5 changed files with 60 additions and 52 deletions

View file

@ -3,6 +3,7 @@ use uuid::Uuid;
use crate::activitypub::{
activity::{create_activity, Activity},
actor::Actor,
constants::AP_PUBLIC,
deliverer::OutgoingActivity,
views::{get_followers_url, get_object_url},
@ -10,9 +11,9 @@ use crate::activitypub::{
};
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::relationships::queries::get_followers;
use crate::models::users::types::User;
fn build_announce_note(
@ -36,6 +37,26 @@ fn build_announce_note(
activity
}
pub async fn get_announce_note_recipients(
db_client: &impl GenericClient,
instance_url: &str,
current_user: &User,
post: &Post,
) -> Result<(Vec<Actor>, String), DatabaseError> {
let followers = get_followers(db_client, &current_user.id, None, None).await?;
let mut recipients: Vec<Actor> = Vec::new();
for profile in followers {
if let Some(remote_actor) = profile.actor_json {
recipients.push(remote_actor);
};
};
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))
}
pub async fn prepare_announce_note(
db_client: &impl GenericClient,
instance: Instance,
@ -44,8 +65,12 @@ pub async fn prepare_announce_note(
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 (recipients, _) = get_announce_note_recipients(
db_client,
&instance.url(),
user,
post,
).await?;
let activity = build_announce_note(
&instance.url(),
&user.profile,

View file

@ -3,6 +3,7 @@ use uuid::Uuid;
use crate::activitypub::{
activity::{create_activity, Activity},
actor::Actor,
constants::AP_PUBLIC,
deliverer::OutgoingActivity,
views::get_object_url,
@ -10,7 +11,6 @@ use crate::activitypub::{
};
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;
@ -35,6 +35,19 @@ fn build_like_note(
activity
}
pub async fn get_like_note_recipients(
_db_client: &impl GenericClient,
instance_url: &str,
post: &Post,
) -> Result<(Vec<Actor>, String), DatabaseError> {
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() {
recipients.push(remote_actor.clone());
};
Ok((recipients, primary_recipient))
}
pub async fn prepare_like_note(
db_client: &impl GenericClient,
instance: Instance,
@ -42,8 +55,11 @@ pub async fn prepare_like_note(
post: &Post,
reaction_id: &Uuid,
) -> Result<OutgoingActivity, DatabaseError> {
let Audience { recipients, primary_recipient } =
get_like_recipients(db_client, &instance.url(), post).await?;
let (recipients, primary_recipient) = get_like_note_recipients(
db_client,
&instance.url(),
post,
).await?;
let note_id = post.get_object_id(&instance.url());
let activity = build_like_note(
&instance.url(),

View file

@ -10,10 +10,10 @@ use crate::activitypub::{
};
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;
use super::announce_note::get_announce_note_recipients;
fn build_undo_announce(
instance_url: &str,
@ -49,8 +49,12 @@ pub async fn prepare_undo_announce_note(
repost_id: &Uuid,
) -> Result<OutgoingActivity, DatabaseError> {
assert_ne!(&post.id, repost_id);
let Audience { recipients, primary_recipient } =
get_announce_recipients(db_client, &instance.url(), user, post).await?;
let (recipients, primary_recipient) = get_announce_note_recipients(
db_client,
&instance.url(),
user,
post,
).await?;
let activity = build_undo_announce(
&instance.url(),
&user.profile,

View file

@ -10,10 +10,10 @@ use crate::activitypub::{
};
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;
use super::like_note::get_like_note_recipients;
fn build_undo_like(
instance_url: &str,
@ -44,8 +44,11 @@ pub async fn prepare_undo_like_note(
post: &Post,
reaction_id: &Uuid,
) -> Result<OutgoingActivity, DatabaseError> {
let Audience { recipients, primary_recipient } =
get_like_recipients(db_client, &instance.url(), post).await?;
let (recipients, primary_recipient) = get_like_note_recipients(
db_client,
&instance.url(),
post,
).await?;
let activity = build_undo_like(
&instance.url(),
&user.profile,

View file

@ -1,54 +1,14 @@
use tokio_postgres::GenericClient;
use crate::activitypub::actor::Actor;
use crate::errors::DatabaseError;
use crate::models::posts::helpers::{
add_user_actions,
add_reposted_posts,
};
use crate::models::posts::types::Post;
use crate::models::relationships::queries::get_followers;
use crate::models::users::types::User;
use super::types::Status;
pub struct Audience {
pub recipients: Vec<Actor>,
pub primary_recipient: String,
}
pub async fn get_like_recipients(
_db_client: &impl GenericClient,
instance_url: &str,
post: &Post,
) -> Result<Audience, DatabaseError> {
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() {
recipients.push(remote_actor.clone());
};
Ok(Audience { recipients, primary_recipient })
}
pub async fn get_announce_recipients(
db_client: &impl GenericClient,
instance_url: &str,
current_user: &User,
post: &Post,
) -> Result<Audience, DatabaseError> {
let followers = get_followers(db_client, &current_user.id, None, None).await?;
let mut recipients: Vec<Actor> = Vec::new();
for profile in followers {
if let Some(remote_actor) = profile.actor_json {
recipients.push(remote_actor);
};
};
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(Audience { recipients, primary_recipient })
}
/// Load related objects and build status for API response
pub async fn build_status(
db_client: &impl GenericClient,