Re-fetch remote profiles automatically
This commit is contained in:
parent
2a626a1859
commit
5d3e5dc693
3 changed files with 28 additions and 7 deletions
|
@ -5,7 +5,10 @@ use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::activity::Object;
|
use crate::activitypub::activity::Object;
|
||||||
use crate::activitypub::actor::ActorAddress;
|
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::activitypub::receiver::parse_object_id;
|
||||||
use crate::config::{Config, Instance};
|
use crate::config::{Config, Instance};
|
||||||
use crate::errors::{DatabaseError, HttpError, ValidationError};
|
use crate::errors::{DatabaseError, HttpError, ValidationError};
|
||||||
|
@ -18,6 +21,7 @@ use crate::models::profiles::queries::{
|
||||||
};
|
};
|
||||||
use crate::models::profiles::types::DbActorProfile;
|
use crate::models::profiles::types::DbActorProfile;
|
||||||
use super::fetchers::{
|
use super::fetchers::{
|
||||||
|
fetch_actor,
|
||||||
fetch_object,
|
fetch_object,
|
||||||
fetch_profile_by_actor_id,
|
fetch_profile_by_actor_id,
|
||||||
perform_webfinger_query,
|
perform_webfinger_query,
|
||||||
|
@ -62,7 +66,16 @@ pub async fn get_or_import_profile_by_actor_id(
|
||||||
return Err(ImportError::LocalObject);
|
return Err(ImportError::LocalObject);
|
||||||
};
|
};
|
||||||
let profile = match get_profile_by_actor_id(db_client, actor_id).await {
|
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(_)) => {
|
Err(DatabaseError::NotFound(_)) => {
|
||||||
let mut profile_data = fetch_profile_by_actor_id(
|
let mut profile_data = fetch_profile_by_actor_id(
|
||||||
instance, actor_id, media_dir,
|
instance, actor_id, media_dir,
|
||||||
|
|
|
@ -14,7 +14,7 @@ use crate::models::profiles::queries::{
|
||||||
get_profile_by_actor_id,
|
get_profile_by_actor_id,
|
||||||
update_profile,
|
update_profile,
|
||||||
};
|
};
|
||||||
use crate::models::profiles::types::ProfileUpdateData;
|
use crate::models::profiles::types::{DbActorProfile, ProfileUpdateData};
|
||||||
use super::HandlerResult;
|
use super::HandlerResult;
|
||||||
|
|
||||||
pub async fn handle_update_person(
|
pub async fn handle_update_person(
|
||||||
|
@ -35,7 +35,7 @@ pub async fn update_actor(
|
||||||
db_client: &impl GenericClient,
|
db_client: &impl GenericClient,
|
||||||
media_dir: &Path,
|
media_dir: &Path,
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
) -> Result<(), ImportError> {
|
) -> Result<DbActorProfile, ImportError> {
|
||||||
let profile = get_profile_by_actor_id(db_client, &actor.id).await?;
|
let profile = get_profile_by_actor_id(db_client, &actor.id).await?;
|
||||||
let actor_old = profile.actor_json.ok_or(ImportError::LocalObject)?;
|
let actor_old = profile.actor_json.ok_or(ImportError::LocalObject)?;
|
||||||
if actor_old.id != actor.id {
|
if actor_old.id != actor.id {
|
||||||
|
@ -65,6 +65,6 @@ pub async fn update_actor(
|
||||||
actor_json: Some(actor),
|
actor_json: Some(actor),
|
||||||
};
|
};
|
||||||
profile_data.clean()?;
|
profile_data.clean()?;
|
||||||
update_profile(db_client, &profile.id, profile_data).await?;
|
let profile = update_profile(db_client, &profile.id, profile_data).await?;
|
||||||
Ok(())
|
Ok(profile)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Duration, Utc};
|
||||||
use postgres_types::FromSql;
|
use postgres_types::FromSql;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
@ -112,6 +112,14 @@ impl DbActorProfile {
|
||||||
self.acct.clone()
|
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)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in a new issue