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
+228 -15
Diff #1
+80
constellation/src/server/mod.rs
··· 100 100 }), 101 101 ) 102 102 .route( 103 + // deprecated 103 104 "/links", 104 105 get({ 105 106 let store = store.clone(); ··· 111 112 }), 112 113 ) 113 114 .route( 115 + "/xrpc/blue.microcosm.links.getBacklinkDids", 116 + get({ 117 + let store = store.clone(); 118 + move |accept, query| async { 119 + spawn_blocking(|| get_backlink_dids(accept, query, store)) 120 + .await 121 + .map_err(to500)? 122 + } 123 + }), 124 + ) 125 + // deprecated 126 + .route( 114 127 "/links/distinct-dids", 115 128 get({ 116 129 let store = store.clone(); ··· 133 146 } 134 147 }), 135 148 ) 149 + // deprecated 136 150 .route( 137 151 "/links/all", 138 152 get({ ··· 530 544 #[serde(skip_serializing)] 531 545 query: GetLinkItemsQuery, 532 546 } 547 + #[deprecated] 533 548 fn get_links( 534 549 accept: ExtractAccept, 535 550 query: axum_extra::extract::Query<GetLinkItemsQuery>, // supports multiple param occurrences ··· 590 605 GetLinkItemsResponse { 591 606 total: paged.total, 592 607 linking_records: paged.items, 608 + cursor, 609 + query: (*query).clone(), 610 + }, 611 + )) 612 + } 613 + 614 + #[derive(Clone, Deserialize)] 615 + struct GetBacklinkDidsQuery { 616 + subject: String, 617 + source: String, 618 + cursor: Option<OpaqueApiCursor>, 619 + limit: Option<u64>, 620 + // TODO: allow reverse (er, forward) order as well 621 + } 622 + #[derive(Template, Serialize)] 623 + #[template(path = "get-backlink-dids.html.j2")] 624 + struct GetBacklinkDidsResponse { 625 + // what does staleness mean? 626 + // - new links have appeared. would be nice to offer a `since` cursor to fetch these. and/or, 627 + // - links have been deleted. hmm. 628 + total: u64, 629 + linking_dids: Vec<Did>, 630 + cursor: Option<OpaqueApiCursor>, 631 + #[serde(skip_serializing)] 632 + query: GetBacklinkDidsQuery, 633 + } 634 + fn get_backlink_dids( 635 + accept: ExtractAccept, 636 + query: Query<GetBacklinkDidsQuery>, 637 + store: impl LinkReader, 638 + ) -> Result<impl IntoResponse, http::StatusCode> { 639 + let until = query 640 + .cursor 641 + .clone() 642 + .map(|oc| ApiCursor::try_from(oc).map_err(|_| http::StatusCode::BAD_REQUEST)) 643 + .transpose()? 644 + .map(|c| c.next); 645 + 646 + let limit = query.limit.unwrap_or(DEFAULT_CURSOR_LIMIT); 647 + if limit > DEFAULT_CURSOR_LIMIT_MAX { 648 + return Err(http::StatusCode::BAD_REQUEST); 649 + } 650 + 651 + let Some((collection, path)) = query.source.split_once(':') else { 652 + return Err(http::StatusCode::BAD_REQUEST); 653 + }; 654 + let path = format!(".{path}"); 655 + 656 + let paged = store 657 + .get_distinct_dids(&query.subject, &collection, &path, limit, until) 658 + .map_err(|_| http::StatusCode::INTERNAL_SERVER_ERROR)?; 659 + 660 + let cursor = paged.next.map(|next| { 661 + ApiCursor { 662 + version: paged.version, 663 + next, 664 + } 665 + .into() 666 + }); 667 + 668 + Ok(acceptable( 669 + accept, 670 + GetBacklinkDidsResponse { 671 + total: paged.total, 672 + linking_dids: paged.items, 593 673 cursor, 594 674 query: (*query).clone(), 595 675 },
+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
+9
constellation/templates/try-it-macros.html.j2
··· 104 104 </form> 105 105 {% endmacro %} 106 106 107 + {% macro get_backlink_dids(subject, source) %} 108 + <form method="get" action="/xrpc/blue.microcosm.links.getBacklinkDids"> 109 + <pre class="code"><strong>GET</strong> /xrpc/blue.microcosm.links.getBacklinkDids 110 + ?subject= <input type="text" name="subject" value="{{ subject }}" placeholder="subject" /> 111 + &source= <input type="text" name="source" value="{{ source }}" placeholder="source" /> 112 + <button type="submit">get links</button> 113 + </pre> 114 + </form> 115 + {% endmacro %} 107 116 108 117 {% macro links_count(target, collection, path) %} 109 118 <form method="get" action="/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!