mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Bring up the migration where somebody runs into it
Taking back a site had one way in: a button in Settings under Deploy, which said the same thing to everybody and could not say whether anything was actually waiting. On the gateway this was built for, two people out of eight hundred and seven have used it. The moment it is needed is the one somebody types a name they used to own, and until now that moment did the opposite — the name came back taken and the button went dead, because a name being held for its previous owner looks exactly like a stranger's to anyone who does not ask. The gateway can now be asked, so the field says the name is being held for them and offers the step that gets it back, in place of a refusal with nowhere to go. The deploy dialog owns that list rather than the field, because one recognised name is rarely all somebody has waiting. npanel also refuses to add a name it is holding, saying to take it back instead — a sentence that was being shown as a flat red alert with no way to act on it. That refusal now carries the same offer. And the settings card asks how many are waiting instead of guessing. It costs a signature on a page nobody asked to sign on, which is the thing this deliberately did not do before; the difference is that it is now two counted rows rather than a read of every claimed deploy, and that the card says nothing at all unless there is something to say. A card that cannot tell whether it applies to you is one most people learn to ignore before it ever does.
This commit is contained in:
8 files changed
+334
-26
No files matched your search
@@ -271,3 +271,32 @@ describe('checkNameAvailable', () => {
|
||||
).resolves.toMatchObject({ available: false, reason: 'taken' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('checkNameAvailable, for a name held for its previous owner', () => {
|
||||
it('reports waiting rather than taken', async () => {
|
||||
// The state almost every unclaimed name is in: an archive serving it, so
|
||||
// the public answer is taken and the useful answer is that it is theirs.
|
||||
fetchMock
|
||||
.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({ hostname: `x.${DOMAIN}`, available: false, reason: 'taken' })),
|
||||
)
|
||||
.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({ hostname: `x.${DOMAIN}`, available: false, reason: 'waiting' })),
|
||||
);
|
||||
|
||||
await expect(
|
||||
checkNameAvailable(DASHBOARD_HOST, `x.${DOMAIN}`, undefined, signer),
|
||||
).resolves.toMatchObject({ available: false, reason: 'waiting' });
|
||||
});
|
||||
|
||||
it('says nothing more to somebody who did not sign', async () => {
|
||||
fetchMock.mockResolvedValue(
|
||||
new Response(JSON.stringify({ hostname: `x.${DOMAIN}`, available: false, reason: 'taken' })),
|
||||
);
|
||||
|
||||
await expect(checkNameAvailable(DASHBOARD_HOST, `x.${DOMAIN}`)).resolves.toMatchObject({
|
||||
reason: 'taken',
|
||||
});
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -12,11 +12,16 @@ export interface NameAvailability {
|
||||
available: boolean;
|
||||
/**
|
||||
* Why it is not free, or — with `available` — that it is not free of somebody
|
||||
* else. `mine` comes only from a signed request, and only from a gateway new
|
||||
* enough to answer one; an older one says `taken` and is not wrong, just less
|
||||
* use to the person who owns the name.
|
||||
* else.
|
||||
*
|
||||
* `mine` is a name this signer can already point somewhere. `waiting` is one
|
||||
* the gateway is holding for them from before it took the domain over: not
|
||||
* free, not theirs to deploy to yet, and one claim away from both. Both come
|
||||
* only from a signed request, and only from a gateway new enough to answer
|
||||
* one; an older one says `taken`, which is not wrong, just of no use to the
|
||||
* person the name belongs to.
|
||||
*/
|
||||
reason?: 'taken' | 'invalid' | 'mine';
|
||||
reason?: 'taken' | 'invalid' | 'mine' | 'waiting';
|
||||
/** Present when `reason` is `invalid`; written to be shown to whoever typed it. */
|
||||
error?: string;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import { getPublicKey, generateSecretKey } from 'nostr-tools';
|
||||
import type { NostrSigner } from '@nostrify/nostrify';
|
||||
import { fetchNpanelClaimCount } from './npanelApi';
|
||||
|
||||
const DASHBOARD_HOST = 'npanel.example.test';
|
||||
|
||||
const pubkey = getPublicKey(generateSecretKey());
|
||||
|
||||
/**
|
||||
* A signer that fills in the fields a real one would, without signing.
|
||||
*
|
||||
* Signing for real is not available here: jsdom's TextEncoder returns a
|
||||
* Uint8Array from its own realm, which fails the `instanceof` check inside
|
||||
* @noble/hashes, so nostr-tools cannot hash an event under this environment.
|
||||
* Nothing here verifies a signature — what matters is which request goes out
|
||||
* and what is made of the answer.
|
||||
*/
|
||||
const signer: NostrSigner = {
|
||||
getPublicKey: async () => pubkey,
|
||||
signEvent: async (template) => ({
|
||||
...template,
|
||||
id: 'f'.repeat(64),
|
||||
pubkey,
|
||||
sig: '0'.repeat(128),
|
||||
}),
|
||||
} as NostrSigner;
|
||||
|
||||
let fetchMock: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(() => {
|
||||
fetchMock = vi.fn();
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('fetchNpanelClaimCount', () => {
|
||||
it('asks the gateway only for the count', async () => {
|
||||
fetchMock.mockResolvedValue(new Response(JSON.stringify({ waiting: 3, taken: 1 })));
|
||||
|
||||
await expect(fetchNpanelClaimCount(DASHBOARD_HOST, signer)).resolves.toEqual({
|
||||
waiting: 3,
|
||||
taken: 1,
|
||||
});
|
||||
|
||||
const request = fetchMock.mock.calls[0][0] as Request;
|
||||
expect(request.url).toBe(`https://${DASHBOARD_HOST}/api/claims?summary=1`);
|
||||
expect(request.headers.get('Authorization')).toMatch(/^Nostr /);
|
||||
});
|
||||
|
||||
it('reports nothing waiting when the gateway answers with the list instead', async () => {
|
||||
// An older gateway ignores the parameter and returns every claim, which is
|
||||
// a different shape rather than a bigger number — counting it would be
|
||||
// inventing one.
|
||||
fetchMock.mockResolvedValue(
|
||||
new Response(JSON.stringify({ claims: [{ hostname: `a.example.test` }] })),
|
||||
);
|
||||
|
||||
await expect(fetchNpanelClaimCount(DASHBOARD_HOST, signer)).resolves.toEqual({
|
||||
waiting: 0,
|
||||
taken: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it('reports nothing waiting when the gateway cannot be reached', async () => {
|
||||
// This runs because somebody opened a settings page. A number that failed
|
||||
// to arrive is not an error worth showing them.
|
||||
fetchMock.mockRejectedValue(new Error('offline'));
|
||||
|
||||
await expect(fetchNpanelClaimCount(DASHBOARD_HOST, signer)).resolves.toEqual({
|
||||
waiting: 0,
|
||||
taken: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it('reports nothing waiting when the gateway refuses', async () => {
|
||||
fetchMock.mockResolvedValue(new Response('nope', { status: 403 }));
|
||||
|
||||
await expect(fetchNpanelClaimCount(DASHBOARD_HOST, signer)).resolves.toEqual({
|
||||
waiting: 0,
|
||||
taken: 0,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -114,6 +114,50 @@ export async function fetchNpanelClaims(
|
||||
return claims;
|
||||
}
|
||||
|
||||
/** How many names a gateway is holding for somebody, and how many they took. */
|
||||
export interface NpanelClaimCount {
|
||||
waiting: number;
|
||||
taken: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The count alone, without working out what to do about any of them.
|
||||
*
|
||||
* {@link fetchNpanelClaims} reads every file of every claimed deploy and returns
|
||||
* a manifest each, which is the right price for the dialog that hands the names
|
||||
* back and much too high for the sentence that gets somebody to open it. A
|
||||
* gateway too old to know the parameter answers with the list instead, so the
|
||||
* shape is checked rather than assumed — and a gateway that fails outright
|
||||
* reports nothing waiting, since a number nobody can fetch should not become an
|
||||
* error on a settings page.
|
||||
*/
|
||||
export async function fetchNpanelClaimCount(
|
||||
dashboardHost: string,
|
||||
signer: NostrSigner,
|
||||
signal?: AbortSignal,
|
||||
): Promise<NpanelClaimCount> {
|
||||
const none: NpanelClaimCount = { waiting: 0, taken: 0 };
|
||||
|
||||
try {
|
||||
const response = await npanelRequest(
|
||||
dashboardHost,
|
||||
signer,
|
||||
'GET',
|
||||
'/api/claims?summary=1',
|
||||
undefined,
|
||||
signal,
|
||||
);
|
||||
if (!response.ok) return none;
|
||||
|
||||
const body = (await response.json()) as Partial<NpanelClaimCount>;
|
||||
if (typeof body.waiting !== 'number' || typeof body.taken !== 'number') return none;
|
||||
|
||||
return { waiting: body.waiting, taken: body.taken };
|
||||
} catch {
|
||||
return none;
|
||||
}
|
||||
}
|
||||
|
||||
/** Point a name waiting for this signer at a site they published. */
|
||||
export async function claimNpanelHostname(
|
||||
dashboardHost: string,
|
||||
|
||||
Reference in new issue
Block a user