2021-09-25 15:44:52 +00:00
|
|
|
pub mod object_id;
|
|
|
|
pub mod post_or_comment;
|
2021-03-11 04:43:11 +00:00
|
|
|
pub mod search;
|
2021-01-12 16:12:41 +00:00
|
|
|
|
2021-10-18 21:36:44 +00:00
|
|
|
use crate::{
|
|
|
|
fetcher::object_id::ObjectId,
|
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
|
|
|
};
|
2021-01-12 16:12:41 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2021-10-06 20:20:05 +00:00
|
|
|
use lemmy_apub_lib::traits::ActorType;
|
2021-10-18 21:36:44 +00:00
|
|
|
use lemmy_db_schema::naive_now;
|
2021-01-12 16:12:41 +00:00
|
|
|
use lemmy_utils::LemmyError;
|
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
static ACTOR_REFETCH_INTERVAL_SECONDS: i64 = 24 * 60 * 60;
|
|
|
|
static ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG: i64 = 10;
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
/// Get a remote actor from its apub ID (either a person or a community). Thin wrapper around
|
|
|
|
/// `get_or_fetch_and_upsert_person()` and `get_or_fetch_and_upsert_community()`.
|
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-08-17 18:04:58 +00:00
|
|
|
pub(crate) async fn get_or_fetch_and_upsert_actor(
|
2021-09-25 15:44:52 +00:00
|
|
|
apub_id: Url,
|
2021-01-12 16:12:41 +00:00
|
|
|
context: &LemmyContext,
|
|
|
|
recursion_counter: &mut i32,
|
|
|
|
) -> Result<Box<dyn ActorType>, LemmyError> {
|
2021-10-18 21:36:44 +00:00
|
|
|
let community_id = ObjectId::<ApubCommunity>::new(apub_id.clone());
|
2021-09-25 15:44:52 +00:00
|
|
|
let community = community_id.dereference(context, recursion_counter).await;
|
2021-01-12 16:12:41 +00:00
|
|
|
let actor: Box<dyn ActorType> = match community {
|
|
|
|
Ok(c) => Box::new(c),
|
2021-09-25 15:44:52 +00:00
|
|
|
Err(_) => {
|
|
|
|
let person_id = ObjectId::new(apub_id);
|
2021-10-18 21:36:44 +00:00
|
|
|
let person: ApubPerson = person_id.dereference(context, recursion_counter).await?;
|
2021-09-25 15:44:52 +00:00
|
|
|
Box::new(person)
|
|
|
|
}
|
2021-01-12 16:12:41 +00:00
|
|
|
};
|
|
|
|
Ok(actor)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determines when a remote actor should be refetched from its instance. In release builds, this is
|
|
|
|
/// `ACTOR_REFETCH_INTERVAL_SECONDS` after the last refetch, in debug builds
|
|
|
|
/// `ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG`.
|
|
|
|
///
|
|
|
|
/// TODO it won't pick up new avatars, summaries etc until a day after.
|
|
|
|
/// Actors need an "update" activity pushed to other servers to fix this.
|
2021-10-27 16:03:07 +00:00
|
|
|
fn should_refetch_object(last_refreshed: NaiveDateTime) -> bool {
|
2021-01-12 16:12:41 +00:00
|
|
|
let update_interval = if cfg!(debug_assertions) {
|
|
|
|
// avoid infinite loop when fetching community outbox
|
|
|
|
chrono::Duration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG)
|
|
|
|
} else {
|
|
|
|
chrono::Duration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS)
|
|
|
|
};
|
|
|
|
last_refreshed.lt(&(naive_now() - update_interval))
|
|
|
|
}
|