cache fill server function

Orual 93dbab0e 27110a41

+54 -17
+44 -6
crates/weaver-server/src/components/entry.rs
··· 1 #![allow(non_snake_case)] 2 3 - use crate::fetch; 4 - use dioxus::prelude::*; 5 - use jacquard::{smol_str::SmolStr, types::string::AtIdentifier, CowStr}; 6 use weaver_api::sh_weaver::notebook::{entry, BookEntryView}; 7 8 #[component] 9 pub fn Entry(ident: AtIdentifier<'static>, book_title: SmolStr, title: SmolStr) -> Element { 10 let entry = use_resource(use_reactive!(|(ident, book_title, title)| async move { 11 let fetcher = use_context::<fetch::CachedFetcher>(); 12 - fetcher 13 - .get_entry(ident, book_title, title) 14 .await 15 .ok() 16 - .flatten() 17 })); 18 19 rsx! { ··· 63 } 64 } 65 }
··· 1 #![allow(non_snake_case)] 2 3 + #[allow(unused_imports)] 4 + use crate::{blobcache::BlobCache, fetch}; 5 + #[allow(unused_imports)] 6 + use dioxus::{fullstack::extract::Extension, CapturedError}; 7 + use dioxus::{ 8 + fullstack::{get_server_url, reqwest}, 9 + prelude::*, 10 + }; 11 + use jacquard::smol_str::ToSmolStr; 12 + #[allow(unused_imports)] 13 + use jacquard::{ 14 + smol_str::SmolStr, 15 + types::{cid::Cid, string::AtIdentifier}, 16 + }; 17 + #[allow(unused_imports)] 18 + use std::sync::Arc; 19 use weaver_api::sh_weaver::notebook::{entry, BookEntryView}; 20 21 #[component] 22 pub fn Entry(ident: AtIdentifier<'static>, book_title: SmolStr, title: SmolStr) -> Element { 23 let entry = use_resource(use_reactive!(|(ident, book_title, title)| async move { 24 let fetcher = use_context::<fetch::CachedFetcher>(); 25 + let entry = fetcher 26 + .get_entry(ident.clone(), book_title, title) 27 .await 28 .ok() 29 + .flatten(); 30 + if let Some(entry) = &entry { 31 + let entry = &entry.1; 32 + if let Some(embeds) = &entry.embeds { 33 + if let Some(images) = &embeds.images { 34 + for image in &images.images { 35 + let cid = image.image.blob().cid(); 36 + cache_blob( 37 + ident.to_smolstr(), 38 + cid.to_smolstr(), 39 + image.name.as_ref().map(|n| n.to_smolstr()), 40 + ) 41 + .await 42 + .ok(); 43 + } 44 + } 45 + } 46 + } 47 + entry 48 })); 49 50 rsx! { ··· 94 } 95 } 96 } 97 + 98 + #[put("/cache/{ident}/{cid}?name", cache: Extension<Arc<BlobCache>>)] 99 + pub async fn cache_blob(ident: SmolStr, cid: SmolStr, name: Option<SmolStr>) -> Result<()> { 100 + let ident = AtIdentifier::new_owned(ident)?; 101 + let cid = Cid::new_owned(cid.as_bytes())?; 102 + cache.cache(ident, cid, name).await 103 + }
+2 -2
crates/weaver-server/src/fetch.rs
··· 1 - use dioxus::{CapturedError, Result}; 2 use jacquard::{client::BasicClient, smol_str::SmolStr, types::ident::AtIdentifier}; 3 use std::{ 4 sync::{Arc, Mutex}, ··· 8 com_atproto::repo::strong_ref::StrongRef, 9 sh_weaver::notebook::{entry::Entry, BookEntryView, NotebookView}, 10 }; 11 - use weaver_common::view::{entry_by_title, fetch_entry_view, notebook_by_title, view_entry}; 12 13 #[derive(Clone)] 14 pub struct CachedFetcher {
··· 1 + use dioxus::Result; 2 use jacquard::{client::BasicClient, smol_str::SmolStr, types::ident::AtIdentifier}; 3 use std::{ 4 sync::{Arc, Mutex}, ··· 8 com_atproto::repo::strong_ref::StrongRef, 9 sh_weaver::notebook::{entry::Entry, BookEntryView, NotebookView}, 10 }; 11 + use weaver_common::view::{entry_by_title, notebook_by_title}; 12 13 #[derive(Clone)] 14 pub struct CachedFetcher {
+8 -9
crates/weaver-server/src/main.rs
··· 2 // need dioxus 3 use components::{Entry, Repository, RepositoryIndex}; 4 use dioxus::{fullstack::FullstackContext, prelude::*}; 5 - use jacquard::{ 6 - client::BasicClient, smol_str::SmolStr, types::did::Did, types::string::AtIdentifier, CowStr, 7 - }; 8 use std::sync::Arc; 9 use views::{Home, Navbar, Notebook, NotebookIndex, NotebookPage}; 10 ··· 32 Home {}, 33 #[layout(ErrorLayout)] 34 #[nest("/:ident")] 35 - #[layout(components::Repository)] 36 #[route("/")] 37 RepositoryIndex { ident: AtIdentifier<'static> }, 38 #[nest("/:book_title")] 39 - #[layout(views::Notebook)] 40 #[route("/")] 41 NotebookIndex { ident: AtIdentifier<'static>, book_title: SmolStr }, 42 #[route("/:title")] ··· 60 |mut req: Request, next: Next| async move { 61 // Attach some extra state to the request 62 63 use crate::fetch::CachedFetcher; 64 use std::convert::Infallible; 65 use std::sync::Arc; ··· 67 .insert(Arc::new(CachedFetcher::new(Arc::new( 68 BasicClient::unauthenticated(), 69 )))); 70 71 // And then return the response with `next.run() 72 Ok::<_, Infallible>(next.run(req).await) ··· 121 } 122 } 123 } 124 - 125 - struct Config { 126 - did: Option<Did<'static>>, 127 - }
··· 2 // need dioxus 3 use components::{Entry, Repository, RepositoryIndex}; 4 use dioxus::{fullstack::FullstackContext, prelude::*}; 5 + use jacquard::{client::BasicClient, smol_str::SmolStr, types::string::AtIdentifier}; 6 use std::sync::Arc; 7 use views::{Home, Navbar, Notebook, NotebookIndex, NotebookPage}; 8 ··· 30 Home {}, 31 #[layout(ErrorLayout)] 32 #[nest("/:ident")] 33 + #[layout(Repository)] 34 #[route("/")] 35 RepositoryIndex { ident: AtIdentifier<'static> }, 36 #[nest("/:book_title")] 37 + #[layout(Notebook)] 38 #[route("/")] 39 NotebookIndex { ident: AtIdentifier<'static>, book_title: SmolStr }, 40 #[route("/:title")] ··· 58 |mut req: Request, next: Next| async move { 59 // Attach some extra state to the request 60 61 + use crate::blobcache::BlobCache; 62 use crate::fetch::CachedFetcher; 63 use std::convert::Infallible; 64 use std::sync::Arc; ··· 66 .insert(Arc::new(CachedFetcher::new(Arc::new( 67 BasicClient::unauthenticated(), 68 )))); 69 + req.extensions_mut() 70 + .insert(Arc::new(BlobCache::new(Arc::new( 71 + BasicClient::unauthenticated(), 72 + )))); 73 74 // And then return the response with `next.run() 75 Ok::<_, Infallible>(next.run(req).await) ··· 124 } 125 } 126 }