1
0
Fork 0
forked from alemi/upub

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]
pub fn Actor() -> impl IntoView {
pub fn UserPage() -> impl IntoView {
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 {
let uid = format!("{URL_BASE}/users/{uid}");
match CTX.cache.actors.get(&uid) {
Some(x) => x.clone(),
None => reqwest::get(uid)
.await
.unwrap()
.json::<serde_json::Value>()
.await
.unwrap(),
let uri = web_uri("users", &id);
match CTX.cache.actors.get(&uri) {
Some(x) => Some(x.clone()),
None => {
let user = reqwest::get(&uri)
.await
.ok()?
.json::<serde_json::Value>()
.await
.ok()?;
CTX.cache.actors.insert(uri, user.clone());
Some(user)
},
}
}
});
view! {
{move || match actor.get() {
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">
<ActorBanner object=x.clone() />
<p
@ -389,7 +394,7 @@ async fn fetch_activities_with_users(
// TODO don't fail whole timeline fetch when one user fails fetching...
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)))
}

View file

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