chadtmiller.com/group-host
atmosphere group host
0
0
Fields for someone's handle suggest accounts as they are typed
docs/design/signing-in.md+14โ0
| @@ -51,6 +51,20 @@ authorization server, pushes the request with PKCE and a fresh DPoP key, | ||
and sends the browser there. On return it exchanges the code, checks the | ||
token names the DID that started, and revokes the access token at once. | ||
| ||
As they type, the field suggests the accounts whose handle or name | ||
matches, with each one's picture, name and handle; picking one signs in | ||
as it at once. The suggestions come from typeahead.waow.tech, a service | ||
outside this host that knows the accounts of the whole network, so the | ||
browser sends it what is typed, from two letters on. It is the one | ||
server besides this host that the console's scripts may reach, as its | ||
content security policy says. When it cannot answer, the field is a | ||
plain one. | ||
| ||
Every other field for someone's handle suggests accounts the same way, | ||
a pick only filling it in: inviting someone, labelling someone, and the | ||
account to move here. The handle is resolved when the form is sent, so | ||
an invite is still written to the account's DID. | ||
| ||
What it keeps is a cookie, `group_host_session`: the DID and its times, | ||
signed with an HMAC under another key derived from the rotation secret, | ||
good for 30 days. Signing out records the time in `console_logout`, and | ||
harness/e2e/flows.spec.mjs+43โ9
| @@ -3,7 +3,7 @@ | ||
// and what the app then does as the group, read back from the host. | ||
| ||
import { expect, test } from '@playwright/test' | ||
import { App, McpClient, approve, didKey, joins, signIn, signInDialog, signedInAs, url, who, world } from './pages.mjs' | ||
import { App, McpClient, approve, didKey, joins, signIn, signInDialog, signedInAs, suggests, url, who, world } from './pages.mjs' | ||
| ||
test.describe.configure({ mode: 'serial' }) | ||
| ||
| @@ -14,6 +14,8 @@ let bob | ||
test.beforeAll(async ({ browser }) => { | ||
page = await browser.newPage() | ||
bob = await (await browser.newContext()).newPage() | ||
await suggests(page.context()) | ||
await suggests(bob.context()) | ||
}) | ||
| ||
test.afterAll(async () => { | ||
| @@ -36,8 +38,25 @@ test('coming to the host signed out', async () => { | ||
await expect(dialog).toBeVisible() | ||
await expect(dialog.locator('.apps img')).toHaveCount(40) | ||
await expect(dialog.getByRole('link', { name: 'What is an Atmosphere account?' })).toHaveAttribute('href', 'https://atmosphereaccount.com/') | ||
await expect(dialog.getByRole('textbox', { name: 'Handle' })).toBeFocused() | ||
await expect(dialog.getByRole('combobox', { name: 'Handle' })).toBeFocused() | ||
await expect(dialog.getByRole('link', { name: 'Create one' })).toHaveAttribute('href', 'https://bsky.app/') | ||
// Two letters of a handle suggest the accounts it is part of; one, none. | ||
const handle = dialog.getByRole('combobox', { name: 'Handle' }) | ||
await handle.pressSequentially('a') | ||
await expect(handle).toHaveAttribute('aria-expanded', 'false') | ||
await handle.pressSequentially('l') | ||
await expect(dialog.getByRole('option')).toHaveText(['Alice @alice.test']) | ||
await expect(handle).toHaveAttribute('aria-expanded', 'true') | ||
// Escape closes the list, not the dialog; what the arrows chose is let | ||
// go, so Enter after more typing sends what is typed. | ||
await handle.press('ArrowDown') | ||
await expect(dialog.getByRole('option')).toHaveAttribute('aria-selected', 'true') | ||
await handle.press('Escape') | ||
await expect(dialog.getByRole('listbox')).toBeHidden() | ||
await expect(dialog).toBeVisible() | ||
await handle.pressSequentially('i') | ||
await expect(dialog.getByRole('option')).toHaveAttribute('aria-selected', 'false') | ||
await handle.press('Escape') | ||
await dialog.getByRole('button', { name: 'Cancel' }).click() | ||
await expect(dialog).toBeHidden() | ||
}) | ||
| @@ -55,8 +74,17 @@ test("a group's page signed out", async () => { | ||
}) | ||
| ||
test("signing in at her own PDS, from a group's page", async () => { | ||
// Signed in from the page, back on it, as she may see it. | ||
await signIn(page, 'alice', page.getByRole('main').getByRole('button', { name: 'Sign in' })) | ||
// Signed in from the page by picking her account as she types it, with | ||
// the arrows and Enter; back on it, as she may see it. | ||
await page.getByRole('main').getByRole('button', { name: 'Sign in' }).click() | ||
const handle = signInDialog(page).getByRole('combobox', { name: 'Handle' }) | ||
await handle.pressSequentially('alic') | ||
await expect(signInDialog(page).getByRole('option', { name: 'Alice @alice.test' })).toBeVisible() | ||
await handle.press('ArrowDown') | ||
await expect(signInDialog(page).getByRole('option', { name: 'Alice @alice.test' })).toHaveAttribute('aria-selected', 'true') | ||
await handle.press('Enter') | ||
await approve(page, 'alice') | ||
await page.waitForURL(url('/g/riders.groups.test')) | ||
await expect(page).toHaveURL(url('/g/riders.groups.test')) | ||
await expect.poll(() => signedInAs(page)).toBe('@alice.test') | ||
await expect(page.getByText('Sign in to see this group.')).toHaveCount(0) | ||
| @@ -143,7 +171,7 @@ test('inviting someone from the dialog', async () => { | ||
const invite = page.getByRole('button', { name: 'Invite someone' }) | ||
await invite.click() | ||
await expect(dialog.getByText("They'll see it the next time they sign in, here or in any app that shows invites.")).toBeVisible() | ||
await dialog.getByRole('textbox', { name: 'Their handle' }).fill('nobody.test') | ||
await dialog.getByRole('combobox', { name: 'Their handle' }).fill('nobody.test') | ||
await dialog.getByRole('button', { name: 'Send invite' }).click() | ||
await expect(page.getByText('We cannot find nobody.test.')).toBeVisible() | ||
// Cancelled, it sends nothing. | ||
| @@ -154,7 +182,12 @@ test('inviting someone from the dialog', async () => { | ||
// Sent, with a note; taken back; and sent again. | ||
const send = async () => { | ||
await invite.click() | ||
await dialog.getByRole('textbox', { name: 'Their handle' }).fill('@bob.test') | ||
// Picked from the accounts suggested as part of his handle is typed. | ||
const handle = dialog.getByRole('combobox', { name: 'Their handle' }) | ||
await handle.pressSequentially('bo') | ||
await dialog.getByRole('option', { name: 'Bob @bob.test' }).click() | ||
await expect(handle).toHaveValue('bob.test') | ||
await expect(dialog.getByRole('listbox')).toBeHidden() | ||
await dialog.getByRole('textbox', { name: 'A note, if you like' }).fill('Come walk with us.') | ||
await dialog.getByRole('button', { name: 'Send invite' }).click() | ||
await expect(page.locator('.invites', { hasText: who.bob.did })).toBeVisible() | ||
| @@ -384,7 +417,7 @@ test('moderating: a label, and the queue', async () => { | ||
const ejected = page.locator('.subject', { hasText: who.bob.did }) | ||
await expect(ejected.locator('.status')).toHaveText('open') | ||
await expect(ejected.locator('.events')).toContainText('eject') | ||
await page.getByRole('textbox', { name: 'Their handle' }).fill('bob.test') | ||
await page.getByRole('combobox', { name: 'Their handle' }).fill('bob.test') | ||
await page.getByRole('textbox', { name: 'Label' }).fill('troll') | ||
await page.getByRole('button', { name: 'Label them' }).click() | ||
const subject = page.locator('.subject', { hasText: who.bob.did }) | ||
| @@ -468,8 +501,9 @@ test('coming to a request that has lapsed', async () => { | ||
test('moving an account here to become a group', async () => { | ||
await page.goto(url('/new')) | ||
await page.getByRole('link', { name: 'move an account you already have' }).click() | ||
await page.getByRole('textbox', { name: 'The account to move' }).fill(who.harbor.handle) | ||
await page.getByRole('button', { name: 'Sign in to it' }).click() | ||
const moving = page.getByRole('combobox', { name: 'The account to move' }) | ||
await moving.fill(who.harbor.handle) | ||
await moving.press('Enter') | ||
// At its own server, as itself; then back, still alice. | ||
await approve(page, 'harbor') | ||
await page.waitForURL(url('/move')) | ||
harness/e2e/pages.mjs+19โ3
| @@ -35,18 +35,34 @@ export const joins = async (name, handle) => { | ||
expect(said.status, JSON.stringify(said)).toBe('admitted') | ||
} | ||
| ||
/// Stands in for typeahead.waow.tech in the browser `context`, which | ||
/// cannot know the reference PDS's accounts: the world's accounts whose | ||
/// handle holds what is typed, named for themselves. | ||
export const suggests = (context) => | ||
context.route('https://typeahead.waow.tech/xrpc/tech.waow.typeahead.searchActors?*', (route) => { | ||
const asked = new URL(route.request().url()).searchParams | ||
const q = asked.get('q').toLowerCase() | ||
const actors = world.accounts | ||
.filter((a) => a.handle.includes(q)) | ||
.slice(0, Number(asked.get('limit'))) | ||
.map((a) => ({ did: a.did, handle: a.handle, displayName: a.handle[0].toUpperCase() + a.handle.split('.')[0].slice(1) })) | ||
return route.fulfill({ json: { actors }, headers: { 'access-control-allow-origin': '*' } }) | ||
}) | ||
| ||
/// The sign-in dialog, wherever it was opened from. | ||
export const signInDialog = (page) => page.getByRole('dialog', { name: 'Sign in with your Atmosphere account' }) | ||
| ||
/// Signs in to the console as `name` from the dialog, opened by the | ||
/// header's button or by `from`: their handle, then their PDS's page, | ||
/// which sends the browser back. | ||
/// which sends the browser back. Enter sends it as typed, under any | ||
/// accounts the field suggests. | ||
export const signIn = async (page, name, from = page.getByRole('banner').getByRole('button', { name: 'Sign in' })) => { | ||
const back = page.url() | ||
await from.click() | ||
const dialog = signInDialog(page) | ||
await dialog.getByRole('textbox', { name: 'Handle' }).fill(who[name].handle) | ||
await dialog.getByRole('button', { name: 'Sign in' }).click() | ||
const handle = dialog.getByRole('combobox', { name: 'Handle' }) | ||
await handle.fill(who[name].handle) | ||
await handle.press('Enter') | ||
await approve(page, name) | ||
await page.waitForURL(back) | ||
} | ||
web/src/lib/molecules/HandleInput.sveltenew+102โ0
| @@ -0,0 +1,102 @@ | ||
<script lang="ts"> | ||
import Avatar from '#lib/atoms/Avatar.svelte'; | ||
import { type Actor, findActors } from '#lib/typeahead.ts'; | ||
/** | ||
* A field for someone's handle, labelled `label`, with whoever on the | ||
* Atmosphere matches what is typed so far dropping down over what is | ||
* under it. Picking one fills it in, then calls `onpick` with the field | ||
* if one is given, to go on as the form's button would. One's own handle, | ||
* to sign in with, is `autocomplete` username; anyone else's is off, so | ||
* the browser offers no saved sign-in for it. | ||
*/ | ||
let { | ||
id, | ||
label, | ||
name = 'handle', | ||
placeholder, | ||
autocomplete = 'off', | ||
value = $bindable(''), | ||
onpick | ||
}: { id: string; label: string; name?: string; placeholder?: string; autocomplete?: 'username' | 'off'; value?: string; onpick?: (field: HTMLInputElement) => void } = $props(); | ||
let field = $state<HTMLInputElement>(); | ||
let found = $state<Actor[]>([]); | ||
let open = $state(false); | ||
// Which one Enter picks: none till the arrows choose one, so Enter sends what is typed. | ||
let at = $state(-1); | ||
const list = $derived(`${id}-list`); | ||
// Looked up a moment after typing stops; a lookup overtaken by more typing is let go. | ||
$effect(() => { | ||
const q = value; | ||
const ask = new AbortController(); | ||
const wait = setTimeout(() => { | ||
findActors(q, ask.signal) | ||
.then((a) => { | ||
found = a; | ||
at = -1; | ||
}) | ||
.catch(() => {}); | ||
}, 150); | ||
return () => { | ||
clearTimeout(wait); | ||
ask.abort(); | ||
}; | ||
}); | ||
const showing = $derived(open && found.length > 0); | ||
function pick(a: Actor) { | ||
value = a.handle; | ||
close(); | ||
if (!field) return; | ||
// The field holds it now, not only once the page is next drawn, so a form sent at once sends it. | ||
field.value = a.handle; | ||
onpick?.(field); | ||
} | ||
function press(e: MouseEvent, a: Actor) { | ||
// Picked on the press, before the field loses focus and the list closes. | ||
e.preventDefault(); | ||
pick(a); | ||
} | ||
// What the arrows chose was among what matched before; typing or closing lets it go. | ||
function typed() { | ||
open = true; | ||
at = -1; | ||
} | ||
function close() { | ||
open = false; | ||
at = -1; | ||
} | ||
function keydown(e: KeyboardEvent) { | ||
if (!showing) return; | ||
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { | ||
e.preventDefault(); | ||
const n = found.length; | ||
at = e.key === 'ArrowDown' ? (at + 1) % n : (at - 1 + n) % n; | ||
} else if (e.key === 'Enter' && found[at]) { | ||
e.preventDefault(); | ||
pick(found[at]); | ||
} else if (e.key === 'Escape') { | ||
// Closes the list, not the dialog around it. | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
close(); | ||
} | ||
} | ||
</script> | ||
| ||
<div class="field handle-input"> | ||
<label for={id}>{label}</label> | ||
<input bind:this={field} {id} {name} {placeholder} {autocomplete} autocapitalize="none" spellcheck="false" required role="combobox" aria-expanded={showing} aria-controls={list} aria-autocomplete="list" aria-activedescendant={showing && at >= 0 ? `${list}-${at}` : undefined} bind:value oninput={typed} onfocus={() => (open = true)} onblur={close} onkeydown={keydown} /> | ||
{#if showing} | ||
<!-- Over what is under the field, rather than pushing it down. --> | ||
<div id={list} class="handle-list" role="listbox" aria-label="Accounts"> | ||
{#each found as a, i (a.did)} | ||
<div id="{list}-{i}" class="handle-option" role="option" tabindex="-1" aria-selected={i === at} onmousedown={(e) => press(e, a)} onmouseenter={() => (at = i)}> | ||
<Avatar src={a.avatar} /> | ||
<span class="who"> | ||
{#if a.displayName}<strong>{a.displayName}</strong>{/if} | ||
<span class="mute">@{a.handle}</span> | ||
</span> | ||
</div> | ||
{/each} | ||
</div> | ||
{/if} | ||
</div> | ||
web/src/lib/molecules/MoveSignIn.svelte+2โ4
| @@ -1,4 +1,5 @@ | ||
<script lang="ts"> | ||
import HandleInput from '#lib/molecules/HandleInput.svelte'; | ||
/** | ||
* Signing in to an account to move here, at its own server: its handle, | ||
* sent to the group host's `/console/move/login`, which goes there and | ||
| @@ -9,10 +10,7 @@ | ||
| ||
<form class="stack" method="get" action="/console/move/login"> | ||
<input type="hidden" name="back" value={back} /> | ||
<label class="field"> | ||
The account to move | ||
<input name="handle" required autocomplete="off" placeholder="riders.bsky.social" /> | ||
</label> | ||
<HandleInput id="move-handle" label="The account to move" placeholder="riders.bsky.social" /> | ||
<p>Its server asks you to sign in to it, and to let this host move it.</p> | ||
<button type="submit">Sign in to it</button> | ||
</form> | ||
web/src/lib/organisms/InviteDialog.svelte+2โ1
| @@ -1,5 +1,6 @@ | ||
<script lang="ts"> | ||
import Dialog from '#lib/molecules/Dialog.svelte'; | ||
import HandleInput from '#lib/molecules/HandleInput.svelte'; | ||
/** Inviting someone to the group, by their handle, with a note if one likes: opened by a button whose `popovertarget` is `id`. */ | ||
let { id, name }: { id: string; name: string } = $props(); | ||
</script> | ||
| @@ -7,7 +8,7 @@ | ||
<Dialog {id} title={`Invite someone to ${name}`}> | ||
<p class="mute">They'll see it the next time they sign in, here or in any app that shows invites.</p> | ||
<form class="stack" method="post" action="?/invite"> | ||
<label class="field">Their handle <input name="handle" required autocomplete="off" placeholder="someone.bsky.social" /></label> | ||
<HandleInput id="{id}-handle" label="Their handle" placeholder="someone.bsky.social" /> | ||
<label class="field">A note, if you like <textarea name="message" rows="3" maxlength="2000"></textarea></label> | ||
<div class="actions"> | ||
<button class="quiet" type="button" popovertarget={id} popovertargetaction="hide">Cancel</button> | ||
web/src/lib/organisms/LabelForm.svelte+2โ1
| @@ -1,9 +1,10 @@ | ||
<script lang="ts"> | ||
import HandleInput from '#lib/molecules/HandleInput.svelte'; | ||
/** Labelling someone, by their handle, as the group, with a value. */ | ||
</script> | ||
| ||
<form class="stack" method="post" action="?/label"> | ||
<label class="field">Their handle <input name="handle" required /></label> | ||
<HandleInput id="label-handle" label="Their handle" /> | ||
<label class="field">Label <input name="val" required maxlength="128" placeholder="spam" /></label> | ||
<button type="submit">Label them</button> | ||
</form> | ||
web/src/lib/organisms/SignInDialog.svelte+3โ4
| @@ -1,10 +1,12 @@ | ||
<script lang="ts"> | ||
import Dialog from '#lib/molecules/Dialog.svelte'; | ||
import HandleInput from '#lib/molecules/HandleInput.svelte'; | ||
import { SIGN_IN } from '#lib/signIn.ts'; | ||
/** | ||
* Signing in with an Atmosphere account, as oneself: the handle, sent to | ||
* the group host's `/console/login`, which goes to the account's own | ||
* server to approve it and back to `back`, the page it was opened on. | ||
* Picking an account the field suggests signs in as it at once. | ||
*/ | ||
let { back = '/' }: { back?: string } = $props(); | ||
// Apps on the Atmosphere, any of whose accounts signs in here: a row | ||
| @@ -22,10 +24,7 @@ | ||
<p class="what"><a href="https://atmosphereaccount.com/" target="_blank" rel="noopener noreferrer">What is an Atmosphere account?</a></p> | ||
<form class="stack" method="get" action="/console/login"> | ||
<input type="hidden" name="back" value={back} /> | ||
<label class="field"> | ||
Handle | ||
<input name="handle" required autocomplete="username" autocapitalize="none" spellcheck="false" placeholder="you.bsky.social" /> | ||
</label> | ||
<HandleInput id="sign-in-handle" label="Handle" autocomplete="username" placeholder="you.bsky.social" onpick={(f) => f.form?.requestSubmit()} /> | ||
<div class="actions"> | ||
<button class="quiet" type="button" popovertarget={SIGN_IN} popovertargetaction="hide">Cancel</button> | ||
<button type="submit">Sign in</button> | ||
web/src/lib/typeahead.tsnew+18โ0
| @@ -0,0 +1,18 @@ | ||
// Finding an account by what has been typed of its handle or name, from the | ||
// typeahead service typeahead.waow.tech, which knows everyone on the | ||
// Atmosphere. What is typed goes to it; the console's CSP lets it. | ||
| ||
/** An account the typeahead service matched. */ | ||
export type Actor = { did: string; handle: string; displayName?: string; avatar?: string }; | ||
| ||
const SEARCH = 'https://typeahead.waow.tech/xrpc/tech.waow.typeahead.searchActors'; | ||
| ||
/** Who matches what is typed, best first: none for under two letters, or when the service cannot say. */ | ||
export async function findActors(q: string, signal?: AbortSignal, limit = 6): Promise<Actor[]> { | ||
const text = q.trim().replace(/^@/, ''); | ||
if (text.length < 2) return []; | ||
const res = await fetch(`${SEARCH}?q=${encodeURIComponent(text)}&limit=${limit}`, { signal }).catch(() => null); | ||
if (!res?.ok) return []; | ||
const body = (await res.json().catch(() => null)) as { actors?: Actor[] } | null; | ||
return (body?.actors ?? []).filter((a) => a.handle && a.handle !== 'handle.invalid'); | ||
} | ||
web/src/app.css+11โ0
| @@ -126,6 +126,17 @@ ol > li::marker{color:var(--primary);font-weight:var(--weight-bold);font-family: | ||
.group-row a{color:var(--fg);font:var(--weight-bold) var(--text-lg) var(--font-round);text-decoration:none} | ||
.group-row a:hover{color:var(--primary)} | ||
.standing form{display:flex;align-items:center;gap:var(--space-3);flex-wrap:wrap} | ||
/* A field for someone's handle, the accounts matching what is typed | ||
dropping down over what is under it, as a menu does; in a dialog, past | ||
its edge. */ | ||
.handle-input{position:relative} | ||
.dialog:has(.handle-input){overflow:visible} | ||
.handle-list{position:absolute;z-index:1;top:calc(100% + var(--space-1));left:0;right:0;padding:var(--space-1);background:var(--panel);border:var(--border);border-radius:var(--radius-md);box-shadow:var(--shadow-pop);font-weight:var(--weight-normal)} | ||
.handle-option{display:flex;align-items:center;gap:var(--space-3);padding:var(--space-2) var(--space-3);border-radius:var(--radius-sm);cursor:pointer} | ||
.handle-option[aria-selected='true']{background:var(--soft)} | ||
.handle-option .av{width:var(--avatar-pick);height:var(--avatar-pick)} | ||
.handle-option .who{display:flex;flex-direction:column;min-width:0;font-size:var(--text-sm);line-height:var(--leading-tight);gap:var(--space-1)} | ||
.handle-option .who > *{overflow:hidden;text-overflow:ellipsis;white-space:nowrap} | ||
| ||
/* โโ Templates โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ */ | ||
| ||