~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

A group's settings set where it meets, its topics and its languages

Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:web/profile-fields
harness/e2e/flows.spec.mjs+21−4
@@ -344,11 +344,25 @@ test("changing a group's profile", async () => {
await page.goto(url('/g/hikers.groups.test'))
await page.getByRole('link', { name: 'Settings' }).click()
await page.getByRole('textbox', { name: 'Name', exact: true }).fill('Hikers of the Hills')
// Where it meets: a town asks for its country.
await page.getByRole('textbox', { name: 'Town or city it meets in' }).fill('Kraków')
await expect(page.getByRole('combobox', { name: 'Country' })).toHaveAttribute('required', '')
await page.getByRole('combobox', { name: 'Country' }).selectOption({ label: 'Poland' })
await page.getByRole('checkbox', { name: 'It meets online' }).check()
await page.getByRole('textbox', { name: 'Topics' }).fill('hiking, , ridge walks, hiking')
// Its languages, each added from a list and taken off by its ×.
const add = page.getByRole('combobox', { name: 'Add a language' })
for (const language of ['Polish', 'English', 'German']) await add.selectOption({ label: language })
await page.getByRole('button', { name: 'Remove German' }).click()
const saved = page.waitForResponse((r) => r.request().method() === 'POST' && r.url().includes('/profile'))
await page.locator('form[action="?/profile"]').getByRole('button', { name: 'Save' }).click()
await saved
// Drawn again as saved: each topic once, none blank.
await expect(page.getByRole('textbox', { name: 'Topics' })).toHaveValue('hiking, ridge walks')
await expect(page.getByRole('combobox', { name: 'Country' })).toHaveValue('PL')
await expect(page.getByRole('button', { name: /^Remove / })).toHaveText(['×', '×'])
// Its links, each added from a dialog: with what it is, or without.
const add = async (address, title) => {
const addLink = async (address, title) => {
await page.getByRole('button', { name: 'Add a link' }).click()
const dialog = page.getByRole('dialog', { name: 'Add a link' })
await dialog.getByRole('textbox', { name: 'Its address' }).fill(address)
@@ -356,14 +370,17 @@ test("changing a group's profile", async () => {
await dialog.getByRole('button', { name: 'Add', exact: true }).click()
await expect(page.locator('.link-list', { hasText: address })).toBeVisible()
}
await add('https://chat.hikers.example', 'Our chat')
await add('https://walks.example/')
await add('https://gone.example/')
await addLink('https://chat.hikers.example', 'Our chat')
await addLink('https://walks.example/')
await addLink('https://gone.example/')
await page.getByRole('button', { name: 'Remove gone.example' }).click()
await expect(page.locator('.link-list', { hasText: 'gone.example' })).toHaveCount(0)
await page.goto(url('/g/hikers.groups.test'))
await expect(page.getByRole('heading', { name: 'Hikers of the Hills' })).toBeVisible()
await expect(page.getByText('Walks on Sundays.')).toBeVisible()
// Where it meets, its languages and what it is about, under its words.
await expect(page.locator('.facts')).toHaveText('Meets in Kraków, Poland, and online. Speaks Polish and English.')
await expect(page.locator('.group-card .tag')).toHaveText(['hiking', 'ridge walks'])
// Its links, in a row: by title, or by address.
const links = page.getByRole('navigation', { name: "Hikers of the Hills's links" })
await expect(links.getByRole('link', { name: 'Our chat' })).toHaveAttribute('href', 'https://chat.hikers.example')
harness/look/seeds/full.mjs+8−4
@@ -1,7 +1,7 @@
// A group in full: Lantern Hikers (lanterns.groups.test), alice's, with a
// banner and a picture, words, links, two rules and a moderator role;
// bob, carol (its moderator), dan, erin and frank in it, more than a page
// of members; harbor asking to join with a note; and an invite waiting
// banner and a picture, words, a place, topics, languages, links, two rules
// and a moderator role; bob, carol (its moderator), dan, erin and frank in
// it, more than a page of members; harbor asking to join with a note; and an invite waiting
// for maximilianaquinces.
export default async ({ world, opensocial, ask, page, picture, who, exists }) => {
@@ -9,7 +9,11 @@ export default async ({ world, opensocial, ask, page, picture, who, exists }) =>
if (!(await exists(g))) opensocial('alice', 'group', 'seed', g)
const links = [{ url: 'https://chat.lanterns.example', title: 'Our chat' }, { url: 'https://maps.lanterns.example/routes' }]
const about = 'Evening walks on the ridge trails, lanterns lit, every Thursday from April to October.'
opensocial('alice', 'profile', 'set', g, '--display-name', 'Lantern Hikers', '--description', about, '--links', JSON.stringify(links))
const where = { $type: 'community.lexicon.location.address', locality: 'Ljubljana', country: 'SI' }
const topics = ['night hikes', 'ridge trails', 'lanterns'].map((topic) => ({ topic }))
const languages = ['sl', 'en'].map((language) => ({ language }))
opensocial('alice', 'profile', 'set', g, '--display-name', 'Lantern Hikers', '--description', about, '--links', JSON.stringify(links),
'--location', JSON.stringify(where), '--online', '--topics', JSON.stringify(topics), '--languages', JSON.stringify(languages))
for (const name of ['bob', 'carol', 'dan', 'erin', 'frank']) {
await ask(name, g)
opensocial('alice', 'member', 'assign', g, who[name].handle, name === 'carol' ? 'member,moderator' : 'member')
web/src/lib/molecules/Facts.sveltenew+24−0
@@ -0,0 +1,24 @@
<script lang="ts">
import Tag from '#lib/atoms/Tag.svelte';
import { languageName, placeName, type Place } from '#lib/places.ts';
/**
* What a group says of itself in a few words: where it meets, whether
* online, the languages it uses, and what it is about, each a tag.
* Nothing, where it says none of them.
*/
let {
location,
online = false,
languages = [],
topics = []
}: { location?: Place; online?: boolean; languages?: { language: string }[]; topics?: { topic: string }[] } = $props();
const place = $derived(placeName(location));
const where = $derived(place ? `Meets in ${place}${online ? ', and online' : ''}.` : online ? 'Meets online.' : '');
const spoken = $derived(new Intl.ListFormat('en', { type: 'conjunction' }).format(languages.map((l) => languageName(l.language))));
const said = $derived([where, spoken && `Speaks ${spoken}.`].filter(Boolean).join(' '));
</script>
{#if said}<p class="facts">{said}</p>{/if}
{#if topics.length}
<div class="tags topics">{#each topics as t (t.topic)}<Tag text={t.topic} />{/each}</div>
{/if}
web/src/lib/organisms/GroupCard.svelte+14−3
@@ -1,14 +1,16 @@
<script lang="ts">
import type { Snippet } from 'svelte';
import Avatar from '#lib/atoms/Avatar.svelte';
import Facts from '#lib/molecules/Facts.svelte';
import Links from '#lib/molecules/Links.svelte';
import type { Place } from '#lib/places.ts';
import Rules from '#lib/molecules/Rules.svelte';
/**
* A group as its profile shows it: its banner, when it has one; its
* picture (over the banner's foot), its name and handle beside it, and
* `nav`, the ways to its other pages, across from them; its words, its
* links and its rules; then `children`, where the person looking stands
* with it. With no `name`, its profile is not theirs to see, and it is
* `nav`, the ways to its other pages, across from them; its words;
* where it meets, its languages and topics; its links and its rules;
* then `children`, where the person looking stands with it. With no `name`, its profile is not theirs to see, and it is
* its handle alone.
*/
let {
@@ -17,6 +19,10 @@
description,
avatar,
banner,
location,
online,
languages,
topics,
links = [],
rules = [],
nav,
@@ -27,6 +33,10 @@
description?: string;
avatar?: string | null;
banner?: string | null;
location?: Place;
online?: boolean;
languages?: { language: string }[];
topics?: { topic: string }[];
links?: { url: string; title?: string }[];
rules?: { rkey: string; title: string; text?: string }[];
nav?: Snippet;
@@ -47,6 +57,7 @@
{@render nav?.()}
</div>
{#if description}<p class="mute description">{description}</p>{/if}
<Facts {location} {online} {languages} {topics} />
<Links {name} {links} />
<Rules {rules} />
{:else}
web/src/lib/organisms/ProfileForm.svelte+72−4
@@ -1,20 +1,88 @@
<script lang="ts">
import { ADDRESS, COUNTRIES, LANGUAGES, languageName, type Place } from '#lib/places.ts';
/**
* A group's profile, to change: its name, its words, how people join,
* and its own site. Its links are a list of their own (LinksEditor).
* A group's profile, to change: its name, its words, where it meets
* and whether online, what it is about, the languages it uses, how
* people join, and its own site. Its links are a list of their own
* (LinksEditor).
*/
let {
displayName,
description,
joinPolicy,
url
}: { displayName?: string; description?: string; joinPolicy?: string; url?: string } = $props();
url,
location,
online = false,
topics = [],
languages = []
}: {
displayName?: string;
description?: string;
joinPolicy?: string;
url?: string;
location?: Place;
online?: boolean;
topics?: { topic: string }[];
languages?: { language: string }[];
} = $props();
const policy = $derived(joinPolicy ?? 'approval');
// A place other than an address (a point on a map) is kept as it is
// until a town or country is given in its place.
const address = $derived(location?.$type === ADDRESS ? location : undefined);
// The town as typed, so the country is asked for once there is one.
let town = $state('');
$effect.pre(() => {
town = address?.locality ?? '';
});
// The languages chosen, kept here as they are added and taken off.
let spoken = $state<string[]>([]);
$effect.pre(() => {
spoken = languages.map((l) => l.language);
});
const left = $derived(LANGUAGES.filter((l) => !spoken.includes(l.tag)));
function add(event: Event & { currentTarget: HTMLSelectElement }) {
const tag = event.currentTarget.value;
if (tag) spoken = [...spoken, tag];
event.currentTarget.value = '';
}
</script>
<form class="stack" method="post" action="?/profile">
<label class="field">Name <input name="displayName" required maxlength="64" value={displayName ?? ''} /></label>
<label class="field">What it is for <textarea name="description" maxlength="2000">{description ?? ''}</textarea></label>
<div class="pair">
<label class="field">Town or city it meets in <input name="town" maxlength="128" bind:value={town} /></label>
<label class="field">
Country
<select name="country" value={address?.country ?? ''} required={town.trim() !== ''}>
<option value="">{town.trim() ? 'Choose a country' : 'No country'}</option>
{#each COUNTRIES as c (c.code)}<option value={c.code}>{c.name}</option>{/each}
</select>
</label>
</div>
<input type="hidden" name="online" value="" />
<label><input type="checkbox" name="online" checked={online} /> It meets online</label>
<label class="field">
Topics
<input name="topics" maxlength="700" value={topics.map((t) => t.topic).join(', ')} aria-describedby="topics-hint" />
<span class="mute small" id="topics-hint">Separate up to ten with commas, like climbing, choir or renters' union.</span>
</label>
<div class="field" role="group" aria-labelledby="languages-label">
<span id="languages-label">Languages it uses</span>
<input type="hidden" name="language" value="" />
<div class="choices">
{#each spoken as tag (tag)}
<input type="hidden" name="language" value={tag} />
<span class="chip">{languageName(tag)}<button class="quiet" type="button" aria-label={`Remove ${languageName(tag)}`} onclick={() => (spoken = spoken.filter((t) => t !== tag))}>×</button></span>
{/each}
{#if spoken.length < 5}
<select aria-label="Add a language" onchange={add}>
<option value="">Add a language</option>
{#each left as l (l.tag)}<option value={l.tag}>{l.name}</option>{/each}
</select>
{/if}
</div>
</div>
<label class="field">
How people join
<select name="joinPolicy" value={policy}>
web/src/lib/organisms/RolesEditor.svelte+1−1
@@ -33,7 +33,7 @@
</ul>
<FormDialog id="define-role" title="Create a role" action="?/role" submit="Create" wide>
{#snippet intro()}Choose what people with this role can do, and which roles they can give. You give roles to members from the group's page.{/snippet}
<div class="link-rows">
<div class="pair">
<label class="field">Its key <input name="role" required pattern="[a-z0-9-]+" maxlength="64" placeholder="moderator" /></label>
<label class="field">Its name <input name="name" required maxlength="64" placeholder="Moderator" /></label>
</div>
web/src/lib/server/settings.ts+44−1
@@ -11,6 +11,7 @@ import { fail, type Action } from '@sveltejs/kit';
import { act, xrpc, type Answer } from '#lib/data.ts';
import { applyAsGroup, membersOf, withGroup, type Done, type GroupSession, type Write } from '#lib/asGroup.ts';
import { SCOPES } from '#lib/actions.ts';
import { ADDRESS, type Place } from '#lib/places.ts';
type Handle = `${string}.${string}`;
type Event = { fetch: typeof fetch; params: { handle?: string } };
@@ -69,8 +70,27 @@ const text = (form: FormData, key: string) => String(form.get(key) ?? '').trim()
const PROFILE = 'fyi.opensocial.profile';
const RULE = 'fyi.opensocial.rule';
/** How many links a profile holds, as its lexicon says. */
/** How many links, topics and languages a profile holds, and how long a topic is, as its lexicon says. */
const MAX_LINKS = 5;
const MAX_TOPICS = 10;
const MAX_LANGUAGES = 5;
const TOPIC_AT_MOST = 64;
const segments = new Intl.Segmenter();
/**
* Where the form says the group meets, as an address: its town and
* country, over what else the address it had says; none, with neither; a
* place of another kind kept, while the form gives neither in its place;
* or why not, a town with no country.
*/
function whereFrom(form: FormData, was?: Place): Place | string | undefined {
const town = text(form, 'town');
const country = text(form, 'country');
if (town && !country) return 'Choose the country the town is in.';
if (!country) return was && was.$type !== ADDRESS ? was : undefined;
const { locality: _, ...kept } = was?.$type === ADDRESS ? was : {};
return { ...kept, $type: ADDRESS, country, ...(town ? { locality: town } : {}) };
}
type Did = `did:${string}:${string}`;
type Bindings = Answer<typeof getGroup.main>['bindings'];
@@ -168,6 +188,29 @@ const all = {
if (text(form, k)) profile[k] = text(form, k);
else delete profile[k];
}
if (form.has('country')) {
const place = whereFrom(form, now.location as Place | undefined);
if (typeof place === 'string') return fail(400, { failed: place });
if (place) profile.location = place;
else delete profile.location;
}
if (form.has('topics')) {
const topics = [...new Set(text(form, 'topics').split(',').map((t) => t.trim()).filter(Boolean))];
if (topics.length > MAX_TOPICS) return fail(400, { failed: `A group can list up to ${MAX_TOPICS} topics.` });
if (topics.some((t) => [...segments.segment(t)].length > TOPIC_AT_MOST)) return fail(400, { failed: `Keep each topic to ${TOPIC_AT_MOST} characters.` });
if (topics.length) profile.topics = topics.map((topic) => ({ topic }));
else delete profile.topics;
}
// Each sent with a blank first, so a form with none chosen still says so.
if (form.has('language')) {
const languages = [...new Set(form.getAll('language').map(String).filter(Boolean))].slice(0, MAX_LANGUAGES);
if (languages.length) profile.languages = languages.map((language) => ({ language }));
else delete profile.languages;
}
if (form.has('online')) {
if (form.getAll('online').includes('on')) profile.online = true;
else delete profile.online;
}
return putProfile(session, profile);
});
},
web/src/lib/places.tsnew+62−0
@@ -0,0 +1,62 @@
// Where a group meets and what it speaks, as a profile holds them (a
// country's ISO 3166 code, a language's BCP 47 tag), named for a reader.
/** Every country by its ISO 3166-1 alpha-2 code. */
const COUNTRY_CODES = (
'AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ ' +
'CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO FR ' +
'GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE JM JO JP ' +
'KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO MP MQ MR MS ' +
'MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW PY QA RE RO RS ' +
'RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM TN TO TR TT TV TW ' +
'TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW'
).split(' ');
/** The languages offered to choose from, by their BCP 47 tags. */
const LANGUAGE_TAGS = (
'ar bn ca cs cy da de el en eo es et eu fa fi fil fr ga gl gu he hi hr hu hy id is it ja ka kk ko lt lv mk ml mr ms ' +
'mt nb nl pa pl pt ro ru sk sl sq sr sv sw ta te th tr uk ur uz vi yo zh zu'
).split(' ');
// Named in English on the server and in the browser alike, so a page
// drawn on one reads the same once the other takes it over.
const regions = new Intl.DisplayNames(['en'], { type: 'region' });
const languages = new Intl.DisplayNames(['en'], { type: 'language' });
const named = (names: Intl.DisplayNames, code: string) => {
try {
return names.of(code) ?? code;
} catch {
return code;
}
};
/** A country's name, by its code; the code itself where it names none. */
export const countryName = (code: string) => named(regions, code);
/** A language's name, by its tag; the tag itself where it names none. */
export const languageName = (tag: string) => named(languages, tag);
const byName = (a: { name: string }, b: { name: string }) => a.name.localeCompare(b.name);
/** Every country, by name. */
export const COUNTRIES = COUNTRY_CODES.map((code) => ({ code, name: countryName(code) })).sort(byName);
/** The languages offered, by name. */
export const LANGUAGES = LANGUAGE_TAGS.map((tag) => ({ tag, name: languageName(tag) })).sort(byName);
/** Where a profile says a group meets, as `community.lexicon.location` has it. */
export type Place = { $type?: string; locality?: string; region?: string; country?: string; name?: string };
export const ADDRESS = 'community.lexicon.location.address';
/**
* Where a group meets, in words: an address's town and country, or a
* place's own name; nothing for a place with neither, such as a point on
* a map.
*/
export function placeName(place?: Place) {
if (!place) return '';
if (place.$type === ADDRESS) return [place.locality, place.country && countryName(place.country)].filter(Boolean).join(', ');
return place.name ?? '';
}
web/src/routes/g/[handle]/settings/+page.svelte+1−1
@@ -13,7 +13,7 @@
{#if form?.failed}<p class="danger">{form.failed}</p>{/if}
<Section title="About it">
<ProfileForm displayName={g.displayName} description={g.description} joinPolicy={g.joinPolicy} url={g.url} />
<ProfileForm displayName={g.displayName} description={g.description} joinPolicy={g.joinPolicy} url={g.url} location={g.location} online={g.online} topics={g.topics} languages={g.languages} />
</Section>
<Section title="Links" count={g.links?.length ?? 0}>
{#snippet actions()}
web/src/routes/g/[handle]/+page.svelte+1−1
@@ -37,7 +37,7 @@
{#if form?.failed}<p class="danger">{form.failed}</p>{/if}
{/snippet}
{#snippet card()}
<GroupCard handle={g.handle} name={g.displayName} description={g.description} avatar={image(g.did, g.avatar)} banner={image(g.did, g.banner)} links={g.links} rules={g.rules}>
<GroupCard handle={g.handle} name={g.displayName} description={g.description} avatar={image(g.did, g.avatar)} banner={image(g.did, g.banner)} location={g.location} online={g.online} languages={g.languages} topics={g.topics} links={g.links} rules={g.rules}>
{#snippet nav()}
{#if member && (data.settings || may.includes('mod.read'))}
<nav class="group-nav" aria-label="The group">
web/src/app.css+12−2