fix(web): proper caching of users

This commit is contained in:
əlemi 2024-04-15 23:11:36 +02:00
parent 1cf170c16a
commit 920ef598f5
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 20 additions and 15 deletions

View file

@ -167,26 +167,31 @@ pub fn ActorBanner(object: serde_json::Value) -> impl IntoView {
} }
#[component] #[component]
pub fn Actor() -> impl IntoView { pub fn UserPage() -> impl IntoView {
let params = use_params_map(); let params = use_params_map();
let actor = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), |uid| { let actor = create_local_resource(move || params.get().get("id").cloned().unwrap_or_default(), |id| {
async move { async move {
let uid = format!("{URL_BASE}/users/{uid}"); let uri = web_uri("users", &id);
match CTX.cache.actors.get(&uid) { match CTX.cache.actors.get(&uri) {
Some(x) => x.clone(), Some(x) => Some(x.clone()),
None => reqwest::get(uid) None => {
.await let user = reqwest::get(&uri)
.unwrap() .await
.json::<serde_json::Value>() .ok()?
.await .json::<serde_json::Value>()
.unwrap(), .await
.ok()?;
CTX.cache.actors.insert(uri, user.clone());
Some(user)
},
} }
} }
}); });
view! { view! {
{move || match actor.get() { {move || match actor.get() {
None => view! { <p>loading...</p> }.into_view(), None => view! { <p>loading...</p> }.into_view(),
Some(x) => view! { Some(None) => view! { <p><code>error loading</code></p> }.into_view(),
Some(Some(x)) => view! {
<div class="ml-3 mr-3 mt-3"> <div class="ml-3 mr-3 mt-3">
<ActorBanner object=x.clone() /> <ActorBanner object=x.clone() />
<p <p
@ -389,7 +394,7 @@ async fn fetch_activities_with_users(
// TODO don't fail whole timeline fetch when one user fails fetching... // TODO don't fail whole timeline fetch when one user fails fetching...
let actor = req.send().await?.json::<serde_json::Value>().await?; let actor = req.send().await?.json::<serde_json::Value>().await?;
CTX.cache.actors.insert(uid, actor.clone()); CTX.cache.actors.insert(web_uri("users", &uid), actor.clone());
out.push(x.set_actor(apb::Node::object(actor))) out.push(x.set_actor(apb::Node::object(actor)))
} }

View file

@ -3,7 +3,7 @@ use leptos_router::*;
use leptos_use::{use_cookie, utils::FromToStringCodec}; use leptos_use::{use_cookie, utils::FromToStringCodec};
use upub_web::{ use upub_web::{
Actor, LoginBox, ObjectPage, PostBox, Timeline LoginBox, ObjectPage, UserPage, PostBox, Timeline
}; };
@ -44,7 +44,7 @@ fn main() {
<Routes> <Routes>
<Route path="/" view=move || view! { <Redirect path="/web" /> } /> <Route path="/" view=move || view! { <Redirect path="/web" /> } />
<Route path="/web" view=move || view! { <Timeline token=cookie /> } /> <Route path="/web" view=move || view! { <Timeline token=cookie /> } />
<Route path="/web/users/:id" view=Actor /> <Route path="/web/users/:id" view=UserPage />
<Route path="/web/objects/:id" view=ObjectPage /> <Route path="/web/objects/:id" view=ObjectPage />
</Routes> </Routes>
</main> </main>