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

Finding a person's groups reads their memberships, not every group

Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:host/groups-of-index
crates/group-host/src/authorization.rs+8−4
@@ -6,7 +6,7 @@
use crate::Host;
use crate::api::{Params, param};
use crate::group::meta_space;
use crate::group::{did_rkey, meta_space};
use crate::group_api::{group_of, signed_in};
use crate::mcp;
use crate::oauth_scope::{concrete, set_of};
@@ -64,11 +64,15 @@ impl Host {
Ok(did)
}
/// The groups here, switched on, where `member` holds a role.
/// The groups here, switched on, where `member` holds a role: those
/// whose own members space holds a membership naming them.
pub(crate) fn groups_of(&self, member: &str) -> Result<Vec<String>, XrpcError> {
let accounts = self.store.accounts(None, u32::MAX);
let path = format!("{NS}.membership/{}", did_rkey(member));
let holding = self
.store
.own_space_holding(&format!("{NS}.members"), &path);
let mut groups = Vec::new();
for (did, _) in accounts.map_err(XrpcError::internal)? {
for did in holding.map_err(XrpcError::internal)? {
let roles = self.roles(&did, member).map_err(XrpcError::internal)?;
if group_of(self, &did).is_ok() && self.active(&did).is_ok() && !roles.is_empty() {
groups.push(did);
crates/group-host/src/store.rs+16−0
@@ -469,6 +469,11 @@ pub const MIGRATIONS: &[&str] = &[
authority TEXT NOT NULL,
deleted_at INTEGER NOT NULL
);
",
// A record in a space found by its path alone, as a person's
// memberships are, without reading every account's.
"
CREATE INDEX IF NOT EXISTS space_record_path ON space_record (path);
",
];
@@ -602,6 +607,7 @@ const INVITES_TO: &str = "SELECT seq, grp, invitee, by, message, rkey, created_a
const LAPSED_INVITES: &str = "SELECT seq, grp, invitee, by, message, rkey, created_at, expires_at FROM invite WHERE expires_at <= ?1 ORDER BY seq";
const UNINVITE: &str = "DELETE FROM invite WHERE grp = ?1 AND invitee = ?2";
const SPACES_OF: &str = "SELECT uri FROM space WHERE authority = ?1 ORDER BY uri";
const OWN_SPACE_HOLDING: &str = "SELECT did FROM space_record WHERE path = ?2 AND space = 'at://' || did || '/space/' || ?1 || '/self' ORDER BY did";
const DEACTIVATE: &str = "INSERT INTO deactivation (did, at, by, stewards) VALUES (?1, ?2, ?3, ?4) ON CONFLICT (did) DO NOTHING";
const DEACTIVATION: &str = "SELECT at, by, stewards FROM deactivation WHERE did = ?1";
const ACTIVATE: &str = "DELETE FROM deactivation WHERE did = ?1";
@@ -1807,6 +1813,16 @@ impl Store {
Ok(rows.collect::<rusqlite::Result<_>>()?)
}
/// The accounts whose own repo in their own space of `space_type`,
/// at `self`, holds a record at `path`, in DID order.
pub fn own_space_holding(&self, space_type: &str, path: &str) -> Result<Vec<String>> {
let conn = self.conn();
let mut q = conn.prepare(OWN_SPACE_HOLDING)?;
let rows = q.query_map([space_type, path], |r| r.get(0));
let rows = rows?;
Ok(rows.collect::<rusqlite::Result<_>>()?)
}
/// Lets `did` read or write `space`, or both, as given.
pub fn grant(&self, space: &str, did: &str, read: bool, write: bool) -> Result<()> {
let conn = self.conn();