| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | | use super::settings::{csrf, require}; |
| 9 | | use super::{filters, Error, Page, RepoShell, Shell}; |
| 10 | | use crate::auth::{Access, Viewer}; |
| 11 | | use crate::db; |
| 12 | | use crate::drops::{self, DropFile, NewFile}; |
| 13 | | use crate::render::{highlight, markdown}; |
| 14 | | use crate::Shared; |
| 15 | | use askama::Template; |
| 16 | | use axum::extract::{Form, Path, Query, State}; |
| 17 | | use axum::response::{IntoResponse, Redirect, Response}; |
| 18 | | use serde::Deserialize; |
| 19 | |
|
| 20 | |
|
| 21 | | pub struct DropLine { |
| 22 | | pub url: String, |
| 23 | | pub title: String, |
| 24 | | pub description: String, |
| 25 | | pub secret: bool, |
| 26 | | pub files: Vec<String>, |
| 27 | | pub updated_ms: i64, |
| 28 | | } |
| 29 | |
|
| 30 | | #[derive(Template)] |
| 31 | | #[template(path = "drops.html")] |
| 32 | | pub struct DropsPage { |
| 33 | | shell: Shell, |
| 34 | | owner: String, |
| 35 | | is_me: bool, |
| 36 | | drops: Vec<DropLine>, |
| 37 | | notice: Option<String>, |
| 38 | | } |
| 39 | |
|
| 40 | |
|
| 41 | | pub struct ShownFile { |
| 42 | | pub path: String, |
| 43 | | pub anchor: String, |
| 44 | | pub size: u64, |
| 45 | | pub language: Option<&'static str>, |
| 46 | | |
| 47 | | pub lines: Vec<String>, |
| 48 | | pub markdown: Option<String>, |
| 49 | | |
| 50 | | pub note: Option<String>, |
| 51 | | } |
| 52 | |
|
| 53 | | #[derive(Template)] |
| 54 | | #[template(path = "drop.html")] |
| 55 | | pub struct DropPage { |
| 56 | | shell: Shell, |
| 57 | | repo: RepoShell, |
| 58 | | title: String, |
| 59 | | description: String, |
| 60 | | secret: bool, |
| 61 | | files: Vec<ShownFile>, |
| 62 | | can_edit: bool, |
| 63 | | can_delete: bool, |
| 64 | | |
| 65 | | can_fork: bool, |
| 66 | | |
| 67 | | embed: String, |
| 68 | | |
| 69 | | comments: Vec<ShownComment>, |
| 70 | | |
| 71 | | stars: i64, |
| 72 | | starred: bool, |
| 73 | | signed_in: bool, |
| 74 | | |
| 75 | | forked_from: Option<String>, |
| 76 | | clone_url: String, |
| 77 | | |
| 78 | | target: String, |
| 79 | | at_default: bool, |
| 80 | | created_ms: i64, |
| 81 | | updated_ms: i64, |
| 82 | | } |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | | #[derive(Template)] |
| 87 | | #[template(path = "drop_embed.html")] |
| 88 | | pub struct EmbedPage { |
| 89 | | title: String, |
| 90 | | owner: String, |
| 91 | | |
| 92 | | url: String, |
| 93 | | site: String, |
| 94 | | site_url: String, |
| 95 | | assets: &'static str, |
| 96 | | files: Vec<ShownFile>, |
| 97 | | } |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | | const EMBED_POLICY: &str = |
| 102 | | "default-src 'none'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; frame-ancestors *; base-uri 'none'; form-action 'none'"; |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | 1× | pub async fn embed(State(app): State<Shared>, viewer: Viewer, Path((owner, id)): Path<(String, String)>) -> Result<Response, Error> { |
| 107 | 1× | let shell = Shell::new(&app, &format!("/{owner}/drops/{id}/embed"), &viewer); |
| 108 | 1× | let row = lookup(&app, &shell, &viewer, &owner, &id).await?; |
| 109 | 1× | let (target, files) = drops::files(&app, &row, "").await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 110 | 1× | let url = drops::url_of(&row); |
| 111 | 1× | let title = drops::shown_title(&row, &files); |
| 112 | 1× | let label = target.label.clone(); |
| 113 | 1× | let files = shown(files, &format!("{url}/file/{label}/"), &format!("{url}/raw/{label}/")); |
| 114 | 1× | let base = app.config.base_url().trim_end_matches('/').to_string(); |
| 115 | 1× | let page = EmbedPage { |
| 116 | 1× | title, |
| 117 | 1× | owner: row.owner.clone(), |
| 118 | 1× | url: format!("{base}{url}"), |
| 119 | 1× | site: shell.site.clone(), |
| 120 | 1× | site_url: base, |
| 121 | 1× | assets: shell.assets(), |
| 122 | 1× | files, |
| 123 | 1× | }; |
| 124 | 1× | let html = page.render().map_err(|e| Error::bad(shell.clone(), e.to_string()))?; |
| 125 | 1× | Ok(( |
| 126 | 1× | [(axum::http::header::CONTENT_TYPE, "text/html; charset=utf-8"), (axum::http::header::CONTENT_SECURITY_POLICY, EMBED_POLICY)], |
| 127 | 1× | html, |
| 128 | 1× | ) |
| 129 | 1× | .into_response()) |
| 130 | 1× | } |
| 131 | |
|
| 132 | |
|
| 133 | | pub struct ShownComment { |
| 134 | | pub id: i64, |
| 135 | | pub author: String, |
| 136 | | pub created_ms: i64, |
| 137 | | pub html: String, |
| 138 | | pub may_delete: bool, |
| 139 | | } |
| 140 | |
|
| 141 | | #[derive(Template)] |
| 142 | | #[template(path = "drop_form.html")] |
| 143 | | pub struct DropForm { |
| 144 | | shell: Shell, |
| 145 | | editing: bool, |
| 146 | | action: String, |
| 147 | | cancel: String, |
| 148 | | title: String, |
| 149 | | description: String, |
| 150 | | secret: bool, |
| 151 | | files: Vec<(String, String)>, |
| 152 | | |
| 153 | | kept: Vec<String>, |
| 154 | | message: String, |
| 155 | | delegation: Option<String>, |
| 156 | | error: Option<String>, |
| 157 | | } |
| 158 | |
|
| 159 | |
|
| 160 | 2× | pub async fn mine(State(app): State<Shared>, viewer: Viewer) -> Result<Response, Error> { |
| 161 | 2× | let shell = Shell::new(&app, "/drops", &viewer); |
| 162 | 2× | let me = require(&viewer, &shell)?; |
| 163 | 1× | Ok(Redirect::to(&format!("/{}/drops", me.account.username)).into_response()) |
| 164 | 2× | } |
| 165 | |
|
| 166 | | #[derive(Deserialize, Default)] |
| 167 | | pub struct ListQuery { |
| 168 | | #[serde(default)] |
| 169 | | deleted: Option<String>, |
| 170 | | } |
| 171 | |
|
| 172 | |
|
| 173 | 6× | pub async fn list( |
| 174 | 6× | State(app): State<Shared>, |
| 175 | 6× | viewer: Viewer, |
| 176 | 6× | Path(owner): Path<String>, |
| 177 | 6× | Query(q): Query<ListQuery>, |
| 178 | 6× | ) -> Result<Page<DropsPage>, Error> { |
| 179 | 6× | let shell = Shell::new(&app, &format!("/{owner}/drops"), &viewer); |
| 180 | 6× | let db_err = |e| Error::from_db(shell.clone(), e); |
| 181 | 6× | let Some(account) = db::accounts::account_by_name(&app.db, &owner).await.map_err(db_err)? else { |
| 182 | 0× | return Err(Error::not_found(shell)); |
| 183 | | }; |
| 184 | 6× | let rows = db::repos::drops_of(&app.db, &account.username).await.map_err(db_err)?; |
| 185 | 6× | let mut lines = Vec::new(); |
| 186 | 9× | for row in rows.into_iter().filter(|r| drops::listed_to(viewer.account(), r)) { |
| 187 | 7× | let files = drops::files(&app, &row, "").await.map(|(_, f)| f).unwrap_or_default(); |
| 188 | 7× | lines.push(DropLine { |
| 189 | 7× | url: drops::url_of(&row), |
| 190 | 7× | title: drops::shown_title(&row, &files), |
| 191 | 7× | description: row.description.clone(), |
| 192 | 7× | secret: row.visibility() != hub_api::Visibility::Public, |
| 193 | 7× | files: files.into_iter().map(|f| f.path).collect(), |
| 194 | 7× | updated_ms: row.updated_ms, |
| 195 | | }); |
| 196 | | } |
| 197 | 6× | let is_me = viewer.account().is_some_and(|a| a.id == account.id); |
| 198 | 6× | let notice = q.deleted.map(|_| "The drop was deleted.".to_string()); |
| 199 | 6× | Ok(Page(DropsPage { shell, owner: account.username, is_me, drops: lines, notice })) |
| 200 | 6× | } |
| 201 | |
|
| 202 | |
|
| 203 | 56× | async fn lookup(app: &Shared, shell: &Shell, viewer: &Viewer, owner: &str, id: &str) -> Result<db::repos::RepoRow, Error> { |
| 204 | 56× | let row = drops::find(app, owner, id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 205 | 56× | let row = row.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 206 | 54× | let role = crate::auth::roles::role_for(&app.db, viewer.account(), &row).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 207 | 54× | if !crate::auth::roles::allows(role, Access::Read) { |
| 208 | 0× | return Err(Error::not_found(shell.clone())); |
| 209 | 54× | } |
| 210 | 54× | Ok(row) |
| 211 | 56× | } |
| 212 | |
|
| 213 | |
|
| 214 | 35× | fn anchor_of(path: &str) -> String { |
| 215 | 215× | format!("file-{}", path.trim_matches('/').replace(|c: char| !c.is_ascii_alphanumeric() && c != '.' && c != '_', "-")) |
| 216 | 35× | } |
| 217 | |
|
| 218 | 28× | fn shown(files: Vec<DropFile>, base_file: &str, base_raw: &str) -> Vec<ShownFile> { |
| 219 | 28× | files |
| 220 | 28× | .into_iter() |
| 221 | 35× | .map(|f| { |
| 222 | 35× | let language = highlight::language(&f.path); |
| 223 | 35× | let anchor = anchor_of(&f.path); |
| 224 | 35× | let lower = f.path.to_lowercase(); |
| 225 | 35× | let (lines, md, note) = match &f.text { |
| 226 | 35× | Some(text) if lower.ends_with(".md") || lower.ends_with(".markdown") => { |
| 227 | 5× | (vec![], Some(markdown::render(text, base_file, base_raw)), None) |
| 228 | | } |
| 229 | 30× | Some(text) => (highlight::lines(&f.path, text), None, None), |
| 230 | 0× | None if crate::web::mime_of(&f.path).starts_with("image/") => (vec![], None, Some("An image.".to_string())), |
| 231 | 0× | None => (vec![], None, Some("Not shown: a binary file, or too large to show here.".to_string())), |
| 232 | | }; |
| 233 | 35× | ShownFile { path: f.path, anchor, size: f.size, language, lines, markdown: md, note } |
| 234 | 35× | }) |
| 235 | 28× | .collect() |
| 236 | 28× | } |
| 237 | |
|
| 238 | 29× | async fn page(app: &Shared, viewer: &Viewer, owner: &str, id: &str, r: &str) -> Result<Page<DropPage>, Error> { |
| 239 | 29× | let path = if r.is_empty() { format!("/{owner}/drops/{id}") } else { format!("/{owner}/drops/{id}/tree/{r}") }; |
| 240 | 29× | let shell = Shell::new(app, &path, viewer); |
| 241 | 29× | let row = lookup(app, &shell, viewer, owner, id).await?; |
| 242 | 27× | let (target, files) = drops::files(app, &row, r).await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 243 | 27× | let url = drops::url_of(&row); |
| 244 | 27× | let title = drops::shown_title(&row, &files); |
| 245 | 27× | let label = target.label.clone(); |
| 246 | 27× | let files = shown(files, &format!("{url}/file/{label}/"), &format!("{url}/raw/{label}/")); |
| 247 | 27× | let (can_edit, can_delete) = (drops::may_edit(viewer.account(), &row), drops::may_delete(viewer.account(), &row)); |
| 248 | 27× | let role = if can_edit { crate::auth::Role::Admin } else { crate::auth::Role::Read }; |
| 249 | 27× | let can_fork = viewer.account().is_some_and(|a| a.username != row.owner && !a.blocked()); |
| 250 | 27× | let forked_from = match row.forked_from { |
| 251 | 1× | Some(id) => db::repos::get_by_id(&app.db, id).await.ok().flatten().filter(|r| r.is_drop()).map(|r| drops::url_of(&r)), |
| 252 | 26× | None => None, |
| 253 | | }; |
| 254 | 27× | let comments = db::repos::drop_comments(&app.db, row.id) |
| 255 | 27× | .await |
| 256 | 27× | .unwrap_or_default() |
| 257 | 27× | .into_iter() |
| 258 | 27× | .map(|c| ShownComment { |
| 259 | 8× | may_delete: may_delete_comment(viewer, &row, &c), |
| 260 | 8× | id: c.id, |
| 261 | 8× | html: markdown::render(&c.body, "", ""), |
| 262 | 8× | author: c.author, |
| 263 | 8× | created_ms: c.created_ms, |
| 264 | 8× | }) |
| 265 | 27× | .collect(); |
| 266 | 27× | let embed = format!( |
| 267 | | "<iframe src=\"{}{}/embed\" width=\"100%\" height=\"400\" style=\"border:0\" title=\"{}\"></iframe>", |
| 268 | 27× | app.config.base_url().trim_end_matches('/'), |
| 269 | 27× | drops::url_of(&row), |
| 270 | 27× | title.replace('"', """) |
| 271 | | ); |
| 272 | 27× | let stars = db::repos::counts(&app.db, row.id).await.map(|(s, _)| s).unwrap_or(0); |
| 273 | 27× | let starred = match viewer.account() { |
| 274 | 17× | Some(a) => db::repos::starred(&app.db, a.id, row.id).await.unwrap_or(false), |
| 275 | 10× | None => false, |
| 276 | | }; |
| 277 | 27× | Ok(Page(DropPage { |
| 278 | 27× | embed, |
| 279 | 27× | comments, |
| 280 | 27× | stars, |
| 281 | 27× | starred, |
| 282 | 27× | signed_in: viewer.account().is_some(), |
| 283 | 27× | can_fork, |
| 284 | 27× | forked_from, |
| 285 | 27× | repo: RepoShell::new(app, &row, "code").with_role(role), |
| 286 | 27× | title, |
| 287 | 27× | description: row.description.clone(), |
| 288 | 27× | secret: row.visibility() != hub_api::Visibility::Public, |
| 289 | 27× | files, |
| 290 | 27× | can_edit, |
| 291 | 27× | can_delete, |
| 292 | 27× | clone_url: format!("{}{url}", app.config.base_url()), |
| 293 | 27× | at_default: label == row.default_bookmark, |
| 294 | 27× | target: label, |
| 295 | 27× | created_ms: row.created_ms, |
| 296 | 27× | updated_ms: row.updated_ms, |
| 297 | 27× | shell, |
| 298 | 27× | })) |
| 299 | 29× | } |
| 300 | |
|
| 301 | |
|
| 302 | 28× | pub async fn show(State(app): State<Shared>, viewer: Viewer, Path((owner, id)): Path<(String, String)>) -> Result<Page<DropPage>, Error> { |
| 303 | 28× | page(&app, &viewer, &owner, &id, "").await |
| 304 | 28× | } |
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | 1× | pub async fn show_at( |
| 309 | 1× | State(app): State<Shared>, |
| 310 | 1× | viewer: Viewer, |
| 311 | 1× | Path((owner, id, r)): Path<(String, String, String)>, |
| 312 | 1× | ) -> Result<Page<DropPage>, Error> { |
| 313 | 1× | page(&app, &viewer, &owner, &id, &r).await |
| 314 | 1× | } |
| 315 | |
|
| 316 | |
|
| 317 | | struct Submitted { |
| 318 | | csrf: String, |
| 319 | | title: String, |
| 320 | | description: String, |
| 321 | | secret: bool, |
| 322 | | message: String, |
| 323 | | files: Vec<NewFile>, |
| 324 | | } |
| 325 | |
|
| 326 | 7× | fn submitted(pairs: Vec<(String, String)>) -> Submitted { |
| 327 | 7× | let mut s = Submitted { |
| 328 | 7× | csrf: String::new(), |
| 329 | 7× | title: String::new(), |
| 330 | 7× | description: String::new(), |
| 331 | 7× | secret: false, |
| 332 | 7× | message: String::new(), |
| 333 | 7× | files: vec![], |
| 334 | 7× | }; |
| 335 | 49× | for (k, v) in pairs { |
| 336 | 49× | match k.as_str() { |
| 337 | 49× | "_csrf" => s.csrf = v, |
| 338 | 42× | "title" => s.title = v, |
| 339 | 36× | "description" => s.description = v, |
| 340 | 33× | "visibility" => s.secret = v == "secret", |
| 341 | 27× | "message" => s.message = v, |
| 342 | 26× | "name" => s.files.push(NewFile { name: v, content: String::new() }), |
| 343 | 13× | "content" => match s.files.last_mut() { |
| 344 | 13× | Some(f) if f.content.is_empty() => f.content = v, |
| 345 | 0× | _ => s.files.push(NewFile { name: String::new(), content: v }), |
| 346 | | }, |
| 347 | 0× | _ => {} |
| 348 | | } |
| 349 | | } |
| 350 | | |
| 351 | 13× | s.files.retain(|f| !f.name.trim().is_empty() || !f.content.trim().is_empty()); |
| 352 | 7× | s |
| 353 | 7× | } |
| 354 | |
|
| 355 | 8× | fn form_files(files: &[NewFile]) -> Vec<(String, String)> { |
| 356 | 9× | let mut out: Vec<(String, String)> = files.iter().map(|f| (f.name.clone(), f.content.clone())).collect(); |
| 357 | 8× | out.push((String::new(), String::new())); |
| 358 | 8× | out |
| 359 | 8× | } |
| 360 | |
|
| 361 | |
|
| 362 | 4× | pub async fn new_form(State(app): State<Shared>, viewer: Viewer) -> Result<Page<DropForm>, Error> { |
| 363 | 4× | let shell = Shell::new(&app, "/drops/new", &viewer); |
| 364 | 4× | let me = require(&viewer, &shell)?; |
| 365 | 4× | let delegation = crate::web::edit::signer_for(&app, me.account.id).await.err(); |
| 366 | 4× | Ok(Page(DropForm { |
| 367 | 4× | cancel: format!("/{}/drops", me.account.username), |
| 368 | 4× | shell, |
| 369 | 4× | editing: false, |
| 370 | 4× | action: "/drops/new".into(), |
| 371 | 4× | title: String::new(), |
| 372 | 4× | description: String::new(), |
| 373 | 4× | secret: false, |
| 374 | 4× | files: form_files(&[]), |
| 375 | 4× | kept: vec![], |
| 376 | 4× | message: String::new(), |
| 377 | 4× | delegation, |
| 378 | 4× | error: None, |
| 379 | 4× | })) |
| 380 | 4× | } |
| 381 | |
|
| 382 | |
|
| 383 | 4× | pub async fn create(State(app): State<Shared>, viewer: Viewer, Form(pairs): Form<Vec<(String, String)>>) -> Result<Response, Error> { |
| 384 | 4× | let shell = Shell::new(&app, "/drops/new", &viewer); |
| 385 | 4× | let me = require(&viewer, &shell)?; |
| 386 | 4× | let f = submitted(pairs); |
| 387 | 4× | csrf(&viewer, &f.csrf, &shell)?; |
| 388 | 4× | match drops::create(&app, &me.account, &f.title, &f.description, f.secret, &f.files).await { |
| 389 | 3× | Ok(row) => Ok(Redirect::to(&drops::url_of(&row)).into_response()), |
| 390 | 1× | Err(drops::DropError::Bad(msg)) => { |
| 391 | 1× | let delegation = crate::web::edit::signer_for(&app, me.account.id).await.err(); |
| 392 | 1× | Ok(Page(DropForm { |
| 393 | 1× | cancel: format!("/{}/drops", me.account.username), |
| 394 | 1× | shell, |
| 395 | 1× | editing: false, |
| 396 | 1× | action: "/drops/new".into(), |
| 397 | 1× | title: f.title, |
| 398 | 1× | description: f.description, |
| 399 | 1× | secret: f.secret, |
| 400 | 1× | files: form_files(&f.files), |
| 401 | 1× | kept: vec![], |
| 402 | 1× | message: String::new(), |
| 403 | 1× | delegation, |
| 404 | 1× | error: Some(msg), |
| 405 | 1× | }) |
| 406 | 1× | .into_response()) |
| 407 | | } |
| 408 | 0× | Err(e) => Err(Error { status: axum::http::StatusCode::INTERNAL_SERVER_ERROR, ..Error::bad(shell, e.to_string()) }), |
| 409 | | } |
| 410 | 4× | } |
| 411 | |
|
| 412 | |
|
| 413 | |
|
| 414 | 10× | async fn writable( |
| 415 | 10× | app: &Shared, |
| 416 | 10× | shell: &Shell, |
| 417 | 10× | viewer: &Viewer, |
| 418 | 10× | owner: &str, |
| 419 | 10× | id: &str, |
| 420 | 10× | deleting: bool, |
| 421 | 10× | ) -> Result<db::repos::RepoRow, Error> { |
| 422 | 10× | let row = lookup(app, shell, viewer, owner, id).await?; |
| 423 | 10× | let may = if deleting { drops::may_delete(viewer.account(), &row) } else { drops::may_edit(viewer.account(), &row) }; |
| 424 | 10× | if !may { |
| 425 | 4× | return Err(Error::not_found(shell.clone())); |
| 426 | 6× | } |
| 427 | 6× | Ok(row) |
| 428 | 10× | } |
| 429 | |
|
| 430 | 3× | async fn edit_page( |
| 431 | 3× | app: &Shared, |
| 432 | 3× | shell: Shell, |
| 433 | 3× | viewer: &Viewer, |
| 434 | 3× | row: &db::repos::RepoRow, |
| 435 | 3× | wanted: Option<&Submitted>, |
| 436 | 3× | error: Option<String>, |
| 437 | 3× | ) -> Result<Page<DropForm>, Error> { |
| 438 | 3× | let me = require(viewer, &shell)?; |
| 439 | 3× | let (_, current) = drops::files(app, row, "").await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 440 | 8× | let kept: Vec<String> = current.iter().filter(|f| f.text.is_none()).map(|f| f.path.clone()).collect(); |
| 441 | 3× | let files = match wanted { |
| 442 | 1× | Some(w) => form_files(&w.files), |
| 443 | 2× | None => form_files( |
| 444 | 2× | ¤t |
| 445 | 2× | .iter() |
| 446 | 5× | .filter_map(|f| f.text.as_ref().map(|t| NewFile { name: f.path.clone(), content: t.clone() })) |
| 447 | 2× | .collect::<Vec<_>>(), |
| 448 | | ), |
| 449 | | }; |
| 450 | 3× | let delegation = crate::web::edit::signer_for(app, me.account.id).await.err(); |
| 451 | 3× | let url = drops::url_of(row); |
| 452 | | Ok(Page(DropForm { |
| 453 | 3× | shell, |
| 454 | | editing: true, |
| 455 | 3× | action: format!("{url}/edit"), |
| 456 | 3× | cancel: url, |
| 457 | 3× | title: wanted.map(|w| w.title.clone()).unwrap_or_else(|| row.title.clone()), |
| 458 | 3× | description: wanted.map(|w| w.description.clone()).unwrap_or_else(|| row.description.clone()), |
| 459 | 3× | secret: wanted.map(|w| w.secret).unwrap_or(row.visibility() != hub_api::Visibility::Public), |
| 460 | 3× | files, |
| 461 | 3× | kept, |
| 462 | 3× | message: wanted.map(|w| w.message.clone()).unwrap_or_default(), |
| 463 | 3× | delegation, |
| 464 | 3× | error, |
| 465 | | })) |
| 466 | 3× | } |
| 467 | |
|
| 468 | |
|
| 469 | 4× | pub async fn edit_form( |
| 470 | 4× | State(app): State<Shared>, |
| 471 | 4× | viewer: Viewer, |
| 472 | 4× | Path((owner, id)): Path<(String, String)>, |
| 473 | 4× | ) -> Result<Page<DropForm>, Error> { |
| 474 | 4× | let shell = Shell::new(&app, &format!("/{owner}/drops/{id}/edit"), &viewer); |
| 475 | 4× | require(&viewer, &shell)?; |
| 476 | 4× | let row = writable(&app, &shell, &viewer, &owner, &id, false).await?; |
| 477 | 2× | edit_page(&app, shell, &viewer, &row, None, None).await |
| 478 | 4× | } |
| 479 | |
|
| 480 | |
|
| 481 | |
|
| 482 | |
|
| 483 | 3× | pub async fn save( |
| 484 | 3× | State(app): State<Shared>, |
| 485 | 3× | viewer: Viewer, |
| 486 | 3× | Path((owner, id)): Path<(String, String)>, |
| 487 | 3× | Form(pairs): Form<Vec<(String, String)>>, |
| 488 | 3× | ) -> Result<Response, Error> { |
| 489 | 3× | let shell = Shell::new(&app, &format!("/{owner}/drops/{id}/edit"), &viewer); |
| 490 | 3× | let me = require(&viewer, &shell)?; |
| 491 | 3× | let f = submitted(pairs); |
| 492 | 3× | csrf(&viewer, &f.csrf, &shell)?; |
| 493 | 3× | let row = writable(&app, &shell, &viewer, &owner, &id, false).await?; |
| 494 | 2× | let wanted = match drops::check_files(&f.files, false) { |
| 495 | 1× | Ok(w) => w, |
| 496 | 1× | Err(msg) => return Ok(edit_page(&app, shell, &viewer, &row, Some(&f), Some(msg)).await?.into_response()), |
| 497 | | }; |
| 498 | 1× | let (_, current) = drops::files(&app, &row, "").await.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 499 | 1× | let update = drops::Update { |
| 500 | 1× | title: f.title.clone(), |
| 501 | 1× | description: f.description.clone(), |
| 502 | 1× | secret: f.secret, |
| 503 | 1× | files: drops::edits_between(¤t, &wanted), |
| 504 | 1× | message: f.message.clone(), |
| 505 | 1× | }; |
| 506 | 1× | match drops::update(&app, &me.account, &row, update).await { |
| 507 | 1× | Ok(()) => Ok(Redirect::to(&drops::url_of(&row)).into_response()), |
| 508 | 0× | Err(drops::DropError::Bad(msg)) => Ok(edit_page(&app, shell, &viewer, &row, Some(&f), Some(msg)).await?.into_response()), |
| 509 | 0× | Err(e) => Err(Error { status: axum::http::StatusCode::INTERNAL_SERVER_ERROR, ..Error::bad(shell, e.to_string()) }), |
| 510 | | } |
| 511 | 3× | } |
| 512 | |
|
| 513 | | #[derive(Deserialize)] |
| 514 | | pub struct DeleteForm { |
| 515 | | #[serde(rename = "_csrf")] |
| 516 | | csrf_token: String, |
| 517 | | } |
| 518 | |
|
| 519 | |
|
| 520 | 3× | pub async fn delete( |
| 521 | 3× | State(app): State<Shared>, |
| 522 | 3× | viewer: Viewer, |
| 523 | 3× | Path((owner, id)): Path<(String, String)>, |
| 524 | 3× | Form(f): Form<DeleteForm>, |
| 525 | 3× | ) -> Result<Response, Error> { |
| 526 | 3× | let shell = Shell::new(&app, &format!("/{owner}/drops/{id}"), &viewer); |
| 527 | 3× | let me = require(&viewer, &shell)?; |
| 528 | 3× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 529 | 3× | let row = writable(&app, &shell, &viewer, &owner, &id, true).await?; |
| 530 | 2× | drops::delete(&app, &me.account, &row).await.map_err(|e| Error::bad(shell.clone(), e.to_string()))?; |
| 531 | 2× | Ok(Redirect::to(&format!("/{}/drops?deleted=1", row.owner)).into_response()) |
| 532 | 3× | } |
| 533 | |
|
| 534 | |
|
| 535 | |
|
| 536 | 11× | fn may_delete_comment(viewer: &Viewer, row: &db::repos::RepoRow, c: &db::repos::DropComment) -> bool { |
| 537 | 11× | drops::may_delete_comment(viewer.account(), row, c) |
| 538 | 11× | } |
| 539 | |
|
| 540 | | #[derive(Deserialize)] |
| 541 | | pub struct CommentForm { |
| 542 | | #[serde(rename = "_csrf")] |
| 543 | | csrf_token: String, |
| 544 | | body: String, |
| 545 | | } |
| 546 | |
|
| 547 | |
|
| 548 | |
|
| 549 | 2× | pub async fn comment( |
| 550 | 2× | State(app): State<Shared>, |
| 551 | 2× | viewer: Viewer, |
| 552 | 2× | Path((owner, id)): Path<(String, String)>, |
| 553 | 2× | Form(f): Form<CommentForm>, |
| 554 | 2× | ) -> Result<Response, Error> { |
| 555 | 2× | let shell = Shell::new(&app, &format!("/{owner}/drops/{id}"), &viewer); |
| 556 | 2× | let me = require(&viewer, &shell)?; |
| 557 | 2× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 558 | 2× | let row = lookup(&app, &shell, &viewer, &owner, &id).await?; |
| 559 | 2× | let n = drops::add_comment(&app, &me.account, &row, &f.body).await.map_err(|e| Error::bad(shell.clone(), e.to_string()))?; |
| 560 | 2× | let url = drops::url_of(&row); |
| 561 | 2× | Ok(Redirect::to(&format!("{url}#comment-{n}")).into_response()) |
| 562 | 2× | } |
| 563 | |
|
| 564 | |
|
| 565 | 3× | pub async fn delete_comment( |
| 566 | 3× | State(app): State<Shared>, |
| 567 | 3× | viewer: Viewer, |
| 568 | 3× | Path((owner, id, n)): Path<(String, String, i64)>, |
| 569 | 3× | Form(f): Form<DeleteForm>, |
| 570 | 3× | ) -> Result<Response, Error> { |
| 571 | 3× | let shell = Shell::new(&app, &format!("/{owner}/drops/{id}"), &viewer); |
| 572 | 3× | require(&viewer, &shell)?; |
| 573 | 3× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 574 | 3× | let row = lookup(&app, &shell, &viewer, &owner, &id).await?; |
| 575 | 3× | let c = db::repos::drop_comment(&app.db, row.id, n).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 576 | 3× | match c { |
| 577 | 3× | Some(c) if may_delete_comment(&viewer, &row, &c) => { |
| 578 | 2× | db::repos::delete_drop_comment(&app.db, row.id, n).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 579 | 2× | Ok(Redirect::to(&format!("{}#comments", drops::url_of(&row))).into_response()) |
| 580 | | } |
| 581 | 1× | _ => Err(Error::not_found(shell)), |
| 582 | | } |
| 583 | 3× | } |
| 584 | |
|
| 585 | |
|
| 586 | 3× | pub async fn star( |
| 587 | 3× | State(app): State<Shared>, |
| 588 | 3× | viewer: Viewer, |
| 589 | 3× | Path((owner, id)): Path<(String, String)>, |
| 590 | 3× | Form(f): Form<DeleteForm>, |
| 591 | 3× | ) -> Result<Response, Error> { |
| 592 | 3× | let shell = Shell::new(&app, &format!("/{owner}/drops/{id}"), &viewer); |
| 593 | 3× | let me = require(&viewer, &shell)?; |
| 594 | 3× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 595 | 3× | let row = lookup(&app, &shell, &viewer, &owner, &id).await?; |
| 596 | 3× | db::repos::toggle_star(&app.db, me.account.id, row.id).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 597 | 3× | Ok(Redirect::to(&drops::url_of(&row)).into_response()) |
| 598 | 3× | } |
| 599 | |
|
| 600 | |
|
| 601 | |
|
| 602 | 1× | pub async fn fork( |
| 603 | 1× | State(app): State<Shared>, |
| 604 | 1× | viewer: Viewer, |
| 605 | 1× | Path((owner, id)): Path<(String, String)>, |
| 606 | 1× | Form(f): Form<DeleteForm>, |
| 607 | 1× | ) -> Result<Response, Error> { |
| 608 | 1× | let shell = Shell::new(&app, &format!("/{owner}/drops/{id}"), &viewer); |
| 609 | 1× | let me = require(&viewer, &shell)?; |
| 610 | 1× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 611 | 1× | let row = lookup(&app, &shell, &viewer, &owner, &id).await?; |
| 612 | 1× | let made = drops::fork(&app, &me.account, &row).await.map_err(|e| Error::bad(shell.clone(), e.to_string()))?; |
| 613 | 1× | Ok(Redirect::to(&drops::url_of(&made)).into_response()) |
| 614 | 1× | } |
| 615 | |
|
| 616 | |
|
| 617 | |
|
| 618 | 7× | fn repo_name(id: &str) -> String { |
| 619 | 7× | format!("{}/{id}", crate::repos::names::DROPS) |
| 620 | 7× | } |
| 621 | |
|
| 622 | |
|
| 623 | 2× | pub async fn changes( |
| 624 | 2× | State(app): State<Shared>, |
| 625 | 2× | viewer: Viewer, |
| 626 | 2× | Path((owner, id, r)): Path<(String, String, String)>, |
| 627 | 2× | q: Query<super::history::HistoryQuery>, |
| 628 | 2× | ) -> Result<Response, Error> { |
| 629 | 2× | let row = drop_or_404(&app, &viewer, &owner, &id).await?; |
| 630 | 2× | super::history::history(State(app), viewer, Path((row.owner, repo_name(&id), r)), q).await.map(IntoResponse::into_response) |
| 631 | 2× | } |
| 632 | |
|
| 633 | |
|
| 634 | 1× | pub async fn change( |
| 635 | 1× | State(app): State<Shared>, |
| 636 | 1× | viewer: Viewer, |
| 637 | 1× | Path((owner, id, hash)): Path<(String, String, String)>, |
| 638 | 1× | q: Query<super::history::DiffQuery>, |
| 639 | 1× | ) -> Result<Response, Error> { |
| 640 | 1× | let row = drop_or_404(&app, &viewer, &owner, &id).await?; |
| 641 | 1× | super::history::change(State(app), viewer, Path((row.owner, repo_name(&id), hash)), q).await.map(IntoResponse::into_response) |
| 642 | 1× | } |
| 643 | |
|
| 644 | |
|
| 645 | 4× | pub async fn raw( |
| 646 | 4× | State(app): State<Shared>, |
| 647 | 4× | viewer: Viewer, |
| 648 | 4× | Path((owner, id, r, path)): Path<(String, String, String, String)>, |
| 649 | 4× | ) -> Result<Response, Error> { |
| 650 | 4× | let row = drop_or_404(&app, &viewer, &owner, &id).await?; |
| 651 | 4× | super::repo::raw_file(State(app), viewer, Path((row.owner, repo_name(&id), r, path))).await |
| 652 | 4× | } |
| 653 | |
|
| 654 | |
|
| 655 | |
|
| 656 | 0× | pub async fn file(Path((owner, id, r, path)): Path<(String, String, String, String)>) -> Response { |
| 657 | 0× | Redirect::to(&format!("/{owner}/drops/{id}/tree/{r}#{}", anchor_of(&path))).into_response() |
| 658 | 0× | } |
| 659 | |
|
| 660 | 7× | async fn drop_or_404(app: &Shared, viewer: &Viewer, owner: &str, id: &str) -> Result<db::repos::RepoRow, Error> { |
| 661 | 7× | let shell = Shell::new(app, &format!("/{owner}/drops/{id}"), viewer); |
| 662 | 7× | lookup(app, &shell, viewer, owner, id).await |
| 663 | 7× | } |