Move create_remote_profile() and update_remote_profile() to actors::helpers module
This commit is contained in:
parent
5307a28111
commit
7498fc9dba
6 changed files with 118 additions and 107 deletions
98
src/activitypub/actors/helpers.rs
Normal file
98
src/activitypub/actors/helpers.rs
Normal file
|
@ -0,0 +1,98 @@
|
|||
use std::path::Path;
|
||||
|
||||
use tokio_postgres::GenericClient;
|
||||
|
||||
use crate::activitypub::{
|
||||
actors::types::Actor,
|
||||
fetcher::fetchers::fetch_actor_images,
|
||||
receiver::HandlerError,
|
||||
};
|
||||
use crate::config::Instance;
|
||||
use crate::models::profiles::{
|
||||
queries::{create_profile, update_profile},
|
||||
types::{DbActorProfile, ProfileCreateData, ProfileUpdateData},
|
||||
};
|
||||
|
||||
pub async fn create_remote_profile(
|
||||
db_client: &impl GenericClient,
|
||||
instance: &Instance,
|
||||
media_dir: &Path,
|
||||
actor: Actor,
|
||||
) -> Result<DbActorProfile, HandlerError> {
|
||||
let actor_address = actor.address()?;
|
||||
if actor_address.hostname == instance.hostname() {
|
||||
return Err(HandlerError::LocalObject);
|
||||
};
|
||||
let (maybe_avatar, maybe_banner) = fetch_actor_images(
|
||||
instance,
|
||||
&actor,
|
||||
media_dir,
|
||||
None,
|
||||
None,
|
||||
).await;
|
||||
let (identity_proofs, payment_options, extra_fields) =
|
||||
actor.parse_attachments();
|
||||
let mut profile_data = ProfileCreateData {
|
||||
username: actor.preferred_username.clone(),
|
||||
hostname: Some(actor_address.hostname),
|
||||
display_name: actor.name.clone(),
|
||||
bio: actor.summary.clone(),
|
||||
avatar: maybe_avatar,
|
||||
banner: maybe_banner,
|
||||
identity_proofs,
|
||||
payment_options,
|
||||
extra_fields,
|
||||
actor_json: Some(actor),
|
||||
};
|
||||
profile_data.clean()?;
|
||||
let profile = create_profile(db_client, profile_data).await?;
|
||||
Ok(profile)
|
||||
}
|
||||
|
||||
/// Updates remote actor's profile
|
||||
pub async fn update_remote_profile(
|
||||
db_client: &impl GenericClient,
|
||||
instance: &Instance,
|
||||
media_dir: &Path,
|
||||
profile: DbActorProfile,
|
||||
actor: Actor,
|
||||
) -> Result<DbActorProfile, HandlerError> {
|
||||
let actor_old = profile.actor_json.ok_or(HandlerError::LocalObject)?;
|
||||
if actor_old.id != actor.id {
|
||||
log::warn!(
|
||||
"actor ID changed from {} to {}",
|
||||
actor_old.id,
|
||||
actor.id,
|
||||
);
|
||||
};
|
||||
if actor_old.public_key.public_key_pem != actor.public_key.public_key_pem {
|
||||
log::warn!(
|
||||
"actor public key changed from {} to {}",
|
||||
actor_old.public_key.public_key_pem,
|
||||
actor.public_key.public_key_pem,
|
||||
);
|
||||
};
|
||||
let (maybe_avatar, maybe_banner) = fetch_actor_images(
|
||||
instance,
|
||||
&actor,
|
||||
media_dir,
|
||||
profile.avatar_file_name,
|
||||
profile.banner_file_name,
|
||||
).await;
|
||||
let (identity_proofs, payment_options, extra_fields) =
|
||||
actor.parse_attachments();
|
||||
let mut profile_data = ProfileUpdateData {
|
||||
display_name: actor.name.clone(),
|
||||
bio: actor.summary.clone(),
|
||||
bio_source: actor.summary.clone(),
|
||||
avatar: maybe_avatar,
|
||||
banner: maybe_banner,
|
||||
identity_proofs,
|
||||
payment_options,
|
||||
extra_fields,
|
||||
actor_json: Some(actor),
|
||||
};
|
||||
profile_data.clean()?;
|
||||
let profile = update_profile(db_client, &profile.id, profile_data).await?;
|
||||
Ok(profile)
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
mod attachments;
|
||||
pub mod helpers;
|
||||
pub mod types;
|
||||
|
|
|
@ -5,11 +5,11 @@ use tokio_postgres::GenericClient;
|
|||
|
||||
use crate::activitypub::{
|
||||
activity::Object,
|
||||
actors::types::{Actor, ActorAddress},
|
||||
handlers::{
|
||||
create::handle_note,
|
||||
update::update_remote_profile,
|
||||
actors::{
|
||||
helpers::{create_remote_profile, update_remote_profile},
|
||||
types::ActorAddress,
|
||||
},
|
||||
handlers::create::handle_note,
|
||||
identifiers::parse_local_object_id,
|
||||
receiver::HandlerError,
|
||||
};
|
||||
|
@ -22,52 +22,14 @@ use crate::models::posts::types::Post;
|
|||
use crate::models::profiles::queries::{
|
||||
get_profile_by_acct,
|
||||
get_profile_by_remote_actor_id,
|
||||
create_profile,
|
||||
};
|
||||
use crate::models::profiles::types::{DbActorProfile, ProfileCreateData};
|
||||
use crate::models::profiles::types::DbActorProfile;
|
||||
use super::fetchers::{
|
||||
fetch_actor,
|
||||
fetch_actor_images,
|
||||
fetch_object,
|
||||
perform_webfinger_query,
|
||||
};
|
||||
|
||||
async fn create_remote_profile(
|
||||
db_client: &impl GenericClient,
|
||||
instance: &Instance,
|
||||
media_dir: &Path,
|
||||
actor: Actor,
|
||||
) -> Result<DbActorProfile, HandlerError> {
|
||||
let actor_address = actor.address()?;
|
||||
if actor_address.hostname == instance.hostname() {
|
||||
return Err(HandlerError::LocalObject);
|
||||
};
|
||||
let (maybe_avatar, maybe_banner) = fetch_actor_images(
|
||||
instance,
|
||||
&actor,
|
||||
media_dir,
|
||||
None,
|
||||
None,
|
||||
).await;
|
||||
let (identity_proofs, payment_options, extra_fields) =
|
||||
actor.parse_attachments();
|
||||
let mut profile_data = ProfileCreateData {
|
||||
username: actor.preferred_username.clone(),
|
||||
hostname: Some(actor_address.hostname),
|
||||
display_name: actor.name.clone(),
|
||||
bio: actor.summary.clone(),
|
||||
avatar: maybe_avatar,
|
||||
banner: maybe_banner,
|
||||
identity_proofs,
|
||||
payment_options,
|
||||
extra_fields,
|
||||
actor_json: Some(actor),
|
||||
};
|
||||
profile_data.clean()?;
|
||||
let profile = create_profile(db_client, profile_data).await?;
|
||||
Ok(profile)
|
||||
}
|
||||
|
||||
pub async fn get_or_import_profile_by_actor_id(
|
||||
db_client: &impl GenericClient,
|
||||
instance: &Instance,
|
||||
|
|
|
@ -1,17 +1,17 @@
|
|||
use std::path::Path;
|
||||
|
||||
use chrono::Utc;
|
||||
use serde_json::Value;
|
||||
use tokio_postgres::GenericClient;
|
||||
|
||||
use crate::activitypub::{
|
||||
activity::{Activity, Object},
|
||||
actors::types::Actor,
|
||||
fetcher::fetchers::fetch_actor_images,
|
||||
actors::{
|
||||
helpers::update_remote_profile,
|
||||
types::Actor,
|
||||
},
|
||||
handlers::create::get_note_content,
|
||||
vocabulary::{NOTE, PERSON},
|
||||
};
|
||||
use crate::config::{Config, Instance};
|
||||
use crate::config::Config;
|
||||
use crate::database::DatabaseError;
|
||||
use crate::errors::ValidationError;
|
||||
use crate::models::{
|
||||
|
@ -20,13 +20,9 @@ use crate::models::{
|
|||
update_post,
|
||||
},
|
||||
posts::types::PostUpdateData,
|
||||
profiles::queries::{
|
||||
get_profile_by_remote_actor_id,
|
||||
update_profile,
|
||||
},
|
||||
profiles::types::{DbActorProfile, ProfileUpdateData},
|
||||
profiles::queries::get_profile_by_remote_actor_id,
|
||||
};
|
||||
use super::{HandlerError, HandlerResult};
|
||||
use super::HandlerResult;
|
||||
|
||||
async fn handle_update_note(
|
||||
db_client: &mut impl GenericClient,
|
||||
|
@ -48,54 +44,6 @@ async fn handle_update_note(
|
|||
Ok(Some(NOTE))
|
||||
}
|
||||
|
||||
/// Updates remote actor's profile
|
||||
pub async fn update_remote_profile(
|
||||
db_client: &impl GenericClient,
|
||||
instance: &Instance,
|
||||
media_dir: &Path,
|
||||
profile: DbActorProfile,
|
||||
actor: Actor,
|
||||
) -> Result<DbActorProfile, HandlerError> {
|
||||
let actor_old = profile.actor_json.ok_or(HandlerError::LocalObject)?;
|
||||
if actor_old.id != actor.id {
|
||||
log::warn!(
|
||||
"actor ID changed from {} to {}",
|
||||
actor_old.id,
|
||||
actor.id,
|
||||
);
|
||||
};
|
||||
if actor_old.public_key.public_key_pem != actor.public_key.public_key_pem {
|
||||
log::warn!(
|
||||
"actor public key changed from {} to {}",
|
||||
actor_old.public_key.public_key_pem,
|
||||
actor.public_key.public_key_pem,
|
||||
);
|
||||
};
|
||||
let (maybe_avatar, maybe_banner) = fetch_actor_images(
|
||||
instance,
|
||||
&actor,
|
||||
media_dir,
|
||||
profile.avatar_file_name,
|
||||
profile.banner_file_name,
|
||||
).await;
|
||||
let (identity_proofs, payment_options, extra_fields) =
|
||||
actor.parse_attachments();
|
||||
let mut profile_data = ProfileUpdateData {
|
||||
display_name: actor.name.clone(),
|
||||
bio: actor.summary.clone(),
|
||||
bio_source: actor.summary.clone(),
|
||||
avatar: maybe_avatar,
|
||||
banner: maybe_banner,
|
||||
identity_proofs,
|
||||
payment_options,
|
||||
extra_fields,
|
||||
actor_json: Some(actor),
|
||||
};
|
||||
profile_data.clean()?;
|
||||
let profile = update_profile(db_client, &profile.id, profile_data).await?;
|
||||
Ok(profile)
|
||||
}
|
||||
|
||||
async fn handle_update_person(
|
||||
config: &Config,
|
||||
db_client: &impl GenericClient,
|
||||
|
|
|
@ -6,7 +6,7 @@ mod collections;
|
|||
pub mod constants;
|
||||
mod deliverer;
|
||||
pub mod fetcher;
|
||||
pub mod handlers;
|
||||
mod handlers;
|
||||
pub mod identifiers;
|
||||
mod receiver;
|
||||
pub mod views;
|
||||
|
|
10
src/cli.rs
10
src/cli.rs
|
@ -4,10 +4,12 @@ use clap::Parser;
|
|||
use tokio_postgres::GenericClient;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::activitypub::builders::delete_note::prepare_delete_note;
|
||||
use crate::activitypub::builders::delete_person::prepare_delete_person;
|
||||
use crate::activitypub::fetcher::fetchers::fetch_actor;
|
||||
use crate::activitypub::handlers::update::update_remote_profile;
|
||||
use crate::activitypub::{
|
||||
actors::helpers::update_remote_profile,
|
||||
builders::delete_note::prepare_delete_note,
|
||||
builders::delete_person::prepare_delete_person,
|
||||
fetcher::fetchers::fetch_actor,
|
||||
};
|
||||
use crate::config::Config;
|
||||
use crate::ethereum::signatures::generate_ecdsa_key;
|
||||
use crate::ethereum::sync::save_current_block_number;
|
||||
|
|
Loading…
Reference in a new issue