|
1 |
+ |
//! The group directory: the server's answer about groups, written down locally.
|
|
2 |
+ |
//!
|
|
3 |
+ |
//! # Why it exists
|
|
4 |
+ |
//!
|
|
5 |
+ |
//! A described screen's handler is a synchronous function. That is not a
|
|
6 |
+ |
//! limitation to work around; it is the property that lets one description serve
|
|
7 |
+ |
//! a webview, a terminal and an egui window, because nothing in a description
|
|
8 |
+ |
//! blocks. It does mean a screen cannot fetch anything, so any data a screen
|
|
9 |
+ |
//! needs has to be on this side of the network before the screen is drawn.
|
|
10 |
+ |
//!
|
|
11 |
+ |
//! Everything else a consuming app draws already is: rows come out of its own
|
|
12 |
+ |
//! tables. Groups were the exception. `list_groups` is an HTTP GET, membership
|
|
13 |
+ |
//! lives on the server, and the only local trace of a group was its id in
|
|
14 |
+ |
//! `sync_scope_cursor`, which is a cursor rather than a directory and carries no
|
|
15 |
+ |
//! name.
|
|
16 |
+ |
//!
|
|
17 |
+ |
//! # It costs no request
|
|
18 |
+ |
//!
|
|
19 |
+ |
//! The sync loop has always asked the server which groups to sync, once per
|
|
20 |
+ |
//! cycle, and the answer has always carried the name and the admin. Until
|
|
21 |
+ |
//! 2026-08-24 [`SyncTransport::list_group_directory`](super::sync::SyncTransport::list_group_directory)
|
|
22 |
+ |
//! was `list_group_scopes` and mapped each record down to `(GroupId, i32)`,
|
|
23 |
+ |
//! dropping the rest. Writing it down instead is the whole mechanism: the read
|
|
24 |
+ |
//! already happens, on a schedule, outside any request loop.
|
|
25 |
+ |
//!
|
|
26 |
+ |
//! Members are the one part that is a new request, and it is made only for
|
|
27 |
+ |
//! groups this user administers, since the server refuses a non-admin.
|
|
28 |
+ |
//!
|
|
29 |
+ |
//! # What it is not
|
|
30 |
+ |
//!
|
|
31 |
+ |
//! Not synced state. Both tables are absent from every
|
|
32 |
+ |
//! [`SyncSchema`](super::schema::SyncSchema), so they are never group-scoped,
|
|
33 |
+ |
//! never pushed, never on a shared changelog, and they **do not move the storage
|
|
34 |
+ |
//! version**: nothing here crosses the wire, which is exactly the local-only DDL
|
|
35 |
+ |
//! the gate's own rule exempts.
|
|
36 |
+ |
//!
|
|
37 |
+ |
//! The server is the authority and this is a copy of its answer, so there is
|
|
38 |
+ |
//! nothing to merge and no conflict to resolve. A refresh replaces.
|
|
39 |
+ |
//!
|
|
40 |
+ |
//! # Staleness is a fact, not a failure
|
|
41 |
+ |
//!
|
|
42 |
+ |
//! [`refreshed_at`] is what a screen states when it says how current the list is.
|
|
43 |
+ |
//! A directory that has never been refreshed is empty, which is a true statement
|
|
44 |
+ |
//! about what this device knows rather than a claim that the user has no groups.
|
|
45 |
+ |
|
|
46 |
+ |
use rusqlite::{Connection, OptionalExtension, params};
|
|
47 |
+ |
|
|
48 |
+ |
use super::sync::{KnownGroup, KnownMember};
|
|
49 |
+ |
use crate::GroupId;
|
|
50 |
+ |
use crate::error::Result;
|
|
51 |
+ |
|
|
52 |
+ |
/// Replace the group directory with what the server just reported.
|
|
53 |
+ |
///
|
|
54 |
+ |
/// A whole replace rather than an upsert: a group the user has been removed from
|
|
55 |
+ |
/// is absent from the answer, and leaving it behind would offer a scope they can
|
|
56 |
+ |
/// no longer reach. Members of a departed group go with it, which is why this
|
|
57 |
+ |
/// takes the transaction rather than leaving the caller to remember.
|
|
58 |
+ |
pub fn write_groups(conn: &mut Connection, groups: &[KnownGroup]) -> Result<()> {
|
|
59 |
+ |
let tx = conn.transaction()?;
|
|
60 |
+ |
tx.execute("DELETE FROM sync_group_members", [])?;
|
|
61 |
+ |
tx.execute("DELETE FROM sync_groups", [])?;
|
|
62 |
+ |
for group in groups {
|
|
63 |
+ |
tx.execute(
|
|
64 |
+ |
"INSERT INTO sync_groups (group_id, name, gck_version, is_admin) \
|
|
65 |
+ |
VALUES (?1, ?2, ?3, ?4)",
|
|
66 |
+ |
params![
|
|
67 |
+ |
group.id.to_string(),
|
|
68 |
+ |
group.name,
|
|
69 |
+ |
group.gck_version,
|
|
70 |
+ |
i32::from(group.is_admin),
|
|
71 |
+ |
],
|
|
72 |
+ |
)?;
|
|
73 |
+ |
}
|
|
74 |
+ |
tx.commit()?;
|
|
75 |
+ |
Ok(())
|
|
76 |
+ |
}
|
|
77 |
+ |
|
|
78 |
+ |
/// Replace one group's member list.
|
|
79 |
+ |
///
|
|
80 |
+ |
/// Scoped to the one group, unlike [`write_groups`]: a cycle where the member
|
|
81 |
+ |
/// fetch failed for one group should not blank the others, and a refusal is
|
|
82 |
+ |
/// "no answer this time" rather than "nobody is in it".
|
|
83 |
+ |
pub fn write_members(
|
|
84 |
+ |
conn: &mut Connection,
|
|
85 |
+ |
group_id: GroupId,
|
|
86 |
+ |
members: &[KnownMember],
|
|
87 |
+ |
) -> Result<()> {
|
|
88 |
+ |
let tx = conn.transaction()?;
|
|
89 |
+ |
tx.execute(
|
|
90 |
+ |
"DELETE FROM sync_group_members WHERE group_id = ?1",
|
|
91 |
+ |
params![group_id.to_string()],
|
|
92 |
+ |
)?;
|
|
93 |
+ |
for member in members {
|
|
94 |
+ |
tx.execute(
|
|
95 |
+ |
"INSERT INTO sync_group_members (group_id, user_id, email, role, added_at) \
|
|
96 |
+ |
VALUES (?1, ?2, ?3, ?4, ?5)",
|
|
97 |
+ |
params![
|
|
98 |
+ |
group_id.to_string(),
|
|
99 |
+ |
member.user_id,
|
|
100 |
+ |
member.email,
|
|
101 |
+ |
member.role,
|
|
102 |
+ |
member.added_at,
|
|
103 |
+ |
],
|
|
104 |
+ |
)?;
|
|
105 |
+ |
}
|
|
106 |
+ |
tx.commit()?;
|
|
107 |
+ |
Ok(())
|
|
108 |
+ |
}
|
|
109 |
+ |
|
|
110 |
+ |
/// The groups this device knows about, by name.
|
|
111 |
+ |
///
|
|
112 |
+ |
/// Synchronous and cheap, which is the point: a described handler calls this
|
|
113 |
+ |
/// with a connection from the app's own pool.
|
|
114 |
+ |
pub fn groups(conn: &Connection) -> Result<Vec<KnownGroup>> {
|
|
115 |
+ |
let mut stmt = conn.prepare(
|
|
116 |
+ |
"SELECT group_id, name, gck_version, is_admin FROM sync_groups ORDER BY name COLLATE NOCASE",
|
|
117 |
+ |
)?;
|
|
118 |
+ |
let rows = stmt
|
|
119 |
+ |
.query_map([], |row| {
|
|
120 |
+ |
let raw: String = row.get(0)?;
|
|
121 |
+ |
Ok((
|
|
122 |
+ |
raw,
|
|
123 |
+ |
row.get::<_, String>(1)?,
|
|
124 |
+ |
row.get::<_, i32>(2)?,
|
|
125 |
+ |
row.get::<_, i32>(3)?,
|
|
126 |
+ |
))
|
|
127 |
+ |
})?
|
|
128 |
+ |
.collect::<std::result::Result<Vec<_>, _>>()?;
|
|
129 |
+ |
|
|
130 |
+ |
Ok(rows
|
|
131 |
+ |
.into_iter()
|
|
132 |
+ |
// A row whose id will not parse is skipped rather than failing the read.
|
|
133 |
+ |
// It can only come from a build that wrote a different id shape, and a
|
|
134 |
+ |
// screen that cannot list its groups is worse than one missing a row it
|
|
135 |
+ |
// could not have addressed anyway.
|
|
136 |
+ |
.filter_map(|(raw, name, gck_version, is_admin)| {
|
|
137 |
+ |
Some(KnownGroup {
|
|
138 |
+ |
id: GroupId::new(raw.parse().ok()?),
|
|
139 |
+ |
name,
|
|
140 |
+ |
gck_version,
|
|
141 |
+ |
is_admin: is_admin != 0,
|
|
142 |
+ |
})
|
|
143 |
+ |
})
|
|
144 |
+ |
.collect())
|
|
145 |
+ |
}
|
|
146 |
+ |
|
|
147 |
+ |
/// Whether this device knows the user to be a member of `group_id`.
|
|
148 |
+ |
///
|
|
149 |
+ |
/// The local answer to the question `share_project` used to ask the server. A
|
|
150 |
+ |
/// scope the sync engine holds no key for routes a whole subtree into a changelog
|
|
151 |
+ |
/// that goes nowhere, so the check is worth making; making it against the
|
|
152 |
+ |
/// directory means it can be made from a described handler.
|
|
153 |
+ |
pub fn is_member(conn: &Connection, group_id: GroupId) -> Result<bool> {
|
|
154 |
+ |
let found: Option<i64> = conn
|
|
155 |
+ |
.query_row(
|
|
156 |
+ |
"SELECT 1 FROM sync_groups WHERE group_id = ?1",
|
|
157 |
+ |
params![group_id.to_string()],
|
|
158 |
+ |
|row| row.get(0),
|
|
159 |
+ |
)
|
|
160 |
+ |
.optional()?;
|
|
161 |
+ |
Ok(found.is_some())
|
|
162 |
+ |
}
|
|
163 |
+ |
|
|
164 |
+ |
/// Who is in a group this user administers, earliest first.
|
|
165 |
+ |
///
|
|
166 |
+ |
/// Empty for a group the user merely belongs to, because the server refuses a
|
|
167 |
+ |
/// non-admin the member list. A screen says "you administer this group" from
|
|
168 |
+ |
/// [`KnownGroup::is_admin`] rather than by finding this empty: those are
|
|
169 |
+ |
/// different facts and only one of them is about permission.
|
|
170 |
+ |
pub fn members(conn: &Connection, group_id: GroupId) -> Result<Vec<KnownMember>> {
|
|
171 |
+ |
let mut stmt = conn.prepare(
|
|
172 |
+ |
"SELECT user_id, email, role, added_at FROM sync_group_members \
|
|
173 |
+ |
WHERE group_id = ?1 ORDER BY added_at",
|
|
174 |
+ |
)?;
|
|
175 |
+ |
let rows = stmt
|
|
176 |
+ |
.query_map(params![group_id.to_string()], |row| {
|
|
177 |
+ |
Ok(KnownMember {
|
|
178 |
+ |
user_id: row.get(0)?,
|
|
179 |
+ |
email: row.get(1)?,
|
|
180 |
+ |
role: row.get(2)?,
|
|
181 |
+ |
added_at: row.get(3)?,
|
|
182 |
+ |
})
|
|
183 |
+ |
})?
|
|
184 |
+ |
.collect::<std::result::Result<Vec<_>, _>>()?;
|
|
185 |
+ |
Ok(rows)
|
|
186 |
+ |
}
|
|
187 |
+ |
|
|
188 |
+ |
/// When the directory was last written, RFC 3339, or `None` if it never has
|
|
189 |
+ |
/// been.
|
|
190 |
+ |
///
|
|
191 |
+ |
/// What a screen states when it says how current its list is. The oldest row
|
|
192 |
+ |
/// wins: a directory is only as fresh as its stalest entry, and reporting the
|
|
193 |
+ |
/// newest would call a list current on the strength of the one group that
|
|
194 |
+ |
/// refreshed.
|
|
195 |
+ |
pub fn refreshed_at(conn: &Connection) -> Result<Option<String>> {
|
|
196 |
+ |
Ok(conn
|
|
197 |
+ |
.query_row("SELECT MIN(refreshed_at) FROM sync_groups", [], |row| {
|
|
198 |
+ |
row.get::<_, Option<String>>(0)
|
|
199 |
+ |
})
|
|
200 |
+ |
.optional()?
|
|
201 |
+ |
.flatten())
|
|
202 |
+ |
}
|
|
203 |
+ |
|
|
204 |
+ |
#[cfg(test)]
|
|
205 |
+ |
mod tests {
|
|
206 |
+ |
use super::*;
|
|
207 |
+ |
use crate::store::schema::{SyncSchema, SyncTable};
|
|
208 |
+ |
|
|
209 |
+ |
fn db() -> Connection {
|
|
210 |
+ |
let conn = Connection::open_in_memory().expect("a database");
|
|
211 |
+ |
let schema = SyncSchema::new(vec![SyncTable::full("note", &["id", "name"])]);
|
|
212 |
+ |
conn.execute_batch("CREATE TABLE note (id TEXT PRIMARY KEY, name TEXT);")
|
|
213 |
+ |
.expect("the app table");
|
|
214 |
+ |
conn.execute_batch(&schema.migration_sql())
|
|
215 |
+ |
.expect("the sync tables");
|
|
216 |
+ |
conn
|
|
217 |
+ |
}
|
|
218 |
+ |
|
|
219 |
+ |
fn group(id: u128, name: &str, is_admin: bool) -> KnownGroup {
|
|
220 |
+ |
KnownGroup {
|
|
221 |
+ |
id: GroupId::new(uuid::Uuid::from_u128(id)),
|
|
222 |
+ |
name: name.to_owned(),
|
|
223 |
+ |
gck_version: 1,
|
|
224 |
+ |
is_admin,
|
|
225 |
+ |
}
|
|
226 |
+ |
}
|
|
227 |
+ |
|
|
228 |
+ |
fn member(email: &str, added_at: &str) -> KnownMember {
|
|
229 |
+ |
KnownMember {
|
|
230 |
+ |
user_id: format!("user-{email}"),
|
|
231 |
+ |
email: email.to_owned(),
|
|
232 |
+ |
role: "member".to_owned(),
|
|
233 |
+ |
added_at: added_at.to_owned(),
|
|
234 |
+ |
}
|
|
235 |
+ |
}
|
|
236 |
+ |
|
|
237 |
+ |
#[test]
|
|
238 |
+ |
fn a_store_that_has_never_synced_knows_no_groups() {
|
|
239 |
+ |
let conn = db();
|
|
240 |
+ |
assert!(groups(&conn).unwrap().is_empty());
|
|
241 |
+ |
assert_eq!(refreshed_at(&conn).unwrap(), None);
|
|
242 |
+ |
}
|
|
243 |
+ |
|
|
244 |
+ |
#[test]
|
|
245 |
+ |
fn the_directory_comes_back_by_name() {
|
|
246 |
+ |
let mut conn = db();
|
|
247 |
+ |
write_groups(
|
|
248 |
+ |
&mut conn,
|
|
249 |
+ |
&[group(2, "Zebra", false), group(1, "Aardvark", true)],
|
|
250 |
+ |
)
|
|
251 |
+ |
.unwrap();
|
|
252 |
+ |
|
|
253 |
+ |
let listed = groups(&conn).unwrap();
|
|
254 |
+ |
assert_eq!(listed.len(), 2);
|
|
255 |
+ |
assert_eq!(listed[0].name, "Aardvark", "ordered by name, not by id");
|
|
256 |
+ |
assert_eq!(listed[1].name, "Zebra");
|
|
257 |
+ |
assert!(listed[0].is_admin);
|
|
258 |
+ |
assert!(!listed[1].is_admin);
|
|
259 |
+ |
}
|
|
260 |
+ |
|
|
261 |
+ |
/// A group the user has been removed from is absent from the server's
|
|
262 |
+ |
/// answer, and leaving it behind would offer a scope they cannot reach.
|
|
263 |
+ |
#[test]
|
|
264 |
+ |
fn a_refresh_replaces_rather_than_merges() {
|
|
265 |
+ |
let mut conn = db();
|
|
266 |
+ |
write_groups(
|
|
267 |
+ |
&mut conn,
|
|
268 |
+ |
&[group(1, "Kept", true), group(2, "Removed", true)],
|
|
269 |
+ |
)
|
|
270 |
+ |
.unwrap();
|
|
271 |
+ |
write_groups(&mut conn, &[group(1, "Kept", true)]).unwrap();
|
|
272 |
+ |
|
|
273 |
+ |
let listed = groups(&conn).unwrap();
|
|
274 |
+ |
assert_eq!(listed.len(), 1);
|
|
275 |
+ |
assert_eq!(listed[0].name, "Kept");
|
|
276 |
+ |
}
|
|
277 |
+ |
|
|
278 |
+ |
#[test]
|
|
279 |
+ |
fn losing_a_group_takes_its_members_with_it() {
|
|
280 |
+ |
let mut conn = db();
|
|
281 |
+ |
write_groups(&mut conn, &[group(1, "Gone", true)]).unwrap();
|
|
282 |
+ |
write_members(
|
|
283 |
+ |
&mut conn,
|
|
284 |
+ |
GroupId::new(uuid::Uuid::from_u128(1)),
|
|
285 |
+ |
&[member("a@localhost", "2026-01-01T00:00:00Z")],
|
|
286 |
+ |
)
|
|
287 |
+ |
.unwrap();
|
|
288 |
+ |
|
|
289 |
+ |
write_groups(&mut conn, &[]).unwrap();
|
|
290 |
+ |
assert!(
|
|
291 |
+ |
members(&conn, GroupId::new(uuid::Uuid::from_u128(1)))
|
|
292 |
+ |
.unwrap()
|
|
293 |
+ |
.is_empty(),
|
|
294 |
+ |
"a member list outlived the group it belonged to"
|
|
295 |
+ |
);
|
|
296 |
+ |
}
|
|
297 |
+ |
|
|
298 |
+ |
/// Scoped, unlike the group write: a cycle where one group's member fetch
|
|
299 |
+ |
/// failed must not blank another group's list.
|
|
300 |
+ |
#[test]
|
|
301 |
+ |
fn writing_one_groups_members_leaves_another_groups_alone() {
|
|
302 |
+ |
let mut conn = db();
|
|
303 |
+ |
write_groups(&mut conn, &[group(1, "One", true), group(2, "Two", true)]).unwrap();
|
|
304 |
+ |
let one = GroupId::new(uuid::Uuid::from_u128(1));
|
|
305 |
+ |
let two = GroupId::new(uuid::Uuid::from_u128(2));
|
|
306 |
+ |
|
|
307 |
+ |
write_members(
|
|
308 |
+ |
&mut conn,
|
|
309 |
+ |
one,
|
|
310 |
+ |
&[member("a@localhost", "2026-01-01T00:00:00Z")],
|
|
311 |
+ |
)
|
|
312 |
+ |
.unwrap();
|
|
313 |
+ |
write_members(
|
|
314 |
+ |
&mut conn,
|
|
315 |
+ |
two,
|
|
316 |
+ |
&[member("b@localhost", "2026-01-01T00:00:00Z")],
|
|
317 |
+ |
)
|
|
318 |
+ |
.unwrap();
|
|
319 |
+ |
|
|
320 |
+ |
assert_eq!(members(&conn, one).unwrap().len(), 1);
|
|
321 |
+ |
assert_eq!(members(&conn, two).unwrap()[0].email, "b@localhost");
|
|
322 |
+ |
}
|
|
323 |
+ |
|
|
324 |
+ |
#[test]
|
|
325 |
+ |
fn members_come_back_oldest_first() {
|
|
326 |
+ |
let mut conn = db();
|
|
327 |
+ |
write_groups(&mut conn, &[group(1, "One", true)]).unwrap();
|
|
328 |
+ |
let one = GroupId::new(uuid::Uuid::from_u128(1));
|
|
329 |
+ |
write_members(
|
|
330 |
+ |
&mut conn,
|
|
331 |
+ |
one,
|
|
332 |
+ |
&[
|
|
333 |
+ |
member("late@localhost", "2026-06-01T00:00:00Z"),
|
|
334 |
+ |
member("early@localhost", "2026-01-01T00:00:00Z"),
|
|
335 |
+ |
],
|
|
336 |
+ |
)
|
|
337 |
+ |
.unwrap();
|
|
338 |
+ |
|
|
339 |
+ |
let listed = members(&conn, one).unwrap();
|
|
340 |
+ |
assert_eq!(listed[0].email, "early@localhost");
|
|
341 |
+ |
assert_eq!(listed[1].email, "late@localhost");
|
|
342 |
+ |
}
|
|
343 |
+ |
|
|
344 |
+ |
/// The question `share_project` used to ask the server, answered locally.
|
|
345 |
+ |
#[test]
|
|
346 |
+ |
fn membership_is_answerable_without_a_request() {
|
|
347 |
+ |
let mut conn = db();
|
|
348 |
+ |
write_groups(&mut conn, &[group(1, "Mine", false)]).unwrap();
|
|
349 |
+ |
|
|
350 |
+ |
assert!(is_member(&conn, GroupId::new(uuid::Uuid::from_u128(1))).unwrap());
|
|
351 |
+ |
assert!(!is_member(&conn, GroupId::new(uuid::Uuid::from_u128(9))).unwrap());
|
|
352 |
+ |
}
|
|
353 |
+ |
|
|
354 |
+ |
/// Belonging to a group and administering it are different facts, and only
|
|
355 |
+ |
/// one of them is about permission. A screen must not read the empty member
|
|
356 |
+ |
/// list as "nobody is in it".
|
|
357 |
+ |
#[test]
|
|
358 |
+ |
fn a_group_you_only_belong_to_has_no_members_and_says_so_separately() {
|
|
359 |
+ |
let mut conn = db();
|
|
360 |
+ |
write_groups(&mut conn, &[group(1, "Someone else's", false)]).unwrap();
|
|
361 |
+ |
|
|
362 |
+ |
let listed = groups(&conn).unwrap();
|
|
363 |
+ |
assert!(!listed[0].is_admin, "the fact that gates the member list");
|
|
364 |
+ |
assert!(
|
|
365 |
+ |
members(&conn, GroupId::new(uuid::Uuid::from_u128(1)))
|
|
366 |
+ |
.unwrap()
|
|
367 |
+ |
.is_empty(),
|
|
368 |
+ |
"and the list nobody asked the server for"
|
|
369 |
+ |
);
|
|
370 |
+ |
}
|
|
371 |
+ |
|
|
372 |
+ |
#[test]
|
|
373 |
+ |
fn a_written_directory_reports_when_it_was_written() {
|
|
374 |
+ |
let mut conn = db();
|
|
375 |
+ |
write_groups(&mut conn, &[group(1, "One", true)]).unwrap();
|
|
376 |
+ |
assert!(refreshed_at(&conn).unwrap().is_some());
|
|
377 |
+ |
}
|
|
378 |
+ |
|
|
379 |
+ |
/// Local-only DDL, so the gate must not see it. A table absent from every
|
|
380 |
+ |
/// `SyncSchema` crosses no wire, and the storage version describes the wire.
|
|
381 |
+ |
#[test]
|
|
382 |
+ |
fn the_directory_is_absent_from_the_wire_manifest() {
|
|
383 |
+ |
let schema = SyncSchema::new(vec![SyncTable::full("note", &["id", "name"])]);
|
|
384 |
+ |
let wire = schema.wire_manifest();
|
|
385 |
+ |
assert!(!wire.contains("sync_groups"), "{wire}");
|
|
386 |
+ |
assert!(!wire.contains("sync_group_members"), "{wire}");
|
|
387 |
+ |
}
|
|
388 |
+ |
}
|