laterite-web 0.9.14

Laterite CMF public web layer: themes, static-site generation, page metadata, canonical URLs, and sitemaps
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! Queries over the theme tables.
//!
//! A write that an operator makes from the admin is audited there; this layer
//! keeps the revision that lets a template save be undone, and refuses what the
//! rows forbid: a locked theme, a parent cycle, deleting the active theme.

use super::schema::{ThemeTemplateRevisions, ThemeTemplates, Themes};
use super::{valid_code, valid_path, Kind, Revision, Template, Theme, ThemeConfig, ThemeError};
use laterite_core::query::{bind_values, build, insert_returning_id};
use laterite_core::{AnyRowExt, Db};
use sea_query::{Expr, Order, Query};
use sqlx::any::AnyRow;

fn now() -> String {
    chrono::Utc::now().to_rfc3339()
}

fn theme_from_row(row: &AnyRow) -> Result<Theme, ThemeError> {
    let config_source = row.get_text("config")?;
    let settings = row
        .get_text_opt("settings")?
        .and_then(|text| serde_json::from_str(&text).ok())
        .filter(serde_json::Value::is_object)
        .unwrap_or_else(|| serde_json::json!({}));
    Ok(Theme {
        id: row.get_int("id")?,
        code: row.get_text("code")?,
        parent: row.get_text_opt("parent")?,
        locked: row.get_bool("locked")?,
        config: ThemeConfig::parse(&config_source)?,
        config_source,
        settings,
        created_at: row.get_text("created_at")?,
        updated_at: row.get_text("updated_at")?,
    })
}

fn template_from_row(row: &AnyRow) -> Result<Template, ThemeError> {
    let settings = row.get_text("settings")?;
    Ok(Template {
        id: row.get_int("id")?,
        theme_id: row.get_int("theme_id")?,
        kind: row.get_text("kind")?.parse()?,
        path: row.get_text("path")?,
        settings: serde_json::from_str(&settings).unwrap_or(serde_json::Value::Null),
        markup: row.get_text("markup")?,
        created_at: row.get_text("created_at")?,
        updated_at: row.get_text("updated_at")?,
    })
}

fn revision_from_row(row: &AnyRow) -> Result<Revision, ThemeError> {
    let settings = row.get_text("settings")?;
    Ok(Revision {
        id: row.get_int("id")?,
        template_id: row.get_int("template_id")?,
        settings: serde_json::from_str(&settings).unwrap_or(serde_json::Value::Null),
        markup: row.get_text("markup")?,
        saved_at: row.get_text("saved_at")?,
        saved_by: row.get_int_opt("saved_by")?,
    })
}

const THEME_COLUMNS: [Themes; 8] = [
    Themes::Id,
    Themes::Code,
    Themes::Parent,
    Themes::Locked,
    Themes::Config,
    Themes::Settings,
    Themes::CreatedAt,
    Themes::UpdatedAt,
];

const TEMPLATE_COLUMNS: [ThemeTemplates; 8] = [
    ThemeTemplates::Id,
    ThemeTemplates::ThemeId,
    ThemeTemplates::Kind,
    ThemeTemplates::Path,
    ThemeTemplates::Settings,
    ThemeTemplates::Markup,
    ThemeTemplates::CreatedAt,
    ThemeTemplates::UpdatedAt,
];

/// Every theme, by code.
pub async fn list_themes(db: &Db) -> Result<Vec<Theme>, ThemeError> {
    let (sql, values) = build(
        db.backend,
        Query::select()
            .columns(THEME_COLUMNS)
            .from(Themes::Table)
            .order_by(Themes::Code, Order::Asc)
            .to_owned(),
    );
    let rows = bind_values(sqlx::query(&sql), values)
        .fetch_all(&db.pool)
        .await?;
    rows.iter().map(theme_from_row).collect()
}

/// The theme with this code, if any.
pub async fn find_theme(db: &Db, code: &str) -> Result<Option<Theme>, ThemeError> {
    let (sql, values) = build(
        db.backend,
        Query::select()
            .columns(THEME_COLUMNS)
            .from(Themes::Table)
            .and_where(Expr::col(Themes::Code).eq(code))
            .to_owned(),
    );
    let row = bind_values(sqlx::query(&sql), values)
        .fetch_optional(&db.pool)
        .await?;
    row.as_ref().map(theme_from_row).transpose()
}

async fn require_theme(db: &Db, code: &str) -> Result<Theme, ThemeError> {
    find_theme(db, code)
        .await?
        .ok_or_else(|| ThemeError::NotFound(code.to_string()))
}

/// Refuses a parent that does not exist or that leads back to `code`.
async fn check_parent(db: &Db, code: &str, parent: Option<&str>) -> Result<(), ThemeError> {
    let Some(parent) = parent else {
        return Ok(());
    };
    let mut next = Some(parent.to_string());
    let mut hops = 0;
    while let Some(current) = next {
        if current == code {
            return Err(ThemeError::Parent(format!(
                "the parent {parent:?} leads back to {code:?}"
            )));
        }
        hops += 1;
        if hops > 32 {
            return Err(ThemeError::Parent(format!(
                "the parent chain from {parent:?} is too long"
            )));
        }
        let theme = find_theme(db, &current)
            .await?
            .ok_or_else(|| ThemeError::Parent(format!("no parent theme {current:?}")))?;
        next = theme.parent;
    }
    Ok(())
}

/// Creates a theme from its code and YAML config.
pub async fn create_theme(db: &Db, code: &str, config_yaml: &str) -> Result<Theme, ThemeError> {
    if !valid_code(code) {
        return Err(ThemeError::Code(code.to_string()));
    }
    let config = ThemeConfig::parse(config_yaml)?;
    if find_theme(db, code).await?.is_some() {
        return Err(ThemeError::Exists(code.to_string()));
    }
    check_parent(db, code, config.parent.as_deref()).await?;
    let now = now();
    let insert = Query::insert()
        .into_table(Themes::Table)
        .columns([
            Themes::Code,
            Themes::Parent,
            Themes::Locked,
            Themes::Config,
            Themes::CreatedAt,
            Themes::UpdatedAt,
        ])
        .values_panic([
            code.into(),
            config.parent.clone().into(),
            false.into(),
            config_yaml.into(),
            now.clone().into(),
            now.into(),
        ])
        .to_owned();
    insert_returning_id(db, insert, Themes::Id).await?;
    require_theme(db, code).await
}

/// Replaces a theme's YAML config; the parent column follows it.
pub async fn update_theme_config(
    db: &Db,
    code: &str,
    config_yaml: &str,
) -> Result<Theme, ThemeError> {
    let theme = require_theme(db, code).await?;
    if theme.locked {
        return Err(ThemeError::Locked(code.to_string()));
    }
    let config = ThemeConfig::parse(config_yaml)?;
    check_parent(db, code, config.parent.as_deref()).await?;
    let (sql, values) = build(
        db.backend,
        Query::update()
            .table(Themes::Table)
            .value(Themes::Config, config_yaml)
            .value(Themes::Parent, config.parent.clone())
            .value(Themes::UpdatedAt, now())
            .and_where(Expr::col(Themes::Id).eq(theme.id))
            .to_owned(),
    );
    bind_values(sqlx::query(&sql), values)
        .execute(&db.pool)
        .await?;
    require_theme(db, code).await
}

/// Replaces a theme's settings values. `settings` must be a JSON object.
pub async fn save_settings(
    db: &Db,
    code: &str,
    settings: &serde_json::Value,
) -> Result<(), ThemeError> {
    if !settings.is_object() {
        return Err(ThemeError::Config(
            "the settings must be a JSON object".into(),
        ));
    }
    let theme = require_theme(db, code).await?;
    if theme.locked {
        return Err(ThemeError::Locked(code.to_string()));
    }
    let (sql, values) = build(
        db.backend,
        Query::update()
            .table(Themes::Table)
            .value(Themes::Settings, settings.to_string())
            .value(Themes::UpdatedAt, now())
            .and_where(Expr::col(Themes::Id).eq(theme.id))
            .to_owned(),
    );
    bind_values(sqlx::query(&sql), values)
        .execute(&db.pool)
        .await?;
    Ok(())
}

/// The settings form in force for a chain (nearest first): the first theme
/// that declares one, as the config falls through to the parent.
pub fn form_fields(chain: &[Theme]) -> Vec<laterite_admin::form::FormField> {
    chain
        .iter()
        .find_map(|t| t.config.form.as_ref())
        .map(|f| f.fields.clone())
        .unwrap_or_default()
}

/// Locks or unlocks a theme.
pub async fn set_locked(db: &Db, code: &str, locked: bool) -> Result<(), ThemeError> {
    let theme = require_theme(db, code).await?;
    let (sql, values) = build(
        db.backend,
        Query::update()
            .table(Themes::Table)
            .value(Themes::Locked, locked)
            .value(Themes::UpdatedAt, now())
            .and_where(Expr::col(Themes::Id).eq(theme.id))
            .to_owned(),
    );
    bind_values(sqlx::query(&sql), values)
        .execute(&db.pool)
        .await?;
    Ok(())
}

/// The codes of the themes whose parent is `code`.
pub async fn children(db: &Db, code: &str) -> Result<Vec<String>, ThemeError> {
    let (sql, values) = build(
        db.backend,
        Query::select()
            .column(Themes::Code)
            .from(Themes::Table)
            .and_where(Expr::col(Themes::Parent).eq(code))
            .order_by(Themes::Code, Order::Asc)
            .to_owned(),
    );
    let rows = bind_values(sqlx::query(&sql), values)
        .fetch_all(&db.pool)
        .await?;
    rows.iter().map(|r| Ok(r.get_text("code")?)).collect()
}

/// Deletes a theme and its templates. Refuses the active theme, a locked
/// theme, and a theme other themes name as their parent.
pub async fn delete_theme(db: &Db, code: &str) -> Result<(), ThemeError> {
    let theme = require_theme(db, code).await?;
    if theme.locked {
        return Err(ThemeError::Locked(code.to_string()));
    }
    let settings: super::ThemeSettings = laterite_admin::settings::load(db).await?;
    if settings.active.as_deref() == Some(code) {
        return Err(ThemeError::Active(code.to_string()));
    }
    let dependants = children(db, code).await?;
    if !dependants.is_empty() {
        return Err(ThemeError::Parent(format!(
            "{code:?} is the parent of {}",
            dependants.join(", ")
        )));
    }
    let (sql, values) = build(
        db.backend,
        Query::delete()
            .from_table(Themes::Table)
            .and_where(Expr::col(Themes::Id).eq(theme.id))
            .to_owned(),
    );
    bind_values(sqlx::query(&sql), values)
        .execute(&db.pool)
        .await?;
    Ok(())
}

/// Copies a theme, templates included, under a new code. The copy is unlocked
/// and keeps the original's parent.
pub async fn duplicate_theme(db: &Db, code: &str, new_code: &str) -> Result<Theme, ThemeError> {
    let source = require_theme(db, code).await?;
    let copy = create_theme(db, new_code, &source.config_source).await?;
    for t in list_templates(db, source.id, None).await? {
        save_template(
            db,
            copy.id,
            t.kind,
            &t.path,
            Some(t.settings),
            &t.markup,
            None,
        )
        .await?;
    }
    Ok(copy)
}

/// Creates an empty theme whose parent is `code`, named `name`.
pub async fn create_child_theme(
    db: &Db,
    code: &str,
    new_code: &str,
    name: &str,
) -> Result<Theme, ThemeError> {
    let parent = require_theme(db, code).await?;
    let config = ThemeConfig {
        name: name.to_string(),
        author: parent.config.author.clone(),
        homepage: parent.config.homepage.clone(),
        description: String::new(),
        parent: Some(code.to_string()),
        require: parent.config.require.clone(),
        form: None,
    };
    create_theme(db, new_code, &config.to_yaml()?).await
}

/// A theme's own templates, by kind and path, or those of one kind.
pub async fn list_templates(
    db: &Db,
    theme_id: i64,
    kind: Option<Kind>,
) -> Result<Vec<Template>, ThemeError> {
    // Built and rendered in one block: a statement builder is not `Send`, and
    // one left in scope across the await makes the whole future not `Send`.
    let (sql, values) = {
        let mut query = Query::select()
            .columns(TEMPLATE_COLUMNS)
            .from(ThemeTemplates::Table)
            .and_where(Expr::col(ThemeTemplates::ThemeId).eq(theme_id))
            .order_by(ThemeTemplates::Kind, Order::Asc)
            .order_by(ThemeTemplates::Path, Order::Asc)
            .to_owned();
        if let Some(kind) = kind {
            query.and_where(Expr::col(ThemeTemplates::Kind).eq(kind.as_str()));
        }
        build(db.backend, query)
    };
    let rows = bind_values(sqlx::query(&sql), values)
        .fetch_all(&db.pool)
        .await?;
    rows.iter().map(template_from_row).collect()
}

/// A theme's own template at `path`, if any.
pub async fn find_template(
    db: &Db,
    theme_id: i64,
    kind: Kind,
    path: &str,
) -> Result<Option<Template>, ThemeError> {
    let (sql, values) = build(
        db.backend,
        Query::select()
            .columns(TEMPLATE_COLUMNS)
            .from(ThemeTemplates::Table)
            .and_where(Expr::col(ThemeTemplates::ThemeId).eq(theme_id))
            .and_where(Expr::col(ThemeTemplates::Kind).eq(kind.as_str()))
            .and_where(Expr::col(ThemeTemplates::Path).eq(path))
            .to_owned(),
    );
    let row = bind_values(sqlx::query(&sql), values)
        .fetch_optional(&db.pool)
        .await?;
    row.as_ref().map(template_from_row).transpose()
}

/// A template by id.
pub async fn template(db: &Db, id: i64) -> Result<Option<Template>, ThemeError> {
    let (sql, values) = build(
        db.backend,
        Query::select()
            .columns(TEMPLATE_COLUMNS)
            .from(ThemeTemplates::Table)
            .and_where(Expr::col(ThemeTemplates::Id).eq(id))
            .to_owned(),
    );
    let row = bind_values(sqlx::query(&sql), values)
        .fetch_optional(&db.pool)
        .await?;
    row.as_ref().map(template_from_row).transpose()
}

/// The template at `path` for the theme `code`: its own row, else the nearest
/// ancestor's. The theme it came from is returned with it.
pub async fn resolve_template(
    db: &Db,
    code: &str,
    kind: Kind,
    path: &str,
) -> Result<Option<(Theme, Template)>, ThemeError> {
    let mut theme = require_theme(db, code).await?;
    let mut hops = 0;
    loop {
        if let Some(t) = find_template(db, theme.id, kind, path).await? {
            return Ok(Some((theme, t)));
        }
        let Some(parent) = theme.parent.clone() else {
            return Ok(None);
        };
        hops += 1;
        if hops > 32 {
            return Ok(None);
        }
        theme = match find_theme(db, &parent).await? {
            Some(p) => p,
            None => return Ok(None),
        };
    }
}

/// A theme and its ancestors, nearest first: the theme, its parent, the
/// parent's parent. A missing ancestor ends the chain; a cycle is cut at 32.
pub async fn chain(db: &Db, code: &str) -> Result<Vec<Theme>, ThemeError> {
    let mut out = vec![require_theme(db, code).await?];
    while let Some(parent) = out.last().and_then(|t| t.parent.clone()) {
        if out.len() > 32 {
            break;
        }
        match find_theme(db, &parent).await? {
            Some(p) => out.push(p),
            None => break,
        }
    }
    Ok(out)
}

async fn theme_by_id(db: &Db, id: i64) -> Result<Theme, ThemeError> {
    let (sql, values) = build(
        db.backend,
        Query::select()
            .columns(THEME_COLUMNS)
            .from(Themes::Table)
            .and_where(Expr::col(Themes::Id).eq(id))
            .to_owned(),
    );
    let row = bind_values(sqlx::query(&sql), values)
        .fetch_optional(&db.pool)
        .await?;
    match row {
        Some(r) => theme_from_row(&r),
        None => Err(ThemeError::NotFound(id.to_string())),
    }
}

fn touch_theme(
    db: &Db,
    id: i64,
) -> impl std::future::Future<Output = Result<(), sqlx::Error>> + Send + '_ {
    let (sql, values) = build(
        db.backend,
        Query::update()
            .table(Themes::Table)
            .value(Themes::UpdatedAt, now())
            .and_where(Expr::col(Themes::Id).eq(id))
            .to_owned(),
    );
    async move {
        bind_values(sqlx::query(&sql), values)
            .execute(&db.pool)
            .await?;
        Ok(())
    }
}

/// Creates or replaces the template at `path`. `settings` of `None` keeps the
/// current bag (an empty object for a new template). A replaced template's
/// previous content is kept as a revision attributed to `saved_by`. Refused for
/// a locked theme.
pub async fn save_template(
    db: &Db,
    theme_id: i64,
    kind: Kind,
    path: &str,
    settings: Option<serde_json::Value>,
    markup: &str,
    saved_by: Option<i64>,
) -> Result<Template, ThemeError> {
    if !valid_path(path) {
        return Err(ThemeError::Path(path.to_string()));
    }
    let theme = theme_by_id(db, theme_id).await?;
    if theme.locked {
        return Err(ThemeError::Locked(theme.code));
    }
    let now = now();
    let id = match find_template(db, theme_id, kind, path).await? {
        Some(current) => {
            record_revision(db, &current, &now, saved_by).await?;
            let settings = settings.unwrap_or(current.settings);
            let (sql, values) = build(
                db.backend,
                Query::update()
                    .table(ThemeTemplates::Table)
                    .value(ThemeTemplates::Settings, settings.to_string())
                    .value(ThemeTemplates::Markup, markup)
                    .value(ThemeTemplates::UpdatedAt, now.clone())
                    .and_where(Expr::col(ThemeTemplates::Id).eq(current.id))
                    .to_owned(),
            );
            bind_values(sqlx::query(&sql), values)
                .execute(&db.pool)
                .await?;
            current.id
        }
        None => {
            let settings = settings.unwrap_or_else(|| serde_json::json!({}));
            let insert = Query::insert()
                .into_table(ThemeTemplates::Table)
                .columns([
                    ThemeTemplates::ThemeId,
                    ThemeTemplates::Kind,
                    ThemeTemplates::Path,
                    ThemeTemplates::Settings,
                    ThemeTemplates::Markup,
                    ThemeTemplates::CreatedAt,
                    ThemeTemplates::UpdatedAt,
                ])
                .values_panic([
                    theme_id.into(),
                    kind.as_str().into(),
                    path.into(),
                    settings.to_string().into(),
                    markup.into(),
                    now.clone().into(),
                    now.clone().into(),
                ])
                .to_owned();
            insert_returning_id(db, insert, ThemeTemplates::Id).await?
        }
    };
    touch_theme(db, theme_id).await?;
    template(db, id)
        .await?
        .ok_or_else(|| ThemeError::TemplateNotFound(path.to_string()))
}

async fn record_revision(
    db: &Db,
    current: &Template,
    saved_at: &str,
    saved_by: Option<i64>,
) -> Result<(), ThemeError> {
    let insert = Query::insert()
        .into_table(ThemeTemplateRevisions::Table)
        .columns([
            ThemeTemplateRevisions::TemplateId,
            ThemeTemplateRevisions::Settings,
            ThemeTemplateRevisions::Markup,
            ThemeTemplateRevisions::SavedAt,
            ThemeTemplateRevisions::SavedBy,
        ])
        .values_panic([
            current.id.into(),
            current.settings.to_string().into(),
            current.markup.clone().into(),
            saved_at.into(),
            saved_by.into(),
        ])
        .to_owned();
    insert_returning_id(db, insert, ThemeTemplateRevisions::Id).await?;
    Ok(())
}

/// Deletes a template and its revisions. Refused for a locked theme.
pub async fn delete_template(db: &Db, id: i64) -> Result<(), ThemeError> {
    let Some(current) = template(db, id).await? else {
        return Err(ThemeError::TemplateNotFound(id.to_string()));
    };
    let theme = theme_by_id(db, current.theme_id).await?;
    if theme.locked {
        return Err(ThemeError::Locked(theme.code));
    }
    let (sql, values) = build(
        db.backend,
        Query::delete()
            .from_table(ThemeTemplates::Table)
            .and_where(Expr::col(ThemeTemplates::Id).eq(id))
            .to_owned(),
    );
    bind_values(sqlx::query(&sql), values)
        .execute(&db.pool)
        .await?;
    touch_theme(db, theme.id).await?;
    Ok(())
}

/// A template's revisions, newest first.
pub async fn revisions(db: &Db, template_id: i64) -> Result<Vec<Revision>, ThemeError> {
    let (sql, values) = build(
        db.backend,
        Query::select()
            .columns([
                ThemeTemplateRevisions::Id,
                ThemeTemplateRevisions::TemplateId,
                ThemeTemplateRevisions::Settings,
                ThemeTemplateRevisions::Markup,
                ThemeTemplateRevisions::SavedAt,
                ThemeTemplateRevisions::SavedBy,
            ])
            .from(ThemeTemplateRevisions::Table)
            .and_where(Expr::col(ThemeTemplateRevisions::TemplateId).eq(template_id))
            .order_by(ThemeTemplateRevisions::Id, Order::Desc)
            .to_owned(),
    );
    let rows = bind_values(sqlx::query(&sql), values)
        .fetch_all(&db.pool)
        .await?;
    rows.iter().map(revision_from_row).collect()
}

/// Puts a revision's content back as the template's current content. The
/// content it replaces becomes a revision in turn.
pub async fn restore_revision(
    db: &Db,
    revision_id: i64,
    saved_by: Option<i64>,
) -> Result<Template, ThemeError> {
    let (sql, values) = build(
        db.backend,
        Query::select()
            .columns([
                ThemeTemplateRevisions::Id,
                ThemeTemplateRevisions::TemplateId,
                ThemeTemplateRevisions::Settings,
                ThemeTemplateRevisions::Markup,
                ThemeTemplateRevisions::SavedAt,
                ThemeTemplateRevisions::SavedBy,
            ])
            .from(ThemeTemplateRevisions::Table)
            .and_where(Expr::col(ThemeTemplateRevisions::Id).eq(revision_id))
            .to_owned(),
    );
    let row = bind_values(sqlx::query(&sql), values)
        .fetch_optional(&db.pool)
        .await?;
    let revision = match row {
        Some(r) => revision_from_row(&r)?,
        None => return Err(ThemeError::TemplateNotFound(revision_id.to_string())),
    };
    let current = template(db, revision.template_id)
        .await?
        .ok_or_else(|| ThemeError::TemplateNotFound(revision.template_id.to_string()))?;
    save_template(
        db,
        current.theme_id,
        current.kind,
        &current.path,
        Some(revision.settings),
        &revision.markup,
        saved_by,
    )
    .await
}