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.

4.6 KB
Raw Blame
// /api/friends
//   GET  — the caller's whole social graph: accepted friends (with live presence
//          + unread DM counts), incoming requests, outgoing requests.
//   POST — graph mutations via { action, ... }:
//            request  { to }            send a friend request (auto-accepts a reciprocal one)
//            accept   { userId }        accept an incoming request
//            decline  { userId }        decline an incoming request
//            remove   { userId }        remove a friend / cancel an outgoing request
//            mute     { userId }        mute an account (suppress their DMs)
//            unmute   { userId }        unmute an account
//
// Validates the caller owns the account (session or bearer). Guards self-add,
// duplicates, and unknown targets in the store layer; rate-limited per account.

import { cors, error, json, method, readJson, wrap, rateLimited } from '../_lib/http.js';
import { requireCsrf } from '../_lib/csrf.js';
import { clientIp, limits } from '../_lib/rate-limit.js';
import { resolveAccount } from '../_lib/account-auth.js';
import { readPresence, notifyMultiplayer } from '../_lib/presence-store.js';
import {
	listGraph,
	sendRequest,
	acceptRequest,
	declineRequest,
	removeFriend,
	muteUser,
	unmuteUser,
} from '../_lib/friends-store.js';

function isUuid(v) {
	return typeof v === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(v);
}

export default wrap(async (req, res) => {
	if (cors(req, res, { methods: 'GET,POST,OPTIONS', credentials: true })) return;
	if (!method(req, res, ['GET', 'POST'])) return;

	const auth = await resolveAccount(req, res);
	if (!auth) return error(res, 401, 'unauthorized', 'sign in required');
	const me = auth.userId;

	if (req.method === 'GET') {
		const rl = await limits.publicIp(clientIp(req));
		if (!rl.success) return rateLimited(res, rl);
		return json(res, 200, { data: await graphWithPresence(me) });
	}

	// POST — a graph mutation. CSRF-guard cookie-session callers (bearer exempt).
	if (!(await requireCsrf(req, res, me))) return;

	// Tighter limit: 60 actions/min per account.
	const rl = await limits.chatUser(me);
	if (!rl.success) return rateLimited(res, rl, 'slow down');

	const body = await readJson(req).catch(() => ({}));
	const action = String(body.action || '');
	const target = body.to ?? body.userId;
	if (action !== 'request' && action !== 'accept' && action !== 'decline' &&
		action !== 'remove' && action !== 'mute' && action !== 'unmute') {
		return error(res, 400, 'bad_action', 'unknown action');
	}
	if (!isUuid(target)) return error(res, 400, 'bad_target', 'invalid user id');

	switch (action) {
		case 'request': {
			const result = await sendRequest(me, target);
			// Notify the other account: a fresh invite, or that we accepted theirs.
			if (result.status === 'requested') notifyMultiplayer('friend_request', target, { from: me });
			else if (result.status === 'accepted') notifyMultiplayer('friend_accept', target, { from: me });
			return json(res, 200, { data: result });
		}
		case 'accept': {
			const result = await acceptRequest(me, target);
			notifyMultiplayer('friend_accept', target, { from: me });
			return json(res, 200, { data: result });
		}
		case 'decline':
			return json(res, 200, { data: await declineRequest(me, target) });
		case 'remove':
			return json(res, 200, { data: await removeFriend(me, target) });
		case 'mute':
			return json(res, 200, { data: await muteUser(me, target) });
		case 'unmute':
			return json(res, 200, { data: await unmuteUser(me, target) });
	}
});

async function graphWithPresence(me) {
	let graph;
	try {
		graph = await listGraph(me);
	} catch (err) {
		// Friends tables may not be migrated yet: return an empty graph rather than
		// 500. Keyed on the Postgres undefined_table code, never on a substring of
		// the message. A missing-COLUMN error reads "column X of relation Y does not
		// exist", so message matching also swallowed a half-applied migration and
		// answered 200 with an empty graph: the caller saw "you have no friends"
		// instead of an error, and nothing surfaced the broken schema.
		if (err?.code === '42P01') {
			return { friends: [], incoming: [], outgoing: [] };
		}
		throw err;
	}
	const ids = [
		...graph.friends.map((f) => f.id),
		...graph.incoming.map((f) => f.id),
		...graph.outgoing.map((f) => f.id),
	];
	const presence = await readPresence(ids);
	const annotate = (u) => ({ ...u, ...(presence[u.id] || { online: false, realm: null, server: null }) });
	return {
		friends: graph.friends.map(annotate),
		incoming: graph.incoming.map(annotate),
		outgoing: graph.outgoing.map(annotate),
	};
}