Walk the tree
A blog is one shape. A polis site publishes more than posts: replies to other people, tags, signed statements. The SDK hands you all of it, so you can build a portfolio, a reading log or a network page as easily as a blog.
polis.getIndex() reads the index once and groups everything by type:
const tree = await polis.getIndex();
tree.posts; // the writing
tree.comments; // my replies on other sites; each has .inReplyTo
tree.tags; // signed labels
tree.attestations; // signed statements: same-as, projections, grants…
tree.byType; // every type, including ones this SDK version doesn't know yet
polis.walk() goes through it newest first, and reads a node's full content only when you call load(). So
"every post tagged drop-2" or "my last five replies" costs exactly the reads it needs:
for await (const node of polis.walk({ types: ['comment'] })) {
const reply = await node.load(); // parsed: frontmatter, markdown, inReplyTo
if (shelf.push(reply) >= 5) break;
}
And polis.at(site) is the same reader on another site, with your hooks and caching. The sites you
follow are content trees too:
for (const f of await polis.getFollowing()) {
const latest = (await polis.at(f.url).getPosts()).slice(0, 3);
}
This site's tree page draws getIndex() as a literal tree, and its network page is
getFollowing() plus at():
// The content tree: everything vdibart.polis.pub publishes, not just posts.
// polis.getIndex() reads the site's index once and groups it by type; a
// website can shape any of it (this page simply draws it as a tree).
import type { Metadata } from 'next';
import type { Entry } from '@polis-pub/sdk';
import { polis, ourPath, SITE_HOST } from '@/lib/polis';
export const metadata: Metadata = { title: 'The tree', description: `Everything ${SITE_HOST} publishes, as one tree.` };
const ORDER = ['post', 'comment', 'tag', 'attestation'];
const ABOUT: Record<string, string> = {
post: 'signed markdown, the writing itself',
comment: 'replies, published on this site, pointing at someone else’s post',
tag: 'signed labels on addresses anywhere on polis',
attestation: 'signed statements: same-as, projections, grants',
};
function label(e: Entry): string {
if (e.type === 'attestation') return e.title.replace(/^pub\.polis\.attestation\./, '');
return e.title || e.path.split('/').pop() || e.path;
}
function href(e: Entry): string {
if (e.type === 'post') return ourPath(e.url) ?? e.url;
return e.url || e.source;
}
export default async function Tree() {
const tree = await polis.getIndex();
const types = [...ORDER.filter((t) => tree.byType[t]), ...Object.keys(tree.byType).filter((t) => !ORDER.includes(t))];
return (
<div className="page">
<header className="page-head">
<span className="eyebrow">polis.getIndex()</span>
<h1>The tree</h1>
<p>
A polis site is more than its posts. This is everything {SITE_HOST} publishes, read from its own index in
one request: each entry a signed file anyone can fetch. The front page is one branch of it, shaped as a blog.
</p>
</header>
<ul className="counts">
{types.map((t) => (
<li key={t}><b>{tree.byType[t].length}</b> {t}{tree.byType[t].length === 1 ? '' : 's'}</li>
))}
</ul>
<div className="tree" role="tree" aria-label={`The content tree of ${SITE_HOST}`}>
<div className="row"><span className="arrow">◆</span> {SITE_HOST}</div>
{types.map((t, ti) => {
const last = ti === types.length - 1;
const list = tree.byType[t];
return (
<details key={t} open={t === 'post'} role="treeitem">
<summary className="row">
<span className="branch">{last ? '└── ' : '├── '}</span>
<span className="type">{t}/</span> <span className="date">{list.length} · {ABOUT[t] ?? ''}</span>
</summary>
{list.map((e, i) => (
<div className="row" key={e.path}>
<span className="branch">{last ? ' ' : '│ '}{i === list.length - 1 ? '└── ' : '├── '}</span>
<span className="date">{e.published.slice(0, 10)} </span>
<a href={href(e)}>{label(e)}</a>
{e.inReplyTo && (
<span className="date"> <span className="arrow">→</span> {new URL(e.inReplyTo).host}</span>
)}
</div>
))}
</details>
);
})}
</div>
</div>
);
}// The network: the sites vdibart.polis.pub follows, and what each wrote last.
// getFollowing() reads the site's signed follow list; polis.at(site) is the
// same reader on another site. Content trees, plural.
import type { Metadata } from 'next';
import { polis, formatDate, SITE_HOST } from '@/lib/polis';
export const metadata: Metadata = { title: 'Network', description: `The polis sites ${SITE_HOST} follows.` };
// Rebuilt at most hourly: these are other people's sites, with no webhook here.
export const revalidate = 3600;
const SHOWN = 12;
export default async function Network() {
const following = (await polis.getFollowing()).filter((f) => !/^https:\/\/discover\./.test(f.url));
const sites = await Promise.all(
following.slice(0, SHOWN).map(async (f) => {
try {
return { url: f.url, posts: (await polis.at(f.url).getPosts()).slice(0, 3) };
} catch {
return { url: f.url, posts: null };
}
}),
);
return (
<div className="page">
<header className="page-head">
<span className="eyebrow">polis.getFollowing() · polis.at(site)</span>
<h1>Network</h1>
<p>
Who {SITE_HOST} follows, from its signed follow list, and the latest from each, read straight from their own
sites. No platform sits in between: every site here publishes the same plain files.
</p>
</header>
<div className="sites">
{sites.map((s) => (
<section className="site-card" key={s.url}>
<h3><a href={s.url}>{new URL(s.url).host}</a></h3>
{s.posts === null ? (
<p className="quiet">not answering right now</p>
) : s.posts.length === 0 ? (
<p className="quiet">no posts yet</p>
) : (
<ul>
{s.posts.map((p) => (
<li key={p.path}>
<a href={p.url}>{p.title || 'Untitled'}</a>
<div className="quiet">{formatDate(p.published)}</div>
</li>
))}
</ul>
)}
</section>
))}
</div>
{following.length > SHOWN && (
<p className="eyebrow" style={{ marginTop: 24 }}>and {following.length - SHOWN} more</p>
)}
</div>
);
}