zeyn 0.0.3

Rust web framework for data heavy applications, written by AI agents and reviewed by people
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//! Grid endpoints: rows, saved views, row items, inline edits, bulk actions and export.
//!
//! Every endpoint opens the screen with `AppState::open`, finds its grid with `grid_at` (which
//! checks the grid's read access) and runs application code through `AppState::intercept`.

use super::*;

pub(super) fn routes(router: Router<Arc<AppState>>) -> Router<Arc<AppState>> {
    router
        .route("/_zeyn/grid", post(grid_call))
        .route("/_zeyn/grid/view", post(grid_view_call))
        .route("/_zeyn/grid/run", post(grid_run_call))
        .route("/_zeyn/grid/bulk", post(grid_bulk_call))
        .route("/_zeyn/grid/edit", post(grid_edit_call))
        .route("/_zeyn/grid/export", get(grid_export))
}

/// The grid at `node` of an opened screen, after the grid's read access. Every grid endpoint
/// that addresses a grid starts here, so none can skip the check.
fn grid_at<'s>(ctx: &Ctx, screen: &'s Screen, node: &NodePath) -> Result<&'s dyn GridSource, Failure> {
    let grid = screen.node(node).and_then(|n| n.grid()).ok_or(Failure::App(Error::NotFound))?;
    if !ctx.identity().can(&grid.action().access) {
        return Err(Failure::App(Error::Forbidden));
    }
    Ok(grid)
}

#[derive(Deserialize)]
pub(super) struct GridCall {
    pub(super) screen: String,
    #[serde(default)]
    pub(super) params: Json,
    pub(super) node: NodePath,
    #[serde(default)]
    pub(super) query: DataQuery,
    /// Column layout to render the rows in.
    #[serde(default)]
    pub(super) view: Option<DataGridView>,
    /// Whether to send the header row back too (after the layout changed).
    #[serde(default)]
    pub(super) head: bool,
    /// Group values or ancestor keys of the branch to load in a grouped or tree grid.
    #[serde(default)]
    pub(super) path: Vec<Option<String>>,
}

#[derive(Deserialize)]
struct ExportQuery {
    screen: String,
    params: Option<String>,
    node: crate::ui::NodePath,
    /// JSON of `{ "sort": [...], "filters": [...], "columns": [...] }`, all optional.
    q: Option<String>,
    /// `csv` (the default) or `xlsx`.
    format: Option<String>,
}

#[derive(Deserialize)]
struct EditCall {
    screen: String,
    #[serde(default)]
    params: Json,
    node: crate::ui::NodePath,
    key: String,
    /// Typed values of the edited cells and the row version, as a form sends them.
    values: Map<String, Json>,
    /// Layout to render the saved row in.
    #[serde(default)]
    view: Option<DataGridView>,
}

#[derive(Deserialize)]
struct BulkCall {
    screen: String,
    #[serde(default)]
    params: Json,
    node: crate::ui::NodePath,
    action: usize,
    /// Keys picked one by one; ignored when `all` is set.
    #[serde(default)]
    keys: Vec<String>,
    /// Every row that matches `query` (its filters); the server collects the keys.
    #[serde(default)]
    all: bool,
    #[serde(default)]
    query: DataQuery,
}

#[derive(Deserialize)]
struct RowItemCall {
    screen: String,
    #[serde(default)]
    params: Json,
    node: crate::ui::NodePath,
    /// Key of the row, as in `data-zeyn-key`.
    key: String,
    /// Which item: `a0` is the first row action.
    item: String,
}

#[derive(Deserialize)]
struct DataGridViewCall {
    screen: String,
    #[serde(default)]
    params: Json,
    node: NodePath,
    /// The view to save; `null` deletes the saved view.
    view: Option<DataGridView>,
}

async fn grid_call(
    State(st): State<Arc<AppState>>,
    headers: HeaderMap,
    axum::Json(call): axum::Json<GridCall>,
) -> Response {
    let ctx = st.licensed(st.ctx(&headers)).await;
    if let Err(f) = st.check_csrf(&headers) {
        return st.error_response(&ctx, f);
    }
    match st.run_grid(&ctx, call).await {
        Ok(body) => axum::Json(body).into_response(),
        Err(f) => st.error_response(&ctx, f),
    }
}

/// Streams every row that matches the filters as CSV (or XLSX with the `xlsx` feature), page by
/// page through the grid's action, in the user's column layout and language, up to the grid's
/// export limit. With the `pdf` feature, `format=pdf` prints a list report instead. Needs the
/// grid's read access and its export access; interceptors see the call.
async fn grid_export(State(st): State<Arc<AppState>>, headers: HeaderMap, Query(q): Query<ExportQuery>) -> Response {
    let ctx = st.licensed(st.ctx(&headers)).await;
    let xlsx = match q.format.as_deref() {
        None | Some("csv") => false,
        #[cfg(feature = "xlsx")]
        Some("xlsx") => true,
        #[cfg(feature = "pdf")]
        Some("pdf") => false,
        Some(_) => return st.error_response(&ctx, Failure::App(Error::NotFound)),
    };
    let opened = async {
        let params = match &q.params {
            None => Json::Null,
            Some(p) => serde_json::from_str(p).map_err(|_| Failure::App(Error::NotFound))?,
        };
        let spec: Json = match &q.q {
            None => Json::Null,
            Some(s) if s.len() <= 64 * 1024 => serde_json::from_str(s).map_err(|_| Failure::App(Error::NotFound))?,
            Some(_) => return Err(Failure::App(Error::NotFound)),
        };
        let (_, entry, screen) = st.open(&ctx, &q.screen, params.clone()).await?;
        let grid = screen.node(&q.node).and_then(|n| n.grid()).ok_or(Failure::App(Error::NotFound))?;
        let access = grid.export_access().ok_or(Failure::App(Error::NotFound))?;
        if !ctx.identity().can(&grid.action().access) || !ctx.identity().can(&access) {
            return Err(Failure::App(Error::Forbidden));
        }
        let info = Call {
            kind: CallKind::DataGrid,
            screen: entry.meta.name,
            action: Some(grid.action()),
            via_agent: ctx.via_agent(),
        };
        let checked = st.before(&ctx, &info);
        if checked.is_err() {
            st.after(&ctx, &info, Instant::now(), &checked);
        }
        checked?;
        data::note_screen_read(&ctx, &entry.meta, &params);
        Ok((entry, screen, spec, info))
    }
    .await;
    let (entry, screen, spec, info) = match opened {
        Ok(v) => v,
        Err(f) => return st.error_response(&ctx, f),
    };
    let view: Option<DataGridView> =
        serde_json::from_value(json!({ "columns": spec.get("columns").cloned().unwrap_or(json!([])) })).ok();
    let mut query: DataQuery = serde_json::from_value(json!({
        "sort": spec.get("sort").cloned().unwrap_or(json!([])),
        "filters": spec.get("filters").cloned().unwrap_or(json!([])),
    }))
    .unwrap_or_default();
    query.with_total = false;
    #[cfg(feature = "pdf")]
    if q.format.as_deref() == Some("pdf") {
        return pdf::grid_report(&st, &ctx, entry.meta.name, &screen, &q.node, view, query, &info).await;
    }
    let name = entry.meta.name;
    let node = q.node;
    let (tx, rx) = tokio::sync::mpsc::channel(4);
    let state = st.clone();
    tokio::spawn(async move {
        let started = Instant::now();
        let Some(grid) = screen.node(&node).and_then(|n| n.grid()) else {
            return;
        };
        let view = view.map(|v| grid.clean_view(v));
        let r = user_render(name, &ctx);
        #[cfg(feature = "xlsx")]
        let sheet = xlsx.then(|| {
            xlsx::XlsxSink::open(grid.export_columns(&r, view.as_ref()), ctx.locale().to_string(), tx.clone())
        });
        #[cfg(not(feature = "xlsx"))]
        let sheet: Option<std::convert::Infallible> = None;
        if sheet.is_none() {
            let head = format!("\u{feff}{}", grid.csv_head(&r, view.as_ref()));
            if tx.send(Ok(head.into())).await.is_err() {
                return;
            }
        }
        let limit = grid.export_limit();
        let mut sent = 0usize;
        let mut outcome: Result<(), Failure> = Ok(());
        while sent < limit {
            let size = (limit - sent).min(crate::action::MAX_PAGE_SIZE as usize) as u32;
            let mut q = query.clone();
            q.page = crate::action::PageRequest::Offset { offset: sent as u64, limit: size };
            let mut page = match state.timed(grid.fetch(ctx.clone(), q)).await {
                Ok(p) => p,
                Err(f) => {
                    outcome = Err(f);
                    let _ = tx.send(Err(std::io::Error::other("export failed"))).await;
                    break;
                }
            };
            page.view = view.clone();
            let n = page.count;
            let open = match &sheet {
                _ if n == 0 => true,
                #[cfg(feature = "xlsx")]
                Some(s) => s.send(grid.export_values(&page, &r)).await,
                #[cfg(not(feature = "xlsx"))]
                Some(never) => match *never {},
                None => tx.send(Ok(grid.csv_rows(&page, &r).into())).await.is_ok(),
            };
            if !open {
                break;
            }
            sent += n;
            if n < size as usize {
                break;
            }
        }
        #[cfg(feature = "xlsx")]
        if let Some(s) = sheet
            && let Err(e) = s.finish(outcome.is_ok()).await
        {
            tracing::warn!("XLSX export did not finish: {e}");
        }
        note_export(&ctx, grid, if xlsx { "xlsx" } else { "csv" }, sent, &query);
        state.after(&ctx, &info, started, &outcome);
    });
    let mut response = axum::body::Body::from_stream(Chunks(rx)).into_response();
    let h = response.headers_mut();
    let (kind, ext) = if xlsx {
        ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx")
    } else {
        ("text/csv; charset=utf-8", "csv")
    };
    h.insert(header::CONTENT_TYPE, HeaderValue::from_static(kind));
    h.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
    if let Ok(v) = HeaderValue::from_str(&format!("attachment; filename=\"{name}.{ext}\"")) {
        h.insert(header::CONTENT_DISPOSITION, v);
    }
    response
}

/// Records an export of a sensitive grid in the call's audit trail: once, with the row count and
/// the query, so exports cost nothing per row.
pub(super) fn note_export(ctx: &Ctx, grid: &dyn GridSource, format: &'static str, rows: usize, query: &DataQuery) {
    if let Some(model) = grid.sensitive_model() {
        let query = json!({ "sort": query.sort, "filters": query.filters });
        ctx.update_trail(|t| {
            t.export = Some(crate::audit::ExportRecord { model, format, rows: rows as u64, query });
        });
    }
}

async fn grid_edit_call(
    State(st): State<Arc<AppState>>,
    headers: HeaderMap,
    axum::Json(call): axum::Json<EditCall>,
) -> Response {
    let ctx = st.licensed(st.ctx(&headers)).await;
    if let Err(f) = st.check_csrf(&headers) {
        return st.error_response(&ctx, f);
    }
    match st.run_edit(&ctx, call).await {
        Ok(body) => axum::Json(body).into_response(),
        Err(f) => st.error_response(&ctx, f),
    }
}

async fn grid_bulk_call(
    State(st): State<Arc<AppState>>,
    headers: HeaderMap,
    axum::Json(call): axum::Json<BulkCall>,
) -> Response {
    let ctx = st.licensed(st.ctx(&headers)).await;
    if let Err(f) = st.check_csrf(&headers) {
        return st.error_response(&ctx, f);
    }
    match st.run_bulk(&ctx, call).await {
        Ok(body) => axum::Json(body).into_response(),
        Err(f) => st.error_response(&ctx, f),
    }
}

async fn grid_run_call(
    State(st): State<Arc<AppState>>,
    headers: HeaderMap,
    axum::Json(call): axum::Json<RowItemCall>,
) -> Response {
    let ctx = st.licensed(st.ctx(&headers)).await;
    if let Err(f) = st.check_csrf(&headers) {
        return st.error_response(&ctx, f);
    }
    match st.run_row_item(&ctx, call).await {
        Ok(body) => axum::Json(body).into_response(),
        Err(f) => st.error_response(&ctx, f),
    }
}

async fn grid_view_call(
    State(st): State<Arc<AppState>>,
    headers: HeaderMap,
    axum::Json(call): axum::Json<DataGridViewCall>,
) -> Response {
    let ctx = st.licensed(st.ctx(&headers)).await;
    if let Err(f) = st.check_csrf(&headers) {
        return st.error_response(&ctx, f);
    }
    match st.save_view(&ctx, call).await {
        Ok(()) => StatusCode::NO_CONTENT.into_response(),
        Err(f) => st.error_response(&ctx, f),
    }
}

impl AppState {
    pub(super) async fn run_grid(&self, ctx: &Ctx, call: GridCall) -> Result<Json, Failure> {
        if call.path.len() > 32 || call.path.iter().flatten().any(|k| k.len() > 200) {
            return Err(Failure::App(Error::NotFound));
        }
        let (_, entry, screen) = self.open(ctx, &call.screen, call.params).await?;
        let grid = grid_at(ctx, &screen, &call.node)?;
        let action = grid.action();
        let info = Call {
            kind: CallKind::DataGrid,
            screen: entry.meta.name,
            action: Some(action),
            via_agent: ctx.via_agent(),
        };
        let query = call.query;
        self.intercept(ctx, &info, move || async move {
            let mut page = self.timed(grid.fetch_at(ctx.clone(), query, call.path)).await?;
            page.view = call.view.map(|v| grid.clean_view(v));
            let r = user_render(entry.meta.name, ctx);
            Ok(json!({
                "head": call.head.then(|| grid.head_html(&r, page.view.as_ref())),
                "rows": grid.rows_html(&page, &r),
                "foot": grid.foot_html(&page, &r),
                "count": page.count,
                "total": page.total,
                "next": page.next,
                "offset": page.offset,
            }))
        })
        .await
    }

    /// Saves or deletes the user's view of a grid. Needs a store, a signed in user and the grid's
    /// read access; the view is cleaned by the grid first.
    async fn save_view(&self, ctx: &Ctx, call: DataGridViewCall) -> Result<(), Failure> {
        let store = self.views.as_ref().ok_or(Failure::App(Error::NotFound))?;
        let user = ctx.identity().user.clone().ok_or(Failure::App(Error::Forbidden))?;
        let (_, entry, screen) = self.open(ctx, &call.screen, call.params).await?;
        let grid = grid_at(ctx, &screen, &call.node)?;
        let view = call.view.map(|v| grid.clean_view(v));
        self.timed(store.save(&user, &Self::view_key(ctx, entry.meta.name, &call.node), view)).await
    }

    /// Runs a row item. The row is loaded again by key through the grid's action (with the
    /// grid's read access), the item is built from that row, its action's access is checked and
    /// interceptors run. The client only names the row and the item; it never sends the input.
    async fn run_row_item(&self, ctx: &Ctx, call: RowItemCall) -> Result<Json, Failure> {
        if call.key.len() > 200 || call.item.len() > 32 {
            return Err(Failure::App(Error::NotFound));
        }
        let (_, entry, screen) = self.open(ctx, &call.screen, call.params).await?;
        let grid = grid_at(ctx, &screen, &call.node)?;
        let page = self.timed(grid.find_row(ctx.clone(), call.key.clone())).await?;
        let row = grid.row_call(&page, &call.key, &call.item).ok_or(Failure::App(Error::NotFound))?;
        if !ctx.identity().can(&row.meta.access) {
            return Err(Failure::App(Error::Forbidden));
        }
        let info = Call {
            kind: CallKind::DataGrid,
            screen: entry.meta.name,
            action: Some(row.meta),
            via_agent: ctx.via_agent(),
        };
        let result = self.intercept(ctx, &info, || self.timed((row.run)(ctx.clone()))).await;
        result?;
        Ok(json!({ "effects": [] }))
    }

    /// Saves an inline edit: the row is loaded again by key, the values are applied to the
    /// editable columns only and validated like a form, the save action's access is checked,
    /// interceptors run, and the saved row comes back as HTML.
    async fn run_edit(&self, ctx: &Ctx, call: EditCall) -> Result<Json, Failure> {
        if call.key.len() > 200 || call.values.len() > 64 {
            return Err(Failure::App(Error::NotFound));
        }
        let (_, entry, screen) = self.open(ctx, &call.screen, call.params).await?;
        let grid = grid_at(ctx, &screen, &call.node)?;
        let page = self.timed(grid.find_row(ctx.clone(), call.key.clone())).await?;
        let save = grid.edit_call(&page, &call.key, &call.values, ctx.locale(), ctx.time_zone())?;
        if !ctx.identity().can(&save.meta.access) {
            return Err(Failure::App(Error::Forbidden));
        }
        let info =
            Call { kind: CallKind::Form, screen: entry.meta.name, action: Some(save.meta), via_agent: ctx.via_agent() };
        let result = self.intercept(ctx, &info, || self.timed((save.run)(ctx.clone()))).await;
        result?;
        let mut page = self.timed(grid.find_row(ctx.clone(), call.key.clone())).await?;
        page.view = call.view.map(|v| grid.clean_view(v));
        let r = user_render(entry.meta.name, ctx);
        Ok(json!({ "row": grid.row_html(&page, &call.key, &r) }))
    }

    /// Runs a bulk action with the selected keys, or with the keys of every row matching the
    /// filters, which the server collects through the grid's action, page by page.
    async fn run_bulk(&self, ctx: &Ctx, mut call: BulkCall) -> Result<Json, Failure> {
        use crate::grid::MAX_BULK;
        let too_many = || Failure::App(Error::Denied(Text::key("zeyn.grid.too_many").arg("n", MAX_BULK.to_string())));
        let (_, entry, screen) = self.open(ctx, &call.screen, call.params).await?;
        let grid = grid_at(ctx, &screen, &call.node)?;
        let meta = grid.bulk_meta(call.action).ok_or(Failure::App(Error::NotFound))?;
        if !ctx.identity().can(&meta.access) {
            return Err(Failure::App(Error::Forbidden));
        }
        let keys = if call.all {
            let mut keys = Vec::new();
            let mut offset = 0u64;
            loop {
                let mut q = call.query.clone();
                q.page = crate::action::PageRequest::Offset { offset, limit: crate::action::MAX_PAGE_SIZE };
                q.aggregates.clear();
                q.with_total = false;
                let page = self.timed(grid.fetch(ctx.clone(), q)).await?;
                let n = page.count;
                keys.extend(grid.page_keys(&page));
                if keys.len() > MAX_BULK {
                    return Err(too_many());
                }
                if n < crate::action::MAX_PAGE_SIZE as usize {
                    break;
                }
                offset += n as u64;
            }
            keys
        } else {
            if call.keys.len() > MAX_BULK {
                return Err(too_many());
            }
            call.keys.dedup();
            call.keys
                .iter()
                .map(|k| grid.key_json(k))
                .collect::<Option<Vec<_>>>()
                .ok_or(Failure::App(Error::NotFound))?
        };
        if keys.is_empty() {
            return Ok(json!({ "effects": [], "count": 0 }));
        }
        let count = keys.len();
        let run = grid.bulk_call(call.action, keys).ok_or(Failure::App(Error::NotFound))?;
        let info = Call {
            kind: CallKind::DataGrid,
            screen: entry.meta.name,
            action: Some(run.meta),
            via_agent: ctx.via_agent(),
        };
        let result = self.intercept(ctx, &info, || self.timed((run.run)(ctx.clone()))).await;
        result?;
        Ok(json!({ "effects": [], "count": count }))
    }
}