9 community::get_or_fetch_and_upsert_community,
11 person::get_or_fetch_and_upsert_person,
15 use chrono::NaiveDateTime;
17 use lemmy_db_schema::naive_now;
18 use lemmy_utils::LemmyError;
19 use lemmy_websocket::LemmyContext;
20 use serde::Deserialize;
23 static ACTOR_REFETCH_INTERVAL_SECONDS: i64 = 24 * 60 * 60;
24 static ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG: i64 = 10;
26 fn is_deleted<Response>(fetch_response: &Result<Response, FetchError>) -> bool
28 Response: for<'de> Deserialize<'de>,
30 if let Err(e) = fetch_response {
31 if let Some(status) = e.status_code {
32 if status == StatusCode::GONE {
40 /// Get a remote actor from its apub ID (either a person or a community). Thin wrapper around
41 /// `get_or_fetch_and_upsert_person()` and `get_or_fetch_and_upsert_community()`.
43 /// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
44 /// Otherwise it is fetched from the remote instance, stored and returned.
45 pub(crate) async fn get_or_fetch_and_upsert_actor(
47 context: &LemmyContext,
48 recursion_counter: &mut i32,
49 ) -> Result<Box<dyn ActorType>, LemmyError> {
50 let community = get_or_fetch_and_upsert_community(apub_id, context, recursion_counter).await;
51 let actor: Box<dyn ActorType> = match community {
53 Err(_) => Box::new(get_or_fetch_and_upsert_person(apub_id, context, recursion_counter).await?),
58 /// Determines when a remote actor should be refetched from its instance. In release builds, this is
59 /// `ACTOR_REFETCH_INTERVAL_SECONDS` after the last refetch, in debug builds
60 /// `ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG`.
62 /// TODO it won't pick up new avatars, summaries etc until a day after.
63 /// Actors need an "update" activity pushed to other servers to fix this.
64 fn should_refetch_actor(last_refreshed: NaiveDateTime) -> bool {
65 let update_interval = if cfg!(debug_assertions) {
66 // avoid infinite loop when fetching community outbox
67 chrono::Duration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG)
69 chrono::Duration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS)
71 last_refreshed.lt(&(naive_now() - update_interval))