longish, mastodon/activitypub dev stuff, OCRbot, asking for help maybe (or at le...

@lynnesbian@fedi.lynnesbian.space

Content warning: longish, mastodon/activitypub dev stuff, OCRbot, asking for help maybe (or at least commiseration)

while rewriting @OCRbot i rediscovered some !!fun!! aspects of writing software for the fediverse.

here's the main one: statuses (toots/posts/notes/wugs) have an in_reply_to_id when they're replies to something. however, if you're on instance A, and John Tooter from instance B replies to Jane Noter on instance C (or maybe even B as well? not sure), the in_reply_to_id will be null -- the same as if John hadn't replied to anyone. OCRbot used to solve this in a janky way that involved trying to scrape the images from instance C, which often failed because instance C's admins had (rightfully!) blocked bots from downloading their images.

to fix this, you need to:

  • parse the URL of John's post to determine the instance URL (easy) and remote status ID (easy if John's using Mastodon)
  • connect to John's instance's mastodon API
  • fetch John's post using (what you assume is) its ID
  • hope the other instance a) runs mastodon, b) doesn't block anonymous API clients, c) doesn't block your user agent, and d) actually works
  • get John's post as a status object according to his instance
  • use that to find the real in_reply_to_id
  • at this point, you might find that in_reply_to_id is still null. this means that John genuinely didn't reply to anyone. thanks for playing, better luck next time
  • use the in_reply_to_id to fetch instance B's representation of Jane's post (remember Jane?)
  • use that status to get a uri, which is somehow different to a url
  • use your original instance to run a search command and search for instance B's URL representation of Jane's post with resolve=True. this will cause instance A to (attempt to!) federate Jane's post
  • now instance A has Jane's post!!!! you can finally download the images

if anyone knows of a better way to do this, i'm all ears. due to Unnamed Past Incidents i do not consider manually using WebFinger (yes it's called that) and parsing ActivityPub JSON files to be "a better way".

#fedidev #activitypub