notes by alifeee profile picture tagged ActivityPub (2) rss

return to notes / blog / website / weeknotes / linktree

here I may post some short, text-only notes, mostly about programming. source code.

tags: all (33) scripting (11) bash (4) geojson (3) jq (3) linux (3) obsidian (3) ActivityPub (2) github (2) html (2) ............ see all (+42)

finding the account information of a Mastodon account manually via curl requests # prev single next top

tags: ActivityPub • 458 'words', 137 secs @ 200wpm

Then Try This, a non-profit research group, recently changed their mastodon handle from

@thentrythis@mastodon.thentrythis.org

to

@thentrythis@thentrythis.org

to understand how this works (because I like understanding the ActivityPub protocol), I tried to find how my Mastodon client would find the new account.

When you open the original account profile, it opens on social.thentrythis.org, so there must be some path from thentrythis.org to there.

First, I tried accessing several URLs off the top of my head that I thought were used.

https://thentrythis.org/.well-known/webfinger
https://social.thentrythis.org/.well-known/webfinger

They all were blank.

Then, I was pointed in the right direction, and now I could manually make the same requests that my Mastodon client would do using Mastodon's documentation.

The process is:

Given a username, i.e., @thentrythis@thentrythis.org, find the format of the "webfinger request" (which allows you to request data about a user), which should be on /.well-known/host-meta. The key here is that the original site (thentrythis.org) redirects to the "social site" (social.thentrythis.org).

$ curl -i "https://thentrythis.org/.well-known/host-meta" | head -n4
HTTP/1.1 301 Moved Permanently
Date: Sun, 12 Jan 2025 18:56:53 GMT
Server: Apache/2.4.62 (Debian)
Location: https://social.thentrythis.org/.well-known/host-meta

$ curl "https://social.thentrythis.org/.well-known/host-meta"
<?xml version="1.0" encoding="UTF-8"?>
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  <Link rel="lrdd" template="https://social.thentrythis.org/.well-known/webfinger?resource={uri}"/>
</XRD>

Then, we can use the template returned to query a user by placing acct:<user>@<server> into the template, replacing {uri}, i.e.,

curl -s "https://social.thentrythis.org/.well-known/webfinger?resource=acct:thentrythis@thentrythis.org" | jq
{
  "subject": "acct:thentrythis@thentrythis.org",
  "aliases": [
    "https://social.thentrythis.org/@thentrythis",
    "https://social.thentrythis.org/users/thentrythis"
  ],
  "links": [
    {
      "rel": "http://webfinger.net/rel/profile-page",
      "type": "text/html",
      "href": "https://social.thentrythis.org/@thentrythis"
    },
    {
      "rel": "self",
      "type": "application/activity+json",
      "href": "https://social.thentrythis.org/users/thentrythis"
    },
    {
      "rel": "http://ostatus.org/schema/1.0/subscribe",
      "template": "https://social.thentrythis.org/authorize_interaction?uri={uri}"
    },
    {
      "rel": "http://webfinger.net/rel/avatar",
      "type": "image/png",
      "href": "https://social.thentrythis.org/system/accounts/avatars/113/755/294/674/928/838/original/640741180e302572.png"
    }
  ]
}

neat :]

It's always nice to know that I could use Mastodon by reaaaallllyyy slowly issuing my own curl requests (or, what this really means, build my own client).

back to top

ActivityPub posts and the ACCEPT header # prev single next top

tags: ActivityPub • 493 'words', 148 secs @ 200wpm

This note was made to investigate how ActivityPub worked for getting information about posts, as a small investigation into https://gitlab.com/edent/activity-bot/-/issues/2.

We consider two posts:

  1. a post by myself: https://mastodon.social/@alifeee/113410809105877544
  2. a post by @edent's bot "@bot@bot.viii.fi": a. the post on Mastodon: https://mastodon.social/@bot@bot.viii.fi/113402721781767728 b. the post on the original server: https://bot.viii.fi/posts/6723a0f2-d5c4-9276-916d-546aa5f831fd.json

Questions:

  1. how come 2.a displays as nice HTML and 2.b displays as JSON?
  2. I know activity pub requires JSON in some way, so how does a client get the JSON for post 1?

the answer to both questions (spoilers for below) is: it depends if the ACCEPT header is "Accept: text/html" (default in a browser) or "Accept: application/json" (I assume the default for things that interact with ActivityPub)

what you see in a browser

if you are logged in and you visit 2.a., you see a nice preview of the post

if you are not logged in, (open the status in a private browser window), you are redirected to https://mastodon.social/redirect/statuses/113402721781767728 which is a page saying "you are leaving Mastodon", which links to the post on the original instance.

Get post 2.a as HTML

get page (HTML)

curl -i 'https://mastodon.social/@bot@bot.viii.fi/113402721781767728' -H 'Accept: text/html'
# > HTTP 302 to "you are leaving Mastodon" page
curl -i https://mastodon.social/redirect/statuses/113402721781767728 -H 'Accept: text/html'
# > HTTP 200 - but this page is designed for a browser, so it has a link embedded to https://bot.viii.fi/posts/6723a0f2-d5c4-9276-916d-546aa5f831fd.json
curl -i "https://bot.viii.fi/posts/6723a0f2-d5c4-9276-916d-546aa5f831fd.json" -H 'Accept: text/html'
# JSON is returned, as this is simply a file access on the server (does not use PHP file)

Get post 2.a. as JSON

curl -i "https://mastodon.social/@bot@bot.viii.fi/113402721781767728" -H "Accept: application/json"
# sidenote: "/statuses/..." works too (also a 302 redirect)
# curl -i "https://mastodon.social/statuses/113402721781767728" -H "Accept: application/json"
# > HTTP 302 redirect to https://bot.viii.fi/posts/6723a0f2-d5c4-9276-916d-546aa5f831fd.json
curl -i "https://bot.viii.fi/posts/6723a0f2-d5c4-9276-916d-546aa5f831fd.json" -H "Accept: application/json"
# you get JSON as expected

What determines the URL of a post?

it's the ID in the JSON

# (or https://mastodon.social/@alifeee/113410809105877544 is a "nice" url that you get a redirect to if you ask for HTML (i.e., are using a browser))
$ curl -s "https://mastodon.social/users/alifeee/statuses/113410809105877544" -H "Accept: application/json" | jq | grep '"id"'
  "id": "https://mastodon.social/users/alifeee/statuses/113410809105877544",

This knowledge is building on a blog post I read a while ago: https://seb.jambor.dev/posts/understanding-activitypub/.

back to top