From ec2bf0c54245cd84f492847d2a1e070919b14a53 Mon Sep 17 00:00:00 2001 From: bryan newbold Date: Sun, 19 Feb 2023 17:01:07 -0800 Subject: more refactoring of common code and APIs --- adenosine-pds/Cargo.toml | 32 +- adenosine-pds/src/bin/adenosine-pds.rs | 6 +- adenosine-pds/src/bsky.rs | 773 ------------------------------- adenosine-pds/src/db.rs | 15 +- adenosine-pds/src/db_bsky.rs | 778 ++++++++++++++++++++++++++++++++ adenosine-pds/src/lib.rs | 134 ++---- adenosine-pds/src/models.rs | 246 ---------- adenosine-pds/src/web.rs | 11 +- adenosine-pds/tests/bigger.car | Bin 60050 -> 0 bytes adenosine-pds/tests/example_repo.car | Bin 7390 -> 0 bytes adenosine-pds/tests/test_mst_interop.rs | 152 ------- adenosine-pds/tests/test_repro_mst.rs | 26 -- 12 files changed, 852 insertions(+), 1321 deletions(-) delete mode 100644 adenosine-pds/src/bsky.rs create mode 100644 adenosine-pds/src/db_bsky.rs delete mode 100644 adenosine-pds/src/models.rs delete mode 100644 adenosine-pds/tests/bigger.car delete mode 100644 adenosine-pds/tests/example_repo.car delete mode 100644 adenosine-pds/tests/test_mst_interop.rs delete mode 100644 adenosine-pds/tests/test_repro_mst.rs (limited to 'adenosine-pds') diff --git a/adenosine-pds/Cargo.toml b/adenosine-pds/Cargo.toml index 41cfaae..f9dba1a 100644 --- a/adenosine-pds/Cargo.toml +++ b/adenosine-pds/Cargo.toml @@ -13,32 +13,24 @@ readme = "README.md" repository = "https://gitlab.com/bnewbold/adenosine" [dependencies] +adenosine = { version = "0.2.0", path = "../adenosine" } anyhow = "1" -structopt = "0.3" -serde = "1" -serde_json = "1" +askama = { version = "0.11", features = ["serde-json"] } +bcrypt = "0.13" +data-encoding = "2" +dotenvy = "0.15" +#ipfs-sqlite-block-store = "0.13" +lazy_static = "1" +libipld = { version = "0.14", features = ["dag-cbor", "derive"] } log = "0.4" pretty_env_logger = "0.4" -libipld = "0.14" -ipfs-sqlite-block-store = "0.13" +rouille = "3" rusqlite = { version = "0.26", features = ["bundled"] } rusqlite_migration = "1" -# NOTE: lexicon validation not implemented yet -#jsonschema = "*" -#schemafy = "*" -rouille = "3" -# NOTE: vendored for now -#iroh-car = "*" -adenosine = { version = "0.2.0", path = "../adenosine" } +serde = "1" +serde_json = "1" +structopt = "0.3" tokio = { version = "1", features = ["full"] } -futures = "0.3" -lazy_static = "1" -bcrypt = "0.13" -data-encoding = "2" -# TODO: replace this with data-encoding or similar; this is only needed for ucan_p256 stuff -async-trait = "0.1" -dotenvy = "0.15" -askama = { version = "0.11", features = ["serde-json"] } [package.metadata.deb] maintainer = "Bryan Newbold " diff --git a/adenosine-pds/src/bin/adenosine-pds.rs b/adenosine-pds/src/bin/adenosine-pds.rs index c16e7e0..c004098 100644 --- a/adenosine-pds/src/bin/adenosine-pds.rs +++ b/adenosine-pds/src/bin/adenosine-pds.rs @@ -1,5 +1,7 @@ +use adenosine::com_atproto; +use adenosine::crypto::KeyPair; use adenosine::mst; -use adenosine_pds::models::AccountRequest; +use adenosine::repo::RepoStore; use adenosine_pds::*; use anyhow::Result; use serde_json::json; @@ -208,7 +210,7 @@ fn main() -> Result<()> { public_url, did_plc, } => { - let req = AccountRequest { + let req = com_atproto::AccountRequest { email, handle: handle.clone(), password, diff --git a/adenosine-pds/src/bsky.rs b/adenosine-pds/src/bsky.rs deleted file mode 100644 index caa16f6..0000000 --- a/adenosine-pds/src/bsky.rs +++ /dev/null @@ -1,773 +0,0 @@ -use crate::models::*; -/// Helper functions for doing database and repo operations relating to bluesky endpoints and -/// records -use crate::{ - ipld_into_json_value, json_value_into_ipld, AtpDatabase, AtpService, Did, Result, Tid, - XrpcError, -}; -use adenosine::identifiers::{AtUri, DidOrHost, Nsid}; -use adenosine::repo::Mutation; -use anyhow::anyhow; -use libipld::Cid; -use rusqlite::params; -use serde_json::json; -use std::str::FromStr; - -/// Handles updating the database with creation, update, deletion of arbitrary records -pub fn bsky_mutate_db(db: &mut AtpDatabase, did: &Did, mutations: Vec) -> Result<()> { - // TODO: this function could probably be refactored - let bsky_post: Nsid = Nsid::from_str("app.bsky.feed.post").unwrap(); - let bsky_repost: Nsid = Nsid::from_str("app.bsky.feed.repost").unwrap(); - let bsky_like: Nsid = Nsid::from_str("app.bsky.feed.like").unwrap(); - let bsky_follow: Nsid = Nsid::from_str("app.bsky.graph.follow").unwrap(); - for m in mutations.into_iter() { - match m { - Mutation::Create(ref_type, tid, val) | Mutation::Update(ref_type, tid, val) - if ref_type == bsky_post => - { - db.bsky_upsert_post(did, &tid, Some(val))? - } - Mutation::Delete(ref_type, tid) if ref_type == bsky_post => { - db.bsky_upsert_post(did, &tid, None)? - } - Mutation::Create(ref_type, tid, val) | Mutation::Update(ref_type, tid, val) - if ref_type == bsky_repost => - { - db.bsky_upsert_ref("repost", did, &tid, Some(val))? - } - Mutation::Delete(ref_type, tid) if ref_type == bsky_repost => { - db.bsky_upsert_ref("repost", did, &tid, None)? - } - Mutation::Create(ref_type, tid, val) | Mutation::Update(ref_type, tid, val) - if ref_type == bsky_like => - { - db.bsky_upsert_ref("like", did, &tid, Some(val))? - } - Mutation::Delete(ref_type, tid) if ref_type == bsky_like => { - db.bsky_upsert_ref("like", did, &tid, None)? - } - Mutation::Create(ref_type, tid, val) | Mutation::Update(ref_type, tid, val) - if ref_type == bsky_follow => - { - db.bsky_upsert_follow(did, &tid, Some(val))? - } - Mutation::Delete(ref_type, tid) if ref_type == bsky_follow => { - db.bsky_upsert_follow(did, &tid, None)? - } - _ => (), - } - } - Ok(()) -} - -// TODO: should probably return Result>? -pub fn bsky_get_profile(srv: &mut AtpService, did: &Did) -> Result { - // first get the profile record - let mut profile_cid: Option = None; - let commit_cid = match srv.repo.lookup_commit(did)? { - Some(cid) => cid, - None => Err(anyhow!("repository not found: {}", did))?, - }; - let last_commit = srv.repo.get_commit(&commit_cid)?; - let full_map = srv.repo.mst_to_map(&last_commit.mst_cid)?; - let prefix = "/app.bsky.actor.profile/"; - for (mst_key, cid) in full_map.iter() { - if mst_key.starts_with(prefix) { - profile_cid = Some(*cid); - } - } - let (display_name, description): (Option, Option) = - if let Some(cid) = profile_cid { - let record: ProfileRecord = - serde_json::from_value(ipld_into_json_value(srv.repo.get_ipld(&cid)?))?; - (Some(record.displayName), record.description) - } else { - (None, None) - }; - let mut stmt = srv - .atp_db - .conn - .prepare_cached("SELECT handle FROM account WHERE did = $1")?; - let handle: String = stmt.query_row(params!(did.to_string()), |row| row.get(0))?; - let mut stmt = srv - .atp_db - .conn - .prepare_cached("SELECT COUNT(*) FROM bsky_post WHERE did = $1")?; - let post_count: u64 = stmt.query_row(params!(did.to_string()), |row| row.get(0))?; - let mut stmt = srv - .atp_db - .conn - .prepare_cached("SELECT COUNT(*) FROM bsky_follow WHERE did = $1")?; - let follows_count: u64 = stmt.query_row(params!(did.to_string()), |row| row.get(0))?; - let mut stmt = srv - .atp_db - .conn - .prepare_cached("SELECT COUNT(*) FROM bsky_follow WHERE subject_did = $1")?; - let followers_count: u64 = stmt.query_row(params!(did.to_string()), |row| row.get(0))?; - let decl = DeclRef { - actorType: "app.bsky.system.actorUser".to_string(), - cid: "bafyreid27zk7lbis4zw5fz4podbvbs4fc5ivwji3dmrwa6zggnj4bnd57u".to_string(), - }; - Ok(Profile { - did: did.to_string(), - handle, - creator: did.to_string(), - displayName: display_name, - description, - declaration: decl, - followersCount: followers_count, - followsCount: follows_count, - postsCount: post_count, - membersCount: 0, - myState: json!({}), - }) -} - -pub fn bsky_update_profile(srv: &mut AtpService, did: &Did, profile: ProfileRecord) -> Result<()> { - // get the profile record - let mut profile_tid: Option = None; - let commit_cid = match srv.repo.lookup_commit(did)? { - Some(cid) => cid, - None => Err(anyhow!("repository not found: {}", did))?, - }; - let last_commit = srv.repo.get_commit(&commit_cid)?; - let full_map = srv.repo.mst_to_map(&last_commit.mst_cid)?; - let prefix = "/app.bsky.actor.profile/"; - for (mst_key, _cid) in full_map.iter() { - if mst_key.starts_with(prefix) { - profile_tid = Some(Tid::from_str(mst_key.split('/').nth(2).unwrap())?); - } - } - let profile_tid: Tid = profile_tid.unwrap_or(srv.tid_gen.next_tid()); - let mutations: Vec = vec![Mutation::Update( - Nsid::from_str("app.bsky.actor.profile")?, - profile_tid, - json_value_into_ipld(serde_json::to_value(profile)?), - )]; - let keypair = srv.pds_keypair.clone(); - srv.repo.mutate_repo(did, &mutations, &keypair)?; - Ok(()) -} - -struct FeedRow { - pub item_did: Did, - pub item_handle: String, - pub item_post_tid: Tid, - pub item_post_cid: Cid, - pub indexed_at: String, -} - -fn feed_row(row: &rusqlite::Row) -> Result { - let item_did: String = row.get(0)?; - let item_did = Did::from_str(&item_did)?; - let item_handle = row.get(1)?; - let item_post_tid: String = row.get(2)?; - let item_post_tid = Tid::from_str(&item_post_tid)?; - let cid_string: String = row.get(3)?; - let item_post_cid = Cid::from_str(&cid_string)?; - let indexed_at: String = row.get(4)?; - Ok(FeedRow { - item_did, - item_handle, - item_post_tid, - item_post_cid, - indexed_at, - }) -} - -fn feed_row_to_item(srv: &mut AtpService, row: FeedRow) -> Result { - let record_ipld = srv.repo.get_ipld(&row.item_post_cid)?; - let uri = format!( - "at://{}/{}/{}", - row.item_did, "app.bsky.feed.post", row.item_post_tid - ); - - let mut stmt = srv.atp_db.conn.prepare_cached( - "SELECT COUNT(*) FROM bsky_ref WHERE ref_type = 'like' AND subject_uri = $1", - )?; - let like_count: u64 = stmt.query_row(params!(uri), |row| row.get(0))?; - - let mut stmt = srv.atp_db.conn.prepare_cached( - "SELECT COUNT(*) FROM bsky_ref WHERE ref_type = 'repost' AND subject_uri = $1", - )?; - let repost_count: u64 = stmt.query_row(params!(uri), |row| row.get(0))?; - - let mut stmt = srv - .atp_db - .conn - .prepare_cached("SELECT COUNT(*) FROM bsky_post WHERE reply_to_parent_uri = $1")?; - let reply_count: u64 = stmt.query_row(params!(uri), |row| row.get(0))?; - - let feed_item = FeedItem { - uri, - cid: row.item_post_cid.to_string(), - author: User { - did: row.item_did.to_string(), - handle: row.item_handle, - displayName: None, // TODO: fetch from profile (or cache) - }, - repostedBy: None, - record: ipld_into_json_value(record_ipld), - embed: None, - replyCount: reply_count, - repostCount: repost_count, - upvoteCount: like_count, - downvoteCount: 0, - indexedAt: row.indexed_at, - myState: None, - }; - Ok(feed_item) -} - -pub fn bsky_get_timeline(srv: &mut AtpService, did: &Did) -> Result { - let mut feed: Vec = vec![]; - // TODO: also handle reposts - let rows = { - let mut stmt = srv.atp_db - .conn - .prepare_cached("SELECT account.did, account.handle, bsky_post.tid, bsky_post.cid, bsky_post.indexed_at FROM bsky_post LEFT JOIN account ON bsky_post.did = account.did LEFT JOIN bsky_follow ON bsky_post.did = bsky_follow.subject_did WHERE bsky_follow.did = ?1 AND account.did IS NOT NULL ORDER BY bsky_post.tid DESC LIMIT 20")?; - let mut sql_rows = stmt.query(params!(did.to_string()))?; - let mut rows: Vec = vec![]; - while let Some(sql_row) = sql_rows.next()? { - let row = feed_row(sql_row)?; - rows.push(row); - } - rows - }; - for row in rows { - feed.push(feed_row_to_item(srv, row)?); - } - Ok(GenericFeed { feed }) -} - -pub fn bsky_get_author_feed(srv: &mut AtpService, did: &Did) -> Result { - let mut feed: Vec = vec![]; - // TODO: also handle reposts - let rows = { - let mut stmt = srv.atp_db - .conn - .prepare_cached("SELECT account.did, account.handle, bsky_post.tid, bsky_post.cid, bsky_post.indexed_at FROM bsky_post LEFT JOIN account ON bsky_post.did = account.did WHERE bsky_post.did = ?1 ORDER BY bsky_post.tid DESC LIMIT 20")?; - let mut sql_rows = stmt.query(params!(did.to_string()))?; - let mut rows: Vec = vec![]; - while let Some(sql_row) = sql_rows.next()? { - let row = feed_row(sql_row)?; - rows.push(row); - } - rows - }; - for row in rows { - feed.push(feed_row_to_item(srv, row)?); - } - Ok(GenericFeed { feed }) -} - -// TODO: this is a partial implementation -// TODO: should maybe have this take a did and tid instead of a aturi? -pub fn bsky_get_thread( - srv: &mut AtpService, - uri: &AtUri, - _depth: Option, -) -> Result { - // parse the URI - let did = match uri.repository { - DidOrHost::Did(ref did_type, ref did_body) => { - Did::from_str(&format!("did:{}:{}", did_type, did_body))? - } - _ => Err(anyhow!("expected a DID, not handle, in uri: {}", uri))?, - }; - if uri.collection != Some("app.bsky.feed.post".to_string()) { - Err(anyhow!("expected a post collection in uri: {}", uri))?; - }; - let tid = match uri.record { - Some(ref tid) => Tid::from_str(tid)?, - _ => Err(anyhow!("expected a record in uri: {}", uri))?, - }; - - // post itself, as a FeedItem - let post_items = { - let mut stmt = srv.atp_db - .conn - .prepare_cached("SELECT account.did, account.handle, bsky_post.tid, bsky_post.cid, bsky_post.indexed_at FROM bsky_post LEFT JOIN account ON bsky_post.did = account.did WHERE bsky_post.did = ?1 AND bsky_post.tid = ?2")?; - let mut sql_rows = stmt.query(params!(did.to_string(), tid.to_string()))?; - let mut rows: Vec = vec![]; - while let Some(sql_row) = sql_rows.next()? { - let row = feed_row(sql_row)?; - rows.push(row); - } - rows - }; - if post_items.is_empty() { - Err(XrpcError::NotFound("post not found".to_string()))?; - }; - let post_item = feed_row_to_item(srv, post_items.into_iter().next().unwrap())?; - - // TODO: any parent - let parent = None; - - // any children - let mut children = vec![]; - let rows = { - let mut stmt = srv.atp_db - .conn - .prepare_cached("SELECT account.did, account.handle, bsky_post.tid, bsky_post.cid, bsky_post.indexed_at FROM bsky_post LEFT JOIN account ON bsky_post.did = account.did WHERE bsky_post.reply_to_parent_uri = ?1 ORDER BY bsky_post.tid DESC LIMIT 20")?; - let mut sql_rows = stmt.query(params!(uri.to_string()))?; - let mut rows: Vec = vec![]; - while let Some(sql_row) = sql_rows.next()? { - let row = feed_row(sql_row)?; - rows.push(row); - } - rows - }; - for row in rows { - let item = feed_row_to_item(srv, row)?; - children.push(ThreadItem { - uri: item.uri, - cid: item.cid, - author: item.author, - record: item.record, - embed: item.embed, - // don't want a loop here - parent: None, - replyCount: item.replyCount, - // only going to depth of one here - replies: None, - upvoteCount: item.upvoteCount, - downvoteCount: 0, - repostCount: item.repostCount, - indexedAt: item.indexedAt, - myState: None, - }); - } - - let post = ThreadItem { - uri: post_item.uri, - cid: post_item.cid, - author: post_item.author, - record: post_item.record, - embed: post_item.embed, - parent, - replyCount: post_item.replyCount, - replies: Some(children), - upvoteCount: post_item.upvoteCount, - downvoteCount: 0, - repostCount: post_item.repostCount, - indexedAt: post_item.indexedAt, - myState: None, - }; - Ok(PostThread { thread: post }) -} - -#[test] -fn test_bsky_profile() { - use crate::{create_account, created_at_now}; - use libipld::ipld; - - let post_nsid = Nsid::from_str("app.bsky.feed.post").unwrap(); - let follow_nsid = Nsid::from_str("app.bsky.graph.follow").unwrap(); - - let mut srv = AtpService::new_ephemeral().unwrap(); - let req = AccountRequest { - email: "test@bogus.com".to_string(), - handle: "handle.test".to_string(), - password: "bogus".to_string(), - inviteCode: None, - recoveryKey: None, - }; - let session = create_account(&mut srv, &req, true).unwrap(); - let did = Did::from_str(&session.did).unwrap(); - let profile = bsky_get_profile(&mut srv, &did).unwrap(); - assert_eq!(profile.did, session.did); - assert_eq!(profile.handle, req.handle); - assert_eq!(profile.displayName, None); - assert_eq!(profile.description, None); - assert_eq!(profile.followersCount, 0); - assert_eq!(profile.followsCount, 0); - assert_eq!(profile.postsCount, 0); - - let record = ProfileRecord { - displayName: "Test Name".to_string(), - description: Some("short description".to_string()), - }; - bsky_update_profile(&mut srv, &did, record.clone()).unwrap(); - let profile = bsky_get_profile(&mut srv, &did).unwrap(); - assert_eq!(profile.displayName, Some(record.displayName)); - assert_eq!(profile.description, record.description); - - let record = ProfileRecord { - displayName: "New Test Name".to_string(), - description: Some("longer description".to_string()), - }; - bsky_update_profile(&mut srv, &did, record.clone()).unwrap(); - let profile = bsky_get_profile(&mut srv, &did).unwrap(); - assert_eq!(profile.displayName, Some(record.displayName)); - assert_eq!(profile.description, record.description); - - let mutations = vec![ - Mutation::Create( - follow_nsid.clone(), - srv.tid_gen.next_tid(), - ipld!({"subject": {"did": session.did}, "createdAt": created_at_now()}), - ), - Mutation::Create( - follow_nsid.clone(), - srv.tid_gen.next_tid(), - ipld!({"subject": {"did": "did:web:external.domain"}, "createdAt": created_at_now()}), - ), - Mutation::Create( - post_nsid.clone(), - srv.tid_gen.next_tid(), - ipld!({"text": "first post"}), - ), - Mutation::Create( - post_nsid.clone(), - srv.tid_gen.next_tid(), - ipld!({"text": "second post"}), - ), - Mutation::Create( - post_nsid.clone(), - srv.tid_gen.next_tid(), - ipld!({"text": "third post"}), - ), - ]; - srv.repo - .mutate_repo(&did, &mutations, &srv.pds_keypair) - .unwrap(); - bsky_mutate_db(&mut srv.atp_db, &did, mutations).unwrap(); - - let profile = bsky_get_profile(&mut srv, &did).unwrap(); - assert_eq!(profile.followersCount, 1); - assert_eq!(profile.followsCount, 2); - assert_eq!(profile.postsCount, 3); -} - -#[test] -fn test_bsky_feeds() { - // TODO: test that displayName comes through in feeds and timelines (it does not currently) - use crate::{create_account, created_at_now}; - use libipld::ipld; - - let post_nsid = Nsid::from_str("app.bsky.feed.post").unwrap(); - let like_nsid = Nsid::from_str("app.bsky.feed.like").unwrap(); - let repost_nsid = Nsid::from_str("app.bsky.feed.repost").unwrap(); - let follow_nsid = Nsid::from_str("app.bsky.graph.follow").unwrap(); - - let mut srv = AtpService::new_ephemeral().unwrap(); - let alice_did = { - let req = AccountRequest { - email: "alice@bogus.com".to_string(), - handle: "alice.test".to_string(), - password: "bogus".to_string(), - inviteCode: None, - recoveryKey: None, - }; - let session = create_account(&mut srv, &req, true).unwrap(); - Did::from_str(&session.did).unwrap() - }; - let bob_did = { - let req = AccountRequest { - email: "bob@bogus.com".to_string(), - handle: "bob.test".to_string(), - password: "bogus".to_string(), - inviteCode: None, - recoveryKey: None, - }; - let session = create_account(&mut srv, &req, true).unwrap(); - Did::from_str(&session.did).unwrap() - }; - let carol_did = { - let req = AccountRequest { - email: "carol@bogus.com".to_string(), - handle: "carol.test".to_string(), - password: "bogus".to_string(), - inviteCode: None, - recoveryKey: None, - }; - let session = create_account(&mut srv, &req, true).unwrap(); - Did::from_str(&session.did).unwrap() - }; - - // all feeds and timelines should be empty - let alice_feed = bsky_get_author_feed(&mut srv, &alice_did).unwrap(); - let alice_timeline = bsky_get_timeline(&mut srv, &alice_did).unwrap(); - assert!(alice_feed.feed.is_empty()); - assert!(alice_timeline.feed.is_empty()); - let bob_feed = bsky_get_author_feed(&mut srv, &bob_did).unwrap(); - let bob_timeline = bsky_get_timeline(&mut srv, &bob_did).unwrap(); - assert!(bob_feed.feed.is_empty()); - assert!(bob_timeline.feed.is_empty()); - let carol_feed = bsky_get_author_feed(&mut srv, &carol_did).unwrap(); - let carol_timeline = bsky_get_timeline(&mut srv, &carol_did).unwrap(); - assert!(carol_feed.feed.is_empty()); - assert!(carol_timeline.feed.is_empty()); - - // alice does some posts - let alice_post1_tid = srv.tid_gen.next_tid(); - let alice_post2_tid = srv.tid_gen.next_tid(); - let alice_post3_tid = srv.tid_gen.next_tid(); - assert!(alice_post1_tid < alice_post2_tid && alice_post2_tid < alice_post3_tid); - let mutations = vec![ - Mutation::Create( - post_nsid.clone(), - alice_post1_tid.clone(), - ipld!({"text": "alice first post"}), - ), - Mutation::Create( - post_nsid.clone(), - alice_post2_tid.clone(), - ipld!({"text": "alice second post"}), - ), - Mutation::Create( - post_nsid.clone(), - alice_post3_tid.clone(), - ipld!({"text": "alice third post"}), - ), - ]; - srv.repo - .mutate_repo(&alice_did, &mutations, &srv.pds_keypair) - .unwrap(); - bsky_mutate_db(&mut srv.atp_db, &alice_did, mutations).unwrap(); - - // bob follows alice, likes first post, reposts second, replies third - let alice_post3_uri = format!( - "at://{}/{}/{}", - alice_did.to_string(), - post_nsid.to_string(), - alice_post3_tid.to_string() - ); - let mutations = vec![ - Mutation::Create( - follow_nsid.clone(), - srv.tid_gen.next_tid(), - ipld!({"subject": {"did": alice_did.to_string()}, "createdAt": created_at_now()}), - ), - Mutation::Create( - like_nsid.clone(), - srv.tid_gen.next_tid(), - ipld!({"subject": {"uri": format!("at://{}/{}/{}", alice_did.to_string(), post_nsid.to_string(), alice_post1_tid.to_string())}, "createdAt": created_at_now()}), - ), - Mutation::Create( - repost_nsid.clone(), - srv.tid_gen.next_tid(), - ipld!({"subject": {"uri": format!("at://{}/{}/{}", alice_did.to_string(), post_nsid.to_string(), alice_post2_tid.to_string())}, "createdAt": created_at_now()}), - ), - Mutation::Create( - post_nsid.clone(), - srv.tid_gen.next_tid(), - ipld!({"text": "bob comment on alice post3", "reply": {"parent": {"uri": alice_post3_uri.clone()}, "root": {"uri": alice_post3_uri.clone()}}}), - ), - ]; - srv.repo - .mutate_repo(&bob_did, &mutations, &srv.pds_keypair) - .unwrap(); - bsky_mutate_db(&mut srv.atp_db, &bob_did, mutations).unwrap(); - - // carol follows bob - let mutations = vec![Mutation::Create( - follow_nsid.clone(), - srv.tid_gen.next_tid(), - ipld!({"subject": {"did": bob_did.to_string()}, "createdAt": created_at_now()}), - )]; - srv.repo - .mutate_repo(&bob_did, &mutations, &srv.pds_keypair) - .unwrap(); - bsky_mutate_db(&mut srv.atp_db, &carol_did, mutations).unwrap(); - - // test alice profile: counts should be updated - let alice_profile = bsky_get_profile(&mut srv, &alice_did).unwrap(); - assert_eq!(alice_profile.followersCount, 1); - assert_eq!(alice_profile.followsCount, 0); - assert_eq!(alice_profile.postsCount, 3); - - // test alice timeline: still empty (?) - let alice_timeline = bsky_get_timeline(&mut srv, &alice_did).unwrap(); - println!("{:?}", alice_timeline); - assert!(alice_timeline.feed.is_empty()); - - // test alice feed: should have 3 posts, with correct counts - let alice_feed = bsky_get_author_feed(&mut srv, &alice_did).unwrap(); - assert_eq!(alice_feed.feed.len(), 3); - - assert_eq!( - alice_feed.feed[2].uri, - format!( - "at://{}/{}/{}", - alice_did.to_string(), - post_nsid.to_string(), - alice_post1_tid.to_string() - ) - ); - // TODO: CID - assert_eq!(alice_feed.feed[2].author.did, alice_did.to_string()); - assert_eq!(alice_feed.feed[2].author.handle, "alice.test"); - assert_eq!(alice_feed.feed[2].repostedBy, None); - assert_eq!( - alice_feed.feed[2].record["text"].as_str().unwrap(), - "alice first post" - ); - assert_eq!(alice_feed.feed[2].embed, None); - assert_eq!(alice_feed.feed[2].replyCount, 0); - assert_eq!(alice_feed.feed[2].repostCount, 0); - assert_eq!(alice_feed.feed[2].upvoteCount, 1); - assert_eq!(alice_feed.feed[2].downvoteCount, 0); - - assert_eq!(alice_feed.feed[1].author.did, alice_did.to_string()); - assert_eq!(alice_feed.feed[1].replyCount, 0); - assert_eq!(alice_feed.feed[1].repostCount, 1); - assert_eq!(alice_feed.feed[1].upvoteCount, 0); - - assert_eq!(alice_feed.feed[0].author.did, alice_did.to_string()); - assert_eq!(alice_feed.feed[0].replyCount, 1); - assert_eq!(alice_feed.feed[0].repostCount, 0); - assert_eq!(alice_feed.feed[0].upvoteCount, 0); - - // test bob timeline: should include alice posts - let bob_timeline = bsky_get_timeline(&mut srv, &bob_did).unwrap(); - println!("BOB TIMELINE ======"); - for item in bob_timeline.feed.iter() { - println!("{:?}", item); - } - assert_eq!(bob_timeline.feed.len(), 3); - assert_eq!( - bob_timeline.feed[2].uri, - format!( - "at://{}/{}/{}", - alice_did.to_string(), - post_nsid.to_string(), - alice_post1_tid.to_string() - ) - ); - // TODO: CID - assert_eq!(bob_timeline.feed[2].author.did, alice_did.to_string()); - assert_eq!(bob_timeline.feed[2].author.handle, "alice.test"); - - // test bob feed: should include repost and reply - let bob_feed = bsky_get_author_feed(&mut srv, &bob_did).unwrap(); - assert_eq!(bob_feed.feed.len(), 1); - // TODO: handle reposts - /* - assert_eq!(bob_feed.feed.len(), 2); - assert_eq!(bob_feed.feed[1].uri, format!("at://{}/{}/{}", alice_did.to_string(), post_nsid.to_string(), alice_post1_tid.to_string())); - // TODO: CID - assert_eq!(bob_feed.feed[1].author.did, alice_did.to_string()); - assert_eq!(bob_feed.feed[1].author.handle, "alice.test"); - assert_eq!(bob_feed.feed[1].repostedBy.as_ref().unwrap().did, bob_did.to_string()); - assert_eq!(bob_feed.feed[1].repostedBy.as_ref().unwrap().handle, "bob.test"); - // TODO: "is a repost" (check record?) - */ - - assert_eq!(bob_feed.feed[0].author.did, bob_did.to_string()); - assert_eq!(bob_feed.feed[0].author.handle, "bob.test"); - - // test carol timeline: should include bob's repost and reply - let carol_timeline = bsky_get_timeline(&mut srv, &carol_did).unwrap(); - // TODO: handle re-posts (+1 here) - assert_eq!(carol_timeline.feed.len(), 1); - // TODO: details - - // test carol feed: still empty - let carol_feed = bsky_get_author_feed(&mut srv, &carol_did).unwrap(); - assert!(carol_feed.feed.is_empty()); -} - -#[test] -fn test_bsky_thread() { - use crate::create_account; - use libipld::ipld; - - let post_nsid = Nsid::from_str("app.bsky.feed.post").unwrap(); - - let mut srv = AtpService::new_ephemeral().unwrap(); - let alice_did = { - let req = AccountRequest { - email: "alice@bogus.com".to_string(), - handle: "alice.test".to_string(), - password: "bogus".to_string(), - inviteCode: None, - recoveryKey: None, - }; - let session = create_account(&mut srv, &req, true).unwrap(); - Did::from_str(&session.did).unwrap() - }; - let bob_did = { - let req = AccountRequest { - email: "bob@bogus.com".to_string(), - handle: "bob.test".to_string(), - password: "bogus".to_string(), - inviteCode: None, - recoveryKey: None, - }; - let session = create_account(&mut srv, &req, true).unwrap(); - Did::from_str(&session.did).unwrap() - }; - - // alice does a post - let alice_post1_tid = srv.tid_gen.next_tid(); - let mutations = vec![Mutation::Create( - post_nsid.clone(), - alice_post1_tid.clone(), - ipld!({"text": "alice first post"}), - )]; - srv.repo - .mutate_repo(&alice_did, &mutations, &srv.pds_keypair) - .unwrap(); - bsky_mutate_db(&mut srv.atp_db, &alice_did, mutations).unwrap(); - let alice_post1_uri = format!( - "at://{}/{}/{}", - alice_did.to_string(), - post_nsid.to_string(), - alice_post1_tid.to_string() - ); - - // bob likes and replies first post - let bob_post1_tid = srv.tid_gen.next_tid(); - let mutations = vec![Mutation::Create( - post_nsid.clone(), - bob_post1_tid.clone(), - ipld!({"text": "bob comment on alice post1", "reply": {"parent": {"uri": alice_post1_uri.clone()}, "root": {"uri": alice_post1_uri.clone()}}}), - )]; - srv.repo - .mutate_repo(&bob_did, &mutations, &srv.pds_keypair) - .unwrap(); - bsky_mutate_db(&mut srv.atp_db, &bob_did, mutations).unwrap(); - let bob_post1_uri = format!( - "at://{}/{}/{}", - bob_did.to_string(), - post_nsid.to_string(), - bob_post1_tid.to_string() - ); - - // alice replies to bob reply - let alice_post2_tid = srv.tid_gen.next_tid(); - let mutations = vec![Mutation::Create( - post_nsid.clone(), - alice_post2_tid.clone(), - ipld!({"text": "alice second post, replying to bob comment", "reply": {"parent": {"uri": bob_post1_uri.clone()}, "root": {"uri": alice_post1_uri.clone()}}}), - )]; - srv.repo - .mutate_repo(&alice_did, &mutations, &srv.pds_keypair) - .unwrap(); - bsky_mutate_db(&mut srv.atp_db, &alice_did, mutations).unwrap(); - let _alice_post2_uri = format!( - "at://{}/{}/{}", - alice_did.to_string(), - post_nsid.to_string(), - alice_post2_tid.to_string() - ); - - // get thread from bob's post - // TODO: should have both parent and children - let post = bsky_get_thread(&mut srv, &AtUri::from_str(&bob_post1_uri).unwrap(), None) - .unwrap() - .thread; - assert_eq!(post.author.did, bob_did.to_string()); - assert_eq!(post.author.handle, "bob.test".to_string()); - assert_eq!(post.embed, None); - assert_eq!(post.replyCount, 1); - assert_eq!(post.repostCount, 0); - assert_eq!(post.upvoteCount, 0); - assert_eq!(post.replies.as_ref().unwrap().len(), 1); - - let post_replies = post.replies.unwrap(); - assert_eq!(post_replies[0].author.did, alice_did.to_string()); - // TODO: root URI, etc -} diff --git a/adenosine-pds/src/db.rs b/adenosine-pds/src/db.rs index b694bb5..4cbece6 100644 --- a/adenosine-pds/src/db.rs +++ b/adenosine-pds/src/db.rs @@ -1,6 +1,7 @@ -use crate::models::{FollowRecord, Post, RefRecord}; /// ATP database (as distinct from blockstore) -use crate::{created_at_now, ipld_into_json_value, AtpSession, Did, KeyPair, Tid}; +use crate::{created_at_now, ipld_into_json_value, Did, KeyPair, Tid}; +use adenosine::app_bsky; +use adenosine::com_atproto; use anyhow::{anyhow, Result}; use lazy_static::lazy_static; use libipld::cbor::DagCborCodec; @@ -105,7 +106,7 @@ impl AtpDatabase { handle: &str, password: &str, keypair: &KeyPair, - ) -> Result { + ) -> Result { let mut stmt = self .conn .prepare_cached("SELECT did, password_bcrypt FROM account WHERE handle = ?1")?; @@ -120,7 +121,7 @@ impl AtpDatabase { .conn .prepare_cached("INSERT INTO session (did, jwt) VALUES (?1, ?2)")?; stmt.execute(params!(did.to_string(), jwt))?; - Ok(AtpSession { + Ok(com_atproto::Session { did: did.to_string(), name: handle.to_string(), accessJwt: jwt.to_string(), @@ -192,7 +193,7 @@ impl AtpDatabase { // need to re-compute the CID from DagCbor re-encoding, I guess. bleh. let block = Block::::encode(DagCborCodec, Code::Sha2_256, &val)?; let cid = *block.cid(); - let post: Post = serde_json::from_value(ipld_into_json_value(val))?; + let post: app_bsky::Post = serde_json::from_value(ipld_into_json_value(val))?; let (reply_to_parent_uri, reply_to_root_uri) = match post.reply { Some(ref reply) => (Some(reply.parent.uri.clone()), Some(reply.root.uri.clone())), None => (None, None), @@ -226,7 +227,7 @@ impl AtpDatabase { val: Option, ) -> Result<()> { if let Some(val) = val { - let ref_obj: RefRecord = serde_json::from_value(ipld_into_json_value(val))?; + let ref_obj: app_bsky::RefRecord = serde_json::from_value(ipld_into_json_value(val))?; let mut stmt = self .conn .prepare_cached("INSERT INTO bsky_ref (ref_type, did, tid, subject_uri, subject_cid, created_at) VALUES (?1, ?2, ?3, ?4, ?5, ?6)")?; @@ -253,7 +254,7 @@ impl AtpDatabase { pub fn bsky_upsert_follow(&mut self, did: &Did, tid: &Tid, val: Option) -> Result<()> { if let Some(val) = val { - let follow: FollowRecord = serde_json::from_value(ipld_into_json_value(val))?; + let follow: app_bsky::FollowRecord = serde_json::from_value(ipld_into_json_value(val))?; let mut stmt = self .conn .prepare_cached("INSERT INTO bsky_follow (did, tid, subject_did, created_at) VALUES (?1, ?2, ?3, ?4)")?; diff --git a/adenosine-pds/src/db_bsky.rs b/adenosine-pds/src/db_bsky.rs new file mode 100644 index 0000000..ee8e0f3 --- /dev/null +++ b/adenosine-pds/src/db_bsky.rs @@ -0,0 +1,778 @@ +/// Helper functions for doing database and repo operations relating to bluesky endpoints and +/// records +use crate::{AtpDatabase, AtpService, Result, XrpcError}; +use adenosine::app_bsky; +use adenosine::identifiers::{AtUri, Did, DidOrHost, Nsid, Tid}; +use adenosine::ipld::{ipld_into_json_value, json_value_into_ipld}; +use adenosine::repo::Mutation; +use anyhow::anyhow; +use libipld::Cid; +use rusqlite::params; +use serde_json::json; +use std::str::FromStr; + +/// Handles updating the database with creation, update, deletion of arbitrary records +pub fn bsky_mutate_db(db: &mut AtpDatabase, did: &Did, mutations: Vec) -> Result<()> { + // TODO: this function could probably be refactored + let bsky_post: Nsid = Nsid::from_str("app.bsky.feed.post").unwrap(); + let bsky_repost: Nsid = Nsid::from_str("app.bsky.feed.repost").unwrap(); + let bsky_like: Nsid = Nsid::from_str("app.bsky.feed.like").unwrap(); + let bsky_follow: Nsid = Nsid::from_str("app.bsky.graph.follow").unwrap(); + for m in mutations.into_iter() { + match m { + Mutation::Create(ref_type, tid, val) | Mutation::Update(ref_type, tid, val) + if ref_type == bsky_post => + { + db.bsky_upsert_post(did, &tid, Some(val))? + } + Mutation::Delete(ref_type, tid) if ref_type == bsky_post => { + db.bsky_upsert_post(did, &tid, None)? + } + Mutation::Create(ref_type, tid, val) | Mutation::Update(ref_type, tid, val) + if ref_type == bsky_repost => + { + db.bsky_upsert_ref("repost", did, &tid, Some(val))? + } + Mutation::Delete(ref_type, tid) if ref_type == bsky_repost => { + db.bsky_upsert_ref("repost", did, &tid, None)? + } + Mutation::Create(ref_type, tid, val) | Mutation::Update(ref_type, tid, val) + if ref_type == bsky_like => + { + db.bsky_upsert_ref("like", did, &tid, Some(val))? + } + Mutation::Delete(ref_type, tid) if ref_type == bsky_like => { + db.bsky_upsert_ref("like", did, &tid, None)? + } + Mutation::Create(ref_type, tid, val) | Mutation::Update(ref_type, tid, val) + if ref_type == bsky_follow => + { + db.bsky_upsert_follow(did, &tid, Some(val))? + } + Mutation::Delete(ref_type, tid) if ref_type == bsky_follow => { + db.bsky_upsert_follow(did, &tid, None)? + } + _ => (), + } + } + Ok(()) +} + +// TODO: should probably return Result>? +pub fn bsky_get_profile(srv: &mut AtpService, did: &Did) -> Result { + // first get the profile record + let mut profile_cid: Option = None; + let commit_cid = match srv.repo.lookup_commit(did)? { + Some(cid) => cid, + None => Err(anyhow!("repository not found: {}", did))?, + }; + let last_commit = srv.repo.get_commit(&commit_cid)?; + let full_map = srv.repo.mst_to_map(&last_commit.mst_cid)?; + let prefix = "/app.bsky.actor.profile/"; + for (mst_key, cid) in full_map.iter() { + if mst_key.starts_with(prefix) { + profile_cid = Some(*cid); + } + } + let (display_name, description): (Option, Option) = + if let Some(cid) = profile_cid { + let record: app_bsky::ProfileRecord = + serde_json::from_value(ipld_into_json_value(srv.repo.get_ipld(&cid)?))?; + (Some(record.displayName), record.description) + } else { + (None, None) + }; + let mut stmt = srv + .atp_db + .conn + .prepare_cached("SELECT handle FROM account WHERE did = $1")?; + let handle: String = stmt.query_row(params!(did.to_string()), |row| row.get(0))?; + let mut stmt = srv + .atp_db + .conn + .prepare_cached("SELECT COUNT(*) FROM bsky_post WHERE did = $1")?; + let post_count: u64 = stmt.query_row(params!(did.to_string()), |row| row.get(0))?; + let mut stmt = srv + .atp_db + .conn + .prepare_cached("SELECT COUNT(*) FROM bsky_follow WHERE did = $1")?; + let follows_count: u64 = stmt.query_row(params!(did.to_string()), |row| row.get(0))?; + let mut stmt = srv + .atp_db + .conn + .prepare_cached("SELECT COUNT(*) FROM bsky_follow WHERE subject_did = $1")?; + let followers_count: u64 = stmt.query_row(params!(did.to_string()), |row| row.get(0))?; + let decl = app_bsky::DeclRef { + actorType: "app.bsky.system.actorUser".to_string(), + cid: "bafyreid27zk7lbis4zw5fz4podbvbs4fc5ivwji3dmrwa6zggnj4bnd57u".to_string(), + }; + Ok(app_bsky::Profile { + did: did.to_string(), + handle, + creator: did.to_string(), + displayName: display_name, + description, + declaration: decl, + followersCount: followers_count, + followsCount: follows_count, + postsCount: post_count, + membersCount: 0, + myState: json!({}), + }) +} + +pub fn bsky_update_profile( + srv: &mut AtpService, + did: &Did, + profile: app_bsky::ProfileRecord, +) -> Result<()> { + // get the profile record + let mut profile_tid: Option = None; + let commit_cid = match srv.repo.lookup_commit(did)? { + Some(cid) => cid, + None => Err(anyhow!("repository not found: {}", did))?, + }; + let last_commit = srv.repo.get_commit(&commit_cid)?; + let full_map = srv.repo.mst_to_map(&last_commit.mst_cid)?; + let prefix = "/app.bsky.actor.profile/"; + for (mst_key, _cid) in full_map.iter() { + if mst_key.starts_with(prefix) { + profile_tid = Some(Tid::from_str(mst_key.split('/').nth(2).unwrap())?); + } + } + let profile_tid: Tid = profile_tid.unwrap_or(srv.tid_gen.next_tid()); + let mutations: Vec = vec![Mutation::Update( + Nsid::from_str("app.bsky.actor.profile")?, + profile_tid, + json_value_into_ipld(serde_json::to_value(profile)?), + )]; + let keypair = srv.pds_keypair.clone(); + srv.repo.mutate_repo(did, &mutations, &keypair)?; + Ok(()) +} + +struct FeedRow { + pub item_did: Did, + pub item_handle: String, + pub item_post_tid: Tid, + pub item_post_cid: Cid, + pub indexed_at: String, +} + +fn feed_row(row: &rusqlite::Row) -> Result { + let item_did: String = row.get(0)?; + let item_did = Did::from_str(&item_did)?; + let item_handle = row.get(1)?; + let item_post_tid: String = row.get(2)?; + let item_post_tid = Tid::from_str(&item_post_tid)?; + let cid_string: String = row.get(3)?; + let item_post_cid = Cid::from_str(&cid_string)?; + let indexed_at: String = row.get(4)?; + Ok(FeedRow { + item_did, + item_handle, + item_post_tid, + item_post_cid, + indexed_at, + }) +} + +fn feed_row_to_item(srv: &mut AtpService, row: FeedRow) -> Result { + let record_ipld = srv.repo.get_ipld(&row.item_post_cid)?; + let uri = format!( + "at://{}/{}/{}", + row.item_did, "app.bsky.feed.post", row.item_post_tid + ); + + let mut stmt = srv.atp_db.conn.prepare_cached( + "SELECT COUNT(*) FROM bsky_ref WHERE ref_type = 'like' AND subject_uri = $1", + )?; + let like_count: u64 = stmt.query_row(params!(uri), |row| row.get(0))?; + + let mut stmt = srv.atp_db.conn.prepare_cached( + "SELECT COUNT(*) FROM bsky_ref WHERE ref_type = 'repost' AND subject_uri = $1", + )?; + let repost_count: u64 = stmt.query_row(params!(uri), |row| row.get(0))?; + + let mut stmt = srv + .atp_db + .conn + .prepare_cached("SELECT COUNT(*) FROM bsky_post WHERE reply_to_parent_uri = $1")?; + let reply_count: u64 = stmt.query_row(params!(uri), |row| row.get(0))?; + + let feed_item = app_bsky::FeedItem { + uri, + cid: row.item_post_cid.to_string(), + author: app_bsky::User { + did: row.item_did.to_string(), + handle: row.item_handle, + displayName: None, // TODO: fetch from profile (or cache) + }, + repostedBy: None, + record: ipld_into_json_value(record_ipld), + embed: None, + replyCount: reply_count, + repostCount: repost_count, + upvoteCount: like_count, + downvoteCount: 0, + indexedAt: row.indexed_at, + myState: None, + }; + Ok(feed_item) +} + +pub fn bsky_get_timeline(srv: &mut AtpService, did: &Did) -> Result { + let mut feed: Vec = vec![]; + // TODO: also handle reposts + let rows = { + let mut stmt = srv.atp_db + .conn + .prepare_cached("SELECT account.did, account.handle, bsky_post.tid, bsky_post.cid, bsky_post.indexed_at FROM bsky_post LEFT JOIN account ON bsky_post.did = account.did LEFT JOIN bsky_follow ON bsky_post.did = bsky_follow.subject_did WHERE bsky_follow.did = ?1 AND account.did IS NOT NULL ORDER BY bsky_post.tid DESC LIMIT 20")?; + let mut sql_rows = stmt.query(params!(did.to_string()))?; + let mut rows: Vec = vec![]; + while let Some(sql_row) = sql_rows.next()? { + let row = feed_row(sql_row)?; + rows.push(row); + } + rows + }; + for row in rows { + feed.push(feed_row_to_item(srv, row)?); + } + Ok(app_bsky::GenericFeed { feed }) +} + +pub fn bsky_get_author_feed(srv: &mut AtpService, did: &Did) -> Result { + let mut feed: Vec = vec![]; + // TODO: also handle reposts + let rows = { + let mut stmt = srv.atp_db + .conn + .prepare_cached("SELECT account.did, account.handle, bsky_post.tid, bsky_post.cid, bsky_post.indexed_at FROM bsky_post LEFT JOIN account ON bsky_post.did = account.did WHERE bsky_post.did = ?1 ORDER BY bsky_post.tid DESC LIMIT 20")?; + let mut sql_rows = stmt.query(params!(did.to_string()))?; + let mut rows: Vec = vec![]; + while let Some(sql_row) = sql_rows.next()? { + let row = feed_row(sql_row)?; + rows.push(row); + } + rows + }; + for row in rows { + feed.push(feed_row_to_item(srv, row)?); + } + Ok(app_bsky::GenericFeed { feed }) +} + +// TODO: this is a partial implementation +// TODO: should maybe have this take a did and tid instead of a aturi? +pub fn bsky_get_thread( + srv: &mut AtpService, + uri: &AtUri, + _depth: Option, +) -> Result { + // parse the URI + let did = match uri.repository { + DidOrHost::Did(ref did_type, ref did_body) => { + Did::from_str(&format!("did:{}:{}", did_type, did_body))? + } + _ => Err(anyhow!("expected a DID, not handle, in uri: {}", uri))?, + }; + if uri.collection != Some("app.bsky.feed.post".to_string()) { + Err(anyhow!("expected a post collection in uri: {}", uri))?; + }; + let tid = match uri.record { + Some(ref tid) => Tid::from_str(tid)?, + _ => Err(anyhow!("expected a record in uri: {}", uri))?, + }; + + // post itself, as a app_bsky::FeedItem + let post_items = { + let mut stmt = srv.atp_db + .conn + .prepare_cached("SELECT account.did, account.handle, bsky_post.tid, bsky_post.cid, bsky_post.indexed_at FROM bsky_post LEFT JOIN account ON bsky_post.did = account.did WHERE bsky_post.did = ?1 AND bsky_post.tid = ?2")?; + let mut sql_rows = stmt.query(params!(did.to_string(), tid.to_string()))?; + let mut rows: Vec = vec![]; + while let Some(sql_row) = sql_rows.next()? { + let row = feed_row(sql_row)?; + rows.push(row); + } + rows + }; + if post_items.is_empty() { + Err(XrpcError::NotFound("post not found".to_string()))?; + }; + let post_item = feed_row_to_item(srv, post_items.into_iter().next().unwrap())?; + + // TODO: any parent + let parent = None; + + // any children + let mut children = vec![]; + let rows = { + let mut stmt = srv.atp_db + .conn + .prepare_cached("SELECT account.did, account.handle, bsky_post.tid, bsky_post.cid, bsky_post.indexed_at FROM bsky_post LEFT JOIN account ON bsky_post.did = account.did WHERE bsky_post.reply_to_parent_uri = ?1 ORDER BY bsky_post.tid DESC LIMIT 20")?; + let mut sql_rows = stmt.query(params!(uri.to_string()))?; + let mut rows: Vec = vec![]; + while let Some(sql_row) = sql_rows.next()? { + let row = feed_row(sql_row)?; + rows.push(row); + } + rows + }; + for row in rows { + let item = feed_row_to_item(srv, row)?; + children.push(app_bsky::ThreadItem { + uri: item.uri, + cid: item.cid, + author: item.author, + record: item.record, + embed: item.embed, + // don't want a loop here + parent: None, + replyCount: item.replyCount, + // only going to depth of one here + replies: None, + upvoteCount: item.upvoteCount, + downvoteCount: 0, + repostCount: item.repostCount, + indexedAt: item.indexedAt, + myState: None, + }); + } + + let post = app_bsky::ThreadItem { + uri: post_item.uri, + cid: post_item.cid, + author: post_item.author, + record: post_item.record, + embed: post_item.embed, + parent, + replyCount: post_item.replyCount, + replies: Some(children), + upvoteCount: post_item.upvoteCount, + downvoteCount: 0, + repostCount: post_item.repostCount, + indexedAt: post_item.indexedAt, + myState: None, + }; + Ok(app_bsky::PostThread { thread: post }) +} + +#[test] +fn test_bsky_profile() { + use crate::{create_account, created_at_now}; + use adenosine::com_atproto; + use libipld::ipld; + + let post_nsid = Nsid::from_str("app.bsky.feed.post").unwrap(); + let follow_nsid = Nsid::from_str("app.bsky.graph.follow").unwrap(); + + let mut srv = AtpService::new_ephemeral().unwrap(); + let req = com_atproto::AccountRequest { + email: "test@bogus.com".to_string(), + handle: "handle.test".to_string(), + password: "bogus".to_string(), + inviteCode: None, + recoveryKey: None, + }; + let session = create_account(&mut srv, &req, true).unwrap(); + let did = Did::from_str(&session.did).unwrap(); + let profile = bsky_get_profile(&mut srv, &did).unwrap(); + assert_eq!(profile.did, session.did); + assert_eq!(profile.handle, req.handle); + assert_eq!(profile.displayName, None); + assert_eq!(profile.description, None); + assert_eq!(profile.followersCount, 0); + assert_eq!(profile.followsCount, 0); + assert_eq!(profile.postsCount, 0); + + let record = app_bsky::ProfileRecord { + displayName: "Test Name".to_string(), + description: Some("short description".to_string()), + }; + bsky_update_profile(&mut srv, &did, record.clone()).unwrap(); + let profile = bsky_get_profile(&mut srv, &did).unwrap(); + assert_eq!(profile.displayName, Some(record.displayName)); + assert_eq!(profile.description, record.description); + + let record = app_bsky::ProfileRecord { + displayName: "New Test Name".to_string(), + description: Some("longer description".to_string()), + }; + bsky_update_profile(&mut srv, &did, record.clone()).unwrap(); + let profile = bsky_get_profile(&mut srv, &did).unwrap(); + assert_eq!(profile.displayName, Some(record.displayName)); + assert_eq!(profile.description, record.description); + + let mutations = vec![ + Mutation::Create( + follow_nsid.clone(), + srv.tid_gen.next_tid(), + ipld!({"subject": {"did": session.did}, "createdAt": created_at_now()}), + ), + Mutation::Create( + follow_nsid.clone(), + srv.tid_gen.next_tid(), + ipld!({"subject": {"did": "did:web:external.domain"}, "createdAt": created_at_now()}), + ), + Mutation::Create( + post_nsid.clone(), + srv.tid_gen.next_tid(), + ipld!({"text": "first post"}), + ), + Mutation::Create( + post_nsid.clone(), + srv.tid_gen.next_tid(), + ipld!({"text": "second post"}), + ), + Mutation::Create( + post_nsid.clone(), + srv.tid_gen.next_tid(), + ipld!({"text": "third post"}), + ), + ]; + srv.repo + .mutate_repo(&did, &mutations, &srv.pds_keypair) + .unwrap(); + bsky_mutate_db(&mut srv.atp_db, &did, mutations).unwrap(); + + let profile = bsky_get_profile(&mut srv, &did).unwrap(); + assert_eq!(profile.followersCount, 1); + assert_eq!(profile.followsCount, 2); + assert_eq!(profile.postsCount, 3); +} + +#[test] +fn test_bsky_feeds() { + // TODO: test that displayName comes through in feeds and timelines (it does not currently) + use crate::{create_account, created_at_now}; + use adenosine::com_atproto; + use libipld::ipld; + + let post_nsid = Nsid::from_str("app.bsky.feed.post").unwrap(); + let like_nsid = Nsid::from_str("app.bsky.feed.like").unwrap(); + let repost_nsid = Nsid::from_str("app.bsky.feed.repost").unwrap(); + let follow_nsid = Nsid::from_str("app.bsky.graph.follow").unwrap(); + + let mut srv = AtpService::new_ephemeral().unwrap(); + let alice_did = { + let req = com_atproto::AccountRequest { + email: "alice@bogus.com".to_string(), + handle: "alice.test".to_string(), + password: "bogus".to_string(), + inviteCode: None, + recoveryKey: None, + }; + let session = create_account(&mut srv, &req, true).unwrap(); + Did::from_str(&session.did).unwrap() + }; + let bob_did = { + let req = com_atproto::AccountRequest { + email: "bob@bogus.com".to_string(), + handle: "bob.test".to_string(), + password: "bogus".to_string(), + inviteCode: None, + recoveryKey: None, + }; + let session = create_account(&mut srv, &req, true).unwrap(); + Did::from_str(&session.did).unwrap() + }; + let carol_did = { + let req = com_atproto::AccountRequest { + email: "carol@bogus.com".to_string(), + handle: "carol.test".to_string(), + password: "bogus".to_string(), + inviteCode: None, + recoveryKey: None, + }; + let session = create_account(&mut srv, &req, true).unwrap(); + Did::from_str(&session.did).unwrap() + }; + + // all feeds and timelines should be empty + let alice_feed = bsky_get_author_feed(&mut srv, &alice_did).unwrap(); + let alice_timeline = bsky_get_timeline(&mut srv, &alice_did).unwrap(); + assert!(alice_feed.feed.is_empty()); + assert!(alice_timeline.feed.is_empty()); + let bob_feed = bsky_get_author_feed(&mut srv, &bob_did).unwrap(); + let bob_timeline = bsky_get_timeline(&mut srv, &bob_did).unwrap(); + assert!(bob_feed.feed.is_empty()); + assert!(bob_timeline.feed.is_empty()); + let carol_feed = bsky_get_author_feed(&mut srv, &carol_did).unwrap(); + let carol_timeline = bsky_get_timeline(&mut srv, &carol_did).unwrap(); + assert!(carol_feed.feed.is_empty()); + assert!(carol_timeline.feed.is_empty()); + + // alice does some posts + let alice_post1_tid = srv.tid_gen.next_tid(); + let alice_post2_tid = srv.tid_gen.next_tid(); + let alice_post3_tid = srv.tid_gen.next_tid(); + assert!(alice_post1_tid < alice_post2_tid && alice_post2_tid < alice_post3_tid); + let mutations = vec![ + Mutation::Create( + post_nsid.clone(), + alice_post1_tid.clone(), + ipld!({"text": "alice first post"}), + ), + Mutation::Create( + post_nsid.clone(), + alice_post2_tid.clone(), + ipld!({"text": "alice second post"}), + ), + Mutation::Create( + post_nsid.clone(), + alice_post3_tid.clone(), + ipld!({"text": "alice third post"}), + ), + ]; + srv.repo + .mutate_repo(&alice_did, &mutations, &srv.pds_keypair) + .unwrap(); + bsky_mutate_db(&mut srv.atp_db, &alice_did, mutations).unwrap(); + + // bob follows alice, likes first post, reposts second, replies third + let alice_post3_uri = format!( + "at://{}/{}/{}", + alice_did.to_string(), + post_nsid.to_string(), + alice_post3_tid.to_string() + ); + let mutations = vec![ + Mutation::Create( + follow_nsid.clone(), + srv.tid_gen.next_tid(), + ipld!({"subject": {"did": alice_did.to_string()}, "createdAt": created_at_now()}), + ), + Mutation::Create( + like_nsid.clone(), + srv.tid_gen.next_tid(), + ipld!({"subject": {"uri": format!("at://{}/{}/{}", alice_did.to_string(), post_nsid.to_string(), alice_post1_tid.to_string())}, "createdAt": created_at_now()}), + ), + Mutation::Create( + repost_nsid.clone(), + srv.tid_gen.next_tid(), + ipld!({"subject": {"uri": format!("at://{}/{}/{}", alice_did.to_string(), post_nsid.to_string(), alice_post2_tid.to_string())}, "createdAt": created_at_now()}), + ), + Mutation::Create( + post_nsid.clone(), + srv.tid_gen.next_tid(), + ipld!({"text": "bob comment on alice post3", "reply": {"parent": {"uri": alice_post3_uri.clone()}, "root": {"uri": alice_post3_uri.clone()}}}), + ), + ]; + srv.repo + .mutate_repo(&bob_did, &mutations, &srv.pds_keypair) + .unwrap(); + bsky_mutate_db(&mut srv.atp_db, &bob_did, mutations).unwrap(); + + // carol follows bob + let mutations = vec![Mutation::Create( + follow_nsid.clone(), + srv.tid_gen.next_tid(), + ipld!({"subject": {"did": bob_did.to_string()}, "createdAt": created_at_now()}), + )]; + srv.repo + .mutate_repo(&bob_did, &mutations, &srv.pds_keypair) + .unwrap(); + bsky_mutate_db(&mut srv.atp_db, &carol_did, mutations).unwrap(); + + // test alice profile: counts should be updated + let alice_profile = bsky_get_profile(&mut srv, &alice_did).unwrap(); + assert_eq!(alice_profile.followersCount, 1); + assert_eq!(alice_profile.followsCount, 0); + assert_eq!(alice_profile.postsCount, 3); + + // test alice timeline: still empty (?) + let alice_timeline = bsky_get_timeline(&mut srv, &alice_did).unwrap(); + println!("{:?}", alice_timeline); + assert!(alice_timeline.feed.is_empty()); + + // test alice feed: should have 3 posts, with correct counts + let alice_feed = bsky_get_author_feed(&mut srv, &alice_did).unwrap(); + assert_eq!(alice_feed.feed.len(), 3); + + assert_eq!( + alice_feed.feed[2].uri, + format!( + "at://{}/{}/{}", + alice_did.to_string(), + post_nsid.to_string(), + alice_post1_tid.to_string() + ) + ); + // TODO: CID + assert_eq!(alice_feed.feed[2].author.did, alice_did.to_string()); + assert_eq!(alice_feed.feed[2].author.handle, "alice.test"); + assert_eq!(alice_feed.feed[2].repostedBy, None); + assert_eq!( + alice_feed.feed[2].record["text"].as_str().unwrap(), + "alice first post" + ); + assert_eq!(alice_feed.feed[2].embed, None); + assert_eq!(alice_feed.feed[2].replyCount, 0); + assert_eq!(alice_feed.feed[2].repostCount, 0); + assert_eq!(alice_feed.feed[2].upvoteCount, 1); + assert_eq!(alice_feed.feed[2].downvoteCount, 0); + + assert_eq!(alice_feed.feed[1].author.did, alice_did.to_string()); + assert_eq!(alice_feed.feed[1].replyCount, 0); + assert_eq!(alice_feed.feed[1].repostCount, 1); + assert_eq!(alice_feed.feed[1].upvoteCount, 0); + + assert_eq!(alice_feed.feed[0].author.did, alice_did.to_string()); + assert_eq!(alice_feed.feed[0].replyCount, 1); + assert_eq!(alice_feed.feed[0].repostCount, 0); + assert_eq!(alice_feed.feed[0].upvoteCount, 0); + + // test bob timeline: should include alice posts + let bob_timeline = bsky_get_timeline(&mut srv, &bob_did).unwrap(); + println!("BOB TIMELINE ======"); + for item in bob_timeline.feed.iter() { + println!("{:?}", item); + } + assert_eq!(bob_timeline.feed.len(), 3); + assert_eq!( + bob_timeline.feed[2].uri, + format!( + "at://{}/{}/{}", + alice_did.to_string(), + post_nsid.to_string(), + alice_post1_tid.to_string() + ) + ); + // TODO: CID + assert_eq!(bob_timeline.feed[2].author.did, alice_did.to_string()); + assert_eq!(bob_timeline.feed[2].author.handle, "alice.test"); + + // test bob feed: should include repost and reply + let bob_feed = bsky_get_author_feed(&mut srv, &bob_did).unwrap(); + assert_eq!(bob_feed.feed.len(), 1); + // TODO: handle reposts + /* + assert_eq!(bob_feed.feed.len(), 2); + assert_eq!(bob_feed.feed[1].uri, format!("at://{}/{}/{}", alice_did.to_string(), post_nsid.to_string(), alice_post1_tid.to_string())); + // TODO: CID + assert_eq!(bob_feed.feed[1].author.did, alice_did.to_string()); + assert_eq!(bob_feed.feed[1].author.handle, "alice.test"); + assert_eq!(bob_feed.feed[1].repostedBy.as_ref().unwrap().did, bob_did.to_string()); + assert_eq!(bob_feed.feed[1].repostedBy.as_ref().unwrap().handle, "bob.test"); + // TODO: "is a repost" (check record?) + */ + + assert_eq!(bob_feed.feed[0].author.did, bob_did.to_string()); + assert_eq!(bob_feed.feed[0].author.handle, "bob.test"); + + // test carol timeline: should include bob's repost and reply + let carol_timeline = bsky_get_timeline(&mut srv, &carol_did).unwrap(); + // TODO: handle re-posts (+1 here) + assert_eq!(carol_timeline.feed.len(), 1); + // TODO: details + + // test carol feed: still empty + let carol_feed = bsky_get_author_feed(&mut srv, &carol_did).unwrap(); + assert!(carol_feed.feed.is_empty()); +} + +#[test] +fn test_bsky_thread() { + use crate::create_account; + use adenosine::com_atproto; + use libipld::ipld; + + let post_nsid = Nsid::from_str("app.bsky.feed.post").unwrap(); + + let mut srv = AtpService::new_ephemeral().unwrap(); + let alice_did = { + let req = com_atproto::AccountRequest { + email: "alice@bogus.com".to_string(), + handle: "alice.test".to_string(), + password: "bogus".to_string(), + inviteCode: None, + recoveryKey: None, + }; + let session = create_account(&mut srv, &req, true).unwrap(); + Did::from_str(&session.did).unwrap() + }; + let bob_did = { + let req = com_atproto::AccountRequest { + email: "bob@bogus.com".to_string(), + handle: "bob.test".to_string(), + password: "bogus".to_string(), + inviteCode: None, + recoveryKey: None, + }; + let session = create_account(&mut srv, &req, true).unwrap(); + Did::from_str(&session.did).unwrap() + }; + + // alice does a post + let alice_post1_tid = srv.tid_gen.next_tid(); + let mutations = vec![Mutation::Create( + post_nsid.clone(), + alice_post1_tid.clone(), + ipld!({"text": "alice first post"}), + )]; + srv.repo + .mutate_repo(&alice_did, &mutations, &srv.pds_keypair) + .unwrap(); + bsky_mutate_db(&mut srv.atp_db, &alice_did, mutations).unwrap(); + let alice_post1_uri = format!( + "at://{}/{}/{}", + alice_did.to_string(), + post_nsid.to_string(), + alice_post1_tid.to_string() + ); + + // bob likes and replies first post + let bob_post1_tid = srv.tid_gen.next_tid(); + let mutations = vec![Mutation::Create( + post_nsid.clone(), + bob_post1_tid.clone(), + ipld!({"text": "bob comment on alice post1", "reply": {"parent": {"uri": alice_post1_uri.clone()}, "root": {"uri": alice_post1_uri.clone()}}}), + )]; + srv.repo + .mutate_repo(&bob_did, &mutations, &srv.pds_keypair) + .unwrap(); + bsky_mutate_db(&mut srv.atp_db, &bob_did, mutations).unwrap(); + let bob_post1_uri = format!( + "at://{}/{}/{}", + bob_did.to_string(), + post_nsid.to_string(), + bob_post1_tid.to_string() + ); + + // alice replies to bob reply + let alice_post2_tid = srv.tid_gen.next_tid(); + let mutations = vec![Mutation::Create( + post_nsid.clone(), + alice_post2_tid.clone(), + ipld!({"text": "alice second post, replying to bob comment", "reply": {"parent": {"uri": bob_post1_uri.clone()}, "root": {"uri": alice_post1_uri.clone()}}}), + )]; + srv.repo + .mutate_repo(&alice_did, &mutations, &srv.pds_keypair) + .unwrap(); + bsky_mutate_db(&mut srv.atp_db, &alice_did, mutations).unwrap(); + let _alice_post2_uri = format!( + "at://{}/{}/{}", + alice_did.to_string(), + post_nsid.to_string(), + alice_post2_tid.to_string() + ); + + // get thread from bob's post + // TODO: should have both parent and children + let post = bsky_get_thread(&mut srv, &AtUri::from_str(&bob_post1_uri).unwrap(), None) + .unwrap() + .thread; + assert_eq!(post.author.did, bob_did.to_string()); + assert_eq!(post.author.handle, "bob.test".to_string()); + assert_eq!(post.embed, None); + assert_eq!(post.replyCount, 1); + assert_eq!(post.repostCount, 0); + assert_eq!(post.upvoteCount, 0); + assert_eq!(post.replies.as_ref().unwrap().len(), 1); + + let post_replies = post.replies.unwrap(); + assert_eq!(post_replies[0].author.did, alice_did.to_string()); + // TODO: root URI, etc +} diff --git a/adenosine-pds/src/lib.rs b/adenosine-pds/src/lib.rs index 07905f9..cca9b0d 100644 --- a/adenosine-pds/src/lib.rs +++ b/adenosine-pds/src/lib.rs @@ -2,33 +2,51 @@ use adenosine::created_at_now; use adenosine::identifiers::{AtUri, Did, Nsid, Ticker, Tid}; use anyhow::{anyhow, Result}; use askama::Template; -use libipld::Cid; -use libipld::Ipld; use log::{debug, error, info, warn}; use rouille::{router, Request, Response}; use serde_json::{json, Value}; -use std::collections::BTreeMap; use std::fmt; use std::io::Read; use std::path::PathBuf; use std::str::FromStr; use std::sync::Mutex; -mod bsky; mod db; -pub mod models; +mod db_bsky; mod web; -pub use adenosine::crypto::{KeyPair, PubKey}; -pub use adenosine::did; -pub use adenosine::did::DidDocMeta; -pub use adenosine::repo::{Mutation, RepoCommit, RepoStore}; -pub use adenosine::ucan_p256::P256KeyMaterial; -use bsky::*; -pub use db::AtpDatabase; -pub use models::*; +use adenosine::app_bsky; +use adenosine::com_atproto; +use adenosine::crypto::KeyPair; +use adenosine::ipld::{ipld_into_json_value, json_value_into_ipld}; +use adenosine::plc; +use adenosine::plc::DidDocMeta; +use adenosine::repo::{Mutation, RepoStore}; +use db::AtpDatabase; +use db_bsky::*; use web::*; +#[derive(Debug)] +pub enum XrpcError { + BadRequest(String), + NotFound(String), + Forbidden(String), + MutexPoisoned, +} + +impl std::error::Error for XrpcError {} + +impl fmt::Display for XrpcError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::BadRequest(msg) | Self::NotFound(msg) | Self::Forbidden(msg) => { + write!(f, "{}", msg) + } + Self::MutexPoisoned => write!(f, "service mutex poisoned"), + } + } +} + pub struct AtpService { pub repo: RepoStore, pub atp_db: AtpDatabase, @@ -58,27 +76,6 @@ impl Default for AtpServiceConfig { } } -#[derive(Debug)] -enum XrpcError { - BadRequest(String), - NotFound(String), - Forbidden(String), - MutexPoisoned, -} - -impl std::error::Error for XrpcError {} - -impl fmt::Display for XrpcError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::BadRequest(msg) | Self::NotFound(msg) | Self::Forbidden(msg) => { - write!(f, "{}", msg) - } - Self::MutexPoisoned => write!(f, "service mutex poisoned"), - } - } -} - /// Helper to take an XRPC result (always a JSON object), and transform it to a rouille response fn xrpc_wrap(resp: Result) -> Response { match resp { @@ -256,49 +253,6 @@ impl AtpService { } } -/// Intentionally serializing with this instead of DAG-JSON, because ATP schemas don't encode CID -/// links in any special way, they just pass the CID as a string. -fn ipld_into_json_value(val: Ipld) -> Value { - match val { - Ipld::Null => Value::Null, - Ipld::Bool(b) => Value::Bool(b), - Ipld::Integer(v) => json!(v), - Ipld::Float(v) => json!(v), - Ipld::String(s) => Value::String(s), - Ipld::Bytes(b) => Value::String(data_encoding::BASE64_NOPAD.encode(&b)), - Ipld::List(l) => Value::Array(l.into_iter().map(ipld_into_json_value).collect()), - Ipld::Map(m) => Value::Object(serde_json::Map::from_iter( - m.into_iter().map(|(k, v)| (k, ipld_into_json_value(v))), - )), - Ipld::Link(c) => Value::String(c.to_string()), - } -} - -/// Crude reverse generation -/// -/// Does not handle base64 to bytes, and the link generation is pretty simple (object elements with -/// key "car"). Numbers always come through as f64 (float). -fn json_value_into_ipld(val: Value) -> Ipld { - match val { - Value::Null => Ipld::Null, - Value::Bool(b) => Ipld::Bool(b), - Value::String(s) => Ipld::String(s), - // TODO: handle numbers better? - Value::Number(v) => Ipld::Float(v.as_f64().unwrap()), - Value::Array(l) => Ipld::List(l.into_iter().map(json_value_into_ipld).collect()), - Value::Object(m) => { - let map: BTreeMap = BTreeMap::from_iter(m.into_iter().map(|(k, v)| { - if k == "car" && v.is_string() { - (k, Ipld::Link(Cid::from_str(v.as_str().unwrap()).unwrap())) - } else { - (k, json_value_into_ipld(v)) - } - })); - Ipld::Map(map) - } - } -} - fn xrpc_required_param(request: &Request, key: &str) -> Result { Ok(request.get_param(key).ok_or(XrpcError::BadRequest(format!( "require '{}' query parameter", @@ -424,7 +378,7 @@ fn xrpc_get_handler( let mut srv = srv.lock().or(Err(XrpcError::MutexPoisoned))?; let did_doc = srv.atp_db.get_did_doc(&did)?; let collections: Vec = srv.repo.collections(&did)?; - let desc = RepoDescribe { + let desc = com_atproto::repo::Describe { name: did.to_string(), // TODO: handle? did: did.to_string(), didDoc: did_doc, @@ -506,9 +460,9 @@ fn xrpc_get_repo_handler(srv: &Mutex, request: &Request) -> Result Result { +) -> Result { // check if account already exists (fast path, also confirmed by database schema) if srv.atp_db.account_exists(&req.handle, &req.email)? { Err(XrpcError::BadRequest( @@ -520,7 +474,7 @@ pub fn create_account( let (did, did_doc) = if create_did_plc { // generate DID - let create_op = did::CreateOp::new( + let create_op = plc::CreateOp::new( req.handle.clone(), srv.config.public_url.clone(), &srv.pds_keypair, @@ -576,7 +530,7 @@ fn xrpc_post_handler( match method { "com.atproto.account.create" => { // validate account request - let req: AccountRequest = rouille::input::json_input(request) + let req: com_atproto::AccountRequest = rouille::input::json_input(request) .map_err(|e| XrpcError::BadRequest(format!("failed to parse JSON body: {}", e)))?; // TODO: validate handle, email, recoverykey let mut srv = srv.lock().unwrap(); @@ -602,7 +556,7 @@ fn xrpc_post_handler( Ok(json!(sess)) } "com.atproto.session.create" => { - let req: SessionRequest = rouille::input::json_input(request) + let req: com_atproto::SessionRequest = rouille::input::json_input(request) .map_err(|e| XrpcError::BadRequest(format!("failed to parse JSON body: {}", e)))?; let mut srv = srv.lock().unwrap(); let keypair = srv.pds_keypair.clone(); @@ -628,7 +582,7 @@ fn xrpc_post_handler( .resolve_did(&did)? .expect("DID matches to a handle"); - Ok(json!(AtpSession { + Ok(json!(com_atproto::Session { did: did.to_string(), name: handle, accessJwt: jwt.to_string(), @@ -653,7 +607,7 @@ fn xrpc_post_handler( Ok(json!({})) } "com.atproto.repo.batchWrite" => { - let batch: RepoBatchWriteBody = rouille::input::json_input(request)?; + let batch: com_atproto::repo::BatchWriteBody = rouille::input::json_input(request)?; // TODO: validate edits against schemas let did = Did::from_str(&batch.did)?; let mut srv = srv.lock().unwrap(); @@ -690,7 +644,7 @@ fn xrpc_post_handler( } "com.atproto.repo.createRecord" => { // TODO: validate edits against schemas - let create: RepoCreateRecord = rouille::input::json_input(request)?; + let create: com_atproto::repo::CreateRecord = rouille::input::json_input(request)?; let did = Did::from_str(&create.did)?; let collection = Nsid::from_str(&create.collection)?; let mut srv = srv.lock().unwrap(); @@ -707,7 +661,7 @@ fn xrpc_post_handler( } "com.atproto.repo.putRecord" => { // TODO: validate edits against schemas - let put: RepoPutRecord = rouille::input::json_input(request)?; + let put: com_atproto::repo::PutRecord = rouille::input::json_input(request)?; let did = Did::from_str(&put.did)?; let collection = Nsid::from_str(&put.collection)?; let tid = Tid::from_str(&put.rkey)?; @@ -725,7 +679,7 @@ fn xrpc_post_handler( Ok(json!({})) } "com.atproto.repo.deleteRecord" => { - let delete: RepoDeleteRecord = rouille::input::json_input(request)?; + let delete: com_atproto::repo::DeleteRecord = rouille::input::json_input(request)?; let did = Did::from_str(&delete.did)?; let collection = Nsid::from_str(&delete.collection)?; let tid = Tid::from_str(&delete.rkey)?; @@ -754,7 +708,7 @@ fn xrpc_post_handler( } // =========== app.bsky methods "app.bsky.actor.updateProfile" => { - let profile: ProfileRecord = rouille::input::json_input(request)?; + let profile: app_bsky::ProfileRecord = rouille::input::json_input(request)?; let mut srv = srv.lock().unwrap(); let auth_did = &xrpc_check_auth_header(&mut srv, request, None)?; bsky_update_profile(&mut srv, auth_did, profile)?; @@ -865,7 +819,7 @@ fn repo_view_handler(srv: &Mutex, did: &str, request: &Request) -> R let commit_cid = &srv.repo.lookup_commit(&did)?.unwrap(); let commit = srv.repo.get_commit(commit_cid)?; let collections: Vec = srv.repo.collections(&did)?; - let desc = RepoDescribe { + let desc = com_atproto::repo::Describe { name: did.to_string(), // TODO did: did.to_string(), didDoc: did_doc, diff --git a/adenosine-pds/src/models.rs b/adenosine-pds/src/models.rs deleted file mode 100644 index f827a7a..0000000 --- a/adenosine-pds/src/models.rs +++ /dev/null @@ -1,246 +0,0 @@ -use serde_json::Value; - -// =========== com.atproto types (manually entered) - -#[allow(non_snake_case)] -#[derive(Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq)] -pub struct AccountRequest { - pub email: String, - pub handle: String, - pub password: String, - pub inviteCode: Option, - pub recoveryKey: Option, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq)] -pub struct SessionRequest { - pub handle: String, - pub password: String, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct AtpSession { - pub did: String, - pub name: String, - pub accessJwt: String, - pub refreshJwt: String, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct RepoDescribe { - pub name: String, - pub did: String, - pub didDoc: serde_json::Value, - pub collections: Vec, - pub nameIsCorrect: bool, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct RepoCreateRecord { - pub did: String, - pub collection: String, - pub record: serde_json::Value, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct RepoPutRecord { - pub did: String, - pub collection: String, - pub rkey: String, - pub record: serde_json::Value, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct RepoDeleteRecord { - pub did: String, - pub collection: String, - pub rkey: String, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct RepoBatchWriteBody { - pub did: String, - pub writes: Vec, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct RepoBatchWrite { - #[serde(rename = "type")] - pub op_type: String, - pub collection: String, - pub rkey: Option, - pub value: serde_json::Value, -} - -// =========== app.bsky types (manually entered) - -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct Subject { - pub uri: String, - // TODO: CID is required - pub cid: Option, -} - -/// Generic over Re-post and Like -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct RefRecord { - pub subject: Subject, - pub createdAt: String, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct FollowSubject { - pub did: String, - // pub declarationCid: String, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct FollowRecord { - pub subject: FollowSubject, - pub createdAt: String, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct ProfileRecord { - pub displayName: String, - pub description: Option, -} - -// app.bsky.system.actorUser or app.bsky.system.actorScene -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct Declaration { - pub actorType: String, -} - -// actorType: app.bsky.system.actorUser -// cid: bafyreid27zk7lbis4zw5fz4podbvbs4fc5ivwji3dmrwa6zggnj4bnd57u -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct DeclRef { - pub actorType: String, - pub cid: String, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct Profile { - pub did: String, - pub declaration: DeclRef, - pub handle: String, - // for simple accounts, 'creator' is just the did - pub creator: String, - pub displayName: Option, - pub description: Option, - pub followersCount: u64, - pub followsCount: u64, - pub membersCount: u64, - pub postsCount: u64, - pub myState: serde_json::Value, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct GenericFeed { - pub feed: Vec, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct User { - pub did: String, - pub handle: String, - pub displayName: Option, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct FeedItem { - pub uri: String, - pub cid: String, - pub author: User, - pub repostedBy: Option, - pub record: Value, - //pub embed?: RecordEmbed | ExternalEmbed | UnknownEmbed, - pub embed: Option, - pub replyCount: u64, - pub repostCount: u64, - pub upvoteCount: u64, - pub downvoteCount: u64, - pub indexedAt: String, - pub myState: Option, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct Post { - pub text: String, - pub reply: Option, - pub createdAt: Option, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct PostReply { - pub parent: Subject, - pub root: Subject, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct PostThread { - pub thread: ThreadItem, -} - -// TODO: 'parent' and 'replies' should allow "NotFoundPost" for references that point to an unknown -// URI -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct ThreadItem { - pub uri: String, - pub cid: String, - pub author: User, - pub record: Value, - //pub embed?: RecordEmbed | ExternalEmbed | UnknownEmbed, - pub embed: Option, - pub parent: Option>, - pub replyCount: u64, - pub replies: Option>, - pub repostCount: u64, - pub upvoteCount: u64, - pub downvoteCount: u64, - pub indexedAt: String, - pub myState: Option, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct FollowTarget { - // TODO: nested follow list? - pub subject: Subject, - pub did: String, - pub handle: String, - pub displayName: Option, - pub createdAt: Option, - pub indexedAt: String, -} - -#[allow(non_snake_case)] -#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq, Eq)] -pub struct Follow { - // TODO: nested follow list? - pub subject: Subject, - pub follows: FollowTarget, -} diff --git a/adenosine-pds/src/web.rs b/adenosine-pds/src/web.rs index 0d80b4e..8f15a49 100644 --- a/adenosine-pds/src/web.rs +++ b/adenosine-pds/src/web.rs @@ -1,4 +1,5 @@ -use crate::models::*; +use adenosine::app_bsky; +use adenosine::com_atproto; use adenosine::identifiers::{Did, Nsid, Tid}; use adenosine::repo::RepoCommit; use askama::Template; @@ -28,8 +29,8 @@ pub struct AboutView { pub struct AccountView { pub domain: String, pub did: Did, - pub profile: Profile, - pub feed: Vec, + pub profile: app_bsky::Profile, + pub feed: Vec, } #[derive(Template)] @@ -39,7 +40,7 @@ pub struct ThreadView { pub did: Did, pub collection: Nsid, pub tid: Tid, - pub post: ThreadItem, + pub post: app_bsky::ThreadItem, } #[derive(Template)] @@ -48,7 +49,7 @@ pub struct RepoView { pub domain: String, pub did: Did, pub commit: RepoCommit, - pub describe: RepoDescribe, + pub describe: com_atproto::repo::Describe, } #[derive(Template)] diff --git a/adenosine-pds/tests/bigger.car b/adenosine-pds/tests/bigger.car deleted file mode 100644 index 7169013..0000000 Binary files a/adenosine-pds/tests/bigger.car and /dev/null differ diff --git a/adenosine-pds/tests/example_repo.car b/adenosine-pds/tests/example_repo.car deleted file mode 100644 index b2ae723..0000000 Binary files a/adenosine-pds/tests/example_repo.car and /dev/null differ diff --git a/adenosine-pds/tests/test_mst_interop.rs b/adenosine-pds/tests/test_mst_interop.rs deleted file mode 100644 index 8a5becc..0000000 --- a/adenosine-pds/tests/test_mst_interop.rs +++ /dev/null @@ -1,152 +0,0 @@ -use adenosine_pds::RepoStore; -use libipld::Cid; -use std::collections::BTreeMap; -use std::str::FromStr; - -#[test] -fn test_known_maps() { - let mut repo = RepoStore::open_ephemeral().unwrap(); - let cid1 = - Cid::from_str("bafyreie5cvv4h45feadgeuwhbcutmh6t2ceseocckahdoe6uat64zmz454").unwrap(); - - let empty_map: BTreeMap = Default::default(); - assert_eq!( - repo.mst_from_map(&empty_map).unwrap().to_string(), - "bafyreie5737gdxlw5i64vzichcalba3z2v5n6icifvx5xytvske7mr3hpm" - ); - - let mut trivial_map: BTreeMap = Default::default(); - trivial_map.insert("asdf".to_string(), cid1.clone()); - assert_eq!( - repo.mst_from_map(&trivial_map).unwrap().to_string(), - "bafyreidaftbr35xhh4lzmv5jcoeufqjh75ohzmz6u56v7n2ippbtxdgqqe" - ); - - let mut singlelayer2_map: BTreeMap = Default::default(); - singlelayer2_map.insert("com.example.record/9ba1c7247ede".to_string(), cid1.clone()); - assert_eq!( - repo.mst_from_map(&singlelayer2_map).unwrap().to_string(), - "bafyreid4g5smj6ukhrjasebt6myj7wmtm2eijouteoyueoqgoh6vm5jkae" - ); - - let mut simple_map: BTreeMap = Default::default(); - simple_map.insert("asdf".to_string(), cid1.clone()); - simple_map.insert("88bfafc7".to_string(), cid1.clone()); - simple_map.insert("2a92d355".to_string(), cid1.clone()); - simple_map.insert("app.bsky.feed.post/454397e440ec".to_string(), cid1.clone()); - simple_map.insert("app.bsky.feed.post/9adeb165882c".to_string(), cid1.clone()); - assert_eq!( - repo.mst_from_map(&simple_map).unwrap().to_string(), - "bafyreiecb33zh7r2sc3k2wthm6exwzfktof63kmajeildktqc25xj6qzx4" - ); -} - -// TODO: behavior of these wide-char keys is undefined behavior in string MST -#[ignore] -#[test] -fn test_tricky_map() { - let mut repo = RepoStore::open_ephemeral().unwrap(); - let cid1 = - Cid::from_str("bafyreie5cvv4h45feadgeuwhbcutmh6t2ceseocckahdoe6uat64zmz454").unwrap(); - - let mut tricky_map: BTreeMap = Default::default(); - tricky_map.insert("".to_string(), cid1.clone()); - tricky_map.insert("jalapeño".to_string(), cid1.clone()); - tricky_map.insert("coöperative".to_string(), cid1.clone()); - tricky_map.insert("coüperative".to_string(), cid1.clone()); - tricky_map.insert("abc\x00".to_string(), cid1.clone()); - assert_eq!( - repo.mst_from_map(&tricky_map).unwrap().to_string(), - "bafyreiecb33zh7r2sc3k2wthm6exwzfktof63kmajeildktqc25xj6qzx4" - ); -} - -#[test] -fn test_trims_top() { - // "trims top of tree on delete" - - use adenosine_pds::mst::print_mst_keys; - let mut repo = RepoStore::open_ephemeral().unwrap(); - let cid1 = - Cid::from_str("bafyreie5cvv4h45feadgeuwhbcutmh6t2ceseocckahdoe6uat64zmz454").unwrap(); - let l1root = "bafyreihuyj2vzb2vjw3yhxg6dy25achg5fmre6gg5m6fjtxn64bqju4dee"; - let l0root = "bafyreibmijjc63mekkjzl3v2pegngwke5u6cu66g75z6uw27v64bc6ahqi"; - - // NOTE: this test doesn't do much in this case of rust implementation - let mut trim_map: BTreeMap = Default::default(); - trim_map.insert("com.example.record/40c73105b48f".to_string(), cid1.clone()); // level 0 - trim_map.insert("com.example.record/e99bf3ced34b".to_string(), cid1.clone()); // level 0 - trim_map.insert("com.example.record/893e6c08b450".to_string(), cid1.clone()); // level 0 - trim_map.insert("com.example.record/9cd8b6c0cc02".to_string(), cid1.clone()); // level 0 - trim_map.insert("com.example.record/cbe72d33d12a".to_string(), cid1.clone()); // level 0 - trim_map.insert("com.example.record/a15e33ba0f6c".to_string(), cid1.clone()); // level 1 - let trim_before_cid = repo.mst_from_map(&trim_map).unwrap(); - print_mst_keys(&mut repo.db, &trim_before_cid).unwrap(); - assert_eq!(trim_before_cid.to_string(), l1root); - - // NOTE: if we did mutations in-place, this is where we would mutate - - trim_map.remove("com.example.record/a15e33ba0f6c"); - let trim_after_cid = repo.mst_from_map(&trim_map).unwrap(); - assert_eq!(trim_after_cid.to_string(), l0root); -} - -#[test] -fn test_insertion() { - // "handles insertion that splits two layers down" - - let mut repo = RepoStore::open_ephemeral().unwrap(); - let cid1 = - Cid::from_str("bafyreie5cvv4h45feadgeuwhbcutmh6t2ceseocckahdoe6uat64zmz454").unwrap(); - let l1root = "bafyreiagt55jzvkenoa4yik77dhomagq2uj26ix4cijj7kd2py2u3s43ve"; - let l2root = "bafyreiddrz7qbvfattp5dzzh4ldohsaobatsg7f5l6awxnmuydewq66qoa"; - - // TODO: actual mutation instead of rebuild from scratch - let mut insertion_map: BTreeMap = Default::default(); - insertion_map.insert("com.example.record/403e2aeebfdb".to_string(), cid1.clone()); // A; level 0 - insertion_map.insert("com.example.record/40c73105b48f".to_string(), cid1.clone()); // B; level 0 - insertion_map.insert("com.example.record/645787eb4316".to_string(), cid1.clone()); // C; level 0 - insertion_map.insert("com.example.record/7ca4e61d6fbc".to_string(), cid1.clone()); // D; level 1 - insertion_map.insert("com.example.record/893e6c08b450".to_string(), cid1.clone()); // E; level 0 - insertion_map.insert("com.example.record/9cd8b6c0cc02".to_string(), cid1.clone()); // G; level 0 - insertion_map.insert("com.example.record/cbe72d33d12a".to_string(), cid1.clone()); // H; level 0 - insertion_map.insert("com.example.record/dbea731be795".to_string(), cid1.clone()); // I; level 1 - insertion_map.insert("com.example.record/e2ef555433f2".to_string(), cid1.clone()); // J; level 0 - insertion_map.insert("com.example.record/e99bf3ced34b".to_string(), cid1.clone()); // K; level 0 - insertion_map.insert("com.example.record/f728ba61e4b6".to_string(), cid1.clone()); // L; level 0 - let insertion_before_cid = repo.mst_from_map(&insertion_map).unwrap(); - assert_eq!(insertion_before_cid.to_string(), l1root); - - insertion_map.insert("com.example.record/9ba1c7247ede".to_string(), cid1.clone()); - let insertion_after_cid = repo.mst_from_map(&insertion_map).unwrap(); - assert_eq!(insertion_after_cid.to_string(), l2root); -} - -#[test] -fn test_higher_layers() { - // "handles new layers that are two higher than existing" - - use adenosine_pds::mst::print_mst_keys; - let mut repo = RepoStore::open_ephemeral().unwrap(); - let cid1 = - Cid::from_str("bafyreie5cvv4h45feadgeuwhbcutmh6t2ceseocckahdoe6uat64zmz454").unwrap(); - let l0root = "bafyreicivoa3p3ttcebdn2zfkdzenkd2uk3gxxlaz43qvueeip6yysvq2m"; - let l2root = "bafyreidwoqm6xlewxzhrx6ytbyhsazctlv72txtmnd4au6t53z2vpzn7wa"; - let l2root2 = "bafyreiapru27ce4wdlylk5revtr3hewmxhmt3ek5f2ypioiivmdbv5igrm"; - - // TODO: actual mutation instead of rebuild from scratch - let mut higher_map: BTreeMap = Default::default(); - higher_map.insert("com.example.record/403e2aeebfdb".to_string(), cid1.clone()); // A; level 0 - higher_map.insert("com.example.record/cbe72d33d12a".to_string(), cid1.clone()); // C; level 0 - let higher_before_cid = repo.mst_from_map(&higher_map).unwrap(); - assert_eq!(higher_before_cid.to_string(), l0root); - - higher_map.insert("com.example.record/9ba1c7247ede".to_string(), cid1.clone()); // B; level 2 - let higher_after_cid = repo.mst_from_map(&higher_map).unwrap(); - print_mst_keys(&mut repo.db, &higher_after_cid).unwrap(); - assert_eq!(higher_after_cid.to_string(), l2root); - - higher_map.insert("com.example.record/fae7a851fbeb".to_string(), cid1.clone()); // D; level 1 - let higher_after_cid = repo.mst_from_map(&higher_map).unwrap(); - assert_eq!(higher_after_cid.to_string(), l2root2); -} diff --git a/adenosine-pds/tests/test_repro_mst.rs b/adenosine-pds/tests/test_repro_mst.rs deleted file mode 100644 index df88559..0000000 --- a/adenosine-pds/tests/test_repro_mst.rs +++ /dev/null @@ -1,26 +0,0 @@ -use adenosine_pds::RepoStore; -use std::path::PathBuf; -use std::str::FromStr; - -#[test] -fn test_repro_mst() { - let mut repo = RepoStore::open_ephemeral().unwrap(); - let cid = repo - .import_car_path( - &PathBuf::from_str("./tests/example_repo.car").unwrap(), - None, - ) - .unwrap(); - repo.verify_repo_mst(&cid).unwrap(); - let cid = repo - .import_car_path(&PathBuf::from_str("./tests/bigger.car").unwrap(), None) - .unwrap(); - repo.verify_repo_mst(&cid).unwrap(); - - // test round-tripping from export - let car_bytes = repo.export_car(&cid, None).unwrap(); - let mut other_repo = RepoStore::open_ephemeral().unwrap(); - let other_cid = other_repo.import_car_bytes(&car_bytes, None).unwrap(); - other_repo.verify_repo_mst(&cid).unwrap(); - assert_eq!(cid, other_cid); -} -- cgit v1.2.3