2022-06-01 21:39:23 +00:00
|
|
|
use tokio_postgres::GenericClient;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
use crate::activitypub::{
|
|
|
|
activity::{create_activity, Activity},
|
2022-07-23 21:37:21 +00:00
|
|
|
actors::types::{get_local_actor, Actor, ActorKeyError},
|
2022-06-01 21:39:23 +00:00
|
|
|
constants::AP_PUBLIC,
|
|
|
|
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;
|
|
|
|
|
|
|
|
fn build_update_person(
|
|
|
|
instance_url: &str,
|
|
|
|
user: &User,
|
|
|
|
) -> Result<Activity, ActorKeyError> {
|
|
|
|
let actor = get_local_actor(user, instance_url)?;
|
|
|
|
// Update(Person) is idempotent so its ID can be random
|
2022-07-15 17:31:02 +00:00
|
|
|
let activity_id = local_object_id(instance_url, &new_uuid());
|
2022-06-01 21:39:23 +00:00
|
|
|
let activity = create_activity(
|
|
|
|
instance_url,
|
|
|
|
&user.profile.username,
|
|
|
|
UPDATE,
|
|
|
|
activity_id,
|
|
|
|
actor,
|
|
|
|
vec![
|
|
|
|
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
|
|
|
],
|
|
|
|
vec![],
|
|
|
|
);
|
|
|
|
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-07-13 23:40:33 +00:00
|
|
|
) -> Result<OutgoingActivity<Activity>, DatabaseError> {
|
2022-06-01 21:39:23 +00:00
|
|
|
let activity = build_update_person(&instance.url(), user)
|
|
|
|
.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,
|
|
|
|
})
|
|
|
|
}
|