chadtmiller.com/group-host
atmosphere group host
0
0
A person is named, to someone signed in, if the host shows them
crates/group-host/src/actors.rs+42−1
| @@ -1,5 +1,6 @@ | ||
//! People, as the console shows them: who is asking (`getViewer`, as | ||
//! knotless's), and the groups someone is in (`getActorGroups`). | ||
//! knotless's), a person (`getActor`), and the groups someone is in | ||
//! (`getActorGroups`). | ||
//! | ||
//! As the prototype's `methods/groups.ts`'s `getActorGroups`. | ||
| ||
| @@ -8,6 +9,7 @@ use crate::api::{Params, page, param}; | ||
use crate::group::{members_space, meta_space}; | ||
use crate::group_api::{caller, group_of, signed_in}; | ||
use crate::rbac::{Access, NS}; | ||
use crate::store::Store; | ||
use crate::xrpc::XrpcError; | ||
use atproto::did; | ||
use axum::Json; | ||
| @@ -71,6 +73,45 @@ impl Host { | ||
} | ||
} | ||
| ||
impl Store { | ||
/// Whether `did` is asking to join any group here. | ||
pub fn asking_anywhere(&self, did: &str) -> anyhow::Result<bool> { | ||
let sql = "SELECT 1 FROM join_request WHERE did = ?1 LIMIT 1"; | ||
let found = self.conn().query_row(sql, [did], |_| Ok(())); | ||
Ok(rusqlite::OptionalExtension::optional(found)?.is_some()) | ||
} | ||
} | ||
| ||
/// A person, as a view names them, for someone signed in: themselves, or | ||
/// anyone the host shows, a member of one of its groups or someone asking | ||
/// to join one; and to themselves, whether the host starts groups for them. | ||
pub async fn get_actor( | ||
State(host): State<Arc<Host>>, | ||
Query(p): Query<Params>, | ||
headers: HeaderMap, | ||
) -> Answer { | ||
let me = signed_in(&host, &headers, &format!("{NS}.getActor")).await?; | ||
let actor = param(&p, "actor")?; | ||
let shown = actor == me | ||
|| !host.groups_of(actor)?.is_empty() | ||
|| host | ||
.store | ||
.asking_anywhere(actor) | ||
.map_err(XrpcError::internal)?; | ||
if !shown { | ||
return Err(XrpcError::new( | ||
StatusCode::BAD_REQUEST, | ||
"ActorNotFound", | ||
format!("no one here by {actor}"), | ||
)); | ||
} | ||
let mut view = host.actor_view(actor).await; | ||
if actor == me { | ||
view["canCreateGroup"] = json!(host.may_found(&me)); | ||
} | ||
Ok(Json(view)) | ||
} | ||
| ||
/// The groups here someone is in: every one of the caller's own, and of | ||
/// anyone else's only those the caller is in too, a group's members being | ||
/// its members' alone to know. By DID, a page at a time. | ||
crates/group-host/src/lib.rs+1−0
| @@ -358,6 +358,7 @@ pub fn router(host: Arc<Host>) -> Router { | ||
post(group_tokens::revoke_group_token), | ||
) | ||
.route("/xrpc/fyi.opensocial.getViewer", get(actors::get_viewer)) | ||
.route("/xrpc/fyi.opensocial.getActor", get(actors::get_actor)) | ||
.route( | ||
"/xrpc/fyi.opensocial.getActorGroups", | ||
get(actors::get_actor_groups), | ||
crates/group-host/tests/actors.rs+59−0
| @@ -202,3 +202,62 @@ async fn names_by_did_alone_someone_whose_document_it_cannot_read() { | ||
json!({"did": "did:plc:nobodynobodynobodyxx"}) | ||
); | ||
} | ||
| ||
#[tokio::test] | ||
async fn names_a_person_the_host_shows() { | ||
let h = start(&[], false).await; | ||
let [alice, bob, carol] = people(&h).await; | ||
let lxm = format!("{NS}.getActor"); | ||
let actor = async |asker: &Authority, who: &str| { | ||
get(&h, &lxm, &format!("actor={who}"), &[auth(&h, asker, &lxm)]).await | ||
}; | ||
// Oneself, and whether the host starts groups for them. | ||
let (status, me) = actor(&alice, &alice.did).await; | ||
assert_eq!(status, 200, "{me}"); | ||
assert_eq!(me["did"], alice.did.as_str()); | ||
assert_eq!(me["handle"], "authority.test"); | ||
assert_eq!(me["canCreateGroup"], true); | ||
// No one else, until they are in a group here, or asking to join one. | ||
for who in [&bob, &carol] { | ||
let said = actor(&alice, &who.did).await; | ||
assert_eq!( | ||
(said.0, said.1["error"].as_str()), | ||
(400, Some("ActorNotFound")) | ||
); | ||
} | ||
let group = start_group( | ||
&h, | ||
&alice, | ||
json!({"handle": RIDERS, "displayName": "Riders"}), | ||
) | ||
.await; | ||
join(&h, &group, &bob.did).await; | ||
let ask = format!("{NS}.requestJoin"); | ||
let (status, said) = post( | ||
&h, | ||
&ask, | ||
&json!({"group": group}), | ||
&[auth(&h, &carol, &ask)], | ||
) | ||
.await; | ||
assert_eq!(status, 200, "{said}"); | ||
for who in [&bob, &carol] { | ||
let (status, view) = actor(&alice, &who.did).await; | ||
assert_eq!(status, 200, "{view}"); | ||
assert_eq!(view["did"], who.did.as_str()); | ||
assert!(view.get("canCreateGroup").is_none()); | ||
} | ||
let said = get(&h, &lxm, &format!("actor={}", bob.did), &[]).await; | ||
assert_eq!(said.0, 401); | ||
| ||
let closed = start(&[("GROUP_FOUNDERS", "did:plc:someone")], false).await; | ||
let [dana, ..] = people(&closed).await; | ||
let (_, me) = get( | ||
&closed, | ||
&lxm, | ||
&format!("actor={}", dana.did), | ||
&[auth(&closed, &dana, &lxm)], | ||
) | ||
.await; | ||
assert_eq!(me["canCreateGroup"], false); | ||
} | ||
docs/design/groups.md+4−0
| @@ -60,6 +60,10 @@ group for someone or lets them read it themselves. | ||
that lets read the space. | ||
- `fyi.opensocial.listGroups` is every group here, unauthenticated: by | ||
handle, and for one whose meta anyone may read, its profile and rules. | ||
- `fyi.opensocial.getActor` names a person, by their DID, as their DID | ||
document does, for someone signed in: themselves (with whether the host | ||
starts groups for them), or anyone the host shows, a member of one of | ||
its groups or someone asking to join one; no one else. | ||
| ||
## Changing its face | ||
docs/design/README.md+2−1
| @@ -108,7 +108,8 @@ says otherwise. | ||
confidential client, as knotless does, keeping only a signed cookie | ||
(`console_login.rs`; done). | ||
7. **The console.** Signing in as oneself (step 6); who is looking | ||
(`getViewer`, as knotless's) and their groups (`getActorGroups`), on | ||
(`getViewer`, as knotless's), a person (`getActor`) and their groups | ||
(`getActorGroups`), on | ||
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 | ||