2 fetcher::{fetch::fetch_remote_object, is_deleted, should_refetch_actor},
7 use diesel::result::Error::NotFound;
8 use lemmy_api_structs::blocking;
9 use lemmy_db_queries::{source::person::Person_, ApubObject};
10 use lemmy_db_schema::source::person::Person;
11 use lemmy_utils::LemmyError;
12 use lemmy_websocket::LemmyContext;
16 /// Get a person from its apub ID.
18 /// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
19 /// Otherwise it is fetched from the remote instance, stored and returned.
20 pub(crate) async fn get_or_fetch_and_upsert_person(
22 context: &LemmyContext,
23 recursion_counter: &mut i32,
24 ) -> Result<Person, LemmyError> {
25 let apub_id_owned = apub_id.to_owned();
26 let person = blocking(context.pool(), move |conn| {
27 Person::read_from_apub_id(conn, &apub_id_owned.into())
32 // If its older than a day, re-fetch it
33 Ok(u) if !u.local && should_refetch_actor(u.last_refreshed_at) => {
34 debug!("Fetching and updating from remote person: {}", apub_id);
36 fetch_remote_object::<PersonExt>(context.client(), apub_id, recursion_counter).await;
38 if is_deleted(&person) {
39 // TODO: use Person::update_deleted() once implemented
40 blocking(context.pool(), move |conn| {
41 Person::delete_account(conn, u.id)
44 return Err(anyhow!("Person was deleted by remote instance").into());
45 } else if person.is_err() {
49 let person = Person::from_apub(
58 let person_id = person.id;
59 blocking(context.pool(), move |conn| {
60 Person::mark_as_updated(conn, person_id)
68 debug!("Fetching and creating remote person: {}", apub_id);
70 fetch_remote_object::<PersonExt>(context.client(), apub_id, recursion_counter).await?;
72 let person = Person::from_apub(
83 Err(e) => Err(e.into()),