From 0cde298b46b62d344e8fe142849164249e510d65 Mon Sep 17 00:00:00 2001 From: Ricardo Date: Fri, 20 Mar 2026 14:00:44 +0100 Subject: [PATCH] fix: detect own posts in Mastodon API status serialization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Own posts in ap_timeline have author.url set to the publication URL (site root like "https://rmendes.net/") with no /@handle or /users/handle pattern. extractUsername("/") returns "" which falls back to "unknown". Fix: set module-level local identity (publicationUrl + handle) at plugin init via setLocalIdentity(). serializeStatus() compares item.author.url against the publication URL and passes isLocal:true + handle to serializeAccount() when they match. This is zero-cost for callers — no signature changes needed at the 20+ serializeStatus() call sites. --- index.js | 4 ++++ lib/mastodon/entities/status.js | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 98aa545..b53cdb7 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ import express from "express"; import { setupFederation, buildPersonActor } from "./lib/federation-setup.js"; import { createMastodonRouter } from "./lib/mastodon/router.js"; +import { setLocalIdentity } from "./lib/mastodon/entities/status.js"; import { initRedisCache } from "./lib/redis-cache.js"; import { lookupWithSecurity } from "./lib/lookup-helpers.js"; import { @@ -1492,6 +1493,9 @@ export default class ActivityPubEndpoint { routesPublic: this.contentNegotiationRoutes, }); + // Set local identity for own-post detection in status serialization + setLocalIdentity(this._publicationUrl, this.options.actor?.handle || "user"); + // Mastodon Client API — virtual endpoint at root // Mastodon-compatible clients (Phanpy, Elk, etc.) expect /api/v1/*, // /api/v2/*, /oauth/* at the domain root, not under /activitypub. diff --git a/lib/mastodon/entities/status.js b/lib/mastodon/entities/status.js index b561675..73fbb0f 100644 --- a/lib/mastodon/entities/status.js +++ b/lib/mastodon/entities/status.js @@ -16,6 +16,21 @@ import { serializeAccount } from "./account.js"; import { sanitizeHtml } from "./sanitize.js"; +// Module-level defaults set once at startup via setLocalIdentity() +let _localPublicationUrl = ""; +let _localHandle = ""; + +/** + * Set the local identity for own-post detection. + * Called once during plugin init. + * @param {string} publicationUrl - e.g. "https://rmendes.net/" + * @param {string} handle - e.g. "rick" + */ +export function setLocalIdentity(publicationUrl, handle) { + _localPublicationUrl = publicationUrl; + _localHandle = handle; +} + /** * Serialize an ap_timeline document as a Mastodon Status entity. * @@ -207,7 +222,11 @@ export function serializeStatus(item, { baseUrl, favouritedIds, rebloggedIds, bo reblog: null, application: null, account: item.author - ? serializeAccount(item.author, { baseUrl }) + ? serializeAccount(item.author, { + baseUrl, + isLocal: !!(_localPublicationUrl && item.author.url === _localPublicationUrl), + handle: _localHandle, + }) : null, media_attachments: mediaAttachments, mentions,