Create get_or_import_profile_by_actor_address helper function

This commit is contained in:
silverpill 2023-01-11 02:16:03 +00:00
parent 0ede2093c5
commit 2385601e12
2 changed files with 40 additions and 28 deletions

View file

@ -134,6 +134,32 @@ pub async fn import_profile_by_actor_address(
Ok(profile) Ok(profile)
} }
pub async fn get_or_import_profile_by_actor_address(
db_client: &impl GenericClient,
instance: &Instance,
media_dir: &Path,
actor_address: &ActorAddress,
) -> Result<DbActorProfile, HandlerError> {
let acct = actor_address.acct(&instance.hostname());
let profile = match get_profile_by_acct(
db_client,
&acct,
).await {
Ok(profile) => profile,
Err(DatabaseError::NotFound(_)) => {
// TODO: don't fetch if address is local
import_profile_by_actor_address(
db_client,
instance,
media_dir,
actor_address,
).await?
},
Err(other_error) => return Err(other_error.into()),
};
Ok(profile)
}
pub async fn import_post( pub async fn import_post(
config: &Config, config: &Config,
db_client: &mut impl GenericClient, db_client: &mut impl GenericClient,

View file

@ -11,16 +11,15 @@ use crate::activitypub::{
constants::{AP_MEDIA_TYPE, AP_PUBLIC, AS_MEDIA_TYPE}, constants::{AP_MEDIA_TYPE, AP_PUBLIC, AS_MEDIA_TYPE},
fetcher::fetchers::fetch_file, fetcher::fetchers::fetch_file,
fetcher::helpers::{ fetcher::helpers::{
get_or_import_profile_by_actor_address,
get_or_import_profile_by_actor_id, get_or_import_profile_by_actor_id,
import_post, import_post,
import_profile_by_actor_address,
}, },
identifiers::parse_local_actor_id, identifiers::parse_local_actor_id,
receiver::{parse_array, parse_property_value, HandlerError}, receiver::{parse_array, parse_property_value, HandlerError},
vocabulary::*, vocabulary::*,
}; };
use crate::config::{Config, Instance}; use crate::config::{Config, Instance};
use crate::database::DatabaseError;
use crate::errors::{ConversionError, ValidationError}; use crate::errors::{ConversionError, ValidationError};
use crate::models::attachments::queries::create_attachment; use crate::models::attachments::queries::create_attachment;
use crate::models::posts::{ use crate::models::posts::{
@ -35,7 +34,6 @@ use crate::models::posts::{
CONTENT_MAX_SIZE, CONTENT_MAX_SIZE,
}, },
}; };
use crate::models::profiles::queries::get_profile_by_acct;
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
use crate::models::users::queries::get_user_by_name; use crate::models::users::queries::get_user_by_name;
use crate::utils::html::clean_html; use crate::utils::html::clean_html;
@ -270,14 +268,7 @@ pub async fn handle_note(
}, },
}; };
if let Ok(actor_address) = mention_to_address(&tag_name) { if let Ok(actor_address) = mention_to_address(&tag_name) {
let acct = actor_address.acct(&instance.hostname()); let profile = match get_or_import_profile_by_actor_address(
let profile = match get_profile_by_acct(
db_client,
&acct,
).await {
Ok(profile) => profile,
Err(DatabaseError::NotFound(_)) => {
match import_profile_by_actor_address(
db_client, db_client,
instance, instance,
media_dir, media_dir,
@ -288,17 +279,12 @@ pub async fn handle_note(
// Ignore mention if fetcher fails // Ignore mention if fetcher fails
log::warn!( log::warn!(
"failed to find mentioned profile {}: {}", "failed to find mentioned profile {}: {}",
acct, actor_address,
error, error,
); );
continue; continue;
}, },
Err(other_error) => { Err(other_error) => return Err(other_error),
return Err(other_error);
},
}
},
Err(other_error) => return Err(other_error.into()),
}; };
if !mentions.contains(&profile.id) { if !mentions.contains(&profile.id) {
mentions.push(profile.id); mentions.push(profile.id);