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

Convert existing REST /links/count endpoint to XRPC equivalent #8

closed opened by seoul.systems targeting main from seoul.systems/microcosm-rs: xrpc_backlinks_count

Add getCounts XRPC equivalent to REST /links/count

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

In addition we now ignore rocks.test

Labels

None yet.

Participants 2
AT URI
at://did:plc:53wellrw53o7sw4zlpfenvuh/sh.tangled.repo.pull/3mcybnzocig22
+152 -3
Diff #7
+1
.gitignore
··· 1 /target 2 local/
··· 1 /target 2 local/ 3 + rocks.test
+4
.prettierrc
···
··· 1 + { 2 + "tabWidth": 2, 3 + "useTabs": false 4 + }
+48
constellation/src/server/mod.rs
··· 66 } 67 }), 68 ) 69 .route( 70 "/links/count", 71 get({ 72 let store = store.clone(); 73 move |accept, query| async { 74 spawn_blocking(|| count_links(accept, query, store)) 75 .await 76 .map_err(to500)? 77 } ··· 343 #[serde(skip_serializing)] 344 query: GetLinksCountQuery, 345 } 346 fn count_links( 347 accept: ExtractAccept, 348 query: Query<GetLinksCountQuery>, ··· 361 } 362 363 #[derive(Clone, Deserialize)] 364 struct GetDidsCountQuery { 365 target: String, 366 collection: String, ··· 668 #[serde(skip_serializing)] 669 query: GetAllLinksQuery, 670 } 671 fn count_all_links( 672 accept: ExtractAccept, 673 query: Query<GetAllLinksQuery>,
··· 66 } 67 }), 68 ) 69 + // deprecated 70 .route( 71 "/links/count", 72 get({ 73 let store = store.clone(); 74 move |accept, query| async { 75 spawn_blocking(|| count_links(accept, query, store)) 76 + .await 77 + .map_err(to500)? 78 + } 79 + }), 80 + ) 81 + .route( 82 + "/xrpc/blue.microcosm.links.getBacklinksCount", 83 + get({ 84 + let store = store.clone(); 85 + move |accept, query| async { 86 + spawn_blocking(|| get_backlink_counts(accept, query, store)) 87 .await 88 .map_err(to500)? 89 } ··· 355 #[serde(skip_serializing)] 356 query: GetLinksCountQuery, 357 } 358 + #[deprecated] 359 fn count_links( 360 accept: ExtractAccept, 361 query: Query<GetLinksCountQuery>, ··· 374 } 375 376 #[derive(Clone, Deserialize)] 377 + struct GetItemsCountQuery { 378 + subject: String, 379 + source: String, 380 + } 381 + #[derive(Template, Serialize)] 382 + #[template(path = "get-backlinks-count.html.j2")] 383 + struct GetItemsCountResponse { 384 + total: u64, 385 + #[serde(skip_serializing)] 386 + query: GetItemsCountQuery, 387 + } 388 + fn get_backlink_counts( 389 + accept: ExtractAccept, 390 + query: axum_extra::extract::Query<GetItemsCountQuery>, 391 + store: impl LinkReader, 392 + ) -> Result<impl IntoResponse, http::StatusCode> { 393 + let Some((collection, path)) = query.source.split_once(':') else { 394 + return Err(http::StatusCode::BAD_REQUEST); 395 + }; 396 + let path = format!(".{path}"); 397 + let total = store 398 + .get_count(&query.subject, collection, &path) 399 + .map_err(|_| http::StatusCode::INTERNAL_SERVER_ERROR)?; 400 + 401 + Ok(acceptable( 402 + accept, 403 + GetItemsCountResponse { 404 + total, 405 + query: (*query).clone(), 406 + }, 407 + )) 408 + } 409 + 410 + #[derive(Clone, Deserialize)] 411 struct GetDidsCountQuery { 412 target: String, 413 collection: String, ··· 715 #[serde(skip_serializing)] 716 query: GetAllLinksQuery, 717 } 718 + #[deprecated] 719 fn count_all_links( 720 accept: ExtractAccept, 721 query: Query<GetAllLinksQuery>,
+14 -1
constellation/templates/hello.html.j2
··· 121 {% call try_it::dids("at://did:plc:vc7f4oafdgxsihk4cry2xpze/app.bsky.feed.post/3lgwdn7vd722r", "app.bsky.feed.like", ".subject.uri") %} 122 123 124 - <h3 class="route"><code>GET /links/count</code></h3> 125 126 <p>The total number of links pointing at a given target.</p> 127 ··· 137 <p style="margin-bottom: 0"><strong>Try it:</strong></p> 138 {% call try_it::links_count("did:plc:vc7f4oafdgxsihk4cry2xpze", "app.bsky.graph.block", ".subject") %} 139 140 141 <h3 class="route"><code>GET /links/count/distinct-dids</code></h3> 142
··· 121 {% call try_it::dids("at://did:plc:vc7f4oafdgxsihk4cry2xpze/app.bsky.feed.post/3lgwdn7vd722r", "app.bsky.feed.like", ".subject.uri") %} 122 123 124 + <h3 class="route deprecated"><code>[deprecated] GET /links/count</code></h3> 125 126 <p>The total number of links pointing at a given target.</p> 127 ··· 137 <p style="margin-bottom: 0"><strong>Try it:</strong></p> 138 {% call try_it::links_count("did:plc:vc7f4oafdgxsihk4cry2xpze", "app.bsky.graph.block", ".subject") %} 139 140 + <h3 class="route"><code>GET /xrpc/blue.microcosm.links.getBacklinksCount</code></h3> 141 + 142 + <p>The total number of links pointing at a given target.</p> 143 + 144 + <h4>Query parameters:</h4> 145 + 146 + <ul> 147 + <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> 148 + <li><code>source</code>: required. Collection and path specification for the primary link. Example: <code>app.bsky.feed.like:subject.uri</code></li> 149 + </ul> 150 + 151 + <p style="margin-bottom: 0"><strong>Try it:</strong></p> 152 + {% call try_it::get_backlinks_count("did:plc:vc7f4oafdgxsihk4cry2xpze", "app.bsky.graph.block:subject") %} 153 154 <h3 class="route"><code>GET /links/count/distinct-dids</code></h3> 155
+7
constellation/templates/try-it-macros.html.j2
··· 104 </form> 105 {% endmacro %} 106 107 108 {% macro links_count(target, collection, path) %} 109 <form method="get" action="/links/count">
··· 104 </form> 105 {% endmacro %} 106 107 + {% macro get_backlinks_count(subject, source) %} 108 + <form method="get" action="/xrpc/blue.microcosm.links.getBacklinksCount"> 109 + <pre class="code"><strong>GET</strong> /xrpc/blue.microcosm.links.getBacklinksCount 110 + ?subject= <input type="text" name="subject" value="{{ subject }}" placeholder="subject" /> 111 + &source= <input type="text" name="source" value="{{ source }}" placeholder="source" /> <button type="submit">get links count</button></pre> 112 + </form> 113 + {% endmacro %} 114 115 {% macro links_count(target, collection, path) %} 116 <form method="get" action="/links/count">
+38
lexicons/blue.microcosm/links/getBacklinksCount.json
···
··· 1 + { 2 + "lexicon": 1, 3 + "id": "blue.microcosm.links.getBacklinksCount", 4 + "defs": { 5 + "main": { 6 + "type": "query", 7 + "description": "count records that link to another record", 8 + "parameters": { 9 + "type": "params", 10 + "required": ["subject", "source"], 11 + "properties": { 12 + "subject": { 13 + "type": "string", 14 + "format": "uri", 15 + "description": "the primary target being linked to (at-uri, did, or uri)" 16 + }, 17 + "source": { 18 + "type": "string", 19 + "description": "collection and path specification for the primary link" 20 + } 21 + } 22 + }, 23 + "output": { 24 + "encoding": "application/json", 25 + "schema": { 26 + "type": "object", 27 + "required": ["total"], 28 + "properties": { 29 + "total": { 30 + "type": "integer", 31 + "description": "total number of matching links" 32 + } 33 + } 34 + } 35 + } 36 + } 37 + } 38 + }
+1 -1
readme.md
··· 10 Tutorials, how-to guides, and client SDK libraries are all in the works for gentler on-ramps, but are not quite ready yet. But don't let that stop you! Hop in the [microcosm discord](https://discord.gg/tcDfe4PGVB), or post questions and tag [@bad-example.com](https://bsky.app/profile/bad-example.com) on Bluesky if you get stuck anywhere. 11 12 > [!tip] 13 - > This repository's primary home is moving to tangled: [@microcosm.blue/microcosm-rs](https://tangled.org/microcosm.blue/microcosm-rs). It will continue to be mirrored on [github](https://github.com/at-microcosm/microcosm-rs) for the forseeable future, and it's fine to open issues or pulls in either place! 14 15 16 馃寣 [Constellation](./constellation/)
··· 10 Tutorials, how-to guides, and client SDK libraries are all in the works for gentler on-ramps, but are not quite ready yet. But don't let that stop you! Hop in the [microcosm discord](https://discord.gg/tcDfe4PGVB), or post questions and tag [@bad-example.com](https://bsky.app/profile/bad-example.com) on Bluesky if you get stuck anywhere. 11 12 > [!tip] 13 + > This repository's primary home is moving to tangled: [@microcosm.blue/microcosm-rs](https://tangled.sh/@microcosm.blue/microcosm-rs). It will continue to be mirrored on [github](https://github.com/at-microcosm/microcosm-rs) for the forseeable future, and it's fine to open issues or pulls in either place! 14 15 16 馃寣 [Constellation](./constellation/)
+1 -1
slingshot/api-description.md
··· 90 - [馃巼 Spacedust](https://spacedust.microcosm.blue/), a firehose of all social interactions 91 92 > [!success] 93 - > All microcosm projects are [open source](https://tangled.org/bad-example.com/microcosm-links). **You can help sustain Slingshot** and all of microcosm by becoming a [Github sponsor](https://github.com/sponsors/uniphil/) or a [Ko-fi supporter](https://ko-fi.com/bad_example)!
··· 90 - [馃巼 Spacedust](https://spacedust.microcosm.blue/), a firehose of all social interactions 91 92 > [!success] 93 + > All microcosm projects are [open source](https://tangled.sh/@bad-example.com/microcosm-links). **You can help sustain Slingshot** and all of microcosm by becoming a [Github sponsor](https://github.com/sponsors/uniphil/) or a [Ko-fi supporter](https://ko-fi.com/bad_example)!

History

8 rounds 13 comments
sign up or login to add to the discussion
8 commits
expand
Add getCounts XRPC equivalent to REST /links/count
Modify backlinks counting XRPC endpoint name
Mark /links/count REST endpoint as deprecated
Remove .uri suffix
Reformat existing lexicons
Remove wrongly commited getManyToMany lexicon
Fix Git whitespace error in "hello" template
Format .prettierrc and fix Git whitespace error
expand 3 comments

I'm giving up on the whitespace issue...

max@max-mbpro ~/dev/microcosm-rs (xrpc_backlinks_count) $ git diff --check upstream/main
max@max-mbpro ~/dev/microcosm-rs (xrpc_backlinks_count) $

git-diff --checked doesn't indicate any errors either.

As far as I can tell this seems to be a confirmed Tangled issue

happy to do merging locally as needed!

merged. thanks!

closed without merging
7 commits
expand
Add getCounts XRPC equivalent to REST /links/count
Modify backlinks counting XRPC endpoint name
Mark /links/count REST endpoint as deprecated
Remove .uri suffix
Reformat existing lexicons
Remove wrongly commited getManyToMany lexicon
Fix Git whitespace error in "hello" template
expand 0 comments
6 commits
expand
Add getCounts XRPC equivalent to REST /links/count
Modify backlinks counting XRPC endpoint name
Mark /links/count REST endpoint as deprecated
Remove .uri suffix
Reformat existing lexicons
Remove wrongly commited getManyToMany lexicon
expand 0 comments
3 commits
expand
Add getCounts XRPC equivalent to REST /links/count
Modify backlinks counting XRPC endpoint name
Mark /links/count REST endpoint as deprecated
expand 4 comments

Tangled somehow complains about merge conflicts here, but I couldn't find any after rebasing on upstream/main again?!

very weird!

one tiny thing left: the source for blocks is app.bsky.graph.block:subject (no .uri suffix on the path) -- it's in the hello.html template.

i think some many-to-many order stuff ended up on this branch but i'm too tired for git rn.

if you don't get to it first i'm happy to fix the source and git stuff and merge when i can get to it :)

Sorry about the the m2m stuff that I didn't catch before opening the PR. I did clean this up again, and nothing belonging there should be remaining here; Addresses your above comment regarding the .uri suffix as well.

3 commits
expand
Add getCounts XRPC equivalent to REST /links/count
Modify backlinks counting XRPC endpoint name
Mark /links/count REST endpoint as deprecated
expand 0 comments
1 commit
expand
Add getCounts XRPC equivalent to REST /links/count
expand 6 comments

Had to do some rebasing and cleanup, but good to go now I think.

sweet!

(btw the local/ gitignore is there for doing local/rocks.test, but i'm fine adding it to the top-level gitignore too!)

i think links.getCounts is a little too broad -- what do you think of links.getBacklinksCount?

we also throw a deprecated warning under /links/count on the main page template, matching the one for /links.

it's weird but the whitespace in the forms in try-it-macros is significant (one of my moments of knowingly doing things the wrong way because i was bored, sorry!)

the new form for this endpoint needs little tweaking to match the others.

Ah got it. The rocks tests ended up cluttering the top-level directory so I just added them to .gitignore. I often keep a .local file in .gitignores to ignore whatever's supposed to stay in one's local copy and somehow assumed this had the same intent.

Changed the function and endpoint/method name as requested to get_backlinks_count/getBacklinksCount

The endpoint in the try-it-macro should now match the other existing ones down to how they're formatted with whitespace

3 commits
expand
Make metrics collection opt-in
Increase constellation response limits
Add getCounts XRPC equivalent to REST /links/count
expand 0 comments
7 commits
expand
Make metrics collection opt-in
Increase constellation response limits
wip: m2m
Add tests for new get_many_to_many query handler
Fix get_m2m_empty test
Replace tuple with RecordsBySubject struct
Add getCounts XRPC equivalent to REST /links/count
expand 0 comments