Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(users): count followers from the join so a deleted account stops inflating the profile

nirholas386c6c14f1fb
diff --git a/api/users/[username].js b/api/users/[username].js
index 9ac4029e7..34824a525 100644
--- a/api/users/[username].js
+++ b/api/users/[username].js
@@ -420,10 +420,22 @@ export default wrap(async (req, res) => {
// Follower/following counts live in their own query so a deploy that lands
// before the user_follows migration degrades to zeros rather than 500ing
// every public profile (migrate-then-deploy; see api/_lib/bounty-likes.js).
+ // Counted over the same reachable-profile set the follower/following lists
+ // render (api/users/[username]/follows.js): a live account that has claimed
+ // a username. A raw edge count would show a follower total the list cannot
+ // produce a single row for.
const [followCounts] = await sql`
select
- (select count(*)::int from user_follows where following_id = ${user.id}) as followers,
- (select count(*)::int from user_follows where follower_id = ${user.id}) as following
+ (select count(*)::int
+ from user_follows f
+ join users u on u.id = f.follower_id
+ where f.following_id = ${user.id}
+ and u.username is not null and u.deleted_at is null) as followers,
+ (select count(*)::int
+ from user_follows f
+ join users u on u.id = f.following_id
+ where f.follower_id = ${user.id}
+ and u.username is not null and u.deleted_at is null) as following
`.catch(() => [{ followers: 0, following: 0 }]);
const stats = {
diff --git a/api/users/[username]/collectibles.js b/api/users/[username]/collectibles.js
index bdff4c75b..1b76f60ef 100644
--- a/api/users/[username]/collectibles.js
+++ b/api/users/[username]/collectibles.js
@@ -29,6 +29,11 @@ import { readOwnedCosmetics, normalizeAccountId } from '../../_lib/cosmetics-own
const CACHE_TTL_SECONDS = 10 * 60; // 10m — NFT metadata is effectively static
const NFT_CAP = 60; // max NFTs surfaced across all wallets
const MAX_WALLETS_PER_CHAIN = 8; // cap billed upstream calls per request
+// Both NFT providers are third parties on the request path. Up to 16 of these
+// calls fan out in parallel per cache miss, so an unbounded one turns a slow
+// provider into a hung profile tab: bound each call and let the per-call catch
+// degrade it to an empty list.
+const PROVIDER_TIMEOUT_MS = 8000;
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS', credentials: false })) return;
@@ -226,6 +231,7 @@ async function fetchSolanaNfts(wallet) {
},
},
}),
+ signal: AbortSignal.timeout(PROVIDER_TIMEOUT_MS),
});
if (!resp.ok) throw new Error(`Helius DAS ${resp.status}`);
const data = await resp.json();
@@ -270,7 +276,7 @@ async function fetchEvmNfts(wallet, chainId) {
};
const host = hosts[Number(chainId)] || 'eth-mainnet';
const url = `https://${host}.g.alchemy.com/nft/v3/${apiKey}/getNFTsForOwner?owner=${encodeURIComponent(wallet)}&withMetadata=true&pageSize=${NFT_CAP}&excludeFilters[]=SPAM`;
- const resp = await fetch(url);
+ const resp = await fetch(url, { signal: AbortSignal.timeout(PROVIDER_TIMEOUT_MS) });
if (!resp.ok) throw new Error(`Alchemy ${resp.status}`);
const data = await resp.json();
const base = explorerForEvm(chainId, '').replace(/\/address\/$/, '');
diff --git a/api/users/[username]/creations.js b/api/users/[username]/creations.js
index c411f8200..a1215153e 100644
--- a/api/users/[username]/creations.js
+++ b/api/users/[username]/creations.js
@@ -30,6 +30,7 @@ import { listDioramasByUser } from '../../_lib/diorama-store.js';
import { listRestylesByUser } from '../../_lib/material-restyle-store.js';
const SITE = 'https://three.ws';
+const VALID_TYPES = new Set(['model', 'world', 'restyle']);
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS', credentials: false })) return;
@@ -54,7 +55,18 @@ export default wrap(async (req, res) => {
const url = new URL(req.url, 'http://x');
const typeFilter = url.searchParams.get('type'); // 'model' | 'world' | 'restyle' | null (all)
- const before = url.searchParams.get('before') || undefined;
+ if (typeFilter != null && !VALID_TYPES.has(typeFilter)) {
+ return error(res, 400, 'validation_error', 'type must be model|world|restyle');
+ }
+ // The cursor lands in a `created_at < ${before}` predicate, so a non-date
+ // string reaches Postgres as an invalid timestamp literal. The stores swallow
+ // that as an empty list, which reads to the client as "no more creations"
+ // rather than "your cursor is malformed". Validate it here instead.
+ const beforeRaw = url.searchParams.get('before');
+ if (beforeRaw != null && Number.isNaN(Date.parse(beforeRaw))) {
+ return error(res, 400, 'validation_error', 'before must be an ISO 8601 timestamp');
+ }
+ const before = beforeRaw ? new Date(beforeRaw).toISOString() : undefined;
const limit = Math.min(Math.max(Number(url.searchParams.get('limit')) || 24, 1), 48);
// Over-fetch each source so merging independently-paginated lists still
// yields a full page of the true recency-merged feed, not an undercount.
diff --git a/api/users/[username]/follow.js b/api/users/[username]/follow.js
index 6ca1b62fd..7c689391a 100644
--- a/api/users/[username]/follow.js
+++ b/api/users/[username]/follow.js
@@ -17,11 +17,23 @@ import { requireCsrf } from '../../_lib/csrf.js';
import { limits, clientIp } from '../../_lib/rate-limit.js';
import { publishUserEvent } from '../../_lib/feed.js';
+// Counted over exactly the set GET /api/users/:username/follows renders: a
+// live account that has claimed a username. Counting the raw edges instead
+// would put a "1 follower" badge above a list that comes back empty, since a
+// deleted or username-less account is not a reachable profile.
async function counts(userId) {
const [row] = await sql`
select
- (select count(*)::int from user_follows where following_id = ${userId}) as followers_count,
- (select count(*)::int from user_follows where follower_id = ${userId}) as following_count
+ (select count(*)::int
+ from user_follows f
+ join users u on u.id = f.follower_id
+ where f.following_id = ${userId}
+ and u.username is not null and u.deleted_at is null) as followers_count,
+ (select count(*)::int
+ from user_follows f
+ join users u on u.id = f.following_id
+ where f.follower_id = ${userId}
+ and u.username is not null and u.deleted_at is null) as following_count
`;
return {
followers_count: row?.followers_count ?? 0,
@@ -54,6 +66,13 @@ export default wrap(async (req, res) => {
return error(res, 400, 'validation_error', 'invalid username');
}
+ // Read budget first, before any query: the GET path is public and costs three
+ // statements per call, so it has to be capped like every other public read on
+ // a profile (follows.js, collectibles.js, [username].js all do this). The
+ // stricter write bucket still gates the mutations below.
+ const readRl = await limits.authedReadIp(clientIp(req));
+ if (!readRl.success) return rateLimited(res, readRl);
+
const [target] = await sql`
select id, username from users
where lower(username) = ${username} and deleted_at is null
diff --git a/api/users/[username]/follows.js b/api/users/[username]/follows.js
index c0d3afad1..da8f64b3a 100644
--- a/api/users/[username]/follows.js
+++ b/api/users/[username]/follows.js
@@ -40,6 +40,13 @@ export default wrap(async (req, res) => {
// followers: rows where following_id = target, list the followers (f.follower_id).
// following: rows where follower_id = target, list the followees (f.following_id).
+ //
+ // `u.username is not null` sits in the WHERE clause, not in a post-fetch JS
+ // filter: a profile is only reachable by username, and roughly 4% of accounts
+ // have never claimed one. Dropping those rows after LIMIT/OFFSET would let a
+ // whole page come back empty while has_more said false, stranding the real
+ // rows behind it. Filtering in SQL keeps limit/offset/has_more counting the
+ // same set the client actually renders.
const rows = type === 'followers'
? await sql`
select u.id, u.username, u.display_name, u.avatar_url, u.bio, f.created_at,
@@ -49,7 +56,7 @@ export default wrap(async (req, res) => {
) as is_following
from user_follows f
join users u on u.id = f.follower_id and u.deleted_at is null
- where f.following_id = ${target.id}
+ where f.following_id = ${target.id} and u.username is not null
order by f.created_at desc
limit ${limit} offset ${offset}
`
@@ -61,13 +68,12 @@ export default wrap(async (req, res) => {
) as is_following
from user_follows f
join users u on u.id = f.following_id and u.deleted_at is null
- where f.follower_id = ${target.id}
+ where f.follower_id = ${target.id} and u.username is not null
order by f.created_at desc
limit ${limit} offset ${offset}
`;
const users = rows
- .filter((u) => u.username) // a profile is only reachable by username
.map((u) => ({
username: u.username,
display_name: u.display_name || u.username,