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,35 +268,23 @@ 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, db_client,
&acct, instance,
media_dir,
&actor_address,
).await { ).await {
Ok(profile) => profile, Ok(profile) => profile,
Err(DatabaseError::NotFound(_)) => { Err(HandlerError::FetchError(error)) => {
match import_profile_by_actor_address( // Ignore mention if fetcher fails
db_client, log::warn!(
instance, "failed to find mentioned profile {}: {}",
media_dir, actor_address,
&actor_address, error,
).await { );
Ok(profile) => profile, continue;
Err(HandlerError::FetchError(error)) => {
// Ignore mention if fetcher fails
log::warn!(
"failed to find mentioned profile {}: {}",
acct,
error,
);
continue;
},
Err(other_error) => {
return Err(other_error);
},
}
}, },
Err(other_error) => return Err(other_error.into()), Err(other_error) => return Err(other_error),
}; };
if !mentions.contains(&profile.id) { if !mentions.contains(&profile.id) {
mentions.push(profile.id); mentions.push(profile.id);