Create dedicated types for all activities and remove create_activity() function

This commit is contained in:
silverpill 2022-12-06 20:31:35 +00:00
parent e5c1be2f93
commit d3819c67e6
8 changed files with 212 additions and 130 deletions

View file

@ -1,10 +1,8 @@
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{json, Value}; use serde_json::Value;
use super::constants::AP_CONTEXT; use super::vocabulary::HASHTAG;
use super::identifiers::local_actor_id;
use super::vocabulary::*;
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -123,25 +121,3 @@ pub struct Activity {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub cc: Option<Value>, pub cc: Option<Value>,
} }
pub fn create_activity(
instance_url: &str,
actor_name: &str,
activity_type: &str,
activity_id: String,
object: impl Serialize,
primary_audience: Vec<String>,
secondary_audience: Vec<String>,
) -> Activity {
let actor_id = local_actor_id(instance_url, actor_name);
Activity {
context: json!(AP_CONTEXT),
id: activity_id,
activity_type: activity_type.to_string(),
actor: actor_id,
object: serde_json::to_value(object).unwrap(),
target: None,
to: Some(json!(primary_audience)),
cc: Some(json!(secondary_audience)),
}
}

View file

@ -23,8 +23,8 @@ struct Announce {
#[serde(rename = "type")] #[serde(rename = "type")]
activity_type: String, activity_type: String,
actor: String,
id: String, id: String,
actor: String,
object: String, object: String,
published: DateTime<Utc>, published: DateTime<Utc>,

View file

@ -3,7 +3,7 @@ use serde::Serialize;
use tokio_postgres::GenericClient; use tokio_postgres::GenericClient;
use crate::activitypub::{ use crate::activitypub::{
activity::{create_activity, Activity, Attachment, Tag}, activity::{Attachment, Tag},
actors::types::Actor, actors::types::Actor,
constants::{AP_MEDIA_TYPE, AP_CONTEXT, AP_PUBLIC}, constants::{AP_MEDIA_TYPE, AP_CONTEXT, AP_PUBLIC},
deliverer::OutgoingActivity, deliverer::OutgoingActivity,
@ -163,25 +163,40 @@ pub fn build_note(
} }
} }
#[derive(Serialize)]
pub struct CreateNote {
#[serde(rename = "@context")]
context: String,
#[serde(rename = "type")]
activity_type: String,
id: String,
actor: String,
object: Note,
to: Vec<String>,
cc: Vec<String>,
}
pub fn build_create_note( pub fn build_create_note(
instance_hostname: &str, instance_hostname: &str,
instance_url: &str, instance_url: &str,
post: &Post, post: &Post,
) -> Activity { ) -> CreateNote {
let object = build_note(instance_hostname, instance_url, post); let object = build_note(instance_hostname, instance_url, post);
let primary_audience = object.to.clone(); let primary_audience = object.to.clone();
let secondary_audience = object.cc.clone(); let secondary_audience = object.cc.clone();
let activity_id = format!("{}/create", object.id); let activity_id = format!("{}/create", object.id);
let activity = create_activity( CreateNote {
instance_url, context: AP_CONTEXT.to_string(),
&post.author.username, activity_type: CREATE.to_string(),
CREATE, id: activity_id,
activity_id, actor: object.attributed_to.clone(),
object, object: object,
primary_audience, to: primary_audience,
secondary_audience, cc: secondary_audience,
); }
activity
} }
pub async fn get_note_recipients( pub async fn get_note_recipients(
@ -240,7 +255,6 @@ pub async fn prepare_create_note(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json;
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
use super::*; use super::*;
@ -422,6 +436,9 @@ mod tests {
activity.actor, activity.actor,
format!("{}/users/{}", INSTANCE_URL, author_username), format!("{}/users/{}", INSTANCE_URL, author_username),
); );
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC])); assert_eq!(activity.to, vec![AP_PUBLIC]);
assert_eq!(activity.object.attributed_to, activity.actor);
assert_eq!(activity.object.to, activity.to);
assert_eq!(activity.object.cc, activity.cc);
} }
} }

View file

@ -1,10 +1,12 @@
use serde_json::json; use serde::Serialize;
use tokio_postgres::GenericClient; use tokio_postgres::GenericClient;
use crate::activitypub::activity::{create_activity, Activity, Object}; use crate::activitypub::{
use crate::activitypub::constants::AP_CONTEXT; constants::AP_CONTEXT,
use crate::activitypub::deliverer::OutgoingActivity; deliverer::OutgoingActivity,
use crate::activitypub::vocabulary::{DELETE, NOTE, TOMBSTONE}; identifiers::local_actor_id,
vocabulary::{DELETE, NOTE, TOMBSTONE},
};
use crate::config::Instance; use crate::config::Instance;
use crate::database::DatabaseError; use crate::database::DatabaseError;
use crate::models::posts::helpers::add_related_posts; use crate::models::posts::helpers::add_related_posts;
@ -16,35 +18,59 @@ use super::create_note::{
Note, Note,
}; };
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Tombstone {
id: String,
#[serde(rename = "type")]
object_type: String,
former_type: String,
}
#[derive(Serialize)]
struct DeleteNote {
#[serde(rename = "@context")]
context: String,
#[serde(rename = "type")]
activity_type: String,
id: String,
actor: String,
object: Tombstone,
to: Vec<String>,
cc: Vec<String>,
}
fn build_delete_note( fn build_delete_note(
instance_hostname: &str, instance_hostname: &str,
instance_url: &str, instance_url: &str,
post: &Post, post: &Post,
) -> Activity { ) -> DeleteNote {
let object_id = post.object_id(instance_url); let object_id = post.object_id(instance_url);
let object = Object { let activity_id = format!("{}/delete", object_id);
context: Some(json!(AP_CONTEXT)), let actor_id = local_actor_id(instance_url, &post.author.username);
id: object_id,
object_type: TOMBSTONE.to_string(),
former_type: Some(NOTE.to_string()),
..Default::default()
};
let activity_id = format!("{}/delete", object.id);
let Note { to, cc, .. } = build_note( let Note { to, cc, .. } = build_note(
instance_hostname, instance_hostname,
instance_url, instance_url,
post, post,
); );
let activity = create_activity( DeleteNote {
instance_url, context: AP_CONTEXT.to_string(),
&post.author.username, activity_type: DELETE.to_string(),
DELETE, id: activity_id,
activity_id, actor: actor_id,
object, object: Tombstone {
to, id: object_id,
cc, object_type: TOMBSTONE.to_string(),
); former_type: NOTE.to_string(),
activity },
to: to,
cc: cc,
}
} }
pub async fn prepare_delete_note( pub async fn prepare_delete_note(
@ -72,7 +98,6 @@ pub async fn prepare_delete_note(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json;
use crate::activitypub::{ use crate::activitypub::{
constants::AP_PUBLIC, constants::AP_PUBLIC,
identifiers::local_actor_followers, identifiers::local_actor_followers,
@ -101,13 +126,14 @@ mod tests {
format!("{}/objects/{}/delete", INSTANCE_URL, post.id), format!("{}/objects/{}/delete", INSTANCE_URL, post.id),
); );
assert_eq!( assert_eq!(
activity.object["id"], activity.object.id,
format!("{}/objects/{}", INSTANCE_URL, post.id), format!("{}/objects/{}", INSTANCE_URL, post.id),
); );
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC])); assert_eq!(activity.object.object_type, "Tombstone");
assert_eq!(activity.to, vec![AP_PUBLIC]);
assert_eq!( assert_eq!(
activity.cc.unwrap(), activity.cc,
json!([local_actor_followers(INSTANCE_URL, "author")]), vec![local_actor_followers(INSTANCE_URL, "author")],
); );
} }
} }

View file

@ -1,10 +1,10 @@
use serde::Serialize;
use tokio_postgres::GenericClient; use tokio_postgres::GenericClient;
use uuid::Uuid; use uuid::Uuid;
use crate::activitypub::{ use crate::activitypub::{
activity::{create_activity, Activity},
actors::types::Actor, actors::types::Actor,
constants::AP_PUBLIC, constants::{AP_CONTEXT, AP_PUBLIC},
deliverer::OutgoingActivity, deliverer::OutgoingActivity,
vocabulary::DELETE, vocabulary::DELETE,
}; };
@ -13,21 +13,35 @@ use crate::database::DatabaseError;
use crate::models::relationships::queries::{get_followers, get_following}; use crate::models::relationships::queries::{get_followers, get_following};
use crate::models::users::types::User; use crate::models::users::types::User;
#[derive(Serialize)]
struct DeletePerson {
#[serde(rename = "@context")]
context: String,
#[serde(rename = "type")]
activity_type: String,
id: String,
actor: String,
object: String,
to: Vec<String>,
}
fn build_delete_person( fn build_delete_person(
instance_url: &str, instance_url: &str,
user: &User, user: &User,
) -> Activity { ) -> DeletePerson {
let actor_id = user.profile.actor_id(instance_url); let actor_id = user.profile.actor_id(instance_url);
let activity_id = format!("{}/delete", actor_id); let activity_id = format!("{}/delete", actor_id);
create_activity( DeletePerson {
instance_url, context: AP_CONTEXT.to_string(),
&user.profile.username, activity_type: DELETE.to_string(),
DELETE, id: activity_id,
activity_id, actor: actor_id.clone(),
actor_id, object: actor_id,
vec![AP_PUBLIC.to_string()], to: vec![AP_PUBLIC.to_string()],
vec![], }
)
} }
async fn get_delete_person_recipients( async fn get_delete_person_recipients(
@ -62,7 +76,6 @@ pub async fn prepare_delete_person(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json;
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
use super::*; use super::*;
@ -82,10 +95,11 @@ mod tests {
activity.id, activity.id,
format!("{}/users/testuser/delete", INSTANCE_URL), format!("{}/users/testuser/delete", INSTANCE_URL),
); );
assert_eq!(activity.actor, activity.object);
assert_eq!( assert_eq!(
activity.object, activity.object,
format!("{}/users/testuser", INSTANCE_URL), format!("{}/users/testuser", INSTANCE_URL),
); );
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC])); assert_eq!(activity.to, vec![AP_PUBLIC]);
} }
} }

View file

@ -1,12 +1,12 @@
use serde::Serialize;
use tokio_postgres::GenericClient; use tokio_postgres::GenericClient;
use uuid::Uuid; use uuid::Uuid;
use crate::activitypub::{ use crate::activitypub::{
activity::{create_activity, Activity},
actors::types::Actor, actors::types::Actor,
constants::AP_PUBLIC, constants::{AP_CONTEXT, AP_PUBLIC},
deliverer::OutgoingActivity, deliverer::OutgoingActivity,
identifiers::local_object_id, identifiers::{local_actor_id, local_object_id},
vocabulary::LIKE, vocabulary::LIKE,
}; };
use crate::config::Instance; use crate::config::Instance;
@ -15,6 +15,22 @@ use crate::models::posts::types::{Post, Visibility};
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
use crate::models::users::types::User; use crate::models::users::types::User;
#[derive(Serialize)]
struct Like {
#[serde(rename = "@context")]
context: String,
#[serde(rename = "type")]
activity_type: String,
id: String,
actor: String,
object: String,
to: Vec<String>,
cc: Vec<String>,
}
pub fn get_like_note_audience( pub fn get_like_note_audience(
note_author_id: &str, note_author_id: &str,
note_visibility: &Visibility, note_visibility: &Visibility,
@ -34,20 +50,20 @@ fn build_like_note(
reaction_id: &Uuid, reaction_id: &Uuid,
note_author_id: &str, note_author_id: &str,
note_visibility: &Visibility, note_visibility: &Visibility,
) -> Activity { ) -> Like {
let activity_id = local_object_id(instance_url, reaction_id); let activity_id = local_object_id(instance_url, reaction_id);
let actor_id = local_actor_id(instance_url, &actor_profile.username);
let (primary_audience, secondary_audience) = let (primary_audience, secondary_audience) =
get_like_note_audience(note_author_id, note_visibility); get_like_note_audience(note_author_id, note_visibility);
let activity = create_activity( Like {
instance_url, context: AP_CONTEXT.to_string(),
&actor_profile.username, activity_type: LIKE.to_string(),
LIKE, id: activity_id,
activity_id, actor: actor_id,
note_id, object: note_id.to_string(),
primary_audience, to: primary_audience,
secondary_audience, cc: secondary_audience,
); }
activity
} }
pub async fn get_like_note_recipients( pub async fn get_like_note_recipients(
@ -94,7 +110,6 @@ pub async fn prepare_like_note(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json;
use crate::utils::id::new_uuid; use crate::utils::id::new_uuid;
use super::*; use super::*;
@ -118,7 +133,8 @@ mod tests {
activity.id, activity.id,
format!("{}/objects/{}", INSTANCE_URL, reaction_id), format!("{}/objects/{}", INSTANCE_URL, reaction_id),
); );
assert_eq!(activity.object, json!(note_id)); assert_eq!(activity.object, note_id);
assert_eq!(activity.to.unwrap(), json!([note_author_id, AP_PUBLIC])); assert_eq!(activity.to, vec![note_author_id, AP_PUBLIC]);
assert_eq!(activity.cc.is_empty(), true);
} }
} }

View file

@ -1,11 +1,11 @@
use serde::Serialize;
use tokio_postgres::GenericClient; use tokio_postgres::GenericClient;
use uuid::Uuid; use uuid::Uuid;
use crate::activitypub::{ use crate::activitypub::{
activity::{create_activity, Activity}, constants::{AP_CONTEXT, AP_PUBLIC},
constants::AP_PUBLIC,
deliverer::OutgoingActivity, deliverer::OutgoingActivity,
identifiers::{local_actor_followers, local_object_id}, identifiers::{local_actor_id, local_actor_followers, local_object_id},
vocabulary::UNDO, vocabulary::UNDO,
}; };
use crate::config::Instance; use crate::config::Instance;
@ -15,14 +15,31 @@ use crate::models::profiles::types::DbActorProfile;
use crate::models::users::types::User; use crate::models::users::types::User;
use super::announce_note::get_announce_note_recipients; use super::announce_note::get_announce_note_recipients;
#[derive(Serialize)]
struct UndoAnnounce {
#[serde(rename = "@context")]
context: String,
#[serde(rename = "type")]
activity_type: String,
id: String,
actor: String,
object: String,
to: Vec<String>,
cc: Vec<String>,
}
fn build_undo_announce( fn build_undo_announce(
instance_url: &str, instance_url: &str,
actor_profile: &DbActorProfile, actor_profile: &DbActorProfile,
repost_id: &Uuid, repost_id: &Uuid,
recipient_id: &str, recipient_id: &str,
) -> Activity { ) -> UndoAnnounce {
let object_id = local_object_id(instance_url, repost_id); let object_id = local_object_id(instance_url, repost_id);
let activity_id = format!("{}/undo", object_id); let activity_id = format!("{}/undo", object_id);
let actor_id = local_actor_id(instance_url, &actor_profile.username);
let primary_audience = vec![ let primary_audience = vec![
AP_PUBLIC.to_string(), AP_PUBLIC.to_string(),
recipient_id.to_string(), recipient_id.to_string(),
@ -30,15 +47,15 @@ fn build_undo_announce(
let secondary_audience = vec![ let secondary_audience = vec![
local_actor_followers(instance_url, &actor_profile.username), local_actor_followers(instance_url, &actor_profile.username),
]; ];
create_activity( UndoAnnounce {
instance_url, context: AP_CONTEXT.to_string(),
&actor_profile.username, activity_type: UNDO.to_string(),
UNDO, id: activity_id,
activity_id, actor: actor_id,
object_id, object: object_id,
primary_audience, to: primary_audience,
secondary_audience, cc: secondary_audience,
) }
} }
pub async fn prepare_undo_announce_note( pub async fn prepare_undo_announce_note(
@ -71,7 +88,6 @@ pub async fn prepare_undo_announce_note(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json;
use crate::utils::id::new_uuid; use crate::utils::id::new_uuid;
use super::*; use super::*;
@ -96,6 +112,6 @@ mod tests {
activity.object, activity.object,
format!("{}/objects/{}", INSTANCE_URL, repost_id), format!("{}/objects/{}", INSTANCE_URL, repost_id),
); );
assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC, post_author_id])); assert_eq!(activity.to, vec![AP_PUBLIC, post_author_id]);
} }
} }

View file

@ -1,10 +1,11 @@
use serde::Serialize;
use tokio_postgres::GenericClient; use tokio_postgres::GenericClient;
use uuid::Uuid; use uuid::Uuid;
use crate::activitypub::{ use crate::activitypub::{
activity::{create_activity, Activity}, constants::AP_CONTEXT,
deliverer::OutgoingActivity, deliverer::OutgoingActivity,
identifiers::local_object_id, identifiers::{local_actor_id, local_object_id},
vocabulary::UNDO, vocabulary::UNDO,
}; };
use crate::config::Instance; use crate::config::Instance;
@ -17,26 +18,43 @@ use super::like_note::{
get_like_note_recipients, get_like_note_recipients,
}; };
#[derive(Serialize)]
struct UndoLike {
#[serde(rename = "@context")]
context: String,
#[serde(rename = "type")]
activity_type: String,
id: String,
actor: String,
object: String,
to: Vec<String>,
cc: Vec<String>,
}
fn build_undo_like( fn build_undo_like(
instance_url: &str, instance_url: &str,
actor_profile: &DbActorProfile, actor_profile: &DbActorProfile,
reaction_id: &Uuid, reaction_id: &Uuid,
note_author_id: &str, note_author_id: &str,
note_visibility: &Visibility, note_visibility: &Visibility,
) -> Activity { ) -> UndoLike {
let object_id = local_object_id(instance_url, reaction_id); let object_id = local_object_id(instance_url, reaction_id);
let activity_id = format!("{}/undo", object_id); let activity_id = format!("{}/undo", object_id);
let actor_id = local_actor_id(instance_url, &actor_profile.username);
let (primary_audience, secondary_audience) = let (primary_audience, secondary_audience) =
get_like_note_audience(note_author_id, note_visibility); get_like_note_audience(note_author_id, note_visibility);
create_activity( UndoLike {
instance_url, context: AP_CONTEXT.to_string(),
&actor_profile.username, activity_type: UNDO.to_string(),
UNDO, id: activity_id,
activity_id, actor: actor_id,
object_id, object: object_id,
primary_audience, to: primary_audience,
secondary_audience, cc: secondary_audience,
) }
} }
pub async fn prepare_undo_like_note( pub async fn prepare_undo_like_note(
@ -69,7 +87,6 @@ pub async fn prepare_undo_like_note(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json;
use crate::activitypub::constants::AP_PUBLIC; use crate::activitypub::constants::AP_PUBLIC;
use crate::utils::id::new_uuid; use crate::utils::id::new_uuid;
use super::*; use super::*;
@ -96,6 +113,6 @@ mod tests {
activity.object, activity.object,
format!("{}/objects/{}", INSTANCE_URL, reaction_id), format!("{}/objects/{}", INSTANCE_URL, reaction_id),
); );
assert_eq!(activity.to.unwrap(), json!([note_author_id, AP_PUBLIC])); assert_eq!(activity.to, vec![note_author_id, AP_PUBLIC]);
} }
} }