| 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 | 11× | async fn bookmarks_page( |
| 27 | 11× | app: &Shared, |
| 28 | 11× | shell: Shell, |
| 29 | 11× | repo: crate::repos::RepoName, |
| 30 | 11× | row: db::repos::RepoRow, |
| 31 | 11× | role: Role, |
| 32 | 11× | notice: Option<String>, |
| 33 | 11× | error: Option<String>, |
| 34 | 11× | ) -> Result<Page<BookmarksPage>, Error> { |
| 35 | 11× | let default = row.default_bookmark.clone(); |
| 36 | 11× | let bookmarks = |
| 37 | 11× | app.repos.read(&repo, move |r, cache| view::bookmarks(r, cache, &default)).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 38 | 11× | let can_delete = shell.user.is_some() && role >= Role::Write; |
| 39 | 11× | let can_run = super::flow::why_not_run(crate::flow::enabled(app, &row), shell.user.is_some(), role).is_none(); |
| 40 | 11× | Ok(Page(BookmarksPage { |
| 41 | 11× | shell, |
| 42 | 11× | repo: RepoShell::new(app, &row, "bookmarks").with_role(role), |
| 43 | 11× | bookmarks, |
| 44 | 11× | can_delete, |
| 45 | 11× | can_run, |
| 46 | 11× | notice, |
| 47 | 11× | error, |
| 48 | 11× | })) |
| 49 | 11× | } |
| 50 | |
|
| 51 | 7× | pub async fn bookmarks( |
| 52 | 7× | State(app): State<Shared>, |
| 53 | 7× | viewer: Viewer, |
| 54 | 7× | Path((owner, name)): Path<(String, String)>, |
| 55 | 7× | ) -> Result<Page<BookmarksPage>, Error> { |
| 56 | 7× | let shell = Shell::new(&app, &format!("/{owner}/{name}/bookmarks"), &viewer); |
| 57 | 7× | let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 58 | 7× | bookmarks_page(&app, shell, repo, row, role, None, None).await |
| 59 | 7× | } |
| 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 | 5× | pub async fn delete_bookmark( |
| 72 | 5× | State(app): State<Shared>, |
| 73 | 5× | viewer: Viewer, |
| 74 | 5× | Path((owner, name)): Path<(String, String)>, |
| 75 | 5× | Form(f): Form<DeleteBookmarkForm>, |
| 76 | 5× | ) -> Result<Page<BookmarksPage>, Error> { |
| 77 | 5× | let shell = Shell::new(&app, &format!("/{owner}/{name}/bookmarks"), &viewer); |
| 78 | 5× | let me = require(&viewer, &shell)?; |
| 79 | 4× | let (repo, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 80 | 4× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 81 | 4× | let bookmark = f.bookmark.trim().to_string(); |
| 82 | 4× | if bookmark == row.default_bookmark { |
| 83 | 1× | let msg = format!("{bookmark} is the default bookmark; choose another default in Settings first."); |
| 84 | 1× | return bookmarks_page(&app, shell, repo, row, role, None, Some(msg)).await; |
| 85 | 3× | } |
| 86 | 3× | let target = bookmark.clone(); |
| 87 | 3× | let outcome = app |
| 88 | 3× | .repos |
| 89 | 3× | .write(&repo, move |r, _| { |
| 90 | 3× | let Some(state) = r.view()?.bookmarks.get(&target).copied() else { |
| 91 | 1× | return Ok(Err(format!("There is no bookmark {target}."))); |
| 92 | | }; |
| 93 | 2× | let update = oxy_core::protocol::RefUpdate { |
| 94 | 2× | name: target.clone(), |
| 95 | 2× | old_state: state, |
| 96 | 2× | new_state: oxy_core::hash::ObjectRef::ZERO, |
| 97 | 2× | force: true, |
| 98 | 2× | attestation: None, |
| 99 | 2× | }; |
| 100 | 2× | let report = r.receive_push_report(Default::default(), vec![update], None, vec![])?; |
| 101 | 2× | Ok(match report.results.first() { |
| 102 | 2× | Some(res) if res.ok => Ok(state.to_hex()), |
| 103 | 0× | Some(res) => Err(res.reason.clone()), |
| 104 | 0× | None => Err("nothing happened".into()), |
| 105 | | }) |
| 106 | 3× | }) |
| 107 | 3× | .await |
| 108 | 3× | .map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 109 | 3× | let old_state = match outcome { |
| 110 | 2× | Ok(s) => s, |
| 111 | 1× | Err(msg) => return bookmarks_page(&app, shell, repo, row, role, None, Some(format!("Could not delete {bookmark}: {msg}"))).await, |
| 112 | | }; |
| 113 | 2× | let result = db::repos::PushOutcome { |
| 114 | 2× | reference: bookmark.clone(), |
| 115 | 2× | ok: true, |
| 116 | 2× | reason: "deleted on the web".into(), |
| 117 | 2× | old_state: old_state.clone(), |
| 118 | 2× | new_state: oxy_core::hash::ObjectRef::ZERO.to_hex(), |
| 119 | 2× | heads: 0, |
| 120 | 2× | changes: 0, |
| 121 | 2× | }; |
| 122 | 2× | db::repos::record_push(&app.db, row.id, Some((me.account.id, &me.account.username)), &[result]).await.ok(); |
| 123 | 2× | crate::hooks::emit( |
| 124 | 2× | &app, |
| 125 | 2× | &row, |
| 126 | 2× | "push", |
| 127 | 2× | &me.account.username, |
| 128 | 2× | 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 | 2× | ) |
| 130 | 2× | .await; |
| 131 | 2× | db::accounts::audit(&app.db, Some(&me.account), "bookmark.deleted", &format!("{owner}/{name}"), &bookmark).await.ok(); |
| 132 | 2× | bookmarks_page( |
| 133 | 2× | &app, |
| 134 | 2× | shell, |
| 135 | 2× | repo, |
| 136 | 2× | row, |
| 137 | 2× | role, |
| 138 | 2× | Some(format!("Deleted {bookmark}. Its changes stay while anything else reaches them.")), |
| 139 | 2× | None, |
| 140 | 2× | ) |
| 141 | 2× | .await |
| 142 | 5× | } |
| 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 | | withdrawn: Vec<view::WithdrawnView>, |
| 151 | | |
| 152 | | released: Vec<String>, |
| 153 | | can_create: bool, |
| 154 | | can_withdraw: bool, |
| 155 | | notice: Option<String>, |
| 156 | | error: Option<String>, |
| 157 | | } |
| 158 | |
|
| 159 | | impl TagsPage { |
| 160 | 25× | fn released(&self, tag: &str) -> bool { |
| 161 | 25× | self.released.iter().any(|t| t == tag) |
| 162 | 25× | } |
| 163 | | } |
| 164 | |
|
| 165 | 9× | async fn tags_page( |
| 166 | 9× | app: &Shared, |
| 167 | 9× | shell: Shell, |
| 168 | 9× | repo: crate::repos::RepoName, |
| 169 | 9× | row: db::repos::RepoRow, |
| 170 | 9× | role: Role, |
| 171 | 9× | notice: Option<String>, |
| 172 | 9× | error: Option<String>, |
| 173 | 9× | ) -> Result<Page<TagsPage>, Error> { |
| 174 | 9× | let (tags, withdrawn) = app |
| 175 | 9× | .repos |
| 176 | 9× | .read(&repo, |r, _| Ok((view::tags(r)?, view::withdrawn_tags(r)?))) |
| 177 | 9× | .await |
| 178 | 9× | .map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 179 | 9× | let released = db::releases::list(&app.db, row.id).await.unwrap_or_default().into_iter().map(|r| r.tag).collect(); |
| 180 | 9× | let signed_in = shell.user.is_some(); |
| 181 | | Ok(Page(TagsPage { |
| 182 | 9× | shell, |
| 183 | 9× | repo: RepoShell::new(app, &row, "tags").with_role(role), |
| 184 | 9× | tags, |
| 185 | 9× | withdrawn, |
| 186 | 9× | released, |
| 187 | 9× | can_create: signed_in && role >= Role::Write, |
| 188 | 9× | can_withdraw: signed_in && role >= Role::Maintain, |
| 189 | 9× | notice, |
| 190 | 9× | error, |
| 191 | | })) |
| 192 | 9× | } |
| 193 | |
|
| 194 | 3× | pub async fn tags(State(app): State<Shared>, viewer: Viewer, Path((owner, name)): Path<(String, String)>) -> Result<Page<TagsPage>, Error> { |
| 195 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}/tags"), &viewer); |
| 196 | 3× | let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 197 | 3× | tags_page(&app, shell, repo, row, role, None, None).await |
| 198 | 3× | } |
| 199 | |
|
| 200 | | #[derive(Deserialize)] |
| 201 | | pub struct CreateTagForm { |
| 202 | | #[serde(rename = "_csrf")] |
| 203 | | csrf_token: String, |
| 204 | | name: String, |
| 205 | | #[serde(default)] |
| 206 | | target: String, |
| 207 | | #[serde(default)] |
| 208 | | message: String, |
| 209 | | } |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | 4× | pub async fn create_tag( |
| 214 | 4× | State(app): State<Shared>, |
| 215 | 4× | viewer: Viewer, |
| 216 | 4× | Path((owner, name)): Path<(String, String)>, |
| 217 | 4× | Form(f): Form<CreateTagForm>, |
| 218 | 4× | ) -> Result<Page<TagsPage>, Error> { |
| 219 | 4× | let shell = Shell::new(&app, &format!("/{owner}/{name}/tags"), &viewer); |
| 220 | 4× | let me = require(&viewer, &shell)?; |
| 221 | 4× | let (repo, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 222 | 4× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 223 | 4× | match crate::tags::create(&app, &row, &repo, &me.account, &f.name, &f.target, &f.message).await { |
| 224 | 2× | Ok(t) => { |
| 225 | 2× | let notice = format!( |
| 226 | | "Tagged {} ({} head{}). Start a release from it under Releases.", |
| 227 | | t.name, |
| 228 | 2× | t.heads.len(), |
| 229 | 2× | if t.heads.len() == 1 { "" } else { "s" } |
| 230 | | ); |
| 231 | 2× | tags_page(&app, shell, repo, row, role, Some(notice), None).await |
| 232 | | } |
| 233 | 2× | Err(e) => tags_page(&app, shell, repo, row, role, None, Some(format!("Could not create the tag: {e}"))).await, |
| 234 | | } |
| 235 | 4× | } |
| 236 | |
|
| 237 | | #[derive(Deserialize)] |
| 238 | | pub struct WithdrawTagForm { |
| 239 | | #[serde(rename = "_csrf")] |
| 240 | | csrf_token: String, |
| 241 | | tag: String, |
| 242 | | #[serde(default)] |
| 243 | | reason: String, |
| 244 | | } |
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | 2× | pub async fn withdraw_tag( |
| 250 | 2× | State(app): State<Shared>, |
| 251 | 2× | viewer: Viewer, |
| 252 | 2× | Path((owner, name)): Path<(String, String)>, |
| 253 | 2× | Form(f): Form<WithdrawTagForm>, |
| 254 | 2× | ) -> Result<Page<TagsPage>, Error> { |
| 255 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/tags"), &viewer); |
| 256 | 2× | let me = require(&viewer, &shell)?; |
| 257 | 2× | let (repo, row, role) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Maintain).await?; |
| 258 | 2× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 259 | 2× | match crate::tags::withdraw(&app, &row, &repo, &me.account, &f.tag, &f.reason).await { |
| 260 | 1× | Ok(t) => { |
| 261 | 1× | let notice = format!("Withdrew {}. Clones drop it on their next fetch; the name is not used again.", t.name); |
| 262 | 1× | tags_page(&app, shell, repo, row, role, Some(notice), None).await |
| 263 | | } |
| 264 | 1× | Err(e) => tags_page(&app, shell, repo, row, role, None, Some(format!("Could not withdraw {}: {e}", f.tag.trim()))).await, |
| 265 | | } |
| 266 | 2× | } |
| 267 | |
|
| 268 | | #[derive(Template)] |
| 269 | | #[template(path = "conflicts.html")] |
| 270 | | pub struct ConflictsPage { |
| 271 | | shell: Shell, |
| 272 | | repo: RepoShell, |
| 273 | | conflicts: Vec<view::ConflictView>, |
| 274 | | } |
| 275 | |
|
| 276 | 8× | pub async fn conflicts( |
| 277 | 8× | State(app): State<Shared>, |
| 278 | 8× | viewer: Viewer, |
| 279 | 8× | Path((owner, name)): Path<(String, String)>, |
| 280 | 8× | ) -> Result<Page<ConflictsPage>, Error> { |
| 281 | 8× | let shell = Shell::new(&app, &format!("/{owner}/{name}/conflicts"), &viewer); |
| 282 | 8× | let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 283 | 8× | let conflicts = app.repos.read(&repo, view::conflicts).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 284 | 8× | Ok(Page(ConflictsPage { shell, repo: RepoShell::new(&app, &row, "conflicts").with_role(role).with_sky(&app, &row).await, conflicts })) |
| 285 | 8× | } |
| 286 | |
|
| 287 | | #[derive(Template)] |
| 288 | | #[template(path = "trust.html")] |
| 289 | | pub struct TrustPage { |
| 290 | | shell: Shell, |
| 291 | | repo: RepoShell, |
| 292 | | t: view::Trust, |
| 293 | | } |
| 294 | |
|
| 295 | 2× | pub async fn trust( |
| 296 | 2× | State(app): State<Shared>, |
| 297 | 2× | viewer: Viewer, |
| 298 | 2× | Path((owner, name)): Path<(String, String)>, |
| 299 | 2× | ) -> Result<Page<TrustPage>, Error> { |
| 300 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/trust"), &viewer); |
| 301 | 2× | let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 302 | 2× | let key = app.hub_key.key; |
| 303 | 2× | let t = app.repos.read(&repo, move |r, cache| view::trust(r, cache, &key)).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 304 | 2× | Ok(Page(TrustPage { shell, repo: RepoShell::new(&app, &row, "trust").with_role(role), t })) |
| 305 | 2× | } |
| 306 | |
|
| 307 | | #[derive(Template)] |
| 308 | | #[template(path = "pushes.html")] |
| 309 | | pub struct PushesPage { |
| 310 | | shell: Shell, |
| 311 | | repo: RepoShell, |
| 312 | | pushes: Vec<db::repos::PushRow>, |
| 313 | | } |
| 314 | |
|
| 315 | 6× | pub async fn pushes( |
| 316 | 6× | State(app): State<Shared>, |
| 317 | 6× | viewer: Viewer, |
| 318 | 6× | Path((owner, name)): Path<(String, String)>, |
| 319 | 6× | ) -> Result<Page<PushesPage>, Error> { |
| 320 | 6× | let shell = Shell::new(&app, &format!("/{owner}/{name}/pushes"), &viewer); |
| 321 | 6× | let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 322 | 6× | let pushes = db::repos::pushes(&app.db, row.id, 200).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 323 | 6× | Ok(Page(PushesPage { shell, repo: RepoShell::new(&app, &row, "pushes").with_role(role), pushes })) |
| 324 | 6× | } |