Changeset 49edc72 in Klonkt for src/routes/activitypub.js


Ignore:
Timestamp:
06/25/2026 08:13:32 PM (3 months ago)
Author:
Robin Genis <roboburr@…>
Branches:
main
Children:
75ab393
Parents:
bf4596d
Message:

fix(ap): /ap/notes/:id redirects browsers to the human post page

Hitting an AP note URL in a browser returned raw activity+json. Now it
content-negotiates like the actor route: HTML clients get a 302 to the
readable post page (post + its fediverse reactions); AP clients still get
the Note JSON. Outbound replies redirect to the source they reply to.

Co-Authored-By: Claude <noreply@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/routes/activitypub.js

    rbf4596d r49edc72  
    9797    // Could be one of OUR outbound replies (ap_outbox), not a post.
    9898    const note = AP.getOutboxNote(baseUrl(req), req.params.id);
    99     if (note) return AP.sendAP(res, { '@context': 'https://www.w3.org/ns/activitystreams', ...note });
    100     return res.status(404).end();
     99    if (!note) return res.status(404).end();
     100    if (!AP.apWants(req)) {
     101      // A browser hit a reply's AP URL → send them to the source it replies to
     102      // (where the post + its reactions live), falling back to the site home.
     103      const src = (typeof note.inReplyTo === 'string' && /^https?:\/\//i.test(note.inReplyTo))
     104        ? note.inReplyTo : (baseUrl(req) + '/');
     105      return res.redirect(302, src);
     106    }
     107    return AP.sendAP(res, { '@context': 'https://www.w3.org/ns/activitystreams', ...note });
    101108  }
    102109  const site = db.prepare('SELECT * FROM sites WHERE id = ?').get(post.site_id);
    103110  if (!site) return res.status(404).end();
    104   AP.sendAP(res, { '@context': 'https://www.w3.org/ns/activitystreams', ...AP.buildNote(baseUrl(req), site, post) });
     111  const note = AP.buildNote(baseUrl(req), site, post);
     112  if (!AP.apWants(req)) {
     113    // A browser hit a post's AP note URL → send them to the human post page
     114    // (which shows the post + its "from the fediverse" reactions).
     115    return res.redirect(302, note.url || (baseUrl(req) + '/'));
     116  }
     117  AP.sendAP(res, { '@context': 'https://www.w3.org/ns/activitystreams', ...note });
    105118});
    106119
Note: See TracChangeset for help on using the changeset viewer.