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