2022-11-07 15:01:17 +00:00
|
|
|
use serde::Serialize;
|
2022-10-29 00:24:17 +00:00
|
|
|
use serde_json::Value;
|
2022-06-01 21:39:23 +00:00
|
|
|
use tokio_postgres::GenericClient;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::activitypub::{
|
2022-07-23 21:37:21 +00:00
|
|
|
actors::types::{get_local_actor, Actor, ActorKeyError},
|
2022-11-07 15:01:17 +00:00
|
|
|
constants::{AP_CONTEXT, AP_PUBLIC},
|
2022-06-01 21:39:23 +00:00
|
|
|
deliverer::OutgoingActivity,
|
2022-07-15 17:31:02 +00:00
|
|
|
identifiers::{local_actor_followers, local_object_id},
|
2022-06-01 21:39:23 +00:00
|
|
|
vocabulary::UPDATE,
|
|
|
|
};
|
|
|
|
use crate::config::Instance;
|
|
|
|
use crate::errors::{ConversionError, DatabaseError};
|
|
|
|
use crate::models::relationships::queries::get_followers;
|
|
|
|
use crate::models::users::types::User;
|
|
|
|
use crate::utils::id::new_uuid;
|
|
|
|
|
2022-11-07 15:01:17 +00:00
|
|
|
#[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>,
|
|
|
|
}
|
|
|
|
|
2022-10-29 20:19:39 +00:00
|
|
|
pub fn build_update_person(
|
2022-06-01 21:39:23 +00:00
|
|
|
instance_url: &str,
|
|
|
|
user: &User,
|
2022-10-29 20:19:39 +00:00
|
|
|
maybe_internal_activity_id: Option<Uuid>,
|
2022-11-07 15:01:17 +00:00
|
|
|
) -> Result<UpdatePerson, ActorKeyError> {
|
2022-06-01 21:39:23 +00:00
|
|
|
let actor = get_local_actor(user, instance_url)?;
|
|
|
|
// Update(Person) is idempotent so its ID can be random
|
2022-10-29 20:19:39 +00:00
|
|
|
let internal_activity_id =
|
|
|
|
maybe_internal_activity_id.unwrap_or(new_uuid());
|
|
|
|
let activity_id = local_object_id(instance_url, &internal_activity_id);
|
2022-11-07 15:01:17 +00:00
|
|
|
let activity = UpdatePerson {
|
|
|
|
context: AP_CONTEXT.to_string(),
|
|
|
|
activity_type: UPDATE.to_string(),
|
|
|
|
id: activity_id,
|
|
|
|
actor: actor.id.clone(),
|
|
|
|
object: actor,
|
|
|
|
to: vec![
|
2022-06-01 21:39:23 +00:00
|
|
|
AP_PUBLIC.to_string(),
|
2022-07-15 17:31:02 +00:00
|
|
|
local_actor_followers(instance_url, &user.profile.username),
|
2022-06-01 21:39:23 +00:00
|
|
|
],
|
2022-11-07 15:01:17 +00:00
|
|
|
};
|
2022-06-01 21:39:23 +00:00
|
|
|
Ok(activity)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_update_person_recipients(
|
|
|
|
db_client: &impl GenericClient,
|
|
|
|
user_id: &Uuid,
|
|
|
|
) -> Result<Vec<Actor>, DatabaseError> {
|
2022-08-06 22:13:47 +00:00
|
|
|
let followers = get_followers(db_client, user_id).await?;
|
2022-06-01 21:39:23 +00:00
|
|
|
let mut recipients: Vec<Actor> = Vec::new();
|
|
|
|
for profile in followers {
|
|
|
|
if let Some(remote_actor) = profile.actor_json {
|
|
|
|
recipients.push(remote_actor);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
Ok(recipients)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn prepare_update_person(
|
|
|
|
db_client: &impl GenericClient,
|
2022-10-22 00:15:28 +00:00
|
|
|
instance: &Instance,
|
2022-06-01 21:39:23 +00:00
|
|
|
user: &User,
|
2022-11-07 15:01:17 +00:00
|
|
|
) -> Result<OutgoingActivity<UpdatePerson>, DatabaseError> {
|
2022-10-29 20:19:39 +00:00
|
|
|
let activity = build_update_person(&instance.url(), user, None)
|
2022-06-01 21:39:23 +00:00
|
|
|
.map_err(|_| ConversionError)?;
|
|
|
|
let recipients = get_update_person_recipients(db_client, &user.id).await?;
|
|
|
|
Ok(OutgoingActivity {
|
2022-10-22 00:15:28 +00:00
|
|
|
instance: instance.clone(),
|
2022-06-01 21:39:23 +00:00
|
|
|
sender: user.clone(),
|
|
|
|
activity,
|
|
|
|
recipients,
|
|
|
|
})
|
|
|
|
}
|
2022-10-29 00:24:17 +00:00
|
|
|
|
|
|
|
pub async fn prepare_signed_update_person(
|
|
|
|
db_client: &impl GenericClient,
|
|
|
|
instance: &Instance,
|
|
|
|
user: &User,
|
|
|
|
activity: Value,
|
|
|
|
) -> Result<OutgoingActivity<Value>, DatabaseError> {
|
|
|
|
let recipients = get_update_person_recipients(db_client, &user.id).await?;
|
|
|
|
Ok(OutgoingActivity {
|
|
|
|
instance: instance.clone(),
|
|
|
|
sender: user.clone(),
|
|
|
|
activity,
|
|
|
|
recipients,
|
|
|
|
})
|
|
|
|
}
|
2022-11-07 15:32:49 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::models::profiles::types::DbActorProfile;
|
2022-11-13 18:43:57 +00:00
|
|
|
use crate::utils::crypto_rsa::{
|
|
|
|
generate_weak_rsa_key,
|
2022-11-07 15:32:49 +00:00
|
|
|
serialize_private_key,
|
|
|
|
};
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
const INSTANCE_URL: &str = "https://example.com";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_build_update_person() {
|
2022-11-13 18:43:57 +00:00
|
|
|
let private_key = generate_weak_rsa_key().unwrap();
|
2022-11-07 15:32:49 +00:00
|
|
|
let private_key_pem = serialize_private_key(&private_key).unwrap();
|
|
|
|
let user = User {
|
|
|
|
private_key: private_key_pem,
|
|
|
|
profile: DbActorProfile {
|
|
|
|
username: "testuser".to_string(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let internal_id = new_uuid();
|
|
|
|
let activity = build_update_person(
|
|
|
|
INSTANCE_URL,
|
|
|
|
&user,
|
|
|
|
Some(internal_id),
|
|
|
|
).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
activity.id,
|
|
|
|
format!("{}/objects/{}", INSTANCE_URL, internal_id),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2022-11-07 15:01:17 +00:00
|
|
|
activity.object.id,
|
2022-11-07 15:32:49 +00:00
|
|
|
format!("{}/users/testuser", INSTANCE_URL),
|
|
|
|
);
|
2022-11-07 15:01:17 +00:00
|
|
|
assert_eq!(activity.to, vec![
|
|
|
|
AP_PUBLIC.to_string(),
|
2022-11-07 15:32:49 +00:00
|
|
|
format!("{}/users/testuser/followers", INSTANCE_URL),
|
2022-11-07 15:01:17 +00:00
|
|
|
]);
|
2022-11-07 15:32:49 +00:00
|
|
|
}
|
|
|
|
}
|