Constellation, Spacedust, Slingshot, UFOs: atproto crates and services for microcosm

Convert existing REST /links/distinct-dids endpoint to XRPC equivalent #8 #9

merged opened by seoul.systems targeting main from seoul.systems/microcosm-rs: xrpc_get_distinct

Simple conversion of the existing endpoint from REST to XRPC. Consecutively marked the pre-existing REST endpoint as deprecated.

Labels

None yet.

Participants 3
AT URI
at://did:plc:53wellrw53o7sw4zlpfenvuh/sh.tangled.repo.pull/3mcysjpzwwp22
+226 -15
Diff #4
+79
constellation/src/server/mod.rs
··· 125 125 }), 126 126 ) 127 127 .route( 128 + // deprecated 128 129 "/links", 129 130 get({ 130 131 let store = store.clone(); ··· 136 137 }), 137 138 ) 138 139 .route( 140 + "/xrpc/blue.microcosm.links.getBacklinkDids", 141 + get({ 142 + let store = store.clone(); 143 + move |accept, query| async { 144 + spawn_blocking(|| get_backlink_dids(accept, query, store)) 145 + .await 146 + .map_err(to500)? 147 + } 148 + }), 149 + ) 150 + // deprecated 151 + .route( 139 152 "/links/distinct-dids", 140 153 get({ 141 154 let store = store.clone(); ··· 158 171 } 159 172 }), 160 173 ) 174 + // deprecated 161 175 .route( 162 176 "/links/all", 163 177 get({ ··· 661 675 GetLinkItemsResponse { 662 676 total: paged.total, 663 677 linking_records: paged.items, 678 + cursor, 679 + query: (*query).clone(), 680 + }, 681 + )) 682 + } 683 + 684 + #[derive(Clone, Deserialize)] 685 + struct GetBacklinkDidsQuery { 686 + subject: String, 687 + source: String, 688 + cursor: Option<OpaqueApiCursor>, 689 + limit: Option<u64>, 690 + // TODO: allow reverse (er, forward) order as well 691 + } 692 + #[derive(Template, Serialize)] 693 + #[template(path = "get-backlink-dids.html.j2")] 694 + struct GetBacklinkDidsResponse { 695 + // what does staleness mean? 696 + // - new links have appeared. would be nice to offer a `since` cursor to fetch these. and/or, 697 + // - links have been deleted. hmm. 698 + total: u64, 699 + linking_dids: Vec<Did>, 700 + cursor: Option<OpaqueApiCursor>, 701 + #[serde(skip_serializing)] 702 + query: GetBacklinkDidsQuery, 703 + } 704 + fn get_backlink_dids( 705 + accept: ExtractAccept, 706 + query: Query<GetBacklinkDidsQuery>, 707 + store: impl LinkReader, 708 + ) -> Result<impl IntoResponse, http::StatusCode> { 709 + let until = query 710 + .cursor 711 + .clone() 712 + .map(|oc| ApiCursor::try_from(oc).map_err(|_| http::StatusCode::BAD_REQUEST)) 713 + .transpose()? 714 + .map(|c| c.next); 715 + 716 + let limit = query.limit.unwrap_or(DEFAULT_CURSOR_LIMIT); 717 + if limit > DEFAULT_CURSOR_LIMIT_MAX { 718 + return Err(http::StatusCode::BAD_REQUEST); 719 + } 720 + 721 + let Some((collection, path)) = query.source.split_once(':') else { 722 + return Err(http::StatusCode::BAD_REQUEST); 723 + }; 724 + let path = format!(".{path}"); 725 + 726 + let paged = store 727 + .get_distinct_dids(&query.subject, collection, &path, limit, until) 728 + .map_err(|_| http::StatusCode::INTERNAL_SERVER_ERROR)?; 729 + 730 + let cursor = paged.next.map(|next| { 731 + ApiCursor { 732 + version: paged.version, 733 + next, 734 + } 735 + .into() 736 + }); 737 + 738 + Ok(acceptable( 739 + accept, 740 + GetBacklinkDidsResponse { 741 + total: paged.total, 742 + linking_dids: paged.items, 664 743 cursor, 665 744 query: (*query).clone(), 666 745 },
+2 -2
constellation/src/storage/mem_store.rs
··· 396 396 ) -> Result<HashMap<String, HashMap<String, CountsByCount>>> { 397 397 let data = self.0.lock().unwrap(); 398 398 let mut out: HashMap<String, HashMap<String, CountsByCount>> = HashMap::new(); 399 - if let Some(asdf) = data.targets.get(&Target::new(target)) { 400 - for (Source { collection, path }, linkers) in asdf { 399 + if let Some(source_linker_pairs) = data.targets.get(&Target::new(target)) { 400 + for (Source { collection, path }, linkers) in source_linker_pairs { 401 401 let records = linkers.iter().flatten().count() as u64; 402 402 let distinct_dids = linkers 403 403 .iter()
+29
constellation/templates/hello.html.j2
··· 82 82 25, 83 83 ) %} 84 84 85 + <h3 class="route"><code>GET /xrpc/blue.microcosm.links.getBacklinkDids</code></h3> 86 + 87 + <p>A list of distinct DIDs (identities) with links to a target.</p> 88 + 89 + <h4>Query parameters:</h4> 90 + 91 + <ul> 92 + <li><code>subject</code>: required, must url-encode. The target being linked to. Example: <code>did:plc:vc7f4oafdgxsihk4cry2xpze</code> or <code>at://did:plc:vc7f4oafdgxsihk4cry2xpze/app.bsky.feed.post/3lgwdn7vd722r</code></li> 93 + <li><code>source</code>: required. Collection and path specification for the primary link. Example: <code>app.bsky.feed.like:subject.uri</code></li> 94 + <li><code>limit</code>: optional. Number of results to return. Default: <code>16</code>. Maximum: <code>100</code></li> 95 + <li><code>cursor</code>: optional, see Definitions.</li> 96 + </ul> 97 + 98 + <p style="margin-bottom: 0"><strong>Try it:</strong></p> 99 + {% call try_it::get_backlink_dids("at://did:plc:vc7f4oafdgxsihk4cry2xpze/app.bsky.feed.post/3lgwdn7vd722r", "app.bsky.feed.like:subject.uri") %} 100 + 85 101 86 102 <h3 class="route"><code>GET /links</code></h3> 87 103 ··· 104 120 <p style="margin-bottom: 0"><strong>Try it:</strong></p> 105 121 {% call try_it::links("at://did:plc:a4pqq234yw7fqbddawjo7y35/app.bsky.feed.post/3m237ilwc372e", "app.bsky.feed.like", ".subject.uri", [""], 16) %} 106 122 123 + <h3 class="route"><code>GET /xrpc/blue.microcosm.links.getBacklinks</code></h3> 124 + 125 + <p>A list of distinct DIDs (identities) with links to a target.</p> 126 + 127 + <h4>Query parameters:</h4> 128 + 129 + <ul> 130 + <li><code>subject</code>: required, must url-encode. Example: <code>at://did:plc:vc7f4oafdgxsihk4cry2xpze/app.bsky.feed.post/3lgwdn7vd722r</code></li> 131 + <li><code>source</code>: required, must url-encode. Example: <code>app.bsky.feed.post:.subject.uri</code></li> 132 + </ul> 133 + 134 + <p style="margin-bottom: 0"><strong>Try it:</strong></p> 135 + {% call try_it::get_backlink_dids("at://did:plc:vc7f4oafdgxsihk4cry2xpze/app.bsky.feed.post/3lgwdn7vd722r", "app.bsky.feed.like:.subject.uri") %} 107 136 108 137 <h3 class="route"><code>GET /links/distinct-dids</code></h3> 109 138
+8
constellation/templates/try-it-macros.html.j2
··· 112 112 </form> 113 113 {% endmacro %} 114 114 115 + {% macro get_backlink_dids(subject, source) %} 116 + <form method="get" action="/xrpc/blue.microcosm.links.getBacklinkDids"> 117 + <pre class="code"><strong>GET</strong> /xrpc/blue.microcosm.links.getBacklinkDids 118 + ?subject= <input type="text" name="subject" value="{{ subject }}" placeholder="subject" /> 119 + &source= <input type="text" name="source" value="{{ source }}" placeholder="source" /> <button type="submit">get links</button></pre> 120 + </form> 121 + {% endmacro %} 122 + 115 123 {% macro links_count(target, collection, path) %} 116 124 <form method="get" action="/links/count"> 117 125 <pre class="code"><strong>GET</strong> /links/count
+56
lexicons/blue.microcosm/links/getBacklinkDids.json
··· 1 + { 2 + "lexicon": 1, 3 + "id": "blue.microcosm.links.getBacklinkDids", 4 + "defs": { 5 + "main": { 6 + "type": "query", 7 + "description": "a list of distinct dids with a specific records linking to a target at a specified path", 8 + "parameters": { 9 + "type": "params", 10 + "required": ["subject", "source"], 11 + "properties": { 12 + "subject": { 13 + "type": "string", 14 + "format": "uri", 15 + "description": "the target being linked to (at-uri, did, or uri)" 16 + }, 17 + "source": { 18 + "type": "string", 19 + "description": "collection and path specification (e.g., 'app.bsky.feed.like:subject.uri')" 20 + }, 21 + "limit": { 22 + "type": "integer", 23 + "minimum": 1, 24 + "maximum": 100, 25 + "default": 16, 26 + "description": "number of results to return" 27 + } 28 + } 29 + }, 30 + "output": { 31 + "encoding": "application/json", 32 + "schema": { 33 + "type": "object", 34 + "required": ["total", "linking_dids"], 35 + "properties": { 36 + "total": { 37 + "type": "integer", 38 + "description": "total number of matching links" 39 + }, 40 + "linking_dids": { 41 + "type": "array", 42 + "items": { 43 + "type": "string", 44 + "format": "did" 45 + } 46 + }, 47 + "cursor": { 48 + "type": "string", 49 + "description": "pagination cursor" 50 + } 51 + } 52 + } 53 + } 54 + } 55 + } 56 + }
+3 -13
lexicons/blue.microcosm/links/getBacklinks.json
··· 7 7 "description": "a list of records linking to any record, identity, or uri", 8 8 "parameters": { 9 9 "type": "params", 10 - "required": [ 11 - "subject", 12 - "source" 13 - ], 10 + "required": ["subject", "source"], 14 11 "properties": { 15 12 "subject": { 16 13 "type": "string", ··· 42 39 "encoding": "application/json", 43 40 "schema": { 44 41 "type": "object", 45 - "required": [ 46 - "total", 47 - "records" 48 - ], 42 + "required": ["total", "records"], 49 43 "properties": { 50 44 "total": { 51 45 "type": "integer", ··· 68 62 }, 69 63 "linkRecord": { 70 64 "type": "object", 71 - "required": [ 72 - "did", 73 - "collection", 74 - "rkey" 75 - ], 65 + "required": ["did", "collection", "rkey"], 76 66 "properties": { 77 67 "did": { 78 68 "type": "string",

History

6 rounds 17 comments
sign up or login to add to the discussion
4 commits
expand
Add getDistinct XRPC equivalent to REST /links/distinct-dids
Rename method name to fetch backlink DIDs
Address fig's comments
Fix botched merge conflict (2d97b62) resolution
expand 0 comments
pull request successfully merged
3 commits
expand
Add getDistinct XRPC equivalent to REST /links/distinct-dids
Rename method name to fetch backlink DIDs
Address fig's comments
expand 3 comments

managed to get it conflicting again after merging #7, sorry! i'm good to go with this otherwise.

i'll let you resolve and resubmit, so it says "merged" on your pr in tangled if you want. realized it wasn't doing that when i was merging manually otherwise.

(but i'm happy to resolve conflicts and merge that way if it's the same to you or if you don't have time to get the changes!)

I resolved the conflict and now things should be ready to merge again

2 commits
expand
Add getDistinct XRPC equivalent to REST /links/distinct-dids
Rename method name to fetch backlink DIDs
expand 3 comments

sorry for getting the PRs mixed up! i'll do a final pass on this tomorrow, it's looking great. two clippy lints (from make check in the repo root):

  1. #[deprecated]: please put a comment instead of using the rust-level marker (which is very cool!). the deprecations in the server code are mostly notices to client using, not internal code (like the web server) :)

  2. unnecessary & on line 728 for the &collection param (just remove the &).

(i gotta get tangled CI going here!)

^^ line 728 of server/mod.rs

3 commits
expand
Add getDistinct XRPC equivalent to REST /links/distinct-dids
Rename method name to fetch backlink DIDs
Replace tabs with spaces in try-it-macros.html.j2
expand 7 comments

git-merge-tree doesn't show any conflicts either. A bit lost what to do tbh

lemme have a closer look at why!

this is an issue on our end, and has been fixed in this PR: https://tangled.org/tangled.org/core/pulls/1033 . I will try to get this merged and deployed shortly, and that should automagically unblock this PR!

Sounds great!

the bug here should be fixed now! the pending conflict is because both files have been modified, sorry for the long wait!

fixed the remaining conflicts and the PR seems good to go now. Thanks for the follow up :)

2 commits
expand
Add getDistinct XRPC equivalent to REST /links/distinct-dids
Rename method name to fetch backlink DIDs
expand 1 comment

The merge conflict label seems to stem from a whitespace related error even though the patch applies cleanly locally. Asking in the Tangled Discord it seems that should've been just a warning instead?

1 commit
expand
Add getDistinct XRPC equivalent to REST /links/distinct-dids
expand 3 comments

i'm getting a compilation error on this branch from the templates -- i think a couple lines got lost from #8

i think the name of this XRPC needs another iteration. it's close to getBacklinks so the name should probably be closer.

pdsls just calls the distinct dids count "repos", and i'm open to taking some inspiration from that. alksjdflkas i don't have a good suggestion right now.

Sorry for the compilation error. I started basing my own work on the new XRPC count endpoint as a reference. Rebased on main again and verified that the compilation errors are gone now.

Regarding the endpoint name, "Repos" isn't too bad either, but maybe getBacklinkDids is the option that is most self-evident. So I might go with that.

Feel free to merge if you agree. If you don't think that's a good idea, just let me know. :)

i think that's a good name. going to sleep on it before merging.

thanks!