Replace generic parameter in OutgoingActivity struct with Value type

This commit is contained in:
silverpill 2022-12-04 22:31:02 +00:00
parent d1939b10d5
commit 10cd778f40
16 changed files with 102 additions and 94 deletions

View file

@ -44,7 +44,7 @@ pub fn prepare_accept_follow(
sender: &User, sender: &User,
source_actor: &Actor, source_actor: &Actor,
follow_activity_id: &str, follow_activity_id: &str,
) -> OutgoingActivity<Activity> { ) -> OutgoingActivity {
let activity = build_accept_follow( let activity = build_accept_follow(
&instance.url(), &instance.url(),
&sender.profile, &sender.profile,
@ -52,12 +52,12 @@ pub fn prepare_accept_follow(
follow_activity_id, follow_activity_id,
); );
let recipients = vec![source_actor.clone()]; let recipients = vec![source_actor.clone()];
OutgoingActivity { OutgoingActivity::new(
instance: instance.clone(), instance,
sender: sender.clone(), sender,
activity, activity,
recipients, recipients,
} )
} }
#[cfg(test)] #[cfg(test)]

View file

@ -12,7 +12,7 @@ use crate::models::users::types::User;
use crate::utils::id::new_uuid; use crate::utils::id::new_uuid;
#[derive(Serialize)] #[derive(Serialize)]
pub struct AddOrRemovePerson { struct AddOrRemovePerson {
#[serde(rename = "@context")] #[serde(rename = "@context")]
context: String, context: String,
@ -55,7 +55,7 @@ pub fn prepare_update_collection(
person: &Actor, person: &Actor,
collection: LocalActorCollection, collection: LocalActorCollection,
remove: bool, remove: bool,
) -> OutgoingActivity<AddOrRemovePerson> { ) -> OutgoingActivity {
let activity = build_update_collection( let activity = build_update_collection(
&instance.url(), &instance.url(),
&sender.profile.username, &sender.profile.username,
@ -64,12 +64,12 @@ pub fn prepare_update_collection(
remove, remove,
); );
let recipients = vec![person.clone()]; let recipients = vec![person.clone()];
OutgoingActivity { OutgoingActivity::new(
instance: instance.clone(), instance,
sender: sender.clone(), sender,
activity, activity,
recipients, recipients,
} )
} }
pub fn prepare_add_person( pub fn prepare_add_person(
@ -77,7 +77,7 @@ pub fn prepare_add_person(
sender: &User, sender: &User,
person: &Actor, person: &Actor,
collection: LocalActorCollection, collection: LocalActorCollection,
) -> OutgoingActivity<AddOrRemovePerson> { ) -> OutgoingActivity {
prepare_update_collection(instance, sender, person, collection, false) prepare_update_collection(instance, sender, person, collection, false)
} }

View file

@ -16,7 +16,7 @@ use crate::models::relationships::queries::get_followers;
use crate::models::users::types::User; use crate::models::users::types::User;
#[derive(Serialize)] #[derive(Serialize)]
pub struct Announce { struct Announce {
#[serde(rename = "@context")] #[serde(rename = "@context")]
context: String, context: String,
@ -80,7 +80,7 @@ pub async fn prepare_announce_note(
instance: &Instance, instance: &Instance,
sender: &User, sender: &User,
repost: &Post, repost: &Post,
) -> Result<OutgoingActivity<Announce>, DatabaseError> { ) -> Result<OutgoingActivity, DatabaseError> {
let post = repost.repost_of.as_ref().unwrap(); let post = repost.repost_of.as_ref().unwrap();
let (recipients, _) = get_announce_note_recipients( let (recipients, _) = get_announce_note_recipients(
db_client, db_client,
@ -93,12 +93,12 @@ pub async fn prepare_announce_note(
&sender.profile.username, &sender.profile.username,
repost, repost,
); );
Ok(OutgoingActivity { Ok(OutgoingActivity::new(
instance: instance.clone(), instance,
sender: sender.clone(), sender,
activity, activity,
recipients, recipients,
}) ))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -222,7 +222,7 @@ pub async fn prepare_create_note(
instance: &Instance, instance: &Instance,
author: &User, author: &User,
post: &Post, post: &Post,
) -> Result<OutgoingActivity<Activity>, DatabaseError> { ) -> Result<OutgoingActivity, DatabaseError> {
assert_eq!(author.id, post.author.id); assert_eq!(author.id, post.author.id);
let activity = build_create_note( let activity = build_create_note(
&instance.hostname(), &instance.hostname(),
@ -230,12 +230,12 @@ pub async fn prepare_create_note(
post, post,
); );
let recipients = get_note_recipients(db_client, author, post).await?; let recipients = get_note_recipients(db_client, author, post).await?;
Ok(OutgoingActivity { Ok(OutgoingActivity::new(
instance: instance.clone(), instance,
sender: author.clone(), author,
activity, activity,
recipients, recipients,
}) ))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -52,7 +52,7 @@ pub async fn prepare_delete_note(
instance: &Instance, instance: &Instance,
author: &User, author: &User,
post: &Post, post: &Post,
) -> Result<OutgoingActivity<Activity>, DatabaseError> { ) -> Result<OutgoingActivity, DatabaseError> {
assert_eq!(author.id, post.author.id); assert_eq!(author.id, post.author.id);
let mut post = post.clone(); let mut post = post.clone();
add_related_posts(db_client, vec![&mut post]).await?; add_related_posts(db_client, vec![&mut post]).await?;
@ -62,12 +62,12 @@ pub async fn prepare_delete_note(
&post, &post,
); );
let recipients = get_note_recipients(db_client, author, &post).await?; let recipients = get_note_recipients(db_client, author, &post).await?;
Ok(OutgoingActivity { Ok(OutgoingActivity::new(
instance: instance.clone(), instance,
sender: author.clone(), author,
activity, activity,
recipients, recipients,
}) ))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -49,15 +49,15 @@ pub async fn prepare_delete_person(
db_client: &impl GenericClient, db_client: &impl GenericClient,
instance: &Instance, instance: &Instance,
user: &User, user: &User,
) -> Result<OutgoingActivity<Activity>, DatabaseError> { ) -> Result<OutgoingActivity, DatabaseError> {
let activity = build_delete_person(&instance.url(), user); let activity = build_delete_person(&instance.url(), user);
let recipients = get_delete_person_recipients(db_client, &user.id).await?; let recipients = get_delete_person_recipients(db_client, &user.id).await?;
Ok(OutgoingActivity { Ok(OutgoingActivity::new(
instance: instance.clone(), instance,
sender: user.clone(), user,
activity, activity,
recipients, recipients,
}) ))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -36,7 +36,7 @@ pub fn prepare_follow(
sender: &User, sender: &User,
target_actor: &Actor, target_actor: &Actor,
follow_request_id: &Uuid, follow_request_id: &Uuid,
) -> OutgoingActivity<Activity> { ) -> OutgoingActivity {
let activity = build_follow( let activity = build_follow(
&instance.url(), &instance.url(),
&sender.profile, &sender.profile,
@ -44,12 +44,12 @@ pub fn prepare_follow(
follow_request_id, follow_request_id,
); );
let recipients = vec![target_actor.clone()]; let recipients = vec![target_actor.clone()];
OutgoingActivity { OutgoingActivity::new(
instance: instance.clone(), instance,
sender: sender.clone(), sender,
activity, activity,
recipients, recipients,
} )
} }
#[cfg(test)] #[cfg(test)]

View file

@ -68,7 +68,7 @@ pub async fn prepare_like_note(
sender: &User, sender: &User,
post: &Post, post: &Post,
reaction_id: &Uuid, reaction_id: &Uuid,
) -> Result<OutgoingActivity<Activity>, DatabaseError> { ) -> Result<OutgoingActivity, DatabaseError> {
let recipients = get_like_note_recipients( let recipients = get_like_note_recipients(
db_client, db_client,
&instance.url(), &instance.url(),
@ -84,12 +84,12 @@ pub async fn prepare_like_note(
&note_author_id, &note_author_id,
&post.visibility, &post.visibility,
); );
Ok(OutgoingActivity { Ok(OutgoingActivity::new(
instance: instance.clone(), instance,
sender: sender.clone(), sender,
activity, activity,
recipients, recipients,
}) ))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,5 +1,4 @@
use serde::Serialize; use serde::Serialize;
use serde_json::Value;
use uuid::Uuid; use uuid::Uuid;
use crate::activitypub::{ use crate::activitypub::{
@ -10,7 +9,6 @@ use crate::activitypub::{
vocabulary::MOVE, vocabulary::MOVE,
}; };
use crate::config::Instance; use crate::config::Instance;
use crate::errors::ConversionError;
use crate::models::users::types::User; use crate::models::users::types::User;
#[derive(Serialize)] #[derive(Serialize)]
@ -55,7 +53,7 @@ pub fn prepare_signed_move_person(
from_actor_id: &str, from_actor_id: &str,
followers: Vec<Actor>, followers: Vec<Actor>,
internal_activity_id: &Uuid, internal_activity_id: &Uuid,
) -> Result<OutgoingActivity<Value>, ConversionError> { ) -> OutgoingActivity {
let followers_ids: Vec<String> = followers.iter() let followers_ids: Vec<String> = followers.iter()
.map(|actor| actor.id.clone()) .map(|actor| actor.id.clone())
.collect(); .collect();
@ -66,12 +64,10 @@ pub fn prepare_signed_move_person(
&followers_ids, &followers_ids,
internal_activity_id, internal_activity_id,
); );
let activity_value = serde_json::to_value(activity) OutgoingActivity::new(
.map_err(|_| ConversionError)?; instance,
Ok(OutgoingActivity { sender,
instance: instance.clone(), activity,
sender: sender.clone(), followers,
activity: activity_value, )
recipients: followers,
})
} }

View file

@ -5,13 +5,13 @@ use crate::activitypub::{
}; };
use crate::config::Instance; use crate::config::Instance;
use crate::models::users::types::User; use crate::models::users::types::User;
use super::add_person::{prepare_update_collection, AddOrRemovePerson}; use super::add_person::prepare_update_collection;
pub fn prepare_remove_person( pub fn prepare_remove_person(
instance: &Instance, instance: &Instance,
sender: &User, sender: &User,
person: &Actor, person: &Actor,
collection: LocalActorCollection, collection: LocalActorCollection,
) -> OutgoingActivity<AddOrRemovePerson> { ) -> OutgoingActivity {
prepare_update_collection(instance, sender, person, collection, true) prepare_update_collection(instance, sender, person, collection, true)
} }

View file

@ -47,7 +47,7 @@ pub async fn prepare_undo_announce_note(
sender: &User, sender: &User,
post: &Post, post: &Post,
repost_id: &Uuid, repost_id: &Uuid,
) -> Result<OutgoingActivity<Activity>, DatabaseError> { ) -> Result<OutgoingActivity, DatabaseError> {
assert_ne!(&post.id, repost_id); assert_ne!(&post.id, repost_id);
let (recipients, primary_recipient) = get_announce_note_recipients( let (recipients, primary_recipient) = get_announce_note_recipients(
db_client, db_client,
@ -61,12 +61,12 @@ pub async fn prepare_undo_announce_note(
repost_id, repost_id,
&primary_recipient, &primary_recipient,
); );
Ok(OutgoingActivity { Ok(OutgoingActivity::new(
instance: instance.clone(), instance,
sender: sender.clone(), sender,
activity, activity,
recipients, recipients,
}) ))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -53,7 +53,7 @@ pub fn prepare_undo_follow(
sender: &User, sender: &User,
target_actor: &Actor, target_actor: &Actor,
follow_request_id: &Uuid, follow_request_id: &Uuid,
) -> OutgoingActivity<Activity> { ) -> OutgoingActivity {
let activity = build_undo_follow( let activity = build_undo_follow(
&instance.url(), &instance.url(),
&sender.profile, &sender.profile,
@ -61,10 +61,10 @@ pub fn prepare_undo_follow(
follow_request_id, follow_request_id,
); );
let recipients = vec![target_actor.clone()]; let recipients = vec![target_actor.clone()];
OutgoingActivity { OutgoingActivity::new(
instance: instance.clone(), instance,
sender: sender.clone(), sender,
activity, activity,
recipients, recipients,
} )
} }

View file

@ -45,7 +45,7 @@ pub async fn prepare_undo_like_note(
sender: &User, sender: &User,
post: &Post, post: &Post,
reaction_id: &Uuid, reaction_id: &Uuid,
) -> Result<OutgoingActivity<Activity>, DatabaseError> { ) -> Result<OutgoingActivity, DatabaseError> {
let recipients = get_like_note_recipients( let recipients = get_like_note_recipients(
db_client, db_client,
&instance.url(), &instance.url(),
@ -59,12 +59,12 @@ pub async fn prepare_undo_like_note(
&note_author_id, &note_author_id,
&post.visibility, &post.visibility,
); );
Ok(OutgoingActivity { Ok(OutgoingActivity::new(
instance: instance.clone(), instance,
sender: sender.clone(), sender,
activity, activity,
recipients, recipients,
}) ))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,5 +1,4 @@
use serde::Serialize; use serde::Serialize;
use serde_json::Value;
use tokio_postgres::GenericClient; use tokio_postgres::GenericClient;
use uuid::Uuid; use uuid::Uuid;
@ -73,16 +72,16 @@ pub async fn prepare_update_person(
db_client: &impl GenericClient, db_client: &impl GenericClient,
instance: &Instance, instance: &Instance,
user: &User, user: &User,
) -> Result<OutgoingActivity<UpdatePerson>, DatabaseError> { ) -> Result<OutgoingActivity, DatabaseError> {
let activity = build_update_person(&instance.url(), user, None) let activity = build_update_person(&instance.url(), user, None)
.map_err(|_| DatabaseTypeError)?; .map_err(|_| DatabaseTypeError)?;
let recipients = get_update_person_recipients(db_client, &user.id).await?; let recipients = get_update_person_recipients(db_client, &user.id).await?;
Ok(OutgoingActivity { Ok(OutgoingActivity::new(
instance: instance.clone(), instance,
sender: user.clone(), user,
activity, activity,
recipients, recipients,
}) ))
} }
pub async fn prepare_signed_update_person( pub async fn prepare_signed_update_person(
@ -90,21 +89,19 @@ pub async fn prepare_signed_update_person(
instance: &Instance, instance: &Instance,
user: &User, user: &User,
internal_activity_id: Uuid, internal_activity_id: Uuid,
) -> Result<OutgoingActivity<Value>, DatabaseError> { ) -> Result<OutgoingActivity, DatabaseError> {
let activity = build_update_person( let activity = build_update_person(
&instance.url(), &instance.url(),
user, user,
Some(internal_activity_id), Some(internal_activity_id),
).map_err(|_| DatabaseTypeError)?; ).map_err(|_| DatabaseTypeError)?;
let activity_value = serde_json::to_value(activity)
.map_err(|_| DatabaseTypeError)?;
let recipients = get_update_person_recipients(db_client, &user.id).await?; let recipients = get_update_person_recipients(db_client, &user.id).await?;
Ok(OutgoingActivity { Ok(OutgoingActivity::new(
instance: instance.clone(), instance,
sender: user.clone(), user,
activity: activity_value, activity,
recipients, recipients,
}) ))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -5,6 +5,7 @@ use actix_web::http::Method;
use reqwest::{Client, Proxy}; use reqwest::{Client, Proxy};
use rsa::RsaPrivateKey; use rsa::RsaPrivateKey;
use serde::Serialize; use serde::Serialize;
use serde_json::Value;
use tokio::time::sleep; use tokio::time::sleep;
use crate::config::Instance; use crate::config::Instance;
@ -111,7 +112,7 @@ fn backoff(retry_count: u32) -> Duration {
async fn deliver_activity_worker( async fn deliver_activity_worker(
instance: Instance, instance: Instance,
sender: User, sender: User,
activity: impl Serialize, activity: Value,
recipients: Vec<Actor>, recipients: Vec<Actor>,
) -> Result<(), DelivererError> { ) -> Result<(), DelivererError> {
let actor_key = deserialize_private_key(&sender.private_key)?; let actor_key = deserialize_private_key(&sender.private_key)?;
@ -123,12 +124,11 @@ async fn deliver_activity_worker(
), ),
ACTOR_KEY_SUFFIX, ACTOR_KEY_SUFFIX,
); );
let activity_value = serde_json::to_value(&activity)?; let activity_signed = if is_object_signed(&activity) {
let activity_signed = if is_object_signed(&activity_value) {
log::warn!("activity is already signed"); log::warn!("activity is already signed");
activity_value activity
} else { } else {
sign_object(&activity_value, &actor_key, &actor_key_id)? sign_object(&activity, &actor_key, &actor_key_id)?
}; };
let activity_json = serde_json::to_string(&activity_signed)?; let activity_json = serde_json::to_string(&activity_signed)?;
@ -189,14 +189,29 @@ async fn deliver_activity_worker(
Ok(()) Ok(())
} }
pub struct OutgoingActivity<A: Serialize> { pub struct OutgoingActivity {
pub instance: Instance, pub instance: Instance,
pub sender: User, pub sender: User,
pub activity: A, pub activity: Value,
pub recipients: Vec<Actor>, pub recipients: Vec<Actor>,
} }
impl<A: Serialize + Send + 'static> OutgoingActivity<A> { impl OutgoingActivity {
pub fn new(
instance: &Instance,
sender: &User,
activity: impl Serialize,
recipients: Vec<Actor>,
) -> Self {
Self {
instance: instance.clone(),
sender: sender.clone(),
activity: serde_json::to_value(activity)
.expect("activity should be serializable"),
recipients,
}
}
pub async fn deliver(self) -> Result<(), DelivererError> { pub async fn deliver(self) -> Result<(), DelivererError> {
deliver_activity_worker( deliver_activity_worker(
self.instance, self.instance,

View file

@ -398,7 +398,7 @@ async fn send_signed_activity(
from_actor_id, from_actor_id,
followers, followers,
internal_activity_id, internal_activity_id,
).map_err(|_| HttpError::InternalError)? )
}, },
ActivityParams::Update { internal_activity_id } => { ActivityParams::Update { internal_activity_id } => {
prepare_signed_update_person( prepare_signed_update_person(