fix(mastodon): inject local profile header/avatar in timeline enrichment

resolveRemoteAccount for the local actor URL loops back via NAT and
always fails/times-out, so ap_profile header never entered the cache.
Timeline statuses authored by the local account always had header: "".

Pass ap_profile to enrichAccountStats; short-circuit for the local
account URL to inject profile.image/icon directly instead of attempting
remote resolution.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
svemagie
2026-04-29 11:05:51 +02:00
parent f03f9050d9
commit aa867b7236
2 changed files with 28 additions and 7 deletions
+22 -4
View File
@@ -17,16 +17,18 @@ import { resolveRemoteAccount } from "./resolve-account.js";
* @param {Array} statuses - Serialized Mastodon Status objects (mutated in place)
* @param {object} pluginOptions - Plugin options with federation context
* @param {string} baseUrl - Server base URL
* @param {object|null} localProfile - ap_profile document for the local account (optional)
*/
export async function enrichAccountStats(statuses, pluginOptions, baseUrl) {
export async function enrichAccountStats(statuses, pluginOptions, baseUrl, localProfile = null) {
if (!statuses?.length || !pluginOptions?.federation) return;
const publicationUrl = pluginOptions.publicationUrl || null;
const uncachedUrls = [];
for (const status of statuses) {
applyCachedOrCollect(status.account, uncachedUrls);
applyCachedOrCollect(status.account, uncachedUrls, localProfile, publicationUrl);
if (status.reblog?.account) {
applyCachedOrCollect(status.reblog.account, uncachedUrls);
applyCachedOrCollect(status.reblog.account, uncachedUrls, localProfile, publicationUrl);
}
}
@@ -41,10 +43,26 @@ export async function enrichAccountStats(statuses, pluginOptions, baseUrl) {
* Apply cached stats to an account, or collect its URL for background resolution.
* @param {object} account - Account object to enrich
* @param {string[]} uncachedUrls - Array to collect uncached URLs into
* @param {object|null} localProfile - ap_profile document for short-circuiting local account
* @param {string|null} publicationUrl - Local publication URL to match against
*/
function applyCachedOrCollect(account, uncachedUrls) {
function applyCachedOrCollect(account, uncachedUrls, localProfile = null, publicationUrl = null) {
if (!account?.url) return;
// Short-circuit for the local account: inject avatar/header from ap_profile directly.
// resolveRemoteAccount for the local URL would loop back via NAT and always fail/timeout.
if (localProfile && publicationUrl && account.url === publicationUrl) {
if (!account.header && localProfile.image) {
account.header = localProfile.image;
account.header_static = localProfile.image;
}
if (!account.avatar && localProfile.icon) {
account.avatar = localProfile.icon;
account.avatar_static = localProfile.icon;
}
return;
}
// Always check cache first — applies avatar + createdAt even for already-enriched accounts.
// avatarUrl is stored in the cache by resolveRemoteAccount so it survives across requests
// even when the timeline item's author.photo is empty (e.g. actor was on a Secure Mode
+6 -3
View File
@@ -84,7 +84,8 @@ router.get("/api/v1/timelines/home", tokenRequired, scopeRequired("read", "read:
// Enrich embedded account objects with real follower/following/post counts.
// Phanpy never calls /accounts/:id — it trusts embedded account data.
const pluginOptions = req.app.locals.mastodonPluginOptions || {};
await enrichAccountStats(statuses, pluginOptions, baseUrl);
const localProfile = await collections.ap_profile.findOne({});
await enrichAccountStats(statuses, pluginOptions, baseUrl, localProfile);
// Apply keyword filters
let filteredStatuses = statuses;
@@ -193,7 +194,8 @@ router.get("/api/v1/timelines/public", async (req, res, next) => {
);
const pluginOpts = req.app.locals.mastodonPluginOptions || {};
await enrichAccountStats(statuses, pluginOpts, baseUrl);
const localProfilePub = await collections.ap_profile.findOne({});
await enrichAccountStats(statuses, pluginOpts, baseUrl, localProfilePub);
// Apply keyword filters
let filteredStatuses = statuses;
@@ -268,7 +270,8 @@ router.get("/api/v1/timelines/tag/:hashtag", async (req, res, next) => {
);
const pluginOpts = req.app.locals.mastodonPluginOptions || {};
await enrichAccountStats(statuses, pluginOpts, baseUrl);
const localProfileTag = await collections.ap_profile.findOne({});
await enrichAccountStats(statuses, pluginOpts, baseUrl, localProfileTag);
// Apply keyword filters
let filteredStatuses = statuses;