chadtmiller.com/group-host
atmosphere group host
0
0
A site lets its visitors join, cancel a request and leave, in its own group
crates/group-host/src/sites.rs+8−2
| @@ -99,8 +99,14 @@ pub const READS: &[&str] = &[ | ||
/// The host's procedures a site's pages may ask, as whoever is looking and | ||
/// signed in: those that act for the visitor alone, and that the host | ||
/// checks as it checks the same from anywhere. Reporting what they see, | ||
/// or appealing what was done to them. | ||
pub const ACTS: &[&str] = &["fyi.opensocial.createReport"]; | ||
/// or appealing what was done to them; asking to join, taking that back, | ||
/// and leaving. The host's page asks each of the site's own group alone. | ||
pub const ACTS: &[&str] = &[ | ||
"fyi.opensocial.createReport", | ||
"fyi.opensocial.requestJoin", | ||
"fyi.opensocial.cancelJoinRequest", | ||
"fyi.opensocial.leaveGroup", | ||
]; | ||
| ||
/// The methods a site's `reads`, or its `acts`, name. | ||
fn named<'a>(site: &'a Value, key: &str) -> Vec<&'a str> { | ||
crates/group-host/tests/sites.rs+16−5
| @@ -694,9 +694,17 @@ async fn a_site_acts_as_its_visitor_only_as_the_host_lets_a_page() { | ||
"{message}" | ||
); | ||
| ||
// Reporting, though it reads and writes nothing, has its pages served | ||
// in the page that holds a session, which is told what they may do. | ||
let acts = json!([{"method": format!("{NS}.createReport")}]); | ||
// Reporting, joining, cancelling a request and leaving, though they | ||
// read and write nothing, have its pages served in the page that | ||
// holds a session, which is told what they may do. | ||
let named = [ | ||
"createReport", | ||
"requestJoin", | ||
"cancelJoinRequest", | ||
"leaveGroup", | ||
]; | ||
let methods: Vec<String> = named.iter().map(|m| format!("{NS}.{m}")).collect(); | ||
let acts: Json = methods.iter().map(|m| json!({"method": m})).collect(); | ||
let said = call( | ||
&h, | ||
&alice, | ||
| @@ -707,17 +715,20 @@ async fn a_site_acts_as_its_visitor_only_as_the_host_lets_a_page() { | ||
assert_eq!(said.0, 200, "{}", said.1); | ||
let shell = page(&h, "/").await; | ||
let (_, config) = shell_of(&shell); | ||
assert_eq!(config["acts"], json!([format!("{NS}.createReport")])); | ||
assert_eq!(config["acts"], json!(methods)); | ||
| ||
// Its OAuth client asks the host for that, as for a read. | ||
// Its OAuth client asks the host for each, as for a read. | ||
let client = page(&h, "/client-metadata.json").await; | ||
let doc: Json = serde_json::from_str(&client.body).unwrap(); | ||
let scope: Vec<&str> = doc["scope"].as_str().unwrap().split(' ').collect(); | ||
let aud = "aud=did%3Aweb%3Ahost.test%23opensocial"; | ||
let wanted = [ | ||
"atproto".to_string(), | ||
format!("rpc:{NS}.cancelJoinRequest?{aud}"), | ||
format!("rpc:{NS}.createReport?{aud}"), | ||
format!("rpc:{NS}.getViewer?{aud}"), | ||
format!("rpc:{NS}.leaveGroup?{aud}"), | ||
format!("rpc:{NS}.requestJoin?{aud}"), | ||
]; | ||
assert_eq!(scope, wanted); | ||
| ||
docs/design/sites.md+6−3
| @@ -36,8 +36,11 @@ call as the visitor who signs in on it. Those are | ||
`getSpaceBlob`, `getActor` and `getViewer`, and no others. | ||
| ||
It may name `acts`, up to 20 of the host's procedures its pages may call | ||
as that person, each acting for them alone. For now that's | ||
`fyi.opensocial.createReport` only. It may name `spaces`, up to 20 of | ||
as that person, each acting for them alone, and only in this group: the | ||
host's page sets each call's `group` itself. Those are | ||
`fyi.opensocial.createReport`, to report what they see, and | ||
`requestJoin`, `cancelJoinRequest` and `leaveGroup`, so a site can let | ||
people ask to join, cancel the request, and leave. It may name `spaces`, up to 20 of | ||
the group's spaces its pages may read whole as that person. Each is a | ||
type, such as `fyi.opensocial.siteSpace`, and may give a `key` to name | ||
one space of that type, such as `board`. Without a key, it's every space | ||
| @@ -308,7 +311,7 @@ gives each ask's parameters, answers and refusals, and an example page: | ||
| `os.group()` | `{did, handle, console}` of the group whose site it is, where `console` is the address of the host's console | | ||
| `os.me()` | `{signedIn, did, handle}` of the visitor | | ||
| `os.call(method, params)` | the method's answer, asked as the visitor. That's its JSON, or a `Blob` for a method that answers with a file (`getSpaceBlob`) | | ||
| `os.act(method, input)` | the procedure's answer, asked as the visitor with `input` as its JSON. For `createReport` that's its `id` and `createdAt` | | ||
| `os.act(method, input)` | the procedure's answer, asked as the visitor with `input` as its JSON, its `group` the site's own. For `createReport` that's its `id` and `createdAt`; for `requestJoin`, its `status` | | ||
| `os.space(uri)` | one of the group's spaces (`at://<group>/space/<type>/<skey>`), with the SDK's `GroupSpace` methods below. The same `uri` gives the same object | | ||
| `space.sync()` | `{failed}`, once the page's records of a space are up to date as the visitor may read them. The site must name the space in `spaces`, by its type alone or by its type and key. `failed` lists the writers whose PDS didn't answer | | ||
| `space.records({collections})` | every record held, right away, each as `{uri, repo, collection, rkey, cid, value}`. Given `collections`, only those collections' records | | ||
docs/reference/site-kit.md+13−3
| @@ -32,7 +32,16 @@ A site declares what its pages may ask in its `fyi.opensocial.site` record. Give | ||
| ||
`spaces` is a list of `{space, key}`. `space` is a type of the group's spaces whose records a page may read whole with [`os.space`](#osspace), such as `fyi.opensocial.siteSpace`. `key`, if given, narrows the entry to the one space of that type with that key, such as `board`. Without it, the entry covers every space of the type. No pair of type and key may be named twice. A type named both whole and by a key is allowed. | ||
| ||
`acts` is a list of `{method}`, each a procedure `os.act` may ask. The only one is `fyi.opensocial.createReport`, which reports what the visitor sees ([moderation](../design/moderation.md)). | ||
`acts` is a list of `{method}`, each a procedure `os.act` may ask, always of the site's own group: | ||
| ||
| Act | What it does for the visitor | | ||
| --- | --- | | ||
| `fyi.opensocial.createReport` | reports what they see, or appeals what was done to them ([moderation](../design/moderation.md)) | | ||
| `fyi.opensocial.requestJoin` | joins the group, or asks to, as its join policy says. It answers `status`, `admitted` or `pending`, and redeems an invite waiting for them | | ||
| `fyi.opensocial.cancelJoinRequest` | cancels their request to join while it waits | | ||
| `fyi.opensocial.leaveGroup` | takes them out of the group. The host refuses with `LastConfigurer` if nobody would be left able to configure it | | ||
| ||
Whether they're a member, or have a request waiting, is in `getGroup`'s `viewer` (`member`, `request`). | ||
| ||
`writes` is a list of `{space, key, collection}`, where `space` is a type of the group's spaces, `key` is optional, and `collection` is an NSID. An entry lets a space's `create`, `put` and `delete` ([`os.space`](#osspace)) write records of that collection in the group's space of that type with that key, or in any space of the type if it has no key. A site with at least one entry in `writes` may also `os.upload` pictures. The space's access record still decides whether a given visitor may write in a given space. | ||
| ||
| @@ -64,7 +73,7 @@ It's refused, unlocked, if `method` is none of the site's `reads`. It's refused | ||
| ||
### os.act | ||
| ||
`os.act(method, input = {})` asks one of the site's `acts` as the visitor, with `input` as its JSON body. It answers the method's JSON, or `{}` if the method answers none. | ||
`os.act(method, input = {})` asks one of the site's `acts` as the visitor, with `input` as its JSON body. Its `group` is always the site's own: the host's page sets it, whatever the page sent, so a site can't act for its visitors in another group. It answers the method's JSON, or `{}` if the method answers none. | ||
| ||
It's refused the same way `os.call` is. If the visitor's sign-in doesn't cover the act, the host's page signs them out and `os.onChange` hears it. Then the ask is refused locked with `sign in`. | ||
| ||
| @@ -127,10 +136,11 @@ A refused ask rejects with an `Error` with these fields: | ||
| Field | Value | | ||
| --- | --- | | ||
| `message` | What was refused, in words. | | ||
| `name` | The method's own error name when it refused with one, such as `AlreadyMember` or `LastConfigurer`, as its lexicon lists them. Otherwise `'Error'`. | | ||
| `locked` | `true` if the visitor may not have this now, and `false` otherwise. | | ||
| `reason` | `'sign in'` or `'not for you'` when `locked`, and `undefined` otherwise. | | ||
| ||
A page tests `locked` and `reason`. It shouldn't test `message`. | ||
A page tests `locked`, `reason` and `name`. It shouldn't test `message`. | ||
| ||
- `reason` is `'sign in'`. Nobody is signed in, or the visitor was just signed out. Show `<os-sign-in>` or a call to `os.signIn()`. | ||
- `reason` is `'not for you'`. The visitor is signed in but may not have this, such as a non-member asking for the members. Show what the page shows anyone. | ||
harness/e2e/flows.spec.mjs+19−0
| @@ -688,6 +688,8 @@ test('a site showing its members to whoever signs in on it, and writing for them | ||
<button id="accept" type="button">Take up your place</button> | ||
<button id="upload" type="button">Upload a picture</button> | ||
<p id="uploaded"></p> | ||
<button id="elsewhere" type="button">Ask to join elsewhere</button> | ||
<p id="joined"></p> | ||
<script> | ||
const list = document.getElementById('members'); | ||
const place = document.getElementById('place'); | ||
| @@ -731,6 +733,17 @@ test('a site showing its members to whoever signs in on it, and writing for them | ||
out.textContent = 'Not uploaded: ' + e.message; | ||
} | ||
}; | ||
// Asks to join a group that isn't this one: the host's page asks this | ||
// one's all the same. | ||
document.getElementById('elsewhere').onclick = async () => { | ||
const out = document.getElementById('joined'); | ||
try { | ||
await os.act('fyi.opensocial.requestJoin', { group: 'did:plc:elsewhere' }); | ||
out.textContent = 'Asked'; | ||
} catch (e) { | ||
out.textContent = 'Refused: ' + e.name; | ||
} | ||
}; | ||
os.onChange(draw); | ||
draw(); | ||
</script>` | ||
| @@ -747,6 +760,7 @@ test('a site showing its members to whoever signs in on it, and writing for them | ||
await publishing.getByText('Advanced').click(); | ||
await publishing.getByLabel('Who the members are').check(); | ||
await publishing.getByLabel("The group's profile and rules").check(); | ||
await publishing.getByLabel('Join the group, or ask to join').check(); | ||
await publishing.getByLabel('Kinds of record, one per line').fill(writes); | ||
await publishing.getByRole('button', { name: 'Publish' }).click(); | ||
await expect(publishing).toBeHidden(); | ||
| @@ -787,6 +801,11 @@ test('a site showing its members to whoever signs in on it, and writing for them | ||
await site.getByRole('button', { name: 'Upload a picture' }).click(); | ||
await expect(site.getByText('Uploaded image/png, 70 bytes')).toBeVisible(); | ||
| ||
// Asking to join another group asks this one, where she is already in, | ||
// and the method's error name reaches the page. | ||
await site.getByRole('button', { name: 'Ask to join elsewhere' }).click(); | ||
await expect(site.getByText('Refused: AlreadyMember')).toBeVisible(); | ||
| ||
// Its page under it is in the address bar and the tab, so a reload | ||
// opens it, signed in still. | ||
await site.getByRole('link', { name: 'Rides' }).click(); | ||
lexicons/fyi/opensocial/site.json+7−2
| @@ -86,13 +86,18 @@ | ||
}, | ||
"act": { | ||
"type": "object", | ||
"description": "One of the host's procedures a site's pages may ask, as the visitor.", | ||
"description": "One of the host's procedures a site's pages may ask, as the visitor, of the group whose site it is.", | ||
"required": ["method"], | ||
"properties": { | ||
"method": { | ||
"type": "string", | ||
"format": "nsid", | ||
"knownValues": ["fyi.opensocial.createReport"], | ||
"knownValues": [ | ||
"fyi.opensocial.createReport", | ||
"fyi.opensocial.requestJoin", | ||
"fyi.opensocial.cancelJoinRequest", | ||
"fyi.opensocial.leaveGroup" | ||
], | ||
"description": "The procedure's NSID, as `knownValues` lists those a site may ask." | ||
} | ||
} | ||
web/shell/kit.js+4−2
| @@ -44,6 +44,7 @@ | ||
pending.delete(m.id); | ||
if (!('error' in m)) return waiting.resolve(m.value); | ||
const error = Object.assign(new Error(m.error), { locked: !!m.locked, reason: m.reason }); | ||
if (m.name) error.name = m.name; | ||
waiting.reject(error); | ||
}); | ||
| ||
| @@ -170,8 +171,9 @@ | ||
call: (method, params = {}) => ask('call', { method, params }), | ||
/** | ||
* One of the host's procedures the site acts by, as whoever is looking: | ||
* `input` its JSON, answered with what the method answers. Such as | ||
* `fyi.opensocial.createReport`, reporting what they see. | ||
* `input` its JSON, answered with what the method answers, its `group` | ||
* always this site's. Such as `fyi.opensocial.createReport`, reporting | ||
* what they see, or `fyi.opensocial.requestJoin`, asking to join. | ||
* | ||
* @param {string} method | ||
* @param {object} [input] | ||
web/shell/protocol.ts+10−2
| @@ -46,8 +46,16 @@ export type Tell = { os: 1 } & ( | ||
| { type: 'at'; path: string; title: string } | ||
); | ||
| ||
/** Why an ask was refused: `locked` when it is for someone else to make, as `reason` says. */ | ||
export type Refusal = { error: string; locked?: true; reason?: 'sign in' | 'not for you' }; | ||
/** | ||
* Why an ask was refused: `locked` when it is for someone else to make, as | ||
* `reason` says; `name`, the method's own error name when it gave one. | ||
*/ | ||
export type Refusal = { | ||
error: string; | ||
name?: string; | ||
locked?: true; | ||
reason?: 'sign in' | 'not for you'; | ||
}; | ||
| ||
/** The host's page's answer to an ask: its value, or its refusal. */ | ||
export type Reply = { os: 1; type: 'reply'; id: number } & ({ value: unknown } | Refusal); | ||
web/shell/shell.ts+10−5
| @@ -96,11 +96,12 @@ async function restore(): Promise<string> { | ||
return asked; | ||
} | ||
| ||
/** A read the host refused, with its status. */ | ||
/** A read the host refused, with its status, and the method's error name when it gave one. */ | ||
class Refused extends Error { | ||
constructor( | ||
message: string, | ||
readonly status: number | ||
readonly status: number, | ||
readonly error?: string | ||
) { | ||
super(message); | ||
} | ||
| @@ -120,20 +121,23 @@ async function ask(method: string, params: object) { | ||
const json = res.headers.get('content-type')?.startsWith('application/json'); | ||
if (res.ok && !json) return await res.blob(); | ||
const body = await res.json().catch(() => null); | ||
if (!res.ok) throw new Refused(body?.message ?? res.statusText, res.status); | ||
if (!res.ok) throw new Refused(body?.message ?? res.statusText, res.status, body?.error); | ||
return body; | ||
} | ||
| ||
/** | ||
* One of the host's procedures, as the visitor, through their PDS: `input` | ||
* its JSON, and its answer. Refused for want of scope, as a write is. | ||
* Every one a site may ask is of a group, and its `group` is always this | ||
* site's: the visitor's sign-in lets the site act for them in any group | ||
* here, which it is never to ask of another. | ||
*/ | ||
async function act(method: string, input: object) { | ||
if (!session) throw new Refused('sign in', 401); | ||
const res = await session.fetchHandler(`/xrpc/${method}`, { | ||
method: 'POST', | ||
headers: { 'atproto-proxy': config.aud, 'content-type': 'application/json' }, | ||
body: JSON.stringify(input) | ||
body: JSON.stringify({ ...input, group: config.group }) | ||
}); | ||
const said = await res.json().catch(() => null); | ||
if (res.ok) return said ?? {}; | ||
| @@ -245,7 +249,7 @@ async function unwritten( | ||
await drop(); | ||
throw new Refused('sign in', 401); | ||
} | ||
throw new Refused(said?.message ?? res.statusText, res.status); | ||
throw new Refused(said?.message ?? res.statusText, res.status, said?.error); | ||
} | ||
| ||
/** | ||
| @@ -460,6 +464,7 @@ function refusal(err: unknown): Refusal { | ||
const reason = err instanceof Error && err.message === 'sign in' ? 'sign in' : 'not for you'; | ||
return { | ||
error: err instanceof Error ? err.message : String(err), | ||
...(err instanceof Refused && err.error ? { name: err.error } : {}), | ||
...(locked ? { locked: true, reason } : {}) | ||
}; | ||
} | ||
web/src/lib/organisms/GroupSite.svelte+6−1
| @@ -25,7 +25,12 @@ | ||
['fyi.opensocial.getActor', "People's profiles"] | ||
]; | ||
// What a site's pages may do for whoever signs in, in the reader's words. | ||
const acts = [['fyi.opensocial.createReport', 'Report posts and members to moderators']]; | ||
const acts = [ | ||
['fyi.opensocial.createReport', 'Report posts and members to moderators'], | ||
['fyi.opensocial.requestJoin', 'Join the group, or ask to join'], | ||
['fyi.opensocial.cancelJoinRequest', 'Cancel their request to join'], | ||
['fyi.opensocial.leaveGroup', 'Leave the group'] | ||
]; | ||
// A space as a person types it: its type, and `/<key>` when it names one. | ||
const spaceText = (s: { space: string; key?: string }) => | ||
s.key === undefined ? s.space : `${s.space}/${s.key}`; | ||