diff --git a/web/src/actors/follow.rs b/web/src/actors/follow.rs index 6cee437..8ede770 100644 --- a/web/src/actors/follow.rs +++ b/web/src/actors/follow.rs @@ -37,7 +37,7 @@ pub fn FollowList(outgoing: bool) -> impl IntoView { each=move || arr.clone() key=|id| id.clone() children=move |id| { - let actor = match CACHE.get(&id) { + let actor = match cache::OBJECTS.get(&id) { Some(x) => x, None => Arc::new(serde_json::Value::String(id)), }; diff --git a/web/src/actors/header.rs b/web/src/actors/header.rs index e8d1f84..0f0b425 100644 --- a/web/src/actors/header.rs +++ b/web/src/actors/header.rs @@ -12,14 +12,14 @@ pub fn ActorHeader() -> impl IntoView { move || params.get().ok().and_then(|x| x.id).unwrap_or_default(), move |id| { async move { - match CACHE.get(&Uri::full(U::Actor, &id)) { + match cache::OBJECTS.get(&Uri::full(U::Actor, &id)) { Some(x) => Ok::<_, String>(x.clone()), None => { let user : serde_json::Value = Http::fetch(&Uri::api(U::Actor, &id, true), auth) .await .map_err(|e| e.to_string())?; let user = std::sync::Arc::new(user); - CACHE.put(Uri::full(U::Actor, &id), user.clone()); + cache::OBJECTS.put(Uri::full(U::Actor, &id), user.clone()); Ok(user) }, } diff --git a/web/src/components/activity.rs b/web/src/components/activity.rs index 0bede37..09721f8 100644 --- a/web/src/components/activity.rs +++ b/web/src/components/activity.rs @@ -11,7 +11,7 @@ pub fn ActivityLine(activity: crate::Object) -> impl IntoView { "↗" }); let actor_id = activity.actor().id().str().unwrap_or_default(); - let actor = CACHE.get_or(&actor_id, serde_json::Value::String(actor_id.clone()).into()); + let actor = cache::OBJECTS.get_or(&actor_id, serde_json::Value::String(actor_id.clone()).into()); let kind = activity.activity_type().unwrap_or(apb::ActivityType::Activity); let href = match kind { apb::ActivityType::Follow => Uri::web(U::Actor, &object_id), @@ -61,11 +61,11 @@ pub fn Item( let object_id = item.object().id().str().unwrap_or_default(); let object = match t { apb::ActivityType::Create | apb::ActivityType::Announce => - CACHE.get(&object_id).map(|obj| { + cache::OBJECTS.get(&object_id).map(|obj| { view! { } }.into_view()), apb::ActivityType::Follow => - CACHE.get(&object_id).map(|obj| { + cache::OBJECTS.get(&object_id).map(|obj| { view! {
diff --git a/web/src/components/object.rs b/web/src/components/object.rs index cb0967f..72ed9ba 100644 --- a/web/src/components/object.rs +++ b/web/src/components/object.rs @@ -110,7 +110,7 @@ pub fn Object(object: crate::Object) -> impl IntoView { let oid = object.id().unwrap_or_default().to_string(); let content = mdhtml::safe_html(object.content().unwrap_or_default()); let author_id = object.attributed_to().id().str().unwrap_or_default(); - let author = CACHE.get_or(&author_id, serde_json::Value::String(author_id.clone()).into()); + let author = cache::OBJECTS.get_or(&author_id, serde_json::Value::String(author_id.clone()).into()); let sensitive = object.sensitive().unwrap_or_default(); let addressed = object.addressed(); let public = addressed.iter().any(|x| x.as_str() == apb::target::PUBLIC); @@ -265,14 +265,14 @@ pub fn LikeButton( Ok(()) => { set_clicked.set(false); set_count.set(count.get() + 1); - if let Some(cached) = CACHE.get(&target) { + if let Some(cached) = cache::OBJECTS.get(&target) { let mut new = (*cached).clone().set_liked_by_me(Some(true)); if let Some(likes) = new.likes().get() { if let Ok(count) = likes.total_items() { new = new.set_likes(apb::Node::object(likes.clone().set_total_items(Some(count + 1)))); } } - CACHE.put(target, Arc::new(new)); + cache::OBJECTS.put(target, Arc::new(new)); } }, Err(e) => tracing::error!("failed sending like: {e}"), diff --git a/web/src/components/post.rs b/web/src/components/post.rs index f4b071e..bc2aeed 100644 --- a/web/src/components/post.rs +++ b/web/src/components/post.rs @@ -1,7 +1,7 @@ use apb::{field::OptionalString, ActivityMut, Base, BaseMut, Object, ObjectMut}; use leptos::*; -use crate::{prelude::*, WEBFINGER}; +use crate::prelude::*; #[derive(Debug, Clone, Copy, Default)] pub struct ReplyControls { @@ -11,7 +11,7 @@ pub struct ReplyControls { impl ReplyControls { pub fn reply(&self, oid: &str) { - if let Some(obj) = CACHE.get(oid) { + if let Some(obj) = cache::OBJECTS.get(oid) { self.context.set(obj.context().id().str()); self.reply_to.set(obj.id().ok().map(|x| x.to_string())); } @@ -24,8 +24,8 @@ impl ReplyControls { } fn post_author(post_id: &str) -> Option { - let usr = CACHE.get(post_id)?.attributed_to().id().str()?; - CACHE.get(&usr) + let usr = cache::OBJECTS.get(post_id)?.attributed_to().id().str()?; + cache::OBJECTS.get(&usr) } #[component] @@ -51,7 +51,7 @@ pub fn PostBox(advanced: WriteSignal) -> impl IntoView { if let Some((user, domain)) = stripped.split_once('@') { if let Some(tld) = domain.split('.').last() { if tld::exist(tld) { - if let Some(uid) = WEBFINGER.blocking_resolve(user, domain).await { + if let Some(uid) = cache::WEBFINGER.blocking_resolve(user, domain).await { out.push(uid); } } @@ -84,7 +84,7 @@ pub fn PostBox(advanced: WriteSignal) -> impl IntoView { } {move || mentions.get() - .map(|x| x.into_iter().map(|u| match CACHE.get(&u) { + .map(|x| x.into_iter().map(|u| match cache::OBJECTS.get(&u) { Some(u) => view! { "📨" }.into_view(), None => view! { "📨"{u} }.into_view(), }) diff --git a/web/src/components/user.rs b/web/src/components/user.rs index d174219..954c9b8 100644 --- a/web/src/components/user.rs +++ b/web/src/components/user.rs @@ -67,7 +67,7 @@ pub fn FollowRequestButtons(activity_id: String, actor_id: String) -> impl IntoV // TODO lmao what is going on with this double move / triple clone ??????????? let _activity_id = activity_id.clone(); let _actor_id = actor_id.clone(); - let from_actor = CACHE.get(&activity_id).map(|x| x.actor().id().str().unwrap_or_default()).unwrap_or_default(); + let from_actor = cache::OBJECTS.get(&activity_id).map(|x| x.actor().id().str().unwrap_or_default()).unwrap_or_default(); let _from_actor = from_actor.clone(); if actor_id == auth.user_id() { Some(view! { diff --git a/web/src/config.rs b/web/src/config.rs index 817be60..b0f2a1d 100644 --- a/web/src/config.rs +++ b/web/src/config.rs @@ -61,7 +61,7 @@ impl FiltersConfig { let mut reply_filter = true; if let Ok(obj_id) = item.object().id() { - if let Some(obj) = crate::CACHE.get(obj_id) { + if let Some(obj) = crate::cache::OBJECTS.get(obj_id) { if obj.in_reply_to().id().is_ok() { reply_filter = self.replies; } diff --git a/web/src/lib.rs b/web/src/lib.rs index 1c92ad3..7918a62 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -26,11 +26,15 @@ pub const DEFAULT_COLOR: &str = "#BF616A"; use std::sync::Arc; use uriproxy::UriClass; +pub mod cache { + use super::{ObjectCache, WebfingerCache}; + lazy_static::lazy_static! { + pub static ref OBJECTS: ObjectCache = ObjectCache::default(); + pub static ref WEBFINGER: WebfingerCache = WebfingerCache::default(); + } +} -lazy_static::lazy_static! { - pub static ref CACHE: ObjectCache = ObjectCache::default(); - pub static ref WEBFINGER: WebfingerCache = WebfingerCache::default(); } pub type Object = Arc; diff --git a/web/src/objects/view.rs b/web/src/objects/view.rs index c126ea1..ddf7d16 100644 --- a/web/src/objects/view.rs +++ b/web/src/objects/view.rs @@ -14,7 +14,7 @@ pub fn ObjectView() -> impl IntoView { let object = create_local_resource( move || params.get().get("id").cloned().unwrap_or_default(), move |oid| async move { - let obj = match CACHE.get(&Uri::full(U::Object, &oid)) { + let obj = match cache::OBJECTS.get(&Uri::full(U::Object, &oid)) { Some(x) => x.clone(), None => { let obj = match Http::fetch::(&Uri::api(U::Object, &oid, true), auth).await { @@ -28,10 +28,10 @@ pub fn ObjectView() -> impl IntoView { if let Ok(user) = Http::fetch::( &Uri::api(U::Actor, author, true), auth ).await { - CACHE.put(Uri::full(U::Actor, author), Arc::new(user)); + cache::OBJECTS.put(Uri::full(U::Actor, author), Arc::new(user)); } } - CACHE.put(Uri::full(U::Object, &oid), obj.clone()); + cache::OBJECTS.put(Uri::full(U::Object, &oid), obj.clone()); obj } }; diff --git a/web/src/page/config.rs b/web/src/page/config.rs index 617dbda..4e01142 100644 --- a/web/src/page/config.rs +++ b/web/src/page/config.rs @@ -19,7 +19,7 @@ pub fn ConfigPage(setter: WriteSignal) -> impl IntoView { let avatar_url_ref: NodeRef = create_node_ref(); let banner_url_ref: NodeRef = create_node_ref(); - let myself = CACHE.get(&auth.userid.get_untracked().unwrap_or_default()); + let myself = cache::OBJECTS.get(&auth.userid.get_untracked().unwrap_or_default()); let curr_display_name = myself.as_ref().and_then(|x| Some(x.name().ok()?.to_string())).unwrap_or_default(); let curr_summary = myself.as_ref().and_then(|x| Some(x.summary().ok()?.to_string())).unwrap_or_default(); let curr_icon = myself.as_ref().and_then(|x| Some(x.icon().get()?.url().id().ok()?.to_string())).unwrap_or_default(); @@ -158,7 +158,7 @@ pub fn ConfigPage(setter: WriteSignal) -> impl IntoView { ); let id = auth.userid.get_untracked().unwrap_or_default(); - let Some(me) = CACHE.get(&id) else { + let Some(me) = cache::OBJECTS.get(&id) else { tracing::error!("self user not in cache! can't update"); return; }; diff --git a/web/src/page/debug.rs b/web/src/page/debug.rs index d5b91a3..2fa7fde 100644 --- a/web/src/page/debug.rs +++ b/web/src/page/debug.rs @@ -23,7 +23,7 @@ pub fn DebugPage() -> impl IntoView { set_error.set(false); if query.is_empty() { return serde_json::Value::Null }; if cached { - match CACHE.get(&query) { + match cache::OBJECTS.get(&query) { Some(x) => (*x).clone(), None => { set_error.set(true); diff --git a/web/src/prelude.rs b/web/src/prelude.rs index 71288ba..070eced 100644 --- a/web/src/prelude.rs +++ b/web/src/prelude.rs @@ -1,6 +1,7 @@ pub use crate::{ + URL_BASE, Http, Uri, - CACHE, URL_BASE, + cache, app::{Feeds, Loader}, auth::Auth, page::*, diff --git a/web/src/timeline/feed.rs b/web/src/timeline/feed.rs index 06a5a1a..117c3ac 100644 --- a/web/src/timeline/feed.rs +++ b/web/src/timeline/feed.rs @@ -19,7 +19,7 @@ pub fn Feed(tl: Timeline) -> impl IntoView { key=|k| k.to_string() let:id > - {match CACHE.get(&id) { + {match cache::OBJECTS.get(&id) { Some(i) => view! { }.into_view(), diff --git a/web/src/timeline/mod.rs b/web/src/timeline/mod.rs index e9a0367..4ca20b6 100644 --- a/web/src/timeline/mod.rs +++ b/web/src/timeline/mod.rs @@ -115,7 +115,7 @@ async fn process_activities(activities: Vec, auth: Auth) -> V actors_seen.insert(attributed_to); } if let Ok(object_uri) = object.id() { - CACHE.put(object_uri.to_string(), Arc::new(object.clone())); + cache::OBJECTS.put(object_uri.to_string(), Arc::new(object.clone())); } else { tracing::warn!("embedded object without id: {object:?}"); } @@ -136,7 +136,7 @@ async fn process_activities(activities: Vec, auth: Auth) -> V let object_id = activity.object().id().str(); if let Some(activity_id) = activity.id().str() { out.push(activity_id.to_string()); - CACHE.put( + cache::OBJECTS.put( activity_id.to_string(), Arc::new(activity.clone().set_object(apb::Node::maybe_link(object_id))) ); @@ -145,14 +145,14 @@ async fn process_activities(activities: Vec, auth: Auth) -> V } if let Some(uid) = activity.attributed_to().id().str() { - if CACHE.get(&uid).is_none() && !gonna_fetch.contains(&uid) { + 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))); } } if let Some(uid) = activity.actor().id().str() { - if CACHE.get(&uid).is_none() && !gonna_fetch.contains(&uid) { + 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))); } @@ -170,14 +170,14 @@ async fn process_activities(activities: Vec, auth: Auth) -> V async fn fetch_and_update(kind: U, id: String, auth: Auth) { match Http::fetch(&Uri::api(kind, &id, false), auth).await { - Ok(data) => CACHE.put(id, Arc::new(data)), + Ok(data) => cache::OBJECTS.put(id, Arc::new(data)), Err(e) => console_warn(&format!("could not fetch '{id}': {e}")), } } async fn fetch_and_update_with_user(kind: U, id: String, auth: Auth) { fetch_and_update(kind, id.clone(), auth).await; - if let Some(obj) = CACHE.get(&id) { + if let Some(obj) = cache::OBJECTS.get(&id) { if let Some(actor_id) = match kind { U::Object => obj.attributed_to().id().str(), U::Activity => obj.actor().id().str(), diff --git a/web/src/timeline/thread.rs b/web/src/timeline/thread.rs index 723ab61..ca88fae 100644 --- a/web/src/timeline/thread.rs +++ b/web/src/timeline/thread.rs @@ -35,11 +35,11 @@ fn FeedRecursive(tl: Timeline, root: String) -> impl IntoView { .get() .into_iter() .filter_map(|x| { - let document = CACHE.get(&x)?; + let document = cache::OBJECTS.get(&x)?; let (oid, reply) = match document.object_type().ok()? { // if it's a create, get and check created object: does it reply to root? apb::ObjectType::Activity(apb::ActivityType::Create) => { - let object = CACHE.get(document.object().id().ok()?)?; + let object = cache::OBJECTS.get(document.object().id().ok()?)?; (object.id().str()?, object.in_reply_to().id().str()?) },