Create dedicated type for Update(Person) activity

This commit is contained in:
silverpill 2022-11-07 15:01:17 +00:00
parent e750b2b718
commit fbfb6bb3c2

View file

@ -1,11 +1,11 @@
use serde::Serialize;
use serde_json::Value; use serde_json::Value;
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::{get_local_actor, Actor, ActorKeyError}, actors::types::{get_local_actor, Actor, ActorKeyError},
constants::AP_PUBLIC, constants::{AP_CONTEXT, AP_PUBLIC},
deliverer::OutgoingActivity, deliverer::OutgoingActivity,
identifiers::{local_actor_followers, local_object_id}, identifiers::{local_actor_followers, local_object_id},
vocabulary::UPDATE, vocabulary::UPDATE,
@ -16,28 +16,42 @@ use crate::models::relationships::queries::get_followers;
use crate::models::users::types::User; use crate::models::users::types::User;
use crate::utils::id::new_uuid; use crate::utils::id::new_uuid;
#[derive(Serialize)]
pub struct UpdatePerson {
#[serde(rename = "@context")]
context: String,
#[serde(rename = "type")]
activity_type: String,
id: String,
actor: String,
object: Actor,
to: Vec<String>,
}
pub fn build_update_person( pub fn build_update_person(
instance_url: &str, instance_url: &str,
user: &User, user: &User,
maybe_internal_activity_id: Option<Uuid>, maybe_internal_activity_id: Option<Uuid>,
) -> Result<Activity, ActorKeyError> { ) -> Result<UpdatePerson, ActorKeyError> {
let actor = get_local_actor(user, instance_url)?; let actor = get_local_actor(user, instance_url)?;
// Update(Person) is idempotent so its ID can be random // Update(Person) is idempotent so its ID can be random
let internal_activity_id = let internal_activity_id =
maybe_internal_activity_id.unwrap_or(new_uuid()); maybe_internal_activity_id.unwrap_or(new_uuid());
let activity_id = local_object_id(instance_url, &internal_activity_id); let activity_id = local_object_id(instance_url, &internal_activity_id);
let activity = create_activity( let activity = UpdatePerson {
instance_url, context: AP_CONTEXT.to_string(),
&user.profile.username, activity_type: UPDATE.to_string(),
UPDATE, id: activity_id,
activity_id, actor: actor.id.clone(),
actor, object: actor,
vec![ to: vec![
AP_PUBLIC.to_string(), AP_PUBLIC.to_string(),
local_actor_followers(instance_url, &user.profile.username), local_actor_followers(instance_url, &user.profile.username),
], ],
vec![], };
);
Ok(activity) Ok(activity)
} }
@ -59,7 +73,7 @@ pub async fn prepare_update_person(
db_client: &impl GenericClient, db_client: &impl GenericClient,
instance: &Instance, instance: &Instance,
user: &User, user: &User,
) -> Result<OutgoingActivity<Activity>, DatabaseError> { ) -> Result<OutgoingActivity<UpdatePerson>, DatabaseError> {
let activity = build_update_person(&instance.url(), user, None) let activity = build_update_person(&instance.url(), user, None)
.map_err(|_| ConversionError)?; .map_err(|_| ConversionError)?;
let recipients = get_update_person_recipients(db_client, &user.id).await?; let recipients = get_update_person_recipients(db_client, &user.id).await?;
@ -88,7 +102,6 @@ pub async fn prepare_signed_update_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 crate::utils::crypto::{ use crate::utils::crypto::{
generate_weak_private_key, generate_weak_private_key,
@ -121,12 +134,12 @@ mod tests {
format!("{}/objects/{}", INSTANCE_URL, internal_id), format!("{}/objects/{}", INSTANCE_URL, internal_id),
); );
assert_eq!( assert_eq!(
activity.object["id"].as_str().unwrap(), activity.object.id,
format!("{}/users/testuser", INSTANCE_URL), format!("{}/users/testuser", INSTANCE_URL),
); );
assert_eq!(activity.to.unwrap(), json!([ assert_eq!(activity.to, vec![
AP_PUBLIC, AP_PUBLIC.to_string(),
format!("{}/users/testuser/followers", INSTANCE_URL), format!("{}/users/testuser/followers", INSTANCE_URL),
])); ]);
} }
} }