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
+212 -17
Diff #5
+79
constellation/src/server/mod.rs
··· 136 }), 137 ) 138 .route( 139 "/links", 140 get({ 141 let store = store.clone(); ··· 147 }), 148 ) 149 .route( 150 "/links/distinct-dids", 151 get({ 152 let store = store.clone(); ··· 169 } 170 }), 171 ) 172 .route( 173 "/links/all", 174 get({ ··· 776 accept, 777 GetManyToManyItemsResponse { 778 items, 779 cursor, 780 query: (*query).clone(), 781 },
··· 136 }), 137 ) 138 .route( 139 + // deprecated 140 "/links", 141 get({ 142 let store = store.clone(); ··· 148 }), 149 ) 150 .route( 151 + "/xrpc/blue.microcosm.links.getBacklinkDids", 152 + get({ 153 + let store = store.clone(); 154 + move |accept, query| async { 155 + spawn_blocking(|| get_backlink_dids(accept, query, store)) 156 + .await 157 + .map_err(to500)? 158 + } 159 + }), 160 + ) 161 + // deprecated 162 + .route( 163 "/links/distinct-dids", 164 get({ 165 let store = store.clone(); ··· 182 } 183 }), 184 ) 185 + // deprecated 186 .route( 187 "/links/all", 188 get({ ··· 790 accept, 791 GetManyToManyItemsResponse { 792 items, 793 + cursor, 794 + query: (*query).clone(), 795 + }, 796 + )) 797 + } 798 + 799 + #[derive(Clone, Deserialize)] 800 + struct GetBacklinkDidsQuery { 801 + subject: String, 802 + source: String, 803 + cursor: Option<OpaqueApiCursor>, 804 + limit: Option<u64>, 805 + // TODO: allow reverse (er, forward) order as well 806 + } 807 + #[derive(Template, Serialize)] 808 + #[template(path = "get-backlink-dids.html.j2")] 809 + struct GetBacklinkDidsResponse { 810 + // what does staleness mean? 811 + // - new links have appeared. would be nice to offer a `since` cursor to fetch these. and/or, 812 + // - links have been deleted. hmm. 813 + total: u64, 814 + linking_dids: Vec<Did>, 815 + cursor: Option<OpaqueApiCursor>, 816 + #[serde(skip_serializing)] 817 + query: GetBacklinkDidsQuery, 818 + } 819 + fn get_backlink_dids( 820 + accept: ExtractAccept, 821 + query: Query<GetBacklinkDidsQuery>, 822 + store: impl LinkReader, 823 + ) -> Result<impl IntoResponse, http::StatusCode> { 824 + let until = query 825 + .cursor 826 + .clone() 827 + .map(|oc| ApiCursor::try_from(oc).map_err(|_| http::StatusCode::BAD_REQUEST)) 828 + .transpose()? 829 + .map(|c| c.next); 830 + 831 + let limit = query.limit.unwrap_or(DEFAULT_CURSOR_LIMIT); 832 + if limit > DEFAULT_CURSOR_LIMIT_MAX { 833 + return Err(http::StatusCode::BAD_REQUEST); 834 + } 835 + 836 + let Some((collection, path)) = query.source.split_once(':') else { 837 + return Err(http::StatusCode::BAD_REQUEST); 838 + }; 839 + let path = format!(".{path}"); 840 + 841 + let paged = store 842 + .get_distinct_dids(&query.subject, collection, &path, limit, until) 843 + .map_err(|_| http::StatusCode::INTERNAL_SERVER_ERROR)?; 844 + 845 + let cursor = paged.next.map(|next| { 846 + ApiCursor { 847 + version: paged.version, 848 + next, 849 + } 850 + .into() 851 + }); 852 + 853 + Ok(acceptable( 854 + accept, 855 + GetBacklinkDidsResponse { 856 + total: paged.total, 857 + linking_dids: paged.items, 858 cursor, 859 query: (*query).clone(), 860 },
+2 -2
constellation/src/storage/mem_store.rs
··· 507 ) -> Result<HashMap<String, HashMap<String, CountsByCount>>> { 508 let data = self.0.lock().unwrap(); 509 let mut out: HashMap<String, HashMap<String, CountsByCount>> = HashMap::new(); 510 - if let Some(asdf) = data.targets.get(&Target::new(target)) { 511 - for (Source { collection, path }, linkers) in asdf { 512 let records = linkers.iter().flatten().count() as u64; 513 let distinct_dids = linkers 514 .iter()
··· 507 ) -> Result<HashMap<String, HashMap<String, CountsByCount>>> { 508 let data = self.0.lock().unwrap(); 509 let mut out: HashMap<String, HashMap<String, CountsByCount>> = HashMap::new(); 510 + if let Some(source_linker_pairs) = data.targets.get(&Target::new(target)) { 511 + for (Source { collection, path }, linkers) in source_linker_pairs { 512 let records = linkers.iter().flatten().count() as u64; 513 let distinct_dids = linkers 514 .iter()
+15 -2
constellation/templates/hello.html.j2
··· 82 25, 83 ) %} 84 85 - 86 <h3 class="route"><code>GET /xrpc/blue.microcosm.links.getManyToMany</code></h3> 87 88 <p>A list of many-to-many join records linking to a target and a secondary target.</p> ··· 101 <p style="margin-bottom: 0"><strong>Try it:</strong></p> 102 {% call try_it::get_many_to_many("at://did:plc:a4pqq234yw7fqbddawjo7y35/app.bsky.feed.post/3m237ilwc372e", "app.bsky.feed.like:subject.uri", "reply.parent.uri", [""], [""], 16) %} 103 104 105 <h3 class="route"><code>GET /links</code></h3> 106 ··· 122 123 <p style="margin-bottom: 0"><strong>Try it:</strong></p> 124 {% call try_it::links("at://did:plc:a4pqq234yw7fqbddawjo7y35/app.bsky.feed.post/3m237ilwc372e", "app.bsky.feed.like", ".subject.uri", [""], 16) %} 125 - 126 127 <h3 class="route"><code>GET /links/distinct-dids</code></h3> 128
··· 82 25, 83 ) %} 84 85 <h3 class="route"><code>GET /xrpc/blue.microcosm.links.getManyToMany</code></h3> 86 87 <p>A list of many-to-many join records linking to a target and a secondary target.</p> ··· 100 <p style="margin-bottom: 0"><strong>Try it:</strong></p> 101 {% call try_it::get_many_to_many("at://did:plc:a4pqq234yw7fqbddawjo7y35/app.bsky.feed.post/3m237ilwc372e", "app.bsky.feed.like:subject.uri", "reply.parent.uri", [""], [""], 16) %} 102 103 + <h3 class="route"><code>GET /xrpc/blue.microcosm.links.getDistinct</code></h3> 104 + 105 + <p>A list of distinct DIDs (identities) with links to a target.</p> 106 + 107 + <h4>Query parameters:</h4> 108 + 109 + <ul> 110 + <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> 111 + <li><code>source</code>: required. Collection and path specification for the primary link. Example: <code>app.bsky.feed.like:subject.uri</code></li> 112 + <li><code>limit</code>: optional. Number of results to return. Default: <code>16</code>. Maximum: <code>100</code></li> 113 + <li><code>cursor</code>: optional, see Definitions.</li> 114 + </ul> 115 + 116 + <p style="margin-bottom: 0"><strong>Try it:</strong></p> 117 + {% call try_it::get_backlink_dids("at://did:plc:vc7f4oafdgxsihk4cry2xpze/app.bsky.feed.post/3lgwdn7vd722r", "app.bsky.feed.like:subject.uri") %} 118 119 <h3 class="route"><code>GET /links</code></h3> 120 ··· 136 137 <p style="margin-bottom: 0"><strong>Try it:</strong></p> 138 {% call try_it::links("at://did:plc:a4pqq234yw7fqbddawjo7y35/app.bsky.feed.post/3m237ilwc372e", "app.bsky.feed.like", ".subject.uri", [""], 16) %} 139 140 <h3 class="route"><code>GET /links/distinct-dids</code></h3> 141
+8
constellation/templates/try-it-macros.html.j2
··· 142 </form> 143 {% endmacro %} 144 145 {% macro links_count(target, collection, path) %} 146 <form method="get" action="/links/count"> 147 <pre class="code"><strong>GET</strong> /links/count
··· 142 </form> 143 {% endmacro %} 144 145 + {% macro get_backlink_dids(subject, source) %} 146 + <form method="get" action="/xrpc/blue.microcosm.links.getBacklinkDids"> 147 + <pre class="code"><strong>GET</strong> /xrpc/blue.microcosm.links.getBacklinkDids 148 + ?subject= <input type="text" name="subject" value="{{ subject }}" placeholder="subject" /> 149 + &source= <input type="text" name="source" value="{{ source }}" placeholder="source" /> <button type="submit">get links</button></pre> 150 + </form> 151 + {% endmacro %} 152 + 153 {% macro links_count(target, collection, path) %} 154 <form method="get" action="/links/count"> 155 <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 "description": "a list of records linking to any record, identity, or uri", 8 "parameters": { 9 "type": "params", 10 - "required": [ 11 - "subject", 12 - "source" 13 - ], 14 "properties": { 15 "subject": { 16 "type": "string", ··· 42 "encoding": "application/json", 43 "schema": { 44 "type": "object", 45 - "required": [ 46 - "total", 47 - "records" 48 - ], 49 "properties": { 50 "total": { 51 "type": "integer", ··· 68 }, 69 "linkRecord": { 70 "type": "object", 71 - "required": [ 72 - "did", 73 - "collection", 74 - "rkey" 75 - ], 76 "properties": { 77 "did": { 78 "type": "string",
··· 7 "description": "a list of records linking to any record, identity, or uri", 8 "parameters": { 9 "type": "params", 10 + "required": ["subject", "source"], 11 "properties": { 12 "subject": { 13 "type": "string", ··· 39 "encoding": "application/json", 40 "schema": { 41 "type": "object", 42 + "required": ["total", "records"], 43 "properties": { 44 "total": { 45 "type": "integer", ··· 62 }, 63 "linkRecord": { 64 "type": "object", 65 + "required": ["did", "collection", "rkey"], 66 "properties": { 67 "did": { 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!