chadtmiller.com/group-host
atmosphere group host
0
0
A group's members are run from its page
crates/group-host/src/actors.rs+10−0
| @@ -33,6 +33,16 @@ pub async fn get_viewer(State(host): State<Arc<Host>>, headers: HeaderMap) -> An | ||
} | ||
| ||
impl Host { | ||
/// A person as a list names them: their DID, and the handle their DID | ||
/// document names, if it can be read. | ||
pub(crate) async fn actor_view(&self, did: &str) -> Value { | ||
let doc = self.identity.document(did).await.ok(); | ||
match doc.as_ref().and_then(did::handle) { | ||
Some(handle) => json!({"did": did, "handle": handle}), | ||
None => json!({"did": did}), | ||
} | ||
} | ||
| ||
/// A group as a list of someone's shows it, to `viewer`, a member: its | ||
/// name and picture as they may read them, and how many are in it. | ||
fn group_view_basic(&self, group: &str, viewer: &str) -> Answer { | ||
crates/group-host/src/api.rs+23−4
| @@ -169,18 +169,37 @@ pub async fn list_repos(State(host): State<Arc<Host>>, Query(p): Query<Params>) | ||
Ok(Json(json!({"repos": repos, "cursor": cursor}))) | ||
} | ||
| ||
pub async fn resolve_handle(State(host): State<Arc<Host>>, Query(p): Query<Params>) -> Answer { | ||
/// The DID a handle names: an account's here, or for someone signed in, | ||
/// anyone's. | ||
pub async fn resolve_handle( | ||
State(host): State<Arc<Host>>, | ||
Query(p): Query<Params>, | ||
headers: HeaderMap, | ||
) -> Answer { | ||
let handle = param(&p, "handle")?.to_ascii_lowercase(); | ||
let account = host.store.account(&handle).map_err(XrpcError::internal)?; | ||
let account = account.filter(|a| a.handle == handle); | ||
let mut did = account.filter(|a| a.handle == handle).map(|a| a.did); | ||
// Anyone else's, as a PDS resolves them, for someone signed in: the | ||
// host asks the handle's own server, which no stranger sends it to. | ||
let lxm = "com.atproto.identity.resolveHandle"; | ||
if did.is_none() | ||
&& crate::group_api::caller(&host, &headers, lxm) | ||
.await? | ||
.is_some() | ||
{ | ||
did = host | ||
.identity | ||
.resolve_handle(&handle, &host.config.handle_resolvers) | ||
.await; | ||
} | ||
let not_found = || { | ||
XrpcError::new( | ||
StatusCode::BAD_REQUEST, | ||
"HandleNotFound", | ||
format!("no account {handle} here"), | ||
format!("no account {handle} we can find"), | ||
) | ||
}; | ||
Ok(Json(json!({"did": account.ok_or_else(not_found)?.did}))) | ||
Ok(Json(json!({ "did": did.ok_or_else(not_found)? }))) | ||
} | ||
| ||
/// A blob a record of the account references, served as a download: a | ||
crates/group-host/src/group_api.rs+15−12
| @@ -252,10 +252,14 @@ pub async fn get_group( | ||
host.group_records(group, &members, &format!("{NS}.{collection}")) | ||
.map_err(XrpcError::internal) | ||
}; | ||
out["members"] = records("membership")? | ||
.into_iter() | ||
.map(|(_, m)| json!({"actor": {"did": m["member"]}, "roles": objects("role", listed(&m, "roles", "role"))})) | ||
.collect(); | ||
let mut people = Vec::new(); | ||
for (_, m) in records("membership")? { | ||
let actor = host | ||
.actor_view(m["member"].as_str().unwrap_or_default()) | ||
.await; | ||
people.push(json!({"actor": actor, "roles": objects("role", listed(&m, "roles", "role"))})); | ||
} | ||
out["members"] = json!(people); | ||
out["roles"] = records("role")? | ||
.into_iter() | ||
.map(|(key, r)| { | ||
| @@ -313,14 +317,13 @@ pub async fn get_group( | ||
.store | ||
.asking(group, None, 100) | ||
.map_err(XrpcError::internal)?; | ||
out["joinRequests"] = waiting | ||
.iter() | ||
.map(|r| { | ||
let mut view = crate::group_join::request_view(r); | ||
view["actor"] = json!({"did": r.did}); | ||
view | ||
}) | ||
.collect(); | ||
let mut asking = Vec::new(); | ||
for r in &waiting { | ||
let mut view = crate::group_join::request_view(r); | ||
view["actor"] = host.actor_view(&r.did).await; | ||
asking.push(view); | ||
} | ||
out["joinRequests"] = json!(asking); | ||
} | ||
Ok(Json(out)) | ||
} | ||
crates/group-host/tests/actors.rs+31−0
| @@ -171,3 +171,34 @@ async fn shows_a_groups_picture() { | ||
let (_, mine) = groups_of(&h, &alice, &format!("actor={}", alice.did)).await; | ||
assert_eq!(mine["groups"][0]["avatar"], image["blob"]["ref"]["$link"]); | ||
} | ||
| ||
#[tokio::test] | ||
async fn names_by_did_alone_someone_whose_document_it_cannot_read() { | ||
let h = start(&[], false).await; | ||
let [alice, ..] = people(&h).await; | ||
let riders = start_group( | ||
&h, | ||
&alice, | ||
json!({"handle": RIDERS, "displayName": "Riders"}), | ||
) | ||
.await; | ||
join(&h, &riders, "did:plc:nobodynobodynobodyxx").await; | ||
let lxm = format!("{NS}.getGroup"); | ||
let (_, view) = get( | ||
&h, | ||
&lxm, | ||
&format!("group={riders}"), | ||
&[auth(&h, &alice, &lxm)], | ||
) | ||
.await; | ||
let theirs = view["members"] | ||
.as_array() | ||
.unwrap() | ||
.iter() | ||
.find(|m| m["actor"]["did"] == "did:plc:nobodynobodynobodyxx") | ||
.unwrap(); | ||
assert_eq!( | ||
theirs["actor"], | ||
json!({"did": "did:plc:nobodynobodynobodyxx"}) | ||
); | ||
} | ||
crates/group-host/tests/group_handle.rs+28−0
| @@ -80,6 +80,34 @@ fn error(said: &(u16, Json)) -> (u16, &str) { | ||
(said.0, said.1["error"].as_str().unwrap_or_default()) | ||
} | ||
| ||
#[tokio::test] | ||
async fn resolves_anyones_handle_for_someone_signed_in() { | ||
let (h, names, riders, [alice, ..]) = here().await; | ||
names | ||
.0 | ||
.lock() | ||
.unwrap() | ||
.insert("alice.example".into(), alice.did.clone()); | ||
let lxm = "com.atproto.identity.resolveHandle"; | ||
// Its own, for anyone. | ||
let (_, ours) = get(&h, lxm, &format!("handle={RIDERS}"), &[]).await; | ||
assert_eq!(ours["did"], riders.as_str()); | ||
// Anyone else's, only for someone signed in: no stranger sends the host | ||
// to a handle's server. | ||
let (status, said) = get(&h, lxm, "handle=alice.example", &[]).await; | ||
assert_eq!( | ||
(status, said["error"].as_str()), | ||
(400, Some("HandleNotFound")) | ||
); | ||
let (status, said) = get(&h, lxm, "handle=alice.example", &[auth(&h, &alice, lxm)]).await; | ||
assert_eq!( | ||
(status, said["did"].as_str()), | ||
(200, Some(alice.did.as_str())) | ||
); | ||
let (status, _) = get(&h, lxm, "handle=nobody.example", &[auth(&h, &alice, lxm)]).await; | ||
assert_eq!(status, 400); | ||
} | ||
| ||
#[tokio::test] | ||
async fn checks_a_domain_before_it_is_taken() { | ||
let (h, names, did, [alice, _, _]) = here().await; | ||
crates/group-host/tests/groups.rs+8−0
| @@ -151,6 +151,14 @@ async fn shows_a_group_as_each_viewer_may_see_it() { | ||
.collect(); | ||
assert_eq!(members.len(), 2); | ||
assert!(members.contains(&alice.did.as_str()) && members.contains(&bob.did.as_str())); | ||
// Each by the handle their DID document names. | ||
assert!( | ||
mine["members"] | ||
.as_array() | ||
.unwrap() | ||
.iter() | ||
.all(|m| m["actor"]["handle"] == "authority.test") | ||
); | ||
assert_eq!( | ||
mine["roles"], | ||
json!([ | ||
crates/group-host/tests/joining.rs+4−0
| @@ -147,6 +147,10 @@ async fn a_group_that_approves_its_members_keeps_requests() { | ||
.map(|r| r["actor"]["did"].as_str().unwrap()) | ||
.collect(); | ||
assert_eq!(waiting, [carol.did.as_str(), bob.did.as_str()]); | ||
assert_eq!( | ||
admins["joinRequests"][0]["actor"]["handle"], | ||
"authority.test" | ||
); | ||
| ||
// Listed for one who may admit, newest first, a page at a time. | ||
let (_, page) = ask( | ||
docs/design/README.md+4−2
| @@ -112,8 +112,10 @@ says otherwise. | ||
home, and approving an app (`/authorize`; `actors.rs`; done); | ||
starting a group (`/new`) and a group's page (`/g/<handle>`: its | ||
profile and rules, joining and leaving, its members, and admitting | ||
those asking; done); then running a group from its page (roles, | ||
invites, its apps and app passwords): the console's pages in | ||
those asking; done); its members run from its page (roles given and | ||
taken, people put out, invites by handle, and invitations taken up or | ||
turned down on home; done); then its settings (profile, rules, its | ||
apps and app passwords): the console's pages in | ||
`apps/web/src/pages`. Each is used end to end in `harness/e2e`. | ||
8. **Moderation, recovery and moving.** Reports, labels, subjects and | ||
history; recovery keys; importing an account; migrating a group to | ||
harness/e2e/flows.spec.mjs+38−2
| @@ -57,9 +57,9 @@ test('asking to join, and being admitted', async () => { | ||
await bob.getByRole('button', { name: 'Ask to join' }).click() | ||
await expect(bob.getByText('You have asked to join.')).toBeVisible() | ||
await page.goto(url('/g/hikers.groups.test')) | ||
const asking = page.locator('.requests li', { hasText: who.bob.did }) | ||
const asking = page.locator('.requests li', { hasText: '@bob.test' }) | ||
await asking.getByRole('button', { name: 'Admit' }).click() | ||
await expect(page.locator('.members', { hasText: who.bob.did })).toBeVisible() | ||
await expect(page.locator('.members', { hasText: '@bob.test' })).toBeVisible() | ||
await bob.reload() | ||
await expect(bob.getByText('You are a member.')).toBeVisible() | ||
}) | ||
| @@ -69,6 +69,42 @@ test('leaving a group', async () => { | ||
await expect(bob.getByRole('button', { name: 'Ask to join' })).toBeVisible() | ||
}) | ||
| ||
test('inviting someone by their handle', async () => { | ||
await page.goto(url('/g/hikers.groups.test')) | ||
await page.getByRole('textbox', { name: 'Their handle' }).fill('nobody.test') | ||
await page.getByRole('button', { name: 'Invite' }).click() | ||
await expect(page.getByText('We cannot find nobody.test.')).toBeVisible() | ||
await page.getByRole('textbox', { name: 'Their handle' }).fill('@bob.test') | ||
await page.getByRole('button', { name: 'Invite' }).click() | ||
await expect(page.locator('.invites', { hasText: who.bob.did })).toBeVisible() | ||
}) | ||
| ||
test('taking up an invitation', async () => { | ||
await bob.goto(url('/')) | ||
await expect(bob.getByRole('heading', { name: 'You are invited' })).toBeVisible() | ||
await bob.getByRole('button', { name: 'Join Hikers' }).click() | ||
await expect(bob.getByRole('heading', { name: 'You are invited' })).toHaveCount(0) | ||
await expect(bob.locator('.group-row', { hasText: 'Hikers' }).first()).toBeVisible() | ||
await page.reload() | ||
await expect(page.locator('.members', { hasText: '@bob.test' })).toBeVisible() | ||
}) | ||
| ||
test('giving roles, and putting someone out', async () => { | ||
const row = page.locator('.member', { hasText: '@bob.test' }) | ||
await expect(row.getByRole('checkbox', { name: 'member' })).toBeChecked() | ||
await row.getByRole('button', { name: "Save @bob.test's roles" }).click() | ||
await expect(page.locator('.member', { hasText: '@bob.test' })).toContainText('member') | ||
await page.locator('.member', { hasText: '@bob.test' }).getByRole('button', { name: 'Put @bob.test out' }).click() | ||
await expect(page.locator('.members', { hasText: '@bob.test' })).toHaveCount(0) | ||
}) | ||
| ||
test('being told why the host refused', async () => { | ||
// The group's only admin may not leave it: the page says so. | ||
await page.getByRole('button', { name: 'Leave' }).click() | ||
await expect(page.locator('.danger')).toBeVisible() | ||
await expect(page.getByText('You are a member.')).toBeVisible() | ||
}) | ||
| ||
test('signing an app in as her group', async () => { | ||
const app = await new App('atproto repo:app.bsky.feed.post').listen() | ||
await page.goto(await app.push()) | ||
web/src/lib/organisms/Invitations.sveltenew+15−0
| @@ -0,0 +1,15 @@ | ||
<script lang="ts"> | ||
/** The groups that have invited the person looking: each to join, or turn down. */ | ||
let { invitations }: { invitations: { group: { did: string; handle: string; displayName?: string }; message?: string }[] } = $props(); | ||
</script> | ||
| ||
<ul class="invitations"> | ||
{#each invitations as i (i.group.did)} | ||
<li class="stack"> | ||
<span><strong>{i.group.displayName ?? i.group.handle}</strong> invites you to join.</span> | ||
{#if i.message}<p>{i.message}</p>{/if} | ||
<form method="post" action="?/accept"><input type="hidden" name="group" value={i.group.did} /><button type="submit">Join {i.group.displayName ?? i.group.handle}</button></form> | ||
<form method="post" action="?/decline"><input type="hidden" name="group" value={i.group.did} /><button type="submit">Turn it down</button></form> | ||
</li> | ||
{/each} | ||
</ul> | ||