import m, {MithrilComponent} from '@garage44/mithril' import {Alert, Button, Card, FieldCheckbox, FieldRadio, FieldText} from '@garage44/mithril/components' import type {Identity, Offer, SigninTicket, TicketSession} from '@/types' import {$s, notifier} from '@/app' import {ProviderLogo} from '@/components/elements/providers/providers' import { adoptTicketSession, appFromTicket, communityFromTicket, createFromTicket, joinFromTicket, linkFromTicket, readTicket, sentence, } from '@/lib/auth' import {ADMIN, COMMUNITY} from '@/lib/base' import {bind, local} from '@/lib/reactive' import {createLabel, safeReturn, welcomeQuery} from '@/lib/signin' /** Where "Back to Cartopolis" goes when the app asked: the app link Android claims. */ const APP_LINK = 'https://cartopolis.org/go' /** How long a short confirmation stays before the page moves on. */ const SETTLE_MS = 1200 /** "Discord server", or the group's own name when it differs from the community's. */ function groupWords(offer: Offer): string { const kind = offer.provider === 'reddit' ? 'Subreddit' : `${offer.provider_name} server` return offer.group_name && offer.group_name !== offer.name ? `${kind} ${offer.group_name}` : kind } /** Which communities are ticked, as a list the form fields can bind to. */ interface Pick { join: boolean offer: Offer } /** * `/welcome`: where every round trip to a provider ends * (`docs/mockups/external-sign-in/`). * * The server sends the browser here with `?ticket=…` whatever happened, and * the ticket says what that was — signed in, a name to pick, a password to * link with, a test that passed, groups to link — so this page is a switch * over the ticket's kind and nothing more. The ticket is read once, in * `oninit` (a second read after it was spent would turn a sign-in into an * error), and taken out of the address bar: for a sign-in it is as good as a * session for the ten minutes it lives. */ export class Welcome extends MithrilComponent { s = local({ busy: false, /** * An app asked for this sign-in: it is signed in only once the code it * shows is typed here (`appView`). Where to go if the person did not. */ appAsk: null as string | null, /** What the person typed for the app's code. */ appCode: '', /** The app the person confirmed is signed in. */ appDone: false, /** A short "done" line before moving on. */ done: '', /** A refusal from the last step, shown in the card. */ error: '', group: '', name: '', password: '', picks: [] as Pick[], /** The server's sentence when there is no ticket to read. */ problem: '', signedInAs: '', ticket: '', what: null as SigninTicket | null, }) oninit() { const q = welcomeQuery(location.search) history.replaceState(null, '', '/welcome') if (!q) { this.s.problem = 'There is nothing to finish here. Start again from the sign-in page.' return } if ('error' in q) { this.s.problem = q.error return } this.s.ticket = q.ticket void this.read(q.ticket) } async read(ticket: string) { const s = this.s s.busy = true try { const what = await readTicket(ticket) s.what = what await this.arrive(what) } catch (e) { s.problem = sentence(e, 'Could not finish signing in') } finally { s.busy = false } } /** What happens as soon as the ticket is known, before anything is drawn for it. */ async arrive(what: SigninTicket) { const s = this.s switch (what.kind) { case 'signed_in': await adoptTicketSession(what) s.signedInAs = what.username if (what.app) { s.appAsk = what.return } else if (what.offers.length) { s.picks = what.offers.map((offer) => ({join: true, offer})) } else { m.route.set(safeReturn(what.return)) } break case 'choose_name': s.name = what.suggestion s.picks = what.offers.map((offer) => ({join: true, offer})) break case 'linked': this.settle(`${what.provider_name} is now a way into your account.`, what.return) break case 'reauthed': this.settle(`Confirmed with ${what.provider_name}.`, what.return) break case 'community_groups': s.group = what.groups[0]?.group_id ?? '' break } } /** Say it briefly, then go back to where the person was. */ settle(done: string, ret: string) { this.s.done = done setTimeout(() => m.route.set(safeReturn(ret)), SETTLE_MS) } /** A session from create or link: kept, and then the app's message or the way back. */ async signedIn(session: TicketSession, ret: string | undefined) { await adoptTicketSession(session) this.s.signedInAs = session.username if (session.app) { this.s.appAsk = ret ?? '/' } else { m.route.set(safeReturn(ret)) } } joining(): string[] { return this.s.picks.filter((p) => p.join).map((p) => p.offer.community) } create = async (e: Event) => { e.preventDefault() const s = this.s const what = s.what if (s.busy || what?.kind !== 'choose_name' || !s.name.trim()) return s.busy = true s.error = '' try { await this.signedIn(await createFromTicket(s.ticket, s.name.trim(), this.joining()), what.return) } catch (err) { // Inline, under the name: "that name is taken" is about the field // the person is looking at. s.error = sentence(err, 'Could not make that account') } finally { s.busy = false } } link = async (e: Event) => { e.preventDefault() const s = this.s const what = s.what if (s.busy || what?.kind !== 'link_existing' || !s.password) return s.busy = true s.error = '' try { await this.signedIn(await linkFromTicket(s.ticket, s.password), what.return) } catch (err) { s.error = sentence(err, 'Could not link that account') } finally { s.password = '' s.busy = false } } join = async (ret: string) => { const s = this.s const join = this.joining() if (join.length) { s.busy = true const joined = await joinFromTicket(s.ticket, join) s.busy = false if (!joined) return notifier.success(joined.length === 1 ? 'You joined 1 community' : `You joined ${joined.length} communities`) } m.route.set(safeReturn(ret)) } linkGroup = async (community: {id: string; name: string}) => { const s = this.s if (s.busy || !s.group) return s.busy = true s.error = '' try { const link = await communityFromTicket(s.ticket, s.group) notifier.success(`${link.group_name} is linked to ${community.name}`) m.route.set(COMMUNITY, {show: community.id}) } catch (err) { s.error = sentence(err, 'Could not link that group') } finally { s.busy = false } } view() { const s = this.s if (s.problem) return this.problemView(s.problem) if (s.appAsk !== null) return this.appView(s.appAsk) if (s.done) { return (
One moment…
Only groups with a Cartopolis community are shown. We don't keep the list of your other groups.