| 1 |
|
| 2 |
|
| 3 |
use super::settings::{csrf, require}; |
| 4 |
use super::{filters, lookup, lookup_role, Error, Page, RepoShell, Shell}; |
| 5 |
use crate::auth::{Access, Role, Viewer}; |
| 6 |
use crate::repos::view; |
| 7 |
use crate::{db, Shared}; |
| 8 |
use askama::Template; |
| 9 |
use axum::extract::{Form, Path, State}; |
| 10 |
use serde::Deserialize; |
| 11 |
|
| 12 |
#[derive(Template)] |
| 13 |
#[template(path = "bookmarks.html")] |
| 14 |
pub struct BookmarksPage { |
| 15 |
shell: Shell, |
| 16 |
repo: RepoShell, |
| 17 |
bookmarks: Vec<view::BookmarkView>, |
| 18 |
|
| 19 |
can_delete: bool, |
| 20 |
|
| 21 |
can_run: bool, |
| 22 |
notice: Option<String>, |
| 23 |
error: Option<String>, |
| 24 |
} |
| 25 |
|
| 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; |
| 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 |
})) |
| 49 |
} |
| 50 |
|
| 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?; |
| 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 |
| 142 |
} |
| 143 |
|
| 144 |
#[derive(Deserialize)] |
| 145 |
pub struct CreateBookmarkForm { |
| 146 |
#[serde(rename = "_csrf")] |
| 147 |
csrf_token: String, |
| 148 |
name: String, |
| 149 |
|
| 150 |
#[serde(default)] |
| 151 |
from: String, |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
pub async fn create_bookmark( |
| 158 |
State(app): State<Shared>, |
| 159 |
viewer: Viewer, |
| 160 |
Path((owner, name)): Path<(String, String)>, |
| 161 |
Form(f): Form<CreateBookmarkForm>, |
| 162 |
) -> Result<Page<BookmarksPage>, Error> { |
| 163 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/bookmarks"), &viewer); |
| 164 |
let me = require(&viewer, &shell)?; |
| 165 |
let (repo, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 166 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 167 |
let bookmark = f.name.trim().to_string(); |
| 168 |
let from = match f.from.trim() { |
| 169 |
"" => row.default_bookmark.clone(), |
| 170 |
s => s.to_string(), |
| 171 |
}; |
| 172 |
if bookmark.is_empty() || bookmark.chars().any(char::is_whitespace) { |
| 173 |
return bookmarks_page(&app, shell, repo, row, role, None, Some("A bookmark's name has no spaces.".into())).await; |
| 174 |
} |
| 175 |
let (wanted, spec) = (bookmark.clone(), from.clone()); |
| 176 |
let found = app |
| 177 |
.repos |
| 178 |
.read(&repo, move |r, cache| { |
| 179 |
if r.view()?.bookmarks.contains_key(&wanted) { |
| 180 |
return Ok(Err(format!("{wanted} is a bookmark already."))); |
| 181 |
} |
| 182 |
Ok(view::resolve(r, cache, &spec).map(|t| t.heads).map_err(|e| e.to_string())) |
| 183 |
}) |
| 184 |
.await |
| 185 |
.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 186 |
let heads = match found { |
| 187 |
Ok(heads) if !heads.is_empty() => heads, |
| 188 |
Ok(_) => return bookmarks_page(&app, shell, repo, row, role, None, Some(format!("{from} holds no changes to start from."))).await, |
| 189 |
Err(msg) => return bookmarks_page(&app, shell, repo, row, role, None, Some(msg)).await, |
| 190 |
}; |
| 191 |
let made = set_bookmark(&app, &repo, me.account.id, &bookmark, heads.clone()).await; |
| 192 |
if let Err(e) = made { |
| 193 |
return bookmarks_page(&app, shell, repo, row, role, None, Some(format!("Could not create {bookmark}: {e}"))).await; |
| 194 |
} |
| 195 |
let made = bookmark.clone(); |
| 196 |
let new_state = app |
| 197 |
.repos |
| 198 |
.read(&repo, move |r, _| Ok(r.view()?.bookmarks.get(&made).map(|s| s.to_hex()).unwrap_or_default())) |
| 199 |
.await |
| 200 |
.unwrap_or_default(); |
| 201 |
let result = db::repos::PushOutcome { |
| 202 |
reference: bookmark.clone(), |
| 203 |
ok: true, |
| 204 |
reason: format!("created on the web from {from}"), |
| 205 |
old_state: oxy_core::hash::ObjectRef::ZERO.to_hex(), |
| 206 |
new_state: new_state.clone(), |
| 207 |
heads: heads.len() as i64, |
| 208 |
changes: 0, |
| 209 |
}; |
| 210 |
db::repos::record_push(&app.db, row.id, Some((me.account.id, &me.account.username)), &[result]).await.ok(); |
| 211 |
crate::hooks::emit( |
| 212 |
&app, |
| 213 |
&row, |
| 214 |
"push", |
| 215 |
&me.account.username, |
| 216 |
serde_json::json!({ "updates": [{ "reference": bookmark, "old_state": oxy_core::hash::ObjectRef::ZERO.to_hex(), "new_state": new_state, "heads": heads.len(), "changes": 0, "created": true }], "via": "web" }), |
| 217 |
) |
| 218 |
.await; |
| 219 |
db::accounts::audit(&app.db, Some(&me.account), "bookmark.created", &format!("{owner}/{name}"), &format!("{bookmark} from {from}")) |
| 220 |
.await |
| 221 |
.ok(); |
| 222 |
let notice = format!("Created {bookmark} from {from}. Propose changes into it, or make it the default in Settings."); |
| 223 |
bookmarks_page(&app, shell, repo, row, role, Some(notice), None).await |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
pub async fn set_bookmark( |
| 230 |
app: &Shared, |
| 231 |
repo: &crate::repos::RepoName, |
| 232 |
account_id: i64, |
| 233 |
bookmark: &str, |
| 234 |
heads: Vec<oxy_core::hash::ChangeHash>, |
| 235 |
) -> Result<(), String> { |
| 236 |
match super::edit::signer_for(app, account_id).await { |
| 237 |
Ok(signer) => crate::repos::edits::move_bookmark(&app.repos, repo, bookmark.to_string(), heads, signer) |
| 238 |
.await |
| 239 |
.map(|_| ()) |
| 240 |
.map_err(|e| e.to_string()), |
| 241 |
Err(why) => { |
| 242 |
let target = bookmark.to_string(); |
| 243 |
let unsigned = app |
| 244 |
.repos |
| 245 |
.write(repo, move |r, _| { |
| 246 |
let update = r.host_ref_update(&target, &heads, None)?; |
| 247 |
let report = r.receive_push_report(Default::default(), vec![update], None, vec![])?; |
| 248 |
Ok(match report.results.first() { |
| 249 |
Some(res) if res.ok => Ok(()), |
| 250 |
Some(res) => Err(res.reason.clone()), |
| 251 |
None => Err("nothing happened".to_string()), |
| 252 |
}) |
| 253 |
}) |
| 254 |
.await |
| 255 |
.map_err(|e| e.to_string()) |
| 256 |
.and_then(|r| r); |
| 257 |
unsigned.map_err(|refused| format!("{refused}. {why}")) |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
#[derive(Template)] |
| 263 |
#[template(path = "tags.html")] |
| 264 |
pub struct TagsPage { |
| 265 |
shell: Shell, |
| 266 |
repo: RepoShell, |
| 267 |
tags: Vec<view::TagView>, |
| 268 |
withdrawn: Vec<view::WithdrawnView>, |
| 269 |
|
| 270 |
released: Vec<String>, |
| 271 |
can_create: bool, |
| 272 |
can_withdraw: bool, |
| 273 |
notice: Option<String>, |
| 274 |
error: Option<String>, |
| 275 |
} |
| 276 |
|
| 277 |
impl TagsPage { |
| 278 |
fn released(&self, tag: &str) -> bool { |
| 279 |
self.released.iter().any(|t| t == tag) |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
async fn tags_page( |
| 284 |
app: &Shared, |
| 285 |
shell: Shell, |
| 286 |
repo: crate::repos::RepoName, |
| 287 |
row: db::repos::RepoRow, |
| 288 |
role: Role, |
| 289 |
notice: Option<String>, |
| 290 |
error: Option<String>, |
| 291 |
) -> Result<Page<TagsPage>, Error> { |
| 292 |
let (tags, withdrawn) = app |
| 293 |
.repos |
| 294 |
.read(&repo, |r, _| Ok((view::tags(r)?, view::withdrawn_tags(r)?))) |
| 295 |
.await |
| 296 |
.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 297 |
let released = db::releases::list(&app.db, row.id).await.unwrap_or_default().into_iter().map(|r| r.tag).collect(); |
| 298 |
let signed_in = shell.user.is_some(); |
| 299 |
Ok(Page(TagsPage { |
| 300 |
shell, |
| 301 |
repo: RepoShell::new(app, &row, "tags").with_role(role), |
| 302 |
tags, |
| 303 |
withdrawn, |
| 304 |
released, |
| 305 |
can_create: signed_in && role >= Role::Write, |
| 306 |
can_withdraw: signed_in && role >= Role::Maintain, |
| 307 |
notice, |
| 308 |
error, |
| 309 |
})) |
| 310 |
} |
| 311 |
|
| 312 |
pub async fn tags(State(app): State<Shared>, viewer: Viewer, Path((owner, name)): Path<(String, String)>) -> Result<Page<TagsPage>, Error> { |
| 313 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/tags"), &viewer); |
| 314 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 315 |
tags_page(&app, shell, repo, row, role, None, None).await |
| 316 |
} |
| 317 |
|
| 318 |
#[derive(Deserialize)] |
| 319 |
pub struct CreateTagForm { |
| 320 |
#[serde(rename = "_csrf")] |
| 321 |
csrf_token: String, |
| 322 |
name: String, |
| 323 |
#[serde(default)] |
| 324 |
target: String, |
| 325 |
#[serde(default)] |
| 326 |
message: String, |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
pub async fn create_tag( |
| 332 |
State(app): State<Shared>, |
| 333 |
viewer: Viewer, |
| 334 |
Path((owner, name)): Path<(String, String)>, |
| 335 |
Form(f): Form<CreateTagForm>, |
| 336 |
) -> Result<Page<TagsPage>, Error> { |
| 337 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/tags"), &viewer); |
| 338 |
let me = require(&viewer, &shell)?; |
| 339 |
let (repo, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 340 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 341 |
match crate::tags::create(&app, &row, &repo, &me.account, &f.name, &f.target, &f.message).await { |
| 342 |
Ok(t) => { |
| 343 |
let notice = format!( |
| 344 |
"Tagged {} ({} head{}). Start a release from it under Releases.", |
| 345 |
t.name, |
| 346 |
t.heads.len(), |
| 347 |
if t.heads.len() == 1 { "" } else { "s" } |
| 348 |
); |
| 349 |
tags_page(&app, shell, repo, row, role, Some(notice), None).await |
| 350 |
} |
| 351 |
Err(e) => tags_page(&app, shell, repo, row, role, None, Some(format!("Could not create the tag: {e}"))).await, |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
#[derive(Deserialize)] |
| 356 |
pub struct WithdrawTagForm { |
| 357 |
#[serde(rename = "_csrf")] |
| 358 |
csrf_token: String, |
| 359 |
tag: String, |
| 360 |
#[serde(default)] |
| 361 |
reason: String, |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
pub async fn withdraw_tag( |
| 368 |
State(app): State<Shared>, |
| 369 |
viewer: Viewer, |
| 370 |
Path((owner, name)): Path<(String, String)>, |
| 371 |
Form(f): Form<WithdrawTagForm>, |
| 372 |
) -> Result<Page<TagsPage>, Error> { |
| 373 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/tags"), &viewer); |
| 374 |
let me = require(&viewer, &shell)?; |
| 375 |
let (repo, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 376 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 377 |
match crate::tags::withdraw(&app, &row, &repo, &me.account, &f.tag, &f.reason).await { |
| 378 |
Ok(t) => { |
| 379 |
let notice = format!("Withdrew {}. Clones drop it on their next fetch; the name is not used again.", t.name); |
| 380 |
tags_page(&app, shell, repo, row, role, Some(notice), None).await |
| 381 |
} |
| 382 |
Err(e) => tags_page(&app, shell, repo, row, role, None, Some(format!("Could not withdraw {}: {e}", f.tag.trim()))).await, |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
#[derive(Template)] |
| 387 |
#[template(path = "conflicts.html")] |
| 388 |
pub struct ConflictsPage { |
| 389 |
shell: Shell, |
| 390 |
repo: RepoShell, |
| 391 |
conflicts: Vec<view::ConflictView>, |
| 392 |
} |
| 393 |
|
| 394 |
pub async fn conflicts( |
| 395 |
State(app): State<Shared>, |
| 396 |
viewer: Viewer, |
| 397 |
Path((owner, name)): Path<(String, String)>, |
| 398 |
) -> Result<Page<ConflictsPage>, Error> { |
| 399 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/conflicts"), &viewer); |
| 400 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 401 |
let conflicts = app.repos.read(&repo, view::conflicts).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 402 |
Ok(Page(ConflictsPage { shell, repo: RepoShell::new(&app, &row, "conflicts").with_role(role).with_sky(&app, &row).await, conflicts })) |
| 403 |
} |
| 404 |
|
| 405 |
#[derive(Template)] |
| 406 |
#[template(path = "trust.html")] |
| 407 |
pub struct TrustPage { |
| 408 |
shell: Shell, |
| 409 |
repo: RepoShell, |
| 410 |
t: view::Trust, |
| 411 |
} |
| 412 |
|
| 413 |
pub async fn trust( |
| 414 |
State(app): State<Shared>, |
| 415 |
viewer: Viewer, |
| 416 |
Path((owner, name)): Path<(String, String)>, |
| 417 |
) -> Result<Page<TrustPage>, Error> { |
| 418 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/trust"), &viewer); |
| 419 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 420 |
let key = app.hub_key.key; |
| 421 |
let t = app.repos.read(&repo, move |r, cache| view::trust(r, cache, &key)).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 422 |
Ok(Page(TrustPage { shell, repo: RepoShell::new(&app, &row, "trust").with_role(role), t })) |
| 423 |
} |
| 424 |
|
| 425 |
#[derive(Template)] |
| 426 |
#[template(path = "pushes.html")] |
| 427 |
pub struct PushesPage { |
| 428 |
shell: Shell, |
| 429 |
repo: RepoShell, |
| 430 |
pushes: Vec<db::repos::PushRow>, |
| 431 |
} |
| 432 |
|
| 433 |
pub async fn pushes( |
| 434 |
State(app): State<Shared>, |
| 435 |
viewer: Viewer, |
| 436 |
Path((owner, name)): Path<(String, String)>, |
| 437 |
) -> Result<Page<PushesPage>, Error> { |
| 438 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/pushes"), &viewer); |
| 439 |
let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 440 |
let pushes = db::repos::pushes(&app.db, row.id, 200).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 441 |
Ok(Page(PushesPage { shell, repo: RepoShell::new(&app, &row, "pushes").with_role(role), pushes })) |
| 442 |
} |