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 serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use serde_json::Value;
use super::constants::AP_CONTEXT;
use super::identifiers::local_actor_id;
use super::vocabulary::*;
use super::vocabulary::HASHTAG;
#[derive(Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
@ -123,25 +121,3 @@ pub struct Activity {
#[serde(skip_serializing_if = "Option::is_none")]
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")]
activity_type: String,
actor: String,
id: String,
actor: String,
object: String,
published: DateTime<Utc>,

View file

@ -3,7 +3,7 @@ use serde::Serialize;
use tokio_postgres::GenericClient;
use crate::activitypub::{
activity::{create_activity, Activity, Attachment, Tag},
activity::{Attachment, Tag},
actors::types::Actor,
constants::{AP_MEDIA_TYPE, AP_CONTEXT, AP_PUBLIC},
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(
instance_hostname: &str,
instance_url: &str,
post: &Post,
) -> Activity {
) -> CreateNote {
let object = build_note(instance_hostname, instance_url, post);
let primary_audience = object.to.clone();
let secondary_audience = object.cc.clone();
let activity_id = format!("{}/create", object.id);
let activity = create_activity(
instance_url,
&post.author.username,
CREATE,
activity_id,
object,
primary_audience,
secondary_audience,
);
activity
CreateNote {
context: AP_CONTEXT.to_string(),
activity_type: CREATE.to_string(),
id: activity_id,
actor: object.attributed_to.clone(),
object: object,
to: primary_audience,
cc: secondary_audience,
}
}
pub async fn get_note_recipients(
@ -240,7 +255,6 @@ pub async fn prepare_create_note(
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::models::profiles::types::DbActorProfile;
use super::*;
@ -422,6 +436,9 @@ mod tests {
activity.actor,
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 crate::activitypub::activity::{create_activity, Activity, Object};
use crate::activitypub::constants::AP_CONTEXT;
use crate::activitypub::deliverer::OutgoingActivity;
use crate::activitypub::vocabulary::{DELETE, NOTE, TOMBSTONE};
use crate::activitypub::{
constants::AP_CONTEXT,
deliverer::OutgoingActivity,
identifiers::local_actor_id,
vocabulary::{DELETE, NOTE, TOMBSTONE},
};
use crate::config::Instance;
use crate::database::DatabaseError;
use crate::models::posts::helpers::add_related_posts;
@ -16,35 +18,59 @@ use super::create_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(
instance_hostname: &str,
instance_url: &str,
post: &Post,
) -> Activity {
) -> DeleteNote {
let object_id = post.object_id(instance_url);
let object = Object {
context: Some(json!(AP_CONTEXT)),
id: object_id,
object_type: TOMBSTONE.to_string(),
former_type: Some(NOTE.to_string()),
..Default::default()
};
let activity_id = format!("{}/delete", object.id);
let activity_id = format!("{}/delete", object_id);
let actor_id = local_actor_id(instance_url, &post.author.username);
let Note { to, cc, .. } = build_note(
instance_hostname,
instance_url,
post,
);
let activity = create_activity(
instance_url,
&post.author.username,
DELETE,
activity_id,
object,
to,
cc,
);
activity
DeleteNote {
context: AP_CONTEXT.to_string(),
activity_type: DELETE.to_string(),
id: activity_id,
actor: actor_id,
object: Tombstone {
id: object_id,
object_type: TOMBSTONE.to_string(),
former_type: NOTE.to_string(),
},
to: to,
cc: cc,
}
}
pub async fn prepare_delete_note(
@ -72,7 +98,6 @@ pub async fn prepare_delete_note(
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::activitypub::{
constants::AP_PUBLIC,
identifiers::local_actor_followers,
@ -101,13 +126,14 @@ mod tests {
format!("{}/objects/{}/delete", INSTANCE_URL, post.id),
);
assert_eq!(
activity.object["id"],
activity.object.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!(
activity.cc.unwrap(),
json!([local_actor_followers(INSTANCE_URL, "author")]),
activity.cc,
vec![local_actor_followers(INSTANCE_URL, "author")],
);
}
}

View file

@ -1,10 +1,10 @@
use serde::Serialize;
use tokio_postgres::GenericClient;
use uuid::Uuid;
use crate::activitypub::{
activity::{create_activity, Activity},
actors::types::Actor,
constants::AP_PUBLIC,
constants::{AP_CONTEXT, AP_PUBLIC},
deliverer::OutgoingActivity,
vocabulary::DELETE,
};
@ -13,21 +13,35 @@ use crate::database::DatabaseError;
use crate::models::relationships::queries::{get_followers, get_following};
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(
instance_url: &str,
user: &User,
) -> Activity {
) -> DeletePerson {
let actor_id = user.profile.actor_id(instance_url);
let activity_id = format!("{}/delete", actor_id);
create_activity(
instance_url,
&user.profile.username,
DELETE,
activity_id,
actor_id,
vec![AP_PUBLIC.to_string()],
vec![],
)
DeletePerson {
context: AP_CONTEXT.to_string(),
activity_type: DELETE.to_string(),
id: activity_id,
actor: actor_id.clone(),
object: actor_id,
to: vec![AP_PUBLIC.to_string()],
}
}
async fn get_delete_person_recipients(
@ -62,7 +76,6 @@ pub async fn prepare_delete_person(
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::models::profiles::types::DbActorProfile;
use super::*;
@ -82,10 +95,11 @@ mod tests {
activity.id,
format!("{}/users/testuser/delete", INSTANCE_URL),
);
assert_eq!(activity.actor, activity.object);
assert_eq!(
activity.object,
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 uuid::Uuid;
use crate::activitypub::{
activity::{create_activity, Activity},
actors::types::Actor,
constants::AP_PUBLIC,
constants::{AP_CONTEXT, AP_PUBLIC},
deliverer::OutgoingActivity,
identifiers::local_object_id,
identifiers::{local_actor_id, local_object_id},
vocabulary::LIKE,
};
use crate::config::Instance;
@ -15,6 +15,22 @@ use crate::models::posts::types::{Post, Visibility};
use crate::models::profiles::types::DbActorProfile;
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(
note_author_id: &str,
note_visibility: &Visibility,
@ -34,20 +50,20 @@ fn build_like_note(
reaction_id: &Uuid,
note_author_id: &str,
note_visibility: &Visibility,
) -> Activity {
) -> Like {
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) =
get_like_note_audience(note_author_id, note_visibility);
let activity = create_activity(
instance_url,
&actor_profile.username,
LIKE,
activity_id,
note_id,
primary_audience,
secondary_audience,
);
activity
Like {
context: AP_CONTEXT.to_string(),
activity_type: LIKE.to_string(),
id: activity_id,
actor: actor_id,
object: note_id.to_string(),
to: primary_audience,
cc: secondary_audience,
}
}
pub async fn get_like_note_recipients(
@ -94,7 +110,6 @@ pub async fn prepare_like_note(
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::utils::id::new_uuid;
use super::*;
@ -118,7 +133,8 @@ mod tests {
activity.id,
format!("{}/objects/{}", INSTANCE_URL, reaction_id),
);
assert_eq!(activity.object, json!(note_id));
assert_eq!(activity.to.unwrap(), json!([note_author_id, AP_PUBLIC]));
assert_eq!(activity.object, note_id);
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 uuid::Uuid;
use crate::activitypub::{
activity::{create_activity, Activity},
constants::AP_PUBLIC,
constants::{AP_CONTEXT, AP_PUBLIC},
deliverer::OutgoingActivity,
identifiers::{local_actor_followers, local_object_id},
identifiers::{local_actor_id, local_actor_followers, local_object_id},
vocabulary::UNDO,
};
use crate::config::Instance;
@ -15,14 +15,31 @@ use crate::models::profiles::types::DbActorProfile;
use crate::models::users::types::User;
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(
instance_url: &str,
actor_profile: &DbActorProfile,
repost_id: &Uuid,
recipient_id: &str,
) -> Activity {
) -> UndoAnnounce {
let object_id = local_object_id(instance_url, repost_id);
let activity_id = format!("{}/undo", object_id);
let actor_id = local_actor_id(instance_url, &actor_profile.username);
let primary_audience = vec![
AP_PUBLIC.to_string(),
recipient_id.to_string(),
@ -30,15 +47,15 @@ fn build_undo_announce(
let secondary_audience = vec![
local_actor_followers(instance_url, &actor_profile.username),
];
create_activity(
instance_url,
&actor_profile.username,
UNDO,
activity_id,
object_id,
primary_audience,
secondary_audience,
)
UndoAnnounce {
context: AP_CONTEXT.to_string(),
activity_type: UNDO.to_string(),
id: activity_id,
actor: actor_id,
object: object_id,
to: primary_audience,
cc: secondary_audience,
}
}
pub async fn prepare_undo_announce_note(
@ -71,7 +88,6 @@ pub async fn prepare_undo_announce_note(
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::utils::id::new_uuid;
use super::*;
@ -96,6 +112,6 @@ mod tests {
activity.object,
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 uuid::Uuid;
use crate::activitypub::{
activity::{create_activity, Activity},
constants::AP_CONTEXT,
deliverer::OutgoingActivity,
identifiers::local_object_id,
identifiers::{local_actor_id, local_object_id},
vocabulary::UNDO,
};
use crate::config::Instance;
@ -17,26 +18,43 @@ use super::like_note::{
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(
instance_url: &str,
actor_profile: &DbActorProfile,
reaction_id: &Uuid,
note_author_id: &str,
note_visibility: &Visibility,
) -> Activity {
) -> UndoLike {
let object_id = local_object_id(instance_url, reaction_id);
let activity_id = format!("{}/undo", object_id);
let actor_id = local_actor_id(instance_url, &actor_profile.username);
let (primary_audience, secondary_audience) =
get_like_note_audience(note_author_id, note_visibility);
create_activity(
instance_url,
&actor_profile.username,
UNDO,
activity_id,
object_id,
primary_audience,
secondary_audience,
)
UndoLike {
context: AP_CONTEXT.to_string(),
activity_type: UNDO.to_string(),
id: activity_id,
actor: actor_id,
object: object_id,
to: primary_audience,
cc: secondary_audience,
}
}
pub async fn prepare_undo_like_note(
@ -69,7 +87,6 @@ pub async fn prepare_undo_like_note(
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::activitypub::constants::AP_PUBLIC;
use crate::utils::id::new_uuid;
use super::*;
@ -96,6 +113,6 @@ mod tests {
activity.object,
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]);
}
}