Re-fetch remote profiles automatically

This commit is contained in:
silverpill 2022-07-08 20:32:08 +00:00
parent 2a626a1859
commit 5d3e5dc693
3 changed files with 28 additions and 7 deletions

View file

@ -5,7 +5,10 @@ use tokio_postgres::GenericClient;
use crate::activitypub::activity::Object;
use crate::activitypub::actor::ActorAddress;
use crate::activitypub::handlers::create_note::handle_note;
use crate::activitypub::handlers::{
create_note::handle_note,
update_person::update_actor,
};
use crate::activitypub::receiver::parse_object_id;
use crate::config::{Config, Instance};
use crate::errors::{DatabaseError, HttpError, ValidationError};
@ -18,6 +21,7 @@ use crate::models::profiles::queries::{
};
use crate::models::profiles::types::DbActorProfile;
use super::fetchers::{
fetch_actor,
fetch_object,
fetch_profile_by_actor_id,
perform_webfinger_query,
@ -62,7 +66,16 @@ pub async fn get_or_import_profile_by_actor_id(
return Err(ImportError::LocalObject);
};
let profile = match get_profile_by_actor_id(db_client, actor_id).await {
Ok(profile) => profile,
Ok(profile) => {
if profile.possibly_outdated() {
let actor = fetch_actor(instance, actor_id).await?;
log::info!("re-fetched profile {}", profile.acct);
let profile = update_actor(db_client, media_dir, actor).await?;
profile
} else {
profile
}
},
Err(DatabaseError::NotFound(_)) => {
let mut profile_data = fetch_profile_by_actor_id(
instance, actor_id, media_dir,

View file

@ -14,7 +14,7 @@ use crate::models::profiles::queries::{
get_profile_by_actor_id,
update_profile,
};
use crate::models::profiles::types::ProfileUpdateData;
use crate::models::profiles::types::{DbActorProfile, ProfileUpdateData};
use super::HandlerResult;
pub async fn handle_update_person(
@ -35,7 +35,7 @@ pub async fn update_actor(
db_client: &impl GenericClient,
media_dir: &Path,
actor: Actor,
) -> Result<(), ImportError> {
) -> Result<DbActorProfile, ImportError> {
let profile = get_profile_by_actor_id(db_client, &actor.id).await?;
let actor_old = profile.actor_json.ok_or(ImportError::LocalObject)?;
if actor_old.id != actor.id {
@ -65,6 +65,6 @@ pub async fn update_actor(
actor_json: Some(actor),
};
profile_data.clean()?;
update_profile(db_client, &profile.id, profile_data).await?;
Ok(())
let profile = update_profile(db_client, &profile.id, profile_data).await?;
Ok(profile)
}

View file

@ -1,4 +1,4 @@
use chrono::{DateTime, Utc};
use chrono::{DateTime, Duration, Utc};
use postgres_types::FromSql;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
@ -112,6 +112,14 @@ impl DbActorProfile {
self.acct.clone()
}
}
pub fn possibly_outdated(&self) -> bool {
if self.is_local() {
false
} else {
self.updated_at < Utc::now() - Duration::days(1)
}
}
}
#[cfg(test)]