1 use crate::{traits::ApubObject, utils::fetch_object_http};
3 use chrono::{Duration as ChronoDuration, NaiveDateTime, Utc};
5 use lemmy_utils::{settings::structs::Settings, LemmyError};
6 use reqwest_middleware::ClientWithMiddleware;
7 use serde::{Deserialize, Serialize};
9 fmt::{Debug, Display, Formatter},
14 /// We store Url on the heap because it is quite large (88 bytes).
15 #[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
17 pub struct ObjectId<Kind>(Box<Url>, #[serde(skip)] PhantomData<Kind>)
19 Kind: ApubObject + Send + 'static,
20 for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>;
22 impl<Kind> ObjectId<Kind>
24 Kind: ApubObject + Send + 'static,
25 for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
27 pub fn new<T>(url: T) -> Self
31 ObjectId(Box::new(url.into()), PhantomData::<Kind>)
34 pub fn inner(&self) -> &Url {
38 pub fn into_inner(self) -> Url {
42 /// Fetches an activitypub object, either from local database (if possible), or over http.
43 pub async fn dereference(
45 data: &<Kind as ApubObject>::DataType,
46 client: &ClientWithMiddleware,
47 request_counter: &mut i32,
48 ) -> Result<Kind, LemmyError> {
49 let db_object = self.dereference_from_db(data).await?;
51 // if its a local object, only fetch it from the database and not over http
52 if self.0.domain() == Some(&Settings::get().get_hostname_without_port()?) {
53 return match db_object {
54 None => Err(NotFound {}.into()),
59 // object found in database
60 if let Some(object) = db_object {
61 // object is old and should be refetched
62 if let Some(last_refreshed_at) = object.last_refreshed_at() {
63 if should_refetch_object(last_refreshed_at) {
65 .dereference_from_http(data, client, request_counter, Some(object))
71 // object not found, need to fetch over http
74 .dereference_from_http(data, client, request_counter, None)
79 /// Fetch an object from the local db. Instead of falling back to http, this throws an error if
80 /// the object is not found in the database.
81 pub async fn dereference_local(
83 data: &<Kind as ApubObject>::DataType,
84 ) -> Result<Kind, LemmyError> {
85 let object = self.dereference_from_db(data).await?;
86 object.ok_or_else(|| anyhow!("object not found in database {}", self).into())
89 /// returning none means the object was not found in local db
90 async fn dereference_from_db(
92 data: &<Kind as ApubObject>::DataType,
93 ) -> Result<Option<Kind>, LemmyError> {
94 let id = self.0.clone();
95 ApubObject::read_from_apub_id(*id, data).await
98 async fn dereference_from_http(
100 data: &<Kind as ApubObject>::DataType,
101 client: &ClientWithMiddleware,
102 request_counter: &mut i32,
103 db_object: Option<Kind>,
104 ) -> Result<Kind, LemmyError> {
105 let res = fetch_object_http(&self.0, client, request_counter).await;
107 if let Err(e) = &res {
109 if e.message == Some("410".to_string()) {
110 if let Some(db_object) = db_object {
111 db_object.delete(data).await?;
113 return Err(anyhow!("Fetched remote object {} which was deleted", self).into());
119 Kind::verify(&res2, self.inner(), data, request_counter).await?;
120 Kind::from_apub(res2, data, request_counter).await
124 static ACTOR_REFETCH_INTERVAL_SECONDS: i64 = 24 * 60 * 60;
125 static ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG: i64 = 20;
127 /// Determines when a remote actor should be refetched from its instance. In release builds, this is
128 /// `ACTOR_REFETCH_INTERVAL_SECONDS` after the last refetch, in debug builds
129 /// `ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG`.
131 /// TODO it won't pick up new avatars, summaries etc until a day after.
132 /// Actors need an "update" activity pushed to other servers to fix this.
133 fn should_refetch_object(last_refreshed: NaiveDateTime) -> bool {
134 let update_interval = if cfg!(debug_assertions) {
135 // avoid infinite loop when fetching community outbox
136 ChronoDuration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS_DEBUG)
138 ChronoDuration::seconds(ACTOR_REFETCH_INTERVAL_SECONDS)
140 let refresh_limit = Utc::now().naive_utc() - update_interval;
141 last_refreshed.lt(&refresh_limit)
144 impl<Kind> Display for ObjectId<Kind>
146 Kind: ApubObject + Send + 'static,
147 for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
149 #[allow(clippy::to_string_in_display)]
150 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
151 // Use to_string here because Url.display is not useful for us
152 write!(f, "{}", self.0)
156 impl<Kind> From<ObjectId<Kind>> for Url
158 Kind: ApubObject + Send + 'static,
159 for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
161 fn from(id: ObjectId<Kind>) -> Self {
169 use crate::object_id::should_refetch_object;
172 fn test_should_refetch_object() {
173 let one_second_ago = Utc::now().naive_utc() - ChronoDuration::seconds(1);
174 assert!(!should_refetch_object(one_second_ago));
176 let two_days_ago = Utc::now().naive_utc() - ChronoDuration::days(2);
177 assert!(should_refetch_object(two_days_ago));