| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | | use super::settings::{csrf, require}; |
| 7 | | use super::{lookup_role, Error, Page, RepoShell, Shell}; |
| 8 | | use crate::auth::{Access, Viewer}; |
| 9 | | use crate::db::{self, accounts}; |
| 10 | | use crate::repos::edits::{self, Signer}; |
| 11 | | use crate::repos::view::{self, FileBody}; |
| 12 | | use crate::Shared; |
| 13 | | use askama::Template; |
| 14 | | use axum::extract::{Form, Path, Query, State}; |
| 15 | | use axum::response::{IntoResponse, Redirect, Response}; |
| 16 | | use oxy_core::identity::{Identity, Scope}; |
| 17 | | use oxy_core::repo::FileEdit; |
| 18 | | use serde::Deserialize; |
| 19 | |
|
| 20 | |
|
| 21 | 165× | pub async fn delegated_identity(app: &Shared, account_id: i64) -> Option<Identity> { |
| 22 | 165× | let linked = accounts::identities_of(&app.db, account_id).await.ok()?; |
| 23 | | |
| 24 | | |
| 25 | 165× | let now = crate::db::now_ms() as u64; |
| 26 | 165× | linked |
| 27 | 165× | .into_iter() |
| 28 | 165× | .filter_map(|l| Identity::decode(&l.object).ok()) |
| 29 | 165× | .find(|i| i.delegates_to(&app.hub_key.key, Scope::CHANGES | Scope::REFS, now)) |
| 30 | 165× | } |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 2× | pub fn hub_signer(app: &Shared) -> Result<Signer, String> { |
| 37 | 2× | let (hub_key, hub_secret) = crate::keys::load_secret(&app.config.keys_dir()).map_err(|e| format!("the hub key is unavailable: {e}"))?; |
| 38 | 2× | let identity = app.hub_key.identity.clone(); |
| 39 | 2× | Ok(Signer { author: identity.clone(), hub_key, hub_secret, hub_identity: identity }) |
| 40 | 2× | } |
| 41 | |
|
| 42 | 91× | pub async fn signer_for(app: &Shared, account_id: i64) -> Result<Signer, String> { |
| 43 | 91× | let author = delegated_identity(app, account_id).await.ok_or_else(|| { |
| 44 | 22× | "You have not delegated to this hub's key yet. In a checkout with your identity, download the hub's key from /.well-known/oxyhub-key and run `oxy identity delegate add oxyhub-key.identity --scope changes,refs`. That makes a new version of your own identity: export it there (`oxy identity export`), upload that file under Settings › Keys and sign the challenge in the same checkout. The hub's key file is not the one to upload.".to_string() |
| 45 | 22× | })?; |
| 46 | 69× | let (hub_key, hub_secret) = crate::keys::load_secret(&app.config.keys_dir()).map_err(|e| format!("the hub key is unavailable: {e}"))?; |
| 47 | 69× | Ok(Signer { author, hub_key, hub_secret, hub_identity: app.hub_key.identity.clone() }) |
| 48 | 91× | } |
| 49 | |
|
| 50 | | #[derive(Template)] |
| 51 | | #[template(path = "edit.html")] |
| 52 | | pub struct EditPage { |
| 53 | | shell: Shell, |
| 54 | | repo: RepoShell, |
| 55 | | r: String, |
| 56 | | path: String, |
| 57 | | content: String, |
| 58 | | new_file: bool, |
| 59 | | is_bookmark: bool, |
| 60 | | conflicted: bool, |
| 61 | | delegation: Option<String>, |
| 62 | | error: Option<String>, |
| 63 | | message: String, |
| 64 | | } |
| 65 | |
|
| 66 | | #[derive(Deserialize)] |
| 67 | | pub struct NewQuery { |
| 68 | | #[serde(default)] |
| 69 | | dir: String, |
| 70 | | } |
| 71 | |
|
| 72 | |
|
| 73 | | #[allow(clippy::too_many_arguments)] |
| 74 | 5× | async fn page( |
| 75 | 5× | app: &Shared, |
| 76 | 5× | viewer: &Viewer, |
| 77 | 5× | owner: &str, |
| 78 | 5× | name: &str, |
| 79 | 5× | r: &str, |
| 80 | 5× | path: Option<&str>, |
| 81 | 5× | dir: &str, |
| 82 | 5× | error: Option<String>, |
| 83 | 5× | content: Option<String>, |
| 84 | 5× | ) -> Result<Page<EditPage>, Error> { |
| 85 | 5× | let shell = Shell::new(app, &format!("/{owner}/{name}/edit/{r}"), viewer); |
| 86 | 5× | let me = require(viewer, &shell)?; |
| 87 | 5× | let (repo, row, role) = lookup_role(app, &shell, viewer, owner, name, Access::Write).await?; |
| 88 | 5× | let limit = app.config.limits.render_limit.0; |
| 89 | 5× | let spec = r.to_string(); |
| 90 | 5× | let p = path.map(String::from); |
| 91 | 5× | let (is_bookmark, file) = app |
| 92 | 5× | .repos |
| 93 | 5× | .read(&repo, move |repo, cache| { |
| 94 | 5× | let target = view::resolve(repo, cache, &spec)?; |
| 95 | 5× | let is_bookmark = target.kind == view::RefKind::Bookmark; |
| 96 | 5× | let file = match &p { |
| 97 | 5× | Some(p) => Some(view::file(repo, cache, target, p, limit)?), |
| 98 | 0× | None => None, |
| 99 | | }; |
| 100 | 5× | Ok((is_bookmark, file)) |
| 101 | 5× | }) |
| 102 | 5× | .await |
| 103 | 5× | .map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 104 | 5× | let (existing, conflicted) = match &file { |
| 105 | 5× | Some(f) => match &f.body { |
| 106 | 5× | FileBody::Text(t) => (t.clone(), f.conflicted), |
| 107 | 0× | _ => return Err(Error::bad(shell, "Only text files within the render limit can be edited in the browser.")), |
| 108 | | }, |
| 109 | 0× | None => (String::new(), false), |
| 110 | | }; |
| 111 | 5× | let delegation = signer_for(app, me.account.id).await.err(); |
| 112 | 5× | let new_file = path.is_none(); |
| 113 | 5× | let default_path = if dir.is_empty() { String::new() } else { format!("{}/", dir.trim_matches('/')) }; |
| 114 | 5× | Ok(Page(EditPage { |
| 115 | 5× | shell, |
| 116 | 5× | repo: RepoShell::new(app, &row, "code").with_role(role), |
| 117 | 5× | r: r.to_string(), |
| 118 | 5× | path: path.map(String::from).unwrap_or(default_path), |
| 119 | 5× | content: content.unwrap_or(existing), |
| 120 | 5× | new_file, |
| 121 | 5× | is_bookmark, |
| 122 | 5× | conflicted, |
| 123 | 5× | delegation, |
| 124 | 5× | error, |
| 125 | 5× | message: String::new(), |
| 126 | 5× | })) |
| 127 | 5× | } |
| 128 | |
|
| 129 | 3× | pub async fn edit_form( |
| 130 | 3× | State(app): State<Shared>, |
| 131 | 3× | viewer: Viewer, |
| 132 | 3× | Path((owner, name, r, path)): Path<(String, String, String, String)>, |
| 133 | 3× | ) -> Result<Page<EditPage>, Error> { |
| 134 | 3× | page(&app, &viewer, &owner, &name, &r, Some(&path), "", None, None).await |
| 135 | 3× | } |
| 136 | |
|
| 137 | 0× | pub async fn new_form( |
| 138 | 0× | State(app): State<Shared>, |
| 139 | 0× | viewer: Viewer, |
| 140 | 0× | Path((owner, name, r)): Path<(String, String, String)>, |
| 141 | 0× | Query(q): Query<NewQuery>, |
| 142 | 0× | ) -> Result<Page<EditPage>, Error> { |
| 143 | 0× | page(&app, &viewer, &owner, &name, &r, None, &q.dir, None, None).await |
| 144 | 0× | } |
| 145 | |
|
| 146 | | #[derive(Deserialize)] |
| 147 | | pub struct SaveForm { |
| 148 | | #[serde(rename = "_csrf")] |
| 149 | | csrf_token: String, |
| 150 | | path: String, |
| 151 | | #[serde(default)] |
| 152 | | original: String, |
| 153 | | #[serde(default)] |
| 154 | | content: String, |
| 155 | | message: String, |
| 156 | | |
| 157 | | #[serde(default = "commit_mode")] |
| 158 | | mode: String, |
| 159 | | #[serde(default)] |
| 160 | | new_bookmark: String, |
| 161 | | #[serde(default)] |
| 162 | | delete: Option<String>, |
| 163 | | } |
| 164 | |
|
| 165 | 2× | fn commit_mode() -> String { |
| 166 | 2× | "commit".into() |
| 167 | 2× | } |
| 168 | |
|
| 169 | 20× | pub async fn save( |
| 170 | 20× | State(app): State<Shared>, |
| 171 | 20× | viewer: Viewer, |
| 172 | 20× | Path((owner, name, r)): Path<(String, String, String)>, |
| 173 | 20× | Form(f): Form<SaveForm>, |
| 174 | 20× | ) -> Result<Response, Error> { |
| 175 | 20× | let shell = Shell::new(&app, &format!("/{owner}/{name}/edit/{r}"), &viewer); |
| 176 | 20× | let me = require(&viewer, &shell)?; |
| 177 | 20× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 178 | 20× | let (repo, row, _) = lookup_role(&app, &shell, &viewer, &owner, &name, Access::Write).await?; |
| 179 | 20× | let again = |msg: String| { |
| 180 | 2× | let (app, viewer, owner, name, r) = (app.clone(), viewer.clone(), owner.clone(), name.clone(), r.clone()); |
| 181 | 2× | let (orig, content) = (f.original.clone(), f.content.clone()); |
| 182 | 2× | async move { |
| 183 | 2× | page(&app, &viewer, &owner, &name, &r, if orig.is_empty() { None } else { Some(orig.as_str()) }, "", Some(msg), Some(content)) |
| 184 | 2× | .await |
| 185 | 2× | .map(IntoResponse::into_response) |
| 186 | 2× | } |
| 187 | 2× | }; |
| 188 | 20× | let signer = match signer_for(&app, me.account.id).await { |
| 189 | 19× | Ok(s) => s, |
| 190 | 1× | Err(msg) => return again(msg).await, |
| 191 | | }; |
| 192 | 19× | let path = f.path.trim().trim_matches('/').to_string(); |
| 193 | 19× | if path.is_empty() { |
| 194 | 0× | return again("Name the file.".into()).await; |
| 195 | 19× | } |
| 196 | 19× | let message = f.message.trim().to_string(); |
| 197 | 19× | if message.is_empty() { |
| 198 | 0× | return again("Describe the change.".into()).await; |
| 199 | 19× | } |
| 200 | | |
| 201 | 19× | let content = f.content.replace("\r\n", "\n"); |
| 202 | 19× | let mut changes = vec![]; |
| 203 | 19× | if f.delete.is_some() { |
| 204 | 1× | changes.push(FileEdit { |
| 205 | 1× | path: if f.original.is_empty() { path.clone() } else { f.original.trim_matches('/').to_string() }, |
| 206 | 1× | content: None, |
| 207 | | }); |
| 208 | | } else { |
| 209 | 18× | if !f.original.is_empty() && f.original.trim_matches('/') != path { |
| 210 | 0× | |
| 211 | 0× | changes.push(FileEdit { path: f.original.trim_matches('/').to_string(), content: None }); |
| 212 | 18× | } |
| 213 | 18× | changes.push(FileEdit { path: path.clone(), content: Some(content.into_bytes()) }); |
| 214 | | } |
| 215 | 19× | let target = if f.mode == "propose" { |
| 216 | 11× | let b = f.new_bookmark.trim(); |
| 217 | 11× | let b = if b.is_empty() { format!("{}-patch-{}", me.account.username, crate::auth::random_hex(3)) } else { b.to_string() }; |
| 218 | 11× | if !crate::repos::names::valid_segment(&b) { |
| 219 | 0× | return again("A bookmark name is letters, digits, dots, dashes and underscores.".into()).await; |
| 220 | 11× | } |
| 221 | 11× | b |
| 222 | | } else { |
| 223 | 8× | r.clone() |
| 224 | | }; |
| 225 | 19× | let done = match edits::commit(&app.repos, &repo, r.clone(), target.clone(), changes, message.clone(), signer).await { |
| 226 | 18× | Ok(d) => d, |
| 227 | 1× | Err(e) => return again(e.to_string()).await, |
| 228 | | }; |
| 229 | 18× | after_web_commit(&app, &me.account, &row, &owner, &name, &done, &message).await; |
| 230 | 18× | if f.mode == "propose" { |
| 231 | 11× | match super::proposals::open_proposal(&app, &repo, &row, &me.account, &message, "", &r, &done.bookmark, false).await { |
| 232 | 11× | Ok(p) => return Ok(Redirect::to(&format!("/{owner}/{name}/proposal/{}", p.number)).into_response()), |
| 233 | 0× | Err(msg) => { |
| 234 | 0× | return Ok(Redirect::to(&format!("/{owner}/{name}/tree/{}?notice={}", done.bookmark, super::settings::urlencode(&msg))) |
| 235 | 0× | .into_response()) |
| 236 | | } |
| 237 | | } |
| 238 | 7× | } |
| 239 | 7× | let to = if f.delete.is_some() { |
| 240 | 1× | format!("/{owner}/{name}/tree/{}", done.bookmark) |
| 241 | | } else { |
| 242 | 6× | format!("/{owner}/{name}/file/{}/{}", done.bookmark, path) |
| 243 | | }; |
| 244 | 7× | Ok(Redirect::to(&to).into_response()) |
| 245 | 20× | } |
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | 22× | pub(super) async fn after_web_commit( |
| 251 | 22× | app: &Shared, |
| 252 | 22× | account: &accounts::Account, |
| 253 | 22× | row: &db::repos::RepoRow, |
| 254 | 22× | owner: &str, |
| 255 | 22× | name: &str, |
| 256 | 22× | done: &edits::Committed, |
| 257 | 22× | message: &str, |
| 258 | 22× | ) { |
| 259 | 22× | let outcome = db::repos::PushOutcome { |
| 260 | 22× | reference: done.bookmark.clone(), |
| 261 | 22× | ok: true, |
| 262 | 22× | reason: "web edit".into(), |
| 263 | 22× | old_state: done.old_state.clone(), |
| 264 | 22× | new_state: done.new_state.clone(), |
| 265 | 22× | heads: done.heads.len() as i64, |
| 266 | 22× | changes: 1, |
| 267 | 22× | }; |
| 268 | 22× | db::repos::record_push(&app.db, row.id, Some((account.id, &account.username)), &[outcome]).await.ok(); |
| 269 | 22× | crate::hooks::emit(app, row, "push", &account.username, serde_json::json!({ "updates": [{ "reference": done.bookmark, "old_state": done.old_state, "new_state": done.new_state, "heads": done.heads.len(), "changes": 1 }], "via": "web" })).await; |
| 270 | 22× | crate::flow::on_push(app, row, &done.bookmark, &done.new_state, &done.heads, &account.username).await; |
| 271 | 22× | if done.bookmark == row.default_bookmark { |
| 272 | 10× | super::issues::close_from_messages(app, row, &[(done.change.to_hex(), message.to_string())]).await; |
| 273 | 10× | crate::mirror::refresh(app, row).await.ok(); |
| 274 | 12× | } |
| 275 | 22× | accounts::audit( |
| 276 | 22× | &app.db, |
| 277 | 22× | Some(account), |
| 278 | 22× | "web.edit", |
| 279 | 22× | &format!("{owner}/{name}"), |
| 280 | 22× | &format!("{} {}", done.bookmark, done.change.short(12)), |
| 281 | 22× | ) |
| 282 | 22× | .await |
| 283 | 22× | .ok(); |
| 284 | 22× | } |