Add owner inbox UI with address release

Web: "My listings" (listings, requests of a listing, conversation) in
three panes on wide screens and one at a time on phones, and a sheet or
dialog to release the exact address. Sign-in uses a link printed by
`node run` (token in the URL fragment, kept in sessionStorage only).

API: GET /api/v1/mine (own listings with request counts and whether an
address is saved); inbox rows gain last_dir, needs_reply, preview,
effort and grant_until, and can be filtered by listing.

Docs: PROTOCOL.md and README describe the owner UI and login.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 20:02:51 +01:00
co-authored by Claude Sonnet 5.5
parent 3b0351c774
commit 8edae3ae56
17 changed files with 2400 additions and 49 deletions
+173
View File
@@ -0,0 +1,173 @@
// The owner's half of the API: everything here needs the admin token.
//
// The token reaches the page in the URL fragment of the login link the node
// prints (#/login/<token>), which a browser never sends to a server. It is kept
// in sessionStorage only, so closing the tab signs the owner out, and it is only
// ever sent to the node that served this page.
import { HttpError } from "./api.ts";
const TOKEN_KEY = "allmende.owner.token";
let memory: string | null = null;
export function getToken(): string | null {
try {
return sessionStorage.getItem(TOKEN_KEY) ?? memory;
} catch {
return memory;
}
}
export function setToken(token: string): void {
memory = token;
try {
sessionStorage.setItem(TOKEN_KEY, token);
} catch {
// keep it in memory: the page still works, it just forgets on reload
}
}
export function clearToken(): void {
memory = null;
try {
sessionStorage.removeItem(TOKEN_KEY);
} catch {
// nothing stored
}
}
/** The node refused the token (or none is set): the owner has to sign in again. */
export class Unauthorized extends Error {
constructor() {
super("unauthorized");
}
}
export interface Mine {
id: string;
title: string;
kind?: string;
price?: { amount_cents: number; currency: string; period?: string };
expires_at: number;
threads: number;
needs_reply: number;
hidden: number;
address_saved: boolean;
}
export interface Thread {
listing: string;
thread: string;
messages: number;
hidden: number;
replied: boolean;
first_at: number;
last_at: number;
last_dir: "in" | "out";
needs_reply: boolean;
preview?: string;
effort: number;
grant_until?: number;
}
export interface OwnerMessage {
hash: string;
dir: "in" | "out";
hidden?: boolean;
reason?: string;
received_at: number;
type: "text" | "grant";
n: number;
sent_at: number;
text?: string;
address?: string;
phone?: string;
email?: string;
note?: string;
valid_until?: number;
}
export interface Address {
address: string;
phone?: string;
email?: string;
}
export interface Reply {
type: "text" | "grant";
text?: string;
address?: string;
phone?: string;
note?: string;
valid_days?: number;
}
async function call(method: string, path: string, body?: unknown): Promise<unknown> {
const token = getToken();
if (!token) throw new Unauthorized();
const res = await fetch(path, {
method,
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
...(body === undefined ? {} : { "Content-Type": "application/json" }),
},
body: body === undefined ? undefined : JSON.stringify(body),
});
if (res.status === 401) {
clearToken();
throw new Unauthorized();
}
if (res.status === 204) return null;
let data: unknown = null;
try {
data = await res.json();
} catch {
// no body
}
if (!res.ok) {
const detail = typeof (data as { error?: unknown } | null)?.error === "string" ? (data as { error: string }).error : res.statusText;
throw new HttpError(res.status, detail);
}
return data;
}
const e = encodeURIComponent;
export async function getMine(): Promise<Mine[]> {
return (await call("GET", "/api/v1/mine")) as Mine[];
}
export async function getThreads(listing: string): Promise<Thread[]> {
return (await call("GET", `/api/v1/inbox?listing=${e(listing)}`)) as Thread[];
}
export async function getMessages(listing: string, thread: string): Promise<OwnerMessage[]> {
return (await call("GET", `/api/v1/inbox/${e(listing)}/${e(thread)}`)) as OwnerMessage[];
}
export async function sendReply(listing: string, thread: string, r: Reply): Promise<void> {
await call("POST", `/api/v1/inbox/${e(listing)}/${e(thread)}`, r);
}
export async function deleteThread(listing: string, thread: string): Promise<void> {
await call("DELETE", `/api/v1/inbox/${e(listing)}/${e(thread)}`);
}
/** The saved address of a listing, or null if none is saved. */
export async function getAddress(listing: string): Promise<Address | null> {
try {
return (await call("GET", `/api/v1/listings/${e(listing)}/address`)) as Address;
} catch (err) {
if (err instanceof HttpError && err.status === 404) return null;
throw err;
}
}
export async function putAddress(listing: string, a: Address): Promise<void> {
await call("PUT", `/api/v1/listings/${e(listing)}/address`, a);
}
export async function deleteAddress(listing: string): Promise<void> {
await call("DELETE", `/api/v1/listings/${e(listing)}/address`);
}