diff options
| author | Bryan Newbold <bnewbold@robocracy.org> | 2022-11-10 16:32:36 -0800 | 
|---|---|---|
| committer | Bryan Newbold <bnewbold@robocracy.org> | 2022-11-10 16:32:36 -0800 | 
| commit | cd67cbbb2827c161aa6e99c93fe57f5500cbb789 (patch) | |
| tree | 74288ae72c0f7ad07cf1f77416b180ab9a4f65c0 /adenosine-pds/src | |
| parent | 5e149eef22d34e5a2b2081de3533dee3373d47f8 (diff) | |
| download | adenosine-cd67cbbb2827c161aa6e99c93fe57f5500cbb789.tar.gz adenosine-cd67cbbb2827c161aa6e99c93fe57f5500cbb789.zip  | |
pds: more web view implementation
Diffstat (limited to 'adenosine-pds/src')
| -rw-r--r-- | adenosine-pds/src/bsky.rs | 2 | ||||
| -rw-r--r-- | adenosine-pds/src/lib.rs | 104 | ||||
| -rw-r--r-- | adenosine-pds/src/models.rs | 2 | ||||
| -rw-r--r-- | adenosine-pds/src/repo.rs | 2 | ||||
| -rw-r--r-- | adenosine-pds/src/web.rs | 59 | 
5 files changed, 96 insertions, 73 deletions
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<u64>, -) -> Result<GenericFeed> { +) -> Result<PostThread> {      // 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<AtpService>, request: &Request) -> Result<String> { -    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<AtpService>, did: &str, request: &Request) -> Result<String> { +fn account_view_handler( +    srv: &Mutex<AtpService>, +    handle: &str, +    request: &Request, +) -> Result<String> {      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<AtpService>, -    did: &str, -    collection: &str, -    tid: &str, +    handle: &str, +    tid: &Tid,      request: &Request,  ) -> Result<String> {      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<AtpService>, did: &str, request: &Request) -> Result<String> { +fn repo_view_handler(srv: &Mutex<AtpService>, did: &str, request: &Request) -> Result<String> {      let host = request.header("Host").unwrap_or("localhost");      let did = Did::from_str(did)?; @@ -756,7 +736,7 @@ fn repo_handler(srv: &Mutex<AtpService>, did: &str, request: &Request) -> Result      .render()?)  } -fn collection_handler( +fn collection_view_handler(      srv: &Mutex<AtpService>,      did: &str,      collection: &str, @@ -794,7 +774,7 @@ fn collection_handler(      .render()?)  } -fn record_handler( +fn record_view_handler(      srv: &Mutex<AtpService>,      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<ThreadItem>,  }  #[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<serde_json::Value>, +    pub profile: Profile, +    pub feed: Vec<FeedItem>,  }  #[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<ThreadItem>,  }  #[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<String> { +        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<String> { +        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<String> { +        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("<MISSING>".to_string()) +        } +    } +}  | 
