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 { accountId } from "../helpers/id-mapping.js";
|
||||||
import { sanitizeHtml, stripHtml } from "./sanitize.js";
|
import { sanitizeHtml, stripHtml } from "./sanitize.js";
|
||||||
|
import { getCachedAccountStats } from "../helpers/account-cache.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serialize an actor as a Mastodon Account entity.
|
* 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,
|
statuses_count: actor.statusesCount || 0,
|
||||||
followers_count: actor.followersCount || 0,
|
followers_count: actor.followersCount || 0,
|
||||||
following_count: actor.followingCount || 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,
|
moved: actor.movedTo || null,
|
||||||
suspended: false,
|
suspended: false,
|
||||||
limited: false,
|
limited: false,
|
||||||
|
|||||||
@@ -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).
|
* Shared by accounts.js (lookup) and search.js (resolve=true).
|
||||||
*/
|
*/
|
||||||
import { serializeAccount } from "../entities/account.js";
|
import { serializeAccount } from "../entities/account.js";
|
||||||
|
import { cacheAccountStats } from "./account-cache.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} acct - Account identifier (user@domain or URL)
|
* @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.following_count = followingCount;
|
||||||
account.statuses_count = statusesCount;
|
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;
|
return account;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(`[Mastodon API] Remote account resolution failed for ${acct}:`, error.message);
|
console.warn(`[Mastodon API] Remote account resolution failed for ${acct}:`, error.message);
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@rmdes/indiekit-endpoint-activitypub",
|
"name": "@rmdes/indiekit-endpoint-activitypub",
|
||||||
"version": "3.6.6",
|
"version": "3.6.7",
|
||||||
"description": "ActivityPub federation endpoint for Indiekit via Fedify. Adds full fediverse support: actor, inbox, outbox, followers, following, syndication, and Mastodon migration.",
|
"description": "ActivityPub federation endpoint for Indiekit via Fedify. Adds full fediverse support: actor, inbox, outbox, followers, following, syndication, and Mastodon migration.",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"indiekit",
|
"indiekit",
|
||||||
|
|||||||
Reference in New Issue
Block a user