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

View file

@ -12,7 +12,7 @@ use crate::models::users::types::User;
use crate::utils::id::new_uuid;
#[derive(Serialize)]
pub struct AddOrRemovePerson {
struct AddOrRemovePerson {
#[serde(rename = "@context")]
context: String,
@ -55,7 +55,7 @@ pub fn prepare_update_collection(
person: &Actor,
collection: LocalActorCollection,
remove: bool,
) -> OutgoingActivity<AddOrRemovePerson> {
) -> OutgoingActivity {
let activity = build_update_collection(
&instance.url(),
&sender.profile.username,
@ -64,12 +64,12 @@ pub fn prepare_update_collection(
remove,
);
let recipients = vec![person.clone()];
OutgoingActivity {
instance: instance.clone(),
sender: sender.clone(),
OutgoingActivity::new(
instance,
sender,
activity,
recipients,
}
)
}
pub fn prepare_add_person(
@ -77,7 +77,7 @@ pub fn prepare_add_person(
sender: &User,
person: &Actor,
collection: LocalActorCollection,
) -> OutgoingActivity<AddOrRemovePerson> {
) -> OutgoingActivity {
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;
#[derive(Serialize)]
pub struct Announce {
struct Announce {
#[serde(rename = "@context")]
context: String,
@ -80,7 +80,7 @@ pub async fn prepare_announce_note(
instance: &Instance,
sender: &User,
repost: &Post,
) -> Result<OutgoingActivity<Announce>, DatabaseError> {
) -> Result<OutgoingActivity, DatabaseError> {
let post = repost.repost_of.as_ref().unwrap();
let (recipients, _) = get_announce_note_recipients(
db_client,
@ -93,12 +93,12 @@ pub async fn prepare_announce_note(
&sender.profile.username,
repost,
);
Ok(OutgoingActivity {
instance: instance.clone(),
sender: sender.clone(),
Ok(OutgoingActivity::new(
instance,
sender,
activity,
recipients,
})
))
}
#[cfg(test)]

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -5,13 +5,13 @@ use crate::activitypub::{
};
use crate::config::Instance;
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(
instance: &Instance,
sender: &User,
person: &Actor,
collection: LocalActorCollection,
) -> OutgoingActivity<AddOrRemovePerson> {
) -> OutgoingActivity {
prepare_update_collection(instance, sender, person, collection, true)
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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