mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Three things the migration got wrong, all of them the same shape: something was known and not said. The archive titled every site it carried after the site's hostname, because an address was all it knew about a site it had only ever seen at one. Handing that back hands back an address where a name should be — a site called "Arguable" comes home titled "arguable.shakespeare.wtf". The gateway can now read the name the site was carrying all along, out of its webmanifest or the metadata it publishes for link previews, and that is what gets signed. A site that never said what it is called gets no title, which is what a manifest without one has always meant. Its `alt` goes too: it says the site is archived so that it keeps working, which is a true sentence about the archive and stops being true here. Names already taken back are listed and fixed the same way, rebuilt from the event as relays hold it rather than from the gateway's copy — a site redeployed since the migration ran has moved on, and rebuilding it from what the gateway fetched would quietly restore an older version of it. Publishing to relays went through a pool that resolves on the first acceptance and discards every other answer, so a manifest that reached one relay out of five was reported exactly like one that reached all five, and the reasons the other four refused were received and thrown away. Of 102 manifests published this way in one sitting, one relay held every one and another held none, and nothing anywhere said so. Every relay is now published to on its own and reported on by name. One acceptance is still a success — the event exists and whoever reads that relay will find it — so this is a note, not a failure. The gaps that leaves are closed without signing anything: the events go out again exactly as they are, ids and signatures unchanged, to the relays whose own answer says they are missing them. A relay that already has one says duplicate and nothing happens; a relay that refused before finally says why, which is the only way to hear a reason that was discarded at the time. Ordinary nsite deploys published the same blind way, so they use the same helper and carry the same coverage. Lastly, names more than one label under the gateway's domain are refused rather than published for. A wildcard certificate covers one label, so `a.b.example.com` can be claimed, published for, and will still fail its handshake for everyone forever. Five are on file, inherited from a host that accepted them; one already has a manifest published for it, which can now be retracted.
159 lines
6.2 KiB
TypeScript
159 lines
6.2 KiB
TypeScript
import { useQuery } from '@tanstack/react-query';
|
|
import { useNostr } from '@nostrify/react';
|
|
import type { NostrEvent } from '@nostrify/nostrify';
|
|
import { useCurrentUser } from '@/hooks/useCurrentUser';
|
|
import type { NpanelProvider } from '@/contexts/DeploySettingsContext';
|
|
import { fetchNpanelClaims, type NpanelClaim } from '@/lib/deploy/npanelApi';
|
|
import {
|
|
NAMED_SITE_KIND,
|
|
hasArchiveTitle,
|
|
isServableHostname,
|
|
manifestIdentifier,
|
|
planClaim,
|
|
type MigrationStep,
|
|
} from '@/lib/deploy/npanelMigration';
|
|
|
|
/** How long the relay round trip for one's own sites is given. */
|
|
const RELAY_TIMEOUT_MS = 6_000;
|
|
|
|
/** A waiting name, with what it would take to get it back. */
|
|
export interface NpanelClaimPlan extends NpanelClaim {
|
|
step: MigrationStep;
|
|
}
|
|
|
|
/** A name already taken back, and the event currently serving it. */
|
|
export interface NpanelTakenSite {
|
|
hostname: string;
|
|
/** The claimant's own manifest, as relays hold it right now. */
|
|
event: NostrEvent;
|
|
/** What the site calls itself, per the gateway. Null when it never said. */
|
|
suggestedTitle: string | null;
|
|
suggestedDescription: string | null;
|
|
/** Whether the event is still titled after its hostname. */
|
|
needsTitle: boolean;
|
|
/** A name no certificate can cover, which should never have been published. */
|
|
stray: boolean;
|
|
}
|
|
|
|
export interface NpanelClaims {
|
|
/** Names still waiting, each with a plan. */
|
|
waiting: NpanelClaimPlan[];
|
|
/** Names already taken back whose manifest could be improved or removed. */
|
|
taken: NpanelTakenSite[];
|
|
/** Every event of the user's this query saw, for checking relay coverage. */
|
|
events: NostrEvent[];
|
|
}
|
|
|
|
/** The `identifier` half of a `kind:pubkey:identifier` address. */
|
|
function addressIdentifier(address: string): string {
|
|
return address.split(':').slice(2).join(':');
|
|
}
|
|
|
|
/** The `d` tag of an event, which for a named site is the site's identifier. */
|
|
function eventIdentifier(event: NostrEvent): string {
|
|
return event.tags.find(([name]) => name === 'd')?.[1] ?? '';
|
|
}
|
|
|
|
/**
|
|
* The names waiting for the logged-in user on a gateway, and what each needs.
|
|
*
|
|
* Half the answer comes from relays rather than the gateway: to know whether a
|
|
* name can simply be repointed, one has to know which sites this user already
|
|
* publishes, and that is on the network, not in npanel. The same query answers
|
|
* two more questions at no extra cost — which taken names are still titled
|
|
* after their hostname, and which of them should never have been published at
|
|
* all — so it is made once and its events are kept.
|
|
*/
|
|
export function useNpanelClaims(provider: NpanelProvider | undefined) {
|
|
const { user } = useCurrentUser();
|
|
const { nostr } = useNostr();
|
|
|
|
return useQuery<NpanelClaims>({
|
|
queryKey: ['npanel-claims', provider?.dashboardHost, user?.pubkey],
|
|
enabled: Boolean(provider && user),
|
|
// Nobody's list of waiting names changes on its own, and every refetch
|
|
// costs a signature — which on a remote signer costs a round trip.
|
|
staleTime: 5 * 60 * 1000,
|
|
refetchOnWindowFocus: false,
|
|
retry: false,
|
|
queryFn: async ({ signal }) => {
|
|
const empty: NpanelClaims = { waiting: [], taken: [], events: [] };
|
|
if (!provider || !user) return empty;
|
|
|
|
const claims = await fetchNpanelClaims(provider.dashboardHost, user.signer, signal, { taken: true });
|
|
if (!claims.length) return empty;
|
|
|
|
// Only the names actually in play are asked about, rather than every
|
|
// site this key has ever published.
|
|
const wanted = new Set<string>();
|
|
for (const claim of claims) {
|
|
const identifier = claim.manifest ? manifestIdentifier(claim.manifest) : '';
|
|
if (identifier) wanted.add(identifier);
|
|
if (claim.address) wanted.add(addressIdentifier(claim.address));
|
|
}
|
|
wanted.delete('');
|
|
|
|
const events = wanted.size
|
|
? await nostr.group(provider.relayUrls).query(
|
|
[{ kinds: [NAMED_SITE_KIND], authors: [user.pubkey], '#d': [...wanted] }],
|
|
{ signal: AbortSignal.any([signal, AbortSignal.timeout(RELAY_TIMEOUT_MS)]) },
|
|
)
|
|
: [];
|
|
|
|
const byIdentifier = new Map<string, NostrEvent>();
|
|
for (const event of events) {
|
|
const identifier = eventIdentifier(event);
|
|
if (!identifier) continue;
|
|
const seen = byIdentifier.get(identifier);
|
|
if (!seen || seen.created_at < event.created_at) byIdentifier.set(identifier, event);
|
|
}
|
|
const published = new Set(byIdentifier.keys());
|
|
|
|
// A name npanel serves from this user's own key is a site this user
|
|
// published, so relays that know nothing about any of them are relays
|
|
// that are not answering. Planning against that silence would mean
|
|
// republishing over live sites, so it fails loudly instead.
|
|
const known = claims
|
|
.filter((claim) => claim.mine && claim.address)
|
|
.map((claim) => addressIdentifier(claim.address ?? ''))
|
|
.filter(Boolean);
|
|
|
|
if (known.length && !known.some((identifier) => published.has(identifier))) {
|
|
throw new Error(
|
|
'Relays did not answer with the sites you already publish, so it is not safe to work out what needs republishing. Try again in a moment.',
|
|
);
|
|
}
|
|
|
|
const waiting: NpanelClaimPlan[] = [];
|
|
const taken: NpanelTakenSite[] = [];
|
|
|
|
for (const claim of claims) {
|
|
if (!claim.claimed) {
|
|
waiting.push({ ...claim, step: planClaim(claim, user.pubkey, published, provider.domain) });
|
|
continue;
|
|
}
|
|
|
|
const identifier = claim.address ? addressIdentifier(claim.address) : '';
|
|
const event = identifier ? byIdentifier.get(identifier) : undefined;
|
|
// A claimed name whose event no relay answered with is not a name with
|
|
// nothing wrong; it is a name nothing can be said about. Leaving it out
|
|
// is the only option that cannot make it worse.
|
|
if (!event) continue;
|
|
|
|
const stray = !isServableHostname(claim.hostname, provider.domain);
|
|
|
|
taken.push({
|
|
hostname: claim.hostname,
|
|
event,
|
|
suggestedTitle: claim.suggestedTitle ?? null,
|
|
suggestedDescription: claim.suggestedDescription ?? null,
|
|
needsTitle: hasArchiveTitle(event, claim.hostname),
|
|
stray,
|
|
});
|
|
}
|
|
|
|
return { waiting, taken, events: [...byIdentifier.values()] };
|
|
},
|
|
});
|
|
}
|