From cd67cbbb2827c161aa6e99c93fe57f5500cbb789 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Thu, 10 Nov 2022 16:32:36 -0800 Subject: pds: more web view implementation --- adenosine-pds/Cargo.toml | 2 +- adenosine-pds/src/bsky.rs | 2 +- adenosine-pds/src/lib.rs | 104 ++++++++++++----------------- adenosine-pds/src/models.rs | 2 +- adenosine-pds/src/repo.rs | 2 +- adenosine-pds/src/web.rs | 59 +++++++++++++--- adenosine-pds/templates/account.html | 36 ++++++++++ adenosine-pds/templates/adenosine.css | 74 +++++++++++++++++++- adenosine-pds/templates/at_collection.html | 7 +- adenosine-pds/templates/at_record.html | 7 +- adenosine-pds/templates/at_repo.html | 16 +++-- adenosine-pds/templates/base.html | 10 +-- adenosine-pds/templates/macro.html | 41 ++++++++++++ adenosine-pds/templates/post.html | 5 -- adenosine-pds/templates/profile.html | 7 -- adenosine-pds/templates/thread.html | 8 +++ 16 files changed, 276 insertions(+), 106 deletions(-) create mode 100644 adenosine-pds/templates/account.html create mode 100644 adenosine-pds/templates/macro.html delete mode 100644 adenosine-pds/templates/post.html delete mode 100644 adenosine-pds/templates/profile.html create mode 100644 adenosine-pds/templates/thread.html (limited to 'adenosine-pds') diff --git a/adenosine-pds/Cargo.toml b/adenosine-pds/Cargo.toml index 1637164..7118956 100644 --- a/adenosine-pds/Cargo.toml +++ b/adenosine-pds/Cargo.toml @@ -43,7 +43,7 @@ ucan = "0.7.0-alpha.1" bs58 = "*" async-trait = "*" dotenv = "*" -askama = "0.11" +askama = { version = "0.11", features = ["serde-json"] } time = { version = "*", features = ["formatting"] } # for vendored iroh-car diff --git a/adenosine-pds/src/bsky.rs b/adenosine-pds/src/bsky.rs index 66b05eb..a4d025b 100644 --- a/adenosine-pds/src/bsky.rs +++ b/adenosine-pds/src/bsky.rs @@ -248,7 +248,7 @@ pub fn bsky_get_thread( _srv: &mut AtpService, _uri: &AtUri, _depth: Option, -) -> Result { +) -> Result { // TODO: what is the best way to implement this? recurisvely? just first-level children to // start? unimplemented!() diff --git a/adenosine-pds/src/lib.rs b/adenosine-pds/src/lib.rs index 7b517f2..894f4a2 100644 --- a/adenosine-pds/src/lib.rs +++ b/adenosine-pds/src/lib.rs @@ -172,10 +172,10 @@ impl AtpService { (GET) ["/"] => { let host = request.header("Host").unwrap_or("localhost"); if Some(host.to_string()) == config.registration_domain { + web_wrap(account_view_handler(&srv, &host, request)) + } else { let view = GenericHomeView { domain: host.to_string() }; Response::html(view.render().unwrap()) - } else { - web_wrap(home_profile_handler(&srv, request)) } }, (GET) ["/about"] => { @@ -184,19 +184,19 @@ impl AtpService { Response::html(view.render().unwrap()) }, (GET) ["/u/{handle}", handle: String] => { - web_wrap(profile_handler(&srv, &handle, request)) + web_wrap(account_view_handler(&srv, &handle, request)) }, - (GET) ["/u/{did}/{collection}/{tid}", did: Did, collection: Nsid, tid: Tid] => { - web_wrap(post_handler(&srv, &did, &collection, &tid, request)) + (GET) ["/u/{did}/post/{tid}", did: Did, tid: Tid] => { + web_wrap(thread_view_handler(&srv, &did, &tid, request)) }, (GET) ["/at/{did}", did: Did] => { - web_wrap(repo_handler(&srv, &did, request)) + web_wrap(repo_view_handler(&srv, &did, request)) }, (GET) ["/at/{did}/{collection}", did: Did, collection: Nsid] => { - web_wrap(collection_handler(&srv, &did, &collection, request)) + web_wrap(collection_view_handler(&srv, &did, &collection, request)) }, (GET) ["/at/{did}/{collection}/{tid}", did: Did, collection: Nsid, tid: Tid] => { - web_wrap(record_handler(&srv, &did, &collection, &tid, request)) + web_wrap(record_view_handler(&srv, &did, &collection, &tid, request)) }, // ============ Static Files (compiled in to executable) (GET) ["/static/adenosine.css"] => { @@ -660,77 +660,57 @@ fn xrpc_post_handler( } } -fn home_profile_handler(srv: &Mutex, request: &Request) -> Result { - let host = request.header("Host").unwrap_or("localhost"); - // XXX - let did = Did::from_str(host)?; - let mut _srv = srv.lock().expect("service mutex"); - - // TODO: get profile (bsky helper) - // TODO: get feed (bsky helper) - - Ok(ProfileView { - domain: host.to_string(), - did: did, - profile: json!({}), - feed: vec![], - } - .render()?) -} - // TODO: did, collection, tid have already been parsed by this point -fn profile_handler(srv: &Mutex, did: &str, request: &Request) -> Result { +fn account_view_handler( + srv: &Mutex, + handle: &str, + request: &Request, +) -> Result { let host = request.header("Host").unwrap_or("localhost"); - let did = Did::from_str(did)?; - let mut _srv = srv.lock().expect("service mutex"); - - // TODO: get profile (bsky helper) - // TODO: get feed (bsky helper) + let mut srv = srv.lock().expect("service mutex"); + // TODO: unwrap as 404 + let did = srv + .atp_db + .resolve_handle(handle)? + .ok_or(XrpcError::NotFound(format!( + "no DID found for handle: {}", + handle + )))?; - Ok(ProfileView { + Ok(AccountView { domain: host.to_string(), - did: did, - profile: json!({}), - feed: vec![], + did: did.clone(), + profile: bsky_get_profile(&mut srv, &did)?, + feed: bsky_get_author_feed(&mut srv, &did)?.feed, } .render()?) } -fn post_handler( +fn thread_view_handler( srv: &Mutex, - did: &str, - collection: &str, - tid: &str, + handle: &str, + tid: &Tid, request: &Request, ) -> Result { let host = request.header("Host").unwrap_or("localhost"); - let did = Did::from_str(did)?; - let collection = Nsid::from_str(collection)?; - let rkey = Tid::from_str(tid)?; + let collection = Nsid::from_str("app.bsky.feed.post")?; let mut srv = srv.lock().expect("service mutex"); + // TODO: not unwrap + let did = srv.atp_db.resolve_handle(handle)?.unwrap(); - let post = match srv.repo.get_atp_record(&did, &collection, &rkey) { - // TODO: format as JSON, not text debug - Ok(Some(ipld)) => ipld_into_json_value(ipld), - Ok(None) => Err(anyhow!(XrpcError::NotFound(format!( - "could not find record: /{}/{}", - collection, rkey - ))))?, - Err(e) => Err(e)?, - }; - - Ok(PostView { + // TODO: could construct URI directly + let uri = AtUri::from_str(&format!("at://{}/{}/{}", did, collection, tid))?; + Ok(ThreadView { domain: host.to_string(), - did: did, - collection: collection, - tid: rkey, - post_text: post["text"].as_str().unwrap().to_string(), // TODO: unwrap - post_created_at: "some-time".to_string(), + did, + collection, + tid: tid.clone(), + thread: bsky_get_thread(&mut srv, &uri, None)?.thread, } .render()?) } -fn repo_handler(srv: &Mutex, did: &str, request: &Request) -> Result { +fn repo_view_handler(srv: &Mutex, did: &str, request: &Request) -> Result { let host = request.header("Host").unwrap_or("localhost"); let did = Did::from_str(did)?; @@ -756,7 +736,7 @@ fn repo_handler(srv: &Mutex, did: &str, request: &Request) -> Result .render()?) } -fn collection_handler( +fn collection_view_handler( srv: &Mutex, did: &str, collection: &str, @@ -794,7 +774,7 @@ fn collection_handler( .render()?) } -fn record_handler( +fn record_view_handler( srv: &Mutex, did: &str, collection: &str, diff --git a/adenosine-pds/src/models.rs b/adenosine-pds/src/models.rs index 0f47c2d..3af780e 100644 --- a/adenosine-pds/src/models.rs +++ b/adenosine-pds/src/models.rs @@ -155,7 +155,7 @@ pub struct PostReply { #[allow(non_snake_case)] #[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] pub struct PostThread { - pub thread: ThreadItem, + pub thread: Vec, } #[allow(non_snake_case)] diff --git a/adenosine-pds/src/repo.rs b/adenosine-pds/src/repo.rs index 1b24be8..9f5fca0 100644 --- a/adenosine-pds/src/repo.rs +++ b/adenosine-pds/src/repo.rs @@ -17,7 +17,7 @@ use std::collections::HashSet; use std::path::PathBuf; use std::str::FromStr; -#[derive(Debug)] +#[derive(Debug, serde::Serialize)] pub struct RepoCommit { pub sig: Box<[u8]>, pub commit_cid: Cid, diff --git a/adenosine-pds/src/web.rs b/adenosine-pds/src/web.rs index e783b5a..5b9d6de 100644 --- a/adenosine-pds/src/web.rs +++ b/adenosine-pds/src/web.rs @@ -17,23 +17,22 @@ pub struct AboutView { } #[derive(Template)] -#[template(path = "profile.html")] -pub struct ProfileView { +#[template(path = "account.html")] +pub struct AccountView { pub domain: String, pub did: Did, - pub profile: serde_json::Value, - pub feed: Vec, + pub profile: Profile, + pub feed: Vec, } #[derive(Template)] -#[template(path = "post.html")] -pub struct PostView { +#[template(path = "thread.html")] +pub struct ThreadView { pub domain: String, pub did: Did, pub collection: Nsid, pub tid: Tid, - pub post_text: String, - pub post_created_at: String, + pub thread: Vec, } #[derive(Template)] @@ -63,3 +62,47 @@ pub struct RecordView { pub tid: Tid, pub record: serde_json::Value, } + +mod filters { + use crate::AtUri; + use std::str::FromStr; + + pub fn aturi_to_path(aturi: &str) -> ::askama::Result { + let aturi = AtUri::from_str(aturi).expect("aturi parse"); + if aturi.record.is_some() { + Ok(format!( + "/at/{}/{}/{}", + aturi.repository, + aturi.collection.unwrap(), + aturi.record.unwrap() + )) + } else if aturi.collection.is_some() { + Ok(format!( + "/at/{}/{}", + aturi.repository, + aturi.collection.unwrap() + )) + } else { + Ok(format!("/at/{}", aturi.repository)) + } + } + + pub fn aturi_to_thread_path(aturi: &str) -> ::askama::Result { + let aturi = AtUri::from_str(aturi).expect("aturi parse"); + Ok(format!( + "/u/{}/post/{}", + aturi.repository, + aturi.record.unwrap() + )) + } + + pub fn aturi_to_tid(aturi: &str) -> ::askama::Result { + let aturi = AtUri::from_str(aturi).expect("aturi parse"); + if aturi.record.is_some() { + Ok(aturi.record.unwrap()) + } else { + // TODO: raise an askama error here? + Ok("".to_string()) + } + } +} diff --git a/adenosine-pds/templates/account.html b/adenosine-pds/templates/account.html new file mode 100644 index 0000000..bd015d5 --- /dev/null +++ b/adenosine-pds/templates/account.html @@ -0,0 +1,36 @@ +{% extends "base.html" %} +{% import "macro.html" as macro %} + +{% block main %} + + + +{% if feed.len() == 0 %} +
--- no posts yet! ---
+{% else %} +
--- showing {{ feed.len() }} of {{ profile.postsCount }} posts ---
+{% endif %} + +{% for item in feed %} + {% call macro::feed_item(item) %} +{% endfor %} + + +{% endblock %} diff --git a/adenosine-pds/templates/adenosine.css b/adenosine-pds/templates/adenosine.css index 0686f9e..9a0f890 100644 --- a/adenosine-pds/templates/adenosine.css +++ b/adenosine-pds/templates/adenosine.css @@ -622,5 +622,75 @@ progress:indeterminate::-moz-progress-bar { } /********** adenosine tweaks **********/ -body { font-family: var(--mono-font); } -a { text-decoration: none; } +body { + font-family: var(--mono-font); +} +a { + text-decoration: none; +} +main { + font-size: smaller; + padding-top: 0px; +} +h2 { + margin-top: 0px; + margin-bottom: 1rem; + font-size: 2.5em; +} +nav.header { + border-bottom: 2px dashed var(--border); +} +nav.header img { + vertical-align: middle; +} +nav.header span { + vertical-align: middle; + color: var(--text); +} +nav.header h1 { + font-weight: normal; + font-size: 2rem; + margin-top: 1rem; + margin-bottom: 0.5rem; +} +.ident, a:hover.ident, a:visited.ident { + color: var(--text-light); +} +.smaller { + font-size: smaller; +} +body footer { + border-top: 2px dashed var(--border); + padding: 1rem 1rem 1rem 1rem; +} +.counts { + color: var(--text-light); +} +.counts a { + color: var(--text-light); +} +p.counts { + margin-bottom: 0px; +} +.display_name { + color: green; + font-weight: normal; +} +.handle { + color: green; + font-weight: bold; +} +.feed_item { + margin-top: 1rem; + margin-bottom: 1rem; +} +.profile h4 { + margin-top: 0px; + margin-bottom: 0px; +} +.repo_aturi, .repo_aturi a, .repo_aturi a:visited { + color: var(--code); + font-size: smaller; + overflow: auto; + font-weight: bold; +} diff --git a/adenosine-pds/templates/at_collection.html b/adenosine-pds/templates/at_collection.html index b4b7036..670cb0d 100644 --- a/adenosine-pds/templates/at_collection.html +++ b/adenosine-pds/templates/at_collection.html @@ -1,12 +1,13 @@ {% extends "base.html" %} {% block main %} -

at://{{ did }}/{{ collection }}/ +

ATP Repository Explorer

+
at://{{ did }}/{{ collection }}/
-

records: +

Records

{% endblock %} diff --git a/adenosine-pds/templates/at_record.html b/adenosine-pds/templates/at_record.html index 10ed838..829ed4a 100644 --- a/adenosine-pds/templates/at_record.html +++ b/adenosine-pds/templates/at_record.html @@ -1,8 +1,9 @@ {% extends "base.html" %} {% block main %} -

at://{{ did }}/{{ collection }}/{{ tid }} +

ATP Repository Explorer

+ -

record json: -

{{ record }}
+

JSON

+
{{ record|json }}
{% endblock %} diff --git a/adenosine-pds/templates/at_repo.html b/adenosine-pds/templates/at_repo.html index dcf4d87..384b8dc 100644 --- a/adenosine-pds/templates/at_repo.html +++ b/adenosine-pds/templates/at_repo.html @@ -1,18 +1,20 @@ {% extends "base.html" %} {% block main %} -

at://{{ did }}/ +

ATP Repository Explorer

+
at://{{ did }}/
-

collections: +

Collections

-

repo commit: -

{{ "{:?}"|format(commit) }}
+

Describe

+
{{ describe|json }}
+ +

Commit Node

+
{{ commit|json }}
-

repo describe -

{{ "{:?}"|format(describe) }}
{% endblock %} diff --git a/adenosine-pds/templates/base.html b/adenosine-pds/templates/base.html index 0ae2061..cb88c0c 100644 --- a/adenosine-pds/templates/base.html +++ b/adenosine-pds/templates/base.html @@ -9,11 +9,11 @@ {% block head %}{% endblock %} -