| d23464c0 Andy McCall ↶ |
1 |
|
| 2 |
|
| e271a37b Andy McCall ↶ |
3 |
use super::settings::{csrf, require}; |
| 4 |
use super::{filters, lookup, lookup_role, Error, Page, RepoShell, Shell}; |
| 5 |
use crate::auth::{Access, Role, Viewer}; |
| d23464c0 Andy McCall ↶ |
6 |
use crate::repos::view; |
| 7 |
use crate::{db, Shared}; |
| 8 |
use askama::Template; |
| e271a37b Andy McCall ↶ |
9 |
use axum::extract::{Form, Path, State}; |
| 10 |
use serde::Deserialize; |
| d23464c0 Andy McCall ↶ |
11 |
|
| 12 |
#[derive(Template)] |
| 13 |
#[template(path = "bookmarks.html")] |
| 14 |
pub struct BookmarksPage { |
| 15 |
shell: Shell, |
| 16 |
repo: RepoShell, |
| 17 |
bookmarks: Vec<view::BookmarkView>, |
| e271a37b Andy McCall ↶ |
18 |
|
| 19 |
can_delete: bool, |
| 4f692533 Andy McCall ↶ |
20 |
|
| 21 |
can_run: bool, |
| e271a37b Andy McCall ↶ |
22 |
notice: Option<String>, |
| 23 |
error: Option<String>, |
| d23464c0 Andy McCall ↶ |
24 |
} |
| 25 |
|
| e271a37b Andy McCall ↶ |
26 |
async fn bookmarks_page( |
| 27 |
app: &Shared, |
| 28 |
shell: Shell, |
| 29 |
repo: crate::repos::RepoName, |
| 30 |
row: db::repos::RepoRow, |
| 31 |
role: Role, |
| 32 |
notice: Option<String>, |
| 33 |
error: Option<String>, |
| 34 |
) -> Result<Page<BookmarksPage>, Error> { |
| 35 |
let default = row.default_bookmark.clone(); |
| 36 |
let bookmarks = |
| 37 |
app.repos.read(&repo, move |r, cache| view::bookmarks(r, cache, &default)).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 38 |
let can_delete = shell.user.is_some() && role >= Role::Write; |
| 4f692533 Andy McCall ↶ |
39 |
let can_run = super::flow::why_not_run(crate::flow::enabled(app, &row), shell.user.is_some(), role).is_none(); |
| 40 |
Ok(Page(BookmarksPage { |
| 41 |
shell, |
| 42 |
repo: RepoShell::new(app, &row, "bookmarks").with_role(role), |
| 43 |
bookmarks, |
| 44 |
can_delete, |
| 45 |
can_run, |
| 46 |
notice, |
| 47 |
error, |
| 48 |
})) |
| e271a37b Andy McCall ↶ |
49 |
} |
| 50 |
|
| 19bb31ff Andy McCall ↶ |
51 |
pub async fn bookmarks( |
| 52 |
State(app): State<Shared>, |
| 53 |
viewer: Viewer, |
| 54 |
Path((owner, name)): Path<(String, String)>, |
| 55 |
) -> Result<Page<BookmarksPage>, Error> { |
| 56 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/bookmarks"), &viewer); |
| 57 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| e271a37b Andy McCall ↶ |
58 |
bookmarks_page(&app, shell, repo, row, role, None, None).await |
| 59 |
} |
| 60 |
|
| 61 |
#[derive(Deserialize)] |
| 62 |
pub struct DeleteBookmarkForm { |
| 63 |
#[serde(rename = "_csrf")] |
| 64 |
csrf_token: String, |
| 65 |
bookmark: String, |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
pub async fn delete_bookmark( |
| 72 |
State(app): State<Shared>, |
| 73 |
viewer: Viewer, |
| 74 |
Path((owner, name)): Path<(String, String)>, |
| 75 |
Form(f): Form<DeleteBookmarkForm>, |
| 76 |
) -> Result<Page<BookmarksPage>, Error> { |
| 77 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/bookmarks"), &viewer); |
| 78 |
let me = require(&viewer, &shell)?; |
| 79 |
let (repo, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 80 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 81 |
let bookmark = f.bookmark.trim().to_string(); |
| 82 |
if bookmark == row.default_bookmark { |
| 83 |
let msg = format!("{bookmark} is the default bookmark; choose another default in Settings first."); |
| 84 |
return bookmarks_page(&app, shell, repo, row, role, None, Some(msg)).await; |
| 85 |
} |
| 86 |
let target = bookmark.clone(); |
| 87 |
let outcome = app |
| 88 |
.repos |
| 89 |
.write(&repo, move |r, _| { |
| 90 |
let Some(state) = r.view()?.bookmarks.get(&target).copied() else { |
| 91 |
return Ok(Err(format!("There is no bookmark {target}."))); |
| 92 |
}; |
| 93 |
let update = oxy_core::protocol::RefUpdate { |
| 94 |
name: target.clone(), |
| 95 |
old_state: state, |
| 96 |
new_state: oxy_core::hash::ObjectRef::ZERO, |
| 97 |
force: true, |
| 98 |
attestation: None, |
| 99 |
}; |
| 100 |
let report = r.receive_push_report(Default::default(), vec![update], None, vec![])?; |
| 101 |
Ok(match report.results.first() { |
| 102 |
Some(res) if res.ok => Ok(state.to_hex()), |
| 103 |
Some(res) => Err(res.reason.clone()), |
| 104 |
None => Err("nothing happened".into()), |
| 105 |
}) |
| 106 |
}) |
| 107 |
.await |
| 108 |
.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 109 |
let old_state = match outcome { |
| 110 |
Ok(s) => s, |
| 111 |
Err(msg) => return bookmarks_page(&app, shell, repo, row, role, None, Some(format!("Could not delete {bookmark}: {msg}"))).await, |
| 112 |
}; |
| 113 |
let result = db::repos::PushOutcome { |
| 114 |
reference: bookmark.clone(), |
| 115 |
ok: true, |
| 116 |
reason: "deleted on the web".into(), |
| 117 |
old_state: old_state.clone(), |
| 118 |
new_state: oxy_core::hash::ObjectRef::ZERO.to_hex(), |
| 119 |
heads: 0, |
| 120 |
changes: 0, |
| 121 |
}; |
| 122 |
db::repos::record_push(&app.db, row.id, Some((me.account.id, &me.account.username)), &[result]).await.ok(); |
| 123 |
crate::hooks::emit( |
| 124 |
&app, |
| 125 |
&row, |
| 126 |
"push", |
| 127 |
&me.account.username, |
| 128 |
serde_json::json!({ "updates": [{ "reference": bookmark, "old_state": old_state, "new_state": oxy_core::hash::ObjectRef::ZERO.to_hex(), "heads": 0, "changes": 0, "deleted": true }], "via": "web" }), |
| 129 |
) |
| 130 |
.await; |
| 131 |
db::accounts::audit(&app.db, Some(&me.account), "bookmark.deleted", &format!("{owner}/{name}"), &bookmark).await.ok(); |
| 132 |
bookmarks_page( |
| 133 |
&app, |
| 134 |
shell, |
| 135 |
repo, |
| 136 |
row, |
| 137 |
role, |
| 138 |
Some(format!("Deleted {bookmark}. Its changes stay while anything else reaches them.")), |
| 139 |
None, |
| 140 |
) |
| 141 |
.await |
| d23464c0 Andy McCall ↶ |
142 |
} |
| 143 |
|
| 144 |
#[derive(Template)] |
| 145 |
#[template(path = "tags.html")] |
| 146 |
pub struct TagsPage { |
| 147 |
shell: Shell, |
| 148 |
repo: RepoShell, |
| 149 |
tags: Vec<view::TagView>, |
| 150 |
} |
| 151 |
|
| 19bb31ff Andy McCall ↶ |
152 |
pub async fn tags(State(app): State<Shared>, viewer: Viewer, Path((owner, name)): Path<(String, String)>) -> Result<Page<TagsPage>, Error> { |
| 153 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/tags"), &viewer); |
| 154 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 9ff27da5 Andy McCall ↶ |
155 |
let tags = app.repos.read(&repo, |r, _| view::tags(r)).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 19bb31ff Andy McCall ↶ |
156 |
Ok(Page(TagsPage { shell, repo: RepoShell::new(&app, &row, "tags").with_role(role), tags })) |
| d23464c0 Andy McCall ↶ |
157 |
} |
| 158 |
|
| 159 |
#[derive(Template)] |
| 160 |
#[template(path = "conflicts.html")] |
| 161 |
pub struct ConflictsPage { |
| 162 |
shell: Shell, |
| 163 |
repo: RepoShell, |
| 164 |
conflicts: Vec<view::ConflictView>, |
| 165 |
} |
| 166 |
|
| 19bb31ff Andy McCall ↶ |
167 |
pub async fn conflicts( |
| 168 |
State(app): State<Shared>, |
| 169 |
viewer: Viewer, |
| 170 |
Path((owner, name)): Path<(String, String)>, |
| 171 |
) -> Result<Page<ConflictsPage>, Error> { |
| 172 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/conflicts"), &viewer); |
| 173 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| d23464c0 Andy McCall ↶ |
174 |
let conflicts = app.repos.read(&repo, view::conflicts).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 19bb31ff Andy McCall ↶ |
175 |
Ok(Page(ConflictsPage { shell, repo: RepoShell::new(&app, &row, "conflicts").with_role(role), conflicts })) |
| d23464c0 Andy McCall ↶ |
176 |
} |
| 177 |
|
| 178 |
#[derive(Template)] |
| 179 |
#[template(path = "trust.html")] |
| 180 |
pub struct TrustPage { |
| 181 |
shell: Shell, |
| 182 |
repo: RepoShell, |
| 183 |
t: view::Trust, |
| 184 |
} |
| 185 |
|
| 19bb31ff Andy McCall ↶ |
186 |
pub async fn trust( |
| 187 |
State(app): State<Shared>, |
| 188 |
viewer: Viewer, |
| 189 |
Path((owner, name)): Path<(String, String)>, |
| 190 |
) -> Result<Page<TrustPage>, Error> { |
| 191 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/trust"), &viewer); |
| 192 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| d23464c0 Andy McCall ↶ |
193 |
let key = app.hub_key.key; |
| 9ff27da5 Andy McCall ↶ |
194 |
let t = app.repos.read(&repo, move |r, cache| view::trust(r, cache, &key)).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 19bb31ff Andy McCall ↶ |
195 |
Ok(Page(TrustPage { shell, repo: RepoShell::new(&app, &row, "trust").with_role(role), t })) |
| d23464c0 Andy McCall ↶ |
196 |
} |
| 197 |
|
| 198 |
#[derive(Template)] |
| 199 |
#[template(path = "pushes.html")] |
| 200 |
pub struct PushesPage { |
| 201 |
shell: Shell, |
| 202 |
repo: RepoShell, |
| 203 |
pushes: Vec<db::repos::PushRow>, |
| 204 |
} |
| 205 |
|
| 19bb31ff Andy McCall ↶ |
206 |
pub async fn pushes( |
| 207 |
State(app): State<Shared>, |
| 208 |
viewer: Viewer, |
| 209 |
Path((owner, name)): Path<(String, String)>, |
| 210 |
) -> Result<Page<PushesPage>, Error> { |
| 211 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/pushes"), &viewer); |
| 212 |
let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| d23464c0 Andy McCall ↶ |
213 |
let pushes = db::repos::pushes(&app.db, row.id, 200).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 19bb31ff Andy McCall ↶ |
214 |
Ok(Page(PushesPage { shell, repo: RepoShell::new(&app, &row, "pushes").with_role(role), pushes })) |
| d23464c0 Andy McCall ↶ |
215 |
} |