aboutsummaryrefslogtreecommitdiffstats
path: root/adenosine-pds/src/db_bsky.rs
diff options
context:
space:
mode:
Diffstat (limited to 'adenosine-pds/src/db_bsky.rs')
-rw-r--r--adenosine-pds/src/db_bsky.rs117
1 files changed, 71 insertions, 46 deletions
diff --git a/adenosine-pds/src/db_bsky.rs b/adenosine-pds/src/db_bsky.rs
index b80ff4f..c603235 100644
--- a/adenosine-pds/src/db_bsky.rs
+++ b/adenosine-pds/src/db_bsky.rs
@@ -8,7 +8,6 @@ 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
@@ -59,7 +58,10 @@ pub fn bsky_mutate_db(db: &mut AtpDatabase, did: &Did, mutations: Vec<Mutation>)
}
// TODO: should probably return Result<Option<Profile>>?
-pub fn bsky_get_profile(srv: &mut AtpService, did: &Did) -> Result<app_bsky::ProfileView> {
+pub fn bsky_get_profile_detailed(
+ srv: &mut AtpService,
+ did: &Did,
+) -> Result<app_bsky::ProfileViewDetailed> {
// first get the profile record
let mut profile_cid: Option<Cid> = None;
let commit_cid = match srv.repo.lookup_commit(did)? {
@@ -78,7 +80,7 @@ pub fn bsky_get_profile(srv: &mut AtpService, did: &Did) -> Result<app_bsky::Pro
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)
+ (record.displayName, record.description)
} else {
(None, None)
};
@@ -102,22 +104,33 @@ pub fn bsky_get_profile(srv: &mut AtpService, did: &Did) -> Result<app_bsky::Pro
.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::ProfileView {
+ Ok(app_bsky::ProfileViewDetailed {
did: did.to_string(),
handle,
- creator: did.to_string(),
displayName: display_name,
description,
- declaration: decl,
+ avatar: None,
+ banner: None,
followersCount: followers_count,
followsCount: follows_count,
postsCount: post_count,
- membersCount: 0,
- viewer: json!({}),
+ viewer: None,
+ labels: None,
+ indexedAt: None,
+ })
+}
+
+pub fn bsky_get_profile(srv: &mut AtpService, did: &Did) -> Result<app_bsky::ProfileView> {
+ let d = bsky_get_profile_detailed(srv, did)?;
+ Ok(app_bsky::ProfileView {
+ did: d.did,
+ handle: d.handle,
+ displayName: d.displayName,
+ description: d.description,
+ avatar: d.avatar,
+ indexedAt: d.indexedAt,
+ viewer: d.viewer,
+ labels: d.labels,
})
}
@@ -177,9 +190,10 @@ fn feed_row(row: &rusqlite::Row) -> Result<FeedRow> {
})
}
-fn feed_row_to_item(srv: &mut AtpService, row: FeedRow) -> Result<app_bsky::FeedPostView> {
+fn feed_row_to_item(srv: &mut AtpService, row: FeedRow) -> Result<app_bsky::FeedViewPost> {
let record_ipld = srv.repo.get_ipld(&row.item_post_cid)?;
- let post_record: app_bsky::Post = serde_json::from_value(ipld_into_json_value(record_ipld))?;
+ let post_record: app_bsky::PostRecord =
+ serde_json::from_value(ipld_into_json_value(record_ipld))?;
let uri = format!(
"at://{}/{}/{}",
row.item_did, "app.bsky.feed.post", row.item_post_tid
@@ -201,31 +215,27 @@ fn feed_row_to_item(srv: &mut AtpService, row: FeedRow) -> Result<app_bsky::Feed
.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 decl = app_bsky::DeclRef {
- actorType: "app.bsky.system.actorUser".to_string(),
- cid: "bafyreid27zk7lbis4zw5fz4podbvbs4fc5ivwji3dmrwa6zggnj4bnd57u".to_string(),
- };
- let feed_item = app_bsky::FeedPostView {
+ let feed_item = app_bsky::FeedViewPost {
post: app_bsky::PostView {
uri,
cid: row.item_post_cid.to_string(),
- author: app_bsky::UserView {
+ author: app_bsky::ProfileViewBasic {
did: row.item_did.to_string(),
handle: row.item_handle,
- declaration: decl,
// TODO:
displayName: None,
avatar: None,
viewer: None,
+ labels: None,
},
record: post_record,
embed: None,
replyCount: reply_count,
repostCount: repost_count,
- upvoteCount: like_count,
- downvoteCount: 0,
+ likeCount: like_count,
indexedAt: row.indexed_at,
viewer: None,
+ labels: None,
},
// TODO:
reason: None,
@@ -235,7 +245,7 @@ fn feed_row_to_item(srv: &mut AtpService, row: FeedRow) -> Result<app_bsky::Feed
}
pub fn bsky_get_timeline(srv: &mut AtpService, did: &Did) -> Result<app_bsky::GenericFeed> {
- let mut feed: Vec<app_bsky::FeedPostView> = vec![];
+ let mut feed: Vec<app_bsky::FeedViewPost> = vec![];
// TODO: also handle reposts
let rows = {
let mut stmt = srv.atp_db
@@ -252,11 +262,11 @@ pub fn bsky_get_timeline(srv: &mut AtpService, did: &Did) -> Result<app_bsky::Ge
for row in rows {
feed.push(feed_row_to_item(srv, row)?);
}
- Ok(app_bsky::GenericFeed { feed })
+ Ok(app_bsky::GenericFeed { feed, cursor: None })
}
pub fn bsky_get_author_feed(srv: &mut AtpService, did: &Did) -> Result<app_bsky::GenericFeed> {
- let mut feed: Vec<app_bsky::FeedPostView> = vec![];
+ let mut feed: Vec<app_bsky::FeedViewPost> = vec![];
// TODO: also handle reposts
let rows = {
let mut stmt = srv.atp_db
@@ -273,7 +283,7 @@ pub fn bsky_get_author_feed(srv: &mut AtpService, did: &Did) -> Result<app_bsky:
for row in rows {
feed.push(feed_row_to_item(srv, row)?);
}
- Ok(app_bsky::GenericFeed { feed })
+ Ok(app_bsky::GenericFeed { feed, cursor: None })
}
// TODO: this is a partial implementation
@@ -298,7 +308,7 @@ pub fn bsky_get_thread(
_ => Err(anyhow!("expected a record in uri: {}", uri))?,
};
- // post itself, as a app_bsky::FeedPostView
+ // post itself, as a app_bsky::FeedViewPost
let post_items = {
let mut stmt = srv.atp_db
.conn
@@ -335,7 +345,7 @@ pub fn bsky_get_thread(
};
for row in rows {
let item = feed_row_to_item(srv, row)?.post;
- children.push(app_bsky::ThreadPostView {
+ children.push(app_bsky::ThreadViewPost {
post: Some(app_bsky::PostView {
uri: item.uri,
cid: item.cid,
@@ -343,11 +353,11 @@ pub fn bsky_get_thread(
record: item.record,
embed: item.embed,
replyCount: item.replyCount,
- upvoteCount: item.upvoteCount,
- downvoteCount: 0,
+ likeCount: item.likeCount,
repostCount: item.repostCount,
indexedAt: item.indexedAt,
viewer: None,
+ labels: None,
}),
// don't want a loop here
parent: None,
@@ -356,11 +366,14 @@ pub fn bsky_get_thread(
// for "notfound"
uri: None,
notFound: None,
+ // for "blocked" variant
+ blocked: None,
+ author: None,
});
}
let pip = post_item.post;
- let post = app_bsky::ThreadPostView {
+ let post = app_bsky::ThreadViewPost {
post: Some(app_bsky::PostView {
uri: pip.uri,
cid: pip.cid,
@@ -368,17 +381,20 @@ pub fn bsky_get_thread(
record: pip.record,
embed: pip.embed,
replyCount: pip.replyCount,
- upvoteCount: pip.upvoteCount,
- downvoteCount: 0,
+ likeCount: pip.likeCount,
repostCount: pip.repostCount,
indexedAt: pip.indexedAt,
viewer: None,
+ labels: None,
}),
parent,
replies: Some(children),
// for "notfound" variant
uri: None,
notFound: None,
+ // for "blocked" variant
+ blocked: None,
+ author: None,
};
Ok(app_bsky::PostThread { thread: post })
}
@@ -394,6 +410,7 @@ fn test_bsky_profile() {
let mut srv = AtpService::new_ephemeral().unwrap();
let req = com_atproto::AccountRequest {
+ did: None,
email: "test@bogus.com".to_string(),
handle: "handle.test".to_string(),
password: "bogus".to_string(),
@@ -402,7 +419,7 @@ fn test_bsky_profile() {
};
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();
+ let profile = bsky_get_profile_detailed(&mut srv, &did).unwrap();
assert_eq!(profile.did, session.did);
assert_eq!(profile.handle, req.handle);
assert_eq!(profile.displayName, None);
@@ -412,21 +429,25 @@ fn test_bsky_profile() {
assert_eq!(profile.postsCount, 0);
let record = app_bsky::ProfileRecord {
- displayName: "Test Name".to_string(),
+ displayName: Some("Test Name".to_string()),
description: Some("short description".to_string()),
+ avatar: None,
+ banner: None,
};
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.displayName, record.displayName);
assert_eq!(profile.description, record.description);
let record = app_bsky::ProfileRecord {
- displayName: "New Test Name".to_string(),
+ displayName: Some("New Test Name".to_string()),
description: Some("longer description".to_string()),
+ avatar: None,
+ banner: None,
};
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.displayName, record.displayName);
assert_eq!(profile.description, record.description);
let mutations = vec![
@@ -461,7 +482,7 @@ fn test_bsky_profile() {
.unwrap();
bsky_mutate_db(&mut srv.atp_db, &did, mutations).unwrap();
- let profile = bsky_get_profile(&mut srv, &did).unwrap();
+ let profile = bsky_get_profile_detailed(&mut srv, &did).unwrap();
assert_eq!(profile.followersCount, 1);
assert_eq!(profile.followsCount, 2);
assert_eq!(profile.postsCount, 3);
@@ -482,6 +503,7 @@ fn test_bsky_feeds() {
let mut srv = AtpService::new_ephemeral().unwrap();
let alice_did = {
let req = com_atproto::AccountRequest {
+ did: None,
email: "alice@bogus.com".to_string(),
handle: "alice.test".to_string(),
password: "bogus".to_string(),
@@ -493,6 +515,7 @@ fn test_bsky_feeds() {
};
let bob_did = {
let req = com_atproto::AccountRequest {
+ did: None,
email: "bob@bogus.com".to_string(),
handle: "bob.test".to_string(),
password: "bogus".to_string(),
@@ -504,6 +527,7 @@ fn test_bsky_feeds() {
};
let carol_did = {
let req = com_atproto::AccountRequest {
+ did: None,
email: "carol@bogus.com".to_string(),
handle: "carol.test".to_string(),
password: "bogus".to_string(),
@@ -596,7 +620,7 @@ fn test_bsky_feeds() {
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();
+ let alice_profile = bsky_get_profile_detailed(&mut srv, &alice_did).unwrap();
assert_eq!(alice_profile.followersCount, 1);
assert_eq!(alice_profile.followsCount, 0);
assert_eq!(alice_profile.postsCount, 3);
@@ -621,18 +645,17 @@ fn test_bsky_feeds() {
assert_eq!(alice_feed.feed[2].post.embed, None);
assert_eq!(alice_feed.feed[2].post.replyCount, 0);
assert_eq!(alice_feed.feed[2].post.repostCount, 0);
- assert_eq!(alice_feed.feed[2].post.upvoteCount, 1);
- assert_eq!(alice_feed.feed[2].post.downvoteCount, 0);
+ assert_eq!(alice_feed.feed[2].post.likeCount, 1);
assert_eq!(alice_feed.feed[1].post.author.did, alice_did.to_string());
assert_eq!(alice_feed.feed[1].post.replyCount, 0);
assert_eq!(alice_feed.feed[1].post.repostCount, 1);
- assert_eq!(alice_feed.feed[1].post.upvoteCount, 0);
+ assert_eq!(alice_feed.feed[1].post.likeCount, 0);
assert_eq!(alice_feed.feed[0].post.author.did, alice_did.to_string());
assert_eq!(alice_feed.feed[0].post.replyCount, 1);
assert_eq!(alice_feed.feed[0].post.repostCount, 0);
- assert_eq!(alice_feed.feed[0].post.upvoteCount, 0);
+ assert_eq!(alice_feed.feed[0].post.likeCount, 0);
// test bob timeline: should include alice posts
let bob_timeline = bsky_get_timeline(&mut srv, &bob_did).unwrap();
@@ -687,6 +710,7 @@ fn test_bsky_thread() {
let mut srv = AtpService::new_ephemeral().unwrap();
let alice_did = {
let req = com_atproto::AccountRequest {
+ did: None,
email: "alice@bogus.com".to_string(),
handle: "alice.test".to_string(),
password: "bogus".to_string(),
@@ -698,6 +722,7 @@ fn test_bsky_thread() {
};
let bob_did = {
let req = com_atproto::AccountRequest {
+ did: None,
email: "bob@bogus.com".to_string(),
handle: "bob.test".to_string(),
password: "bogus".to_string(),
@@ -758,7 +783,7 @@ fn test_bsky_thread() {
assert_eq!(ppost.embed, None);
assert_eq!(ppost.replyCount, 1);
assert_eq!(ppost.repostCount, 0);
- assert_eq!(ppost.upvoteCount, 0);
+ assert_eq!(ppost.likeCount, 0);
assert_eq!(post.replies.as_ref().unwrap().len(), 1);
let post_replies = post.replies.unwrap();