OXYGENHUB
# 34beb1a7e376 oxygenhub / crates / hub / src / web / refs.rs
3.8 KiB · Rust · last changed Milestone 0.2: accounts and access 1 month ago source blame history raw
1
//! Bookmarks, tags, conflicts, trust and pushes pages.
2
3
use super::{filters, lookup, Error, Page, RepoShell, Shell};
4
use crate::auth::Viewer;
5
use crate::repos::view;
6
use crate::{db, Shared};
7
use askama::Template;
8
use axum::extract::{Path, State};
9
10
#[derive(Template)]
11
#[template(path = "bookmarks.html")]
12
pub struct BookmarksPage {
13
    shell: Shell,
14
    repo: RepoShell,
15
    bookmarks: Vec<view::BookmarkView>,
16
}
17
18
pub async fn bookmarks(
19
    State(app): State<Shared>,
20
    viewer: Viewer,
21
    Path((owner, name)): Path<(String, String)>,
22
) -> Result<Page<BookmarksPage>, Error> {
23
    let shell = Shell::new(&app, &format!("/{owner}/{name}/bookmarks"), &viewer);
24
    let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
25
    let default = row.default_bookmark.clone();
26
    let bookmarks =
27
        app.repos.read(&repo, move |r, cache| view::bookmarks(r, cache, &default)).await.map_err(|e| Error::from_repo(shell.clone(), e))?;
28
    Ok(Page(BookmarksPage { shell, repo: RepoShell::new(&app, &row, "bookmarks").with_role(role), bookmarks }))
29
}
30
31
#[derive(Template)]
32
#[template(path = "tags.html")]
33
pub struct TagsPage {
34
    shell: Shell,
35
    repo: RepoShell,
36
    tags: Vec<view::TagView>,
37
}
38
39
pub async fn tags(State(app): State<Shared>, viewer: Viewer, Path((owner, name)): Path<(String, String)>) -> Result<Page<TagsPage>, Error> {
40
    let shell = Shell::new(&app, &format!("/{owner}/{name}/tags"), &viewer);
41
    let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
42
    let tags = app.repos.read(&repo, |r, _| view::tags(r)).await.map_err(|e| Error::from_repo(shell.clone(), e))?;
43
    Ok(Page(TagsPage { shell, repo: RepoShell::new(&app, &row, "tags").with_role(role), tags }))
44
}
45
46
#[derive(Template)]
47
#[template(path = "conflicts.html")]
48
pub struct ConflictsPage {
49
    shell: Shell,
50
    repo: RepoShell,
51
    conflicts: Vec<view::ConflictView>,
52
}
53
54
pub async fn conflicts(
55
    State(app): State<Shared>,
56
    viewer: Viewer,
57
    Path((owner, name)): Path<(String, String)>,
58
) -> Result<Page<ConflictsPage>, Error> {
59
    let shell = Shell::new(&app, &format!("/{owner}/{name}/conflicts"), &viewer);
60
    let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
61
    let conflicts = app.repos.read(&repo, view::conflicts).await.map_err(|e| Error::from_repo(shell.clone(), e))?;
62
    Ok(Page(ConflictsPage { shell, repo: RepoShell::new(&app, &row, "conflicts").with_role(role), conflicts }))
63
}
64
65
#[derive(Template)]
66
#[template(path = "trust.html")]
67
pub struct TrustPage {
68
    shell: Shell,
69
    repo: RepoShell,
70
    t: view::Trust,
71
}
72
73
pub async fn trust(
74
    State(app): State<Shared>,
75
    viewer: Viewer,
76
    Path((owner, name)): Path<(String, String)>,
77
) -> Result<Page<TrustPage>, Error> {
78
    let shell = Shell::new(&app, &format!("/{owner}/{name}/trust"), &viewer);
79
    let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
80
    let key = app.hub_key.key;
81
    let t = app.repos.read(&repo, move |r, cache| view::trust(r, cache, &key)).await.map_err(|e| Error::from_repo(shell.clone(), e))?;
82
    Ok(Page(TrustPage { shell, repo: RepoShell::new(&app, &row, "trust").with_role(role), t }))
83
}
84
85
#[derive(Template)]
86
#[template(path = "pushes.html")]
87
pub struct PushesPage {
88
    shell: Shell,
89
    repo: RepoShell,
90
    pushes: Vec<db::repos::PushRow>,
91
}
92
93
pub async fn pushes(
94
    State(app): State<Shared>,
95
    viewer: Viewer,
96
    Path((owner, name)): Path<(String, String)>,
97
) -> Result<Page<PushesPage>, Error> {
98
    let shell = Shell::new(&app, &format!("/{owner}/{name}/pushes"), &viewer);
99
    let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
100
    let pushes = db::repos::pushes(&app.db, row.id, 200).await.map_err(|e| Error::from_db(shell.clone(), e))?;
101
    Ok(Page(PushesPage { shell, repo: RepoShell::new(&app, &row, "pushes").with_role(role), pushes }))
102
}