2021-01-12 16:12:41 +00:00
|
|
|
use crate::{
|
|
|
|
fetcher::{fetch::fetch_remote_object, is_deleted, should_refetch_actor},
|
|
|
|
objects::FromApub,
|
|
|
|
PersonExt,
|
|
|
|
};
|
|
|
|
use anyhow::anyhow;
|
|
|
|
use diesel::result::Error::NotFound;
|
2021-03-01 13:08:41 +00:00
|
|
|
use lemmy_api_structs::blocking;
|
2021-03-10 22:33:55 +00:00
|
|
|
use lemmy_db_queries::{source::person::Person_, ApubObject};
|
|
|
|
use lemmy_db_schema::source::person::Person;
|
2021-01-12 16:12:41 +00:00
|
|
|
use lemmy_utils::LemmyError;
|
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
use log::debug;
|
|
|
|
use url::Url;
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
/// Get a person from its apub ID.
|
2021-01-12 16:12:41 +00:00
|
|
|
///
|
|
|
|
/// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
|
|
|
|
/// Otherwise it is fetched from the remote instance, stored and returned.
|
2021-03-10 22:33:55 +00:00
|
|
|
pub(crate) async fn get_or_fetch_and_upsert_person(
|
2021-01-12 16:12:41 +00:00
|
|
|
apub_id: &Url,
|
|
|
|
context: &LemmyContext,
|
|
|
|
recursion_counter: &mut i32,
|
2021-03-10 22:33:55 +00:00
|
|
|
) -> Result<Person, LemmyError> {
|
2021-01-12 16:12:41 +00:00
|
|
|
let apub_id_owned = apub_id.to_owned();
|
2021-03-10 22:33:55 +00:00
|
|
|
let person = blocking(context.pool(), move |conn| {
|
|
|
|
Person::read_from_apub_id(conn, &apub_id_owned.into())
|
2021-01-12 16:12:41 +00:00
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
match person {
|
2021-01-12 16:12:41 +00:00
|
|
|
// If its older than a day, re-fetch it
|
|
|
|
Ok(u) if !u.local && should_refetch_actor(u.last_refreshed_at) => {
|
2021-03-10 22:33:55 +00:00
|
|
|
debug!("Fetching and updating from remote person: {}", apub_id);
|
2021-01-12 16:12:41 +00:00
|
|
|
let person =
|
|
|
|
fetch_remote_object::<PersonExt>(context.client(), apub_id, recursion_counter).await;
|
|
|
|
|
|
|
|
if is_deleted(&person) {
|
2021-03-10 22:33:55 +00:00
|
|
|
// TODO: use Person::update_deleted() once implemented
|
2021-01-12 16:12:41 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
Person::delete_account(conn, u.id)
|
2021-01-12 16:12:41 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2021-03-10 22:33:55 +00:00
|
|
|
return Err(anyhow!("Person was deleted by remote instance").into());
|
2021-01-12 16:12:41 +00:00
|
|
|
} else if person.is_err() {
|
|
|
|
return Ok(u);
|
|
|
|
}
|
|
|
|
|
2021-03-19 16:11:34 +00:00
|
|
|
let person = Person::from_apub(
|
2021-03-12 15:43:01 +00:00
|
|
|
&person?,
|
|
|
|
context,
|
2021-03-16 17:26:19 +00:00
|
|
|
apub_id.to_owned(),
|
2021-03-12 15:43:01 +00:00
|
|
|
recursion_counter,
|
2021-03-16 17:26:19 +00:00
|
|
|
false,
|
2021-03-12 15:43:01 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2021-01-12 16:12:41 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = person.id;
|
2021-01-12 16:12:41 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
Person::mark_as_updated(conn, person_id)
|
2021-01-12 16:12:41 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
Ok(person)
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
|
|
|
Ok(u) => Ok(u),
|
|
|
|
Err(NotFound {}) => {
|
2021-03-10 22:33:55 +00:00
|
|
|
debug!("Fetching and creating remote person: {}", apub_id);
|
2021-01-12 16:12:41 +00:00
|
|
|
let person =
|
|
|
|
fetch_remote_object::<PersonExt>(context.client(), apub_id, recursion_counter).await?;
|
|
|
|
|
2021-03-19 16:11:34 +00:00
|
|
|
let person = Person::from_apub(
|
2021-03-12 15:43:01 +00:00
|
|
|
&person,
|
|
|
|
context,
|
2021-03-16 17:26:19 +00:00
|
|
|
apub_id.to_owned(),
|
2021-03-12 15:43:01 +00:00
|
|
|
recursion_counter,
|
2021-03-16 17:26:19 +00:00
|
|
|
false,
|
2021-03-12 15:43:01 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2021-01-12 16:12:41 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
Ok(person)
|
2021-01-12 16:12:41 +00:00
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}
|
|
|
|
}
|