mirror of
https://github.com/svemagie/indiekit-endpoint-activitypub.git
synced 2026-04-02 15:44:58 +02:00
feat: cache remote account stats for embedded status accounts
Phanpy never calls /accounts/:id for timeline authors — it uses the embedded account object from the status response. These had 0 counts because the timeline author data doesn't include follower stats. Fix: in-memory LRU cache (500 entries, 1h TTL) stores account stats from remote resolutions. serializeAccount() reads from cache when the actor has 0 counts, enriching embedded accounts with real data. Cache is populated by resolveRemoteAccount() (lookup, search, and /accounts/:id calls). Once a profile has been viewed once, all subsequent status embeds for that author show real counts.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
import { accountId } from "../helpers/id-mapping.js";
|
||||
import { sanitizeHtml, stripHtml } from "./sanitize.js";
|
||||
import { getCachedAccountStats } from "../helpers/account-cache.js";
|
||||
|
||||
/**
|
||||
* Serialize an actor as a Mastodon Account entity.
|
||||
@@ -111,6 +112,20 @@ export function serializeAccount(actor, { baseUrl, isLocal = false, handle = ""
|
||||
statuses_count: actor.statusesCount || 0,
|
||||
followers_count: actor.followersCount || 0,
|
||||
following_count: actor.followingCount || 0,
|
||||
// Enrich from cache if counts are 0 (embedded accounts in statuses lack counts)
|
||||
...((!actor.statusesCount && !actor.followersCount && !isLocal)
|
||||
? (() => {
|
||||
const cached = getCachedAccountStats(url);
|
||||
return cached
|
||||
? {
|
||||
statuses_count: cached.statusesCount || 0,
|
||||
followers_count: cached.followersCount || 0,
|
||||
following_count: cached.followingCount || 0,
|
||||
created_at: cached.createdAt || actor.createdAt || new Date().toISOString(),
|
||||
}
|
||||
: {};
|
||||
})()
|
||||
: {}),
|
||||
moved: actor.movedTo || null,
|
||||
suspended: false,
|
||||
limited: false,
|
||||
|
||||
51
lib/mastodon/helpers/account-cache.js
Normal file
51
lib/mastodon/helpers/account-cache.js
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* In-memory cache for remote account stats (followers, following, statuses).
|
||||
*
|
||||
* Populated by resolveRemoteAccount() when a profile is fetched.
|
||||
* Read by serializeAccount() to enrich embedded account objects in statuses.
|
||||
*
|
||||
* LRU-style with TTL — entries expire after 1 hour.
|
||||
*/
|
||||
|
||||
const CACHE_TTL_MS = 60 * 60 * 1000; // 1 hour
|
||||
const MAX_ENTRIES = 500;
|
||||
|
||||
// Map<actorUrl, { followersCount, followingCount, statusesCount, createdAt, cachedAt }>
|
||||
const cache = new Map();
|
||||
|
||||
/**
|
||||
* Store account stats in cache.
|
||||
* @param {string} actorUrl - The actor's URL (cache key)
|
||||
* @param {object} stats - { followersCount, followingCount, statusesCount, createdAt }
|
||||
*/
|
||||
export function cacheAccountStats(actorUrl, stats) {
|
||||
if (!actorUrl) return;
|
||||
|
||||
// Evict oldest if at capacity
|
||||
if (cache.size >= MAX_ENTRIES) {
|
||||
const oldest = cache.keys().next().value;
|
||||
cache.delete(oldest);
|
||||
}
|
||||
|
||||
cache.set(actorUrl, { ...stats, cachedAt: Date.now() });
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cached account stats.
|
||||
* @param {string} actorUrl - The actor's URL
|
||||
* @returns {object|null} Stats or null if not cached/expired
|
||||
*/
|
||||
export function getCachedAccountStats(actorUrl) {
|
||||
if (!actorUrl) return null;
|
||||
|
||||
const entry = cache.get(actorUrl);
|
||||
if (!entry) return null;
|
||||
|
||||
// Check TTL
|
||||
if (Date.now() - entry.cachedAt > CACHE_TTL_MS) {
|
||||
cache.delete(actorUrl);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
* Shared by accounts.js (lookup) and search.js (resolve=true).
|
||||
*/
|
||||
import { serializeAccount } from "../entities/account.js";
|
||||
import { cacheAccountStats } from "./account-cache.js";
|
||||
|
||||
/**
|
||||
* @param {string} acct - Account identifier (user@domain or URL)
|
||||
@@ -115,6 +116,14 @@ export async function resolveRemoteAccount(acct, pluginOptions, baseUrl) {
|
||||
account.following_count = followingCount;
|
||||
account.statuses_count = statusesCount;
|
||||
|
||||
// Cache stats so embedded account objects in statuses can use them
|
||||
cacheAccountStats(actorUrl, {
|
||||
followersCount,
|
||||
followingCount,
|
||||
statusesCount,
|
||||
createdAt: published || undefined,
|
||||
});
|
||||
|
||||
return account;
|
||||
} catch (error) {
|
||||
console.warn(`[Mastodon API] Remote account resolution failed for ${acct}:`, error.message);
|
||||
|
||||
Reference in New Issue
Block a user