From c56e69aa20bd413c15f4e1f8fefbc195620df299 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 28 Dec 2024 20:18:01 +0100 Subject: [PATCH] fix(web): properly fetch attributed_to/quote_url also use cache::resolve so we centralize fetching a bit --- web/src/actors/header.rs | 3 --- web/src/lib.rs | 5 +++++ web/src/objects/view.rs | 6 ++++++ web/src/timeline/mod.rs | 37 ++++++++++--------------------------- 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/web/src/actors/header.rs b/web/src/actors/header.rs index b73c57f..ee9721e 100644 --- a/web/src/actors/header.rs +++ b/web/src/actors/header.rs @@ -17,9 +17,6 @@ pub fn ActorHeader() -> impl IntoView { Some(x) => Some(x.clone()), None => { let user = cache::OBJECTS.resolve(&id, U::Actor, auth).await?; - if let Ok(url) = user.url().id() { - cache::WEBFINGER.store(&url, user.id().unwrap_or_default().to_string()); - } Some(user) }, } diff --git a/web/src/lib.rs b/web/src/lib.rs index 4402439..2461f0a 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -108,6 +108,11 @@ impl DashmapCache { }, }; cache::OBJECTS.store(&full_key, obj.clone()); + if matches!(kind, UriClass::Actor) { + if let Ok(url) = apb::Object::url(&*obj).id() { // TODO name clashes + cache::WEBFINGER.store(&url, full_key); + } + } Some(obj) }, } diff --git a/web/src/objects/view.rs b/web/src/objects/view.rs index eb8f34c..8a9412a 100644 --- a/web/src/objects/view.rs +++ b/web/src/objects/view.rs @@ -19,9 +19,15 @@ pub fn ObjectView() -> impl IntoView { move |(oid, _loading)| async move { tracing::info!("rerunning fetcher"); let obj = cache::OBJECTS.resolve(&oid, U::Object, auth).await?; + + // TODO these two can be parallelized if let Ok(author) = obj.attributed_to().id() { cache::OBJECTS.resolve(&author, U::Actor, auth).await; } + if let Ok(quote) = obj.quote_url().id() { + cache::OBJECTS.resolve("e, U::Object, auth).await; + } + Some(obj) // if let Ok(ctx) = obj.context().id() { diff --git a/web/src/timeline/mod.rs b/web/src/timeline/mod.rs index 3bc463e..27a1a68 100644 --- a/web/src/timeline/mod.rs +++ b/web/src/timeline/mod.rs @@ -169,20 +169,20 @@ pub async fn process_activities(activities: Vec, auth: Auth) if let Ok(uid) = activity.attributed_to().id() { if cache::OBJECTS.get(&uid).is_none() && !gonna_fetch.contains(&uid) { gonna_fetch.insert(uid.clone()); - sub_tasks.push(Box::pin(fetch_and_update(U::Actor, uid, auth))); + sub_tasks.push(Box::pin(deep_fetch_and_update(U::Actor, uid, auth))); } } if let Ok(uid) = activity.actor().id() { if cache::OBJECTS.get(&uid).is_none() && !gonna_fetch.contains(&uid) { gonna_fetch.insert(uid.clone()); - sub_tasks.push(Box::pin(fetch_and_update(U::Actor, uid, auth))); + sub_tasks.push(Box::pin(deep_fetch_and_update(U::Actor, uid, auth))); } } } for user in actors_seen { - sub_tasks.push(Box::pin(fetch_and_update(U::Actor, user, auth))); + sub_tasks.push(Box::pin(deep_fetch_and_update(U::Actor, user, auth))); } futures::future::join_all(sub_tasks).await; @@ -190,33 +190,16 @@ pub async fn process_activities(activities: Vec, auth: Auth) out } -async fn fetch_and_update(kind: U, id: String, auth: Auth) { - match Http::fetch::(&Uri::api(kind, &id, false), auth).await { - Err(e) => console_warn(&format!("could not fetch '{id}': {e}")), - Ok(data) => { - if data.actor_type().is_ok() { - if let Ok(url) = data.url().id() { - cache::WEBFINGER.store(&id, url); - } - } - cache::OBJECTS.store(&id, Arc::new(data)); - }, - } -} - async fn deep_fetch_and_update(kind: U, id: String, auth: Auth) { - fetch_and_update(kind, id.clone(), auth).await; - if let Some(obj) = cache::OBJECTS.get(&id) { + if let Some(obj) = cache::OBJECTS.resolve(&id, kind, auth).await { if let Ok(quote) = obj.quote_url().id() { - fetch_and_update(U::Object, quote, auth).await; + cache::OBJECTS.resolve("e, U::Object, auth).await; } - if let Some(actor_id) = match kind { - U::Object => obj.attributed_to().id().ok(), - U::Activity => obj.actor().id().ok(), - U::Actor => None, - U::Hashtag => None, - } { - fetch_and_update(U::Actor, actor_id, auth).await; + if let Ok(actor) = obj.actor().id() { + cache::OBJECTS.resolve(&actor, U::Actor, auth).await; + } + if let Ok(attributed_to) = obj.attributed_to().id() { + cache::OBJECTS.resolve(&attributed_to, U::Actor, auth).await; } } }