| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use super::settings::csrf; |
| 5 | | use super::{filters, Error, Page, Shell}; |
| 6 | | use crate::auth::{roles, Access, Viewer}; |
| 7 | | use crate::db::{self, issues as idb, projects as pdb}; |
| 8 | | use crate::Shared; |
| 9 | | use askama::Template; |
| 10 | | use axum::extract::{Form, Path, Query, State}; |
| 11 | | use axum::response::{IntoResponse, Redirect, Response}; |
| 12 | | use serde::Deserialize; |
| 13 | |
|
| 14 | |
|
| 15 | | pub struct Rights { |
| 16 | | |
| 17 | | pub member: bool, |
| 18 | | |
| 19 | | pub edit: bool, |
| 20 | | } |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | 252× | pub async fn rights_of(app: &Shared, account: Option<&db::accounts::Account>, owner: &str) -> sqlx::Result<Option<Rights>> { |
| 25 | 252× | let admin = account.is_some_and(|a| a.is_admin); |
| 26 | 252× | if let Some(a) = db::accounts::account_by_name(&app.db, owner).await? { |
| 27 | 247× | let me = account.is_some_and(|v| v.id == a.id); |
| 28 | 247× | return Ok(Some(Rights { member: me || admin, edit: me || admin })); |
| 29 | 5× | } |
| 30 | 5× | if let Some(o) = db::accounts::org_by_name(&app.db, owner).await? { |
| 31 | 3× | let member = match account { |
| 32 | 3× | Some(v) => db::accounts::org_role(&app.db, o.id, v.id).await?.is_some(), |
| 33 | 0× | None => false, |
| 34 | | }; |
| 35 | 3× | return Ok(Some(Rights { member: member || admin, edit: member || admin })); |
| 36 | 2× | } |
| 37 | 2× | Ok(None) |
| 38 | 252× | } |
| 39 | |
|
| 40 | 95× | async fn rights(app: &Shared, shell: &Shell, viewer: &Viewer, owner: &str) -> Result<Rights, Error> { |
| 41 | 95× | rights_of(app, viewer.account(), owner) |
| 42 | 95× | .await |
| 43 | 95× | .map_err(|e| Error::from_db(shell.clone(), e))? |
| 44 | 95× | .ok_or_else(|| Error::not_found(shell.clone())) |
| 45 | 95× | } |
| 46 | |
|
| 47 | | #[derive(Template)] |
| 48 | | #[template(path = "projects.html")] |
| 49 | | pub struct ProjectsPage { |
| 50 | | shell: Shell, |
| 51 | | owner: String, |
| 52 | | |
| 53 | | projects: Vec<pdb::Project>, |
| 54 | | can_edit: bool, |
| 55 | | error: Option<String>, |
| 56 | | |
| 57 | | |
| 58 | | orgs: Vec<String>, |
| 59 | | |
| 60 | | q: String, |
| 61 | | closed: bool, |
| 62 | | open_count: usize, |
| 63 | | closed_count: usize, |
| 64 | | } |
| 65 | |
|
| 66 | | impl ProjectsPage { |
| 67 | | |
| 68 | | |
| 69 | | #[allow(clippy::too_many_arguments)] |
| 70 | 12× | fn build( |
| 71 | 12× | shell: Shell, |
| 72 | 12× | owner: String, |
| 73 | 12× | all: Vec<pdb::Project>, |
| 74 | 12× | can_edit: bool, |
| 75 | 12× | error: Option<String>, |
| 76 | 12× | orgs: Vec<String>, |
| 77 | 12× | q: Option<String>, |
| 78 | 12× | closed: bool, |
| 79 | 12× | ) -> ProjectsPage { |
| 80 | 12× | let q = q.unwrap_or_default().trim().to_string(); |
| 81 | 12× | let needle = q.to_lowercase(); |
| 82 | 12× | let matching: Vec<pdb::Project> = all |
| 83 | 12× | .into_iter() |
| 84 | 12× | .filter(|p| needle.is_empty() || p.title.to_lowercase().contains(&needle) || p.description.to_lowercase().contains(&needle)) |
| 85 | 12× | .collect(); |
| 86 | 12× | let closed_count = matching.iter().filter(|p| p.closed).count(); |
| 87 | 12× | let open_count = matching.len() - closed_count; |
| 88 | 12× | let projects = matching.into_iter().filter(|p| p.closed == closed).collect(); |
| 89 | 12× | ProjectsPage { shell, owner, projects, can_edit, error, orgs, q, closed, open_count, closed_count } |
| 90 | 12× | } |
| 91 | | } |
| 92 | |
|
| 93 | |
|
| 94 | | #[derive(Deserialize, Default)] |
| 95 | | pub struct ProjectsQuery { |
| 96 | | #[serde(default)] |
| 97 | | q: Option<String>, |
| 98 | | #[serde(default)] |
| 99 | | state: Option<String>, |
| 100 | | } |
| 101 | |
|
| 102 | |
|
| 103 | 12× | async fn own_orgs(app: &Shared, viewer: &Viewer, owner: &str) -> Vec<String> { |
| 104 | 12× | match viewer.account() { |
| 105 | 11× | Some(a) if a.username == owner => { |
| 106 | 10× | crate::db::accounts::orgs_of(&app.db, a.id).await.map(|v| v.into_iter().map(|(o, _)| o.name).collect()).unwrap_or_default() |
| 107 | | } |
| 108 | 2× | _ => vec![], |
| 109 | | } |
| 110 | 12× | } |
| 111 | |
|
| 112 | 13× | pub async fn list( |
| 113 | 13× | State(app): State<Shared>, |
| 114 | 13× | viewer: Viewer, |
| 115 | 13× | Path(owner): Path<String>, |
| 116 | 13× | Query(query): Query<ProjectsQuery>, |
| 117 | 13× | ) -> Result<Page<ProjectsPage>, Error> { |
| 118 | 13× | let shell = Shell::new(&app, &format!("/projects/{owner}"), &viewer); |
| 119 | 13× | let r = rights(&app, &shell, &viewer, &owner).await?; |
| 120 | 12× | let projects = pdb::of_owner(&app.db, &owner) |
| 121 | 12× | .await |
| 122 | 12× | .map_err(|e| Error::from_db(shell.clone(), e))? |
| 123 | 12× | .into_iter() |
| 124 | 12× | .filter(|p| p.public || r.member) |
| 125 | 12× | .collect(); |
| 126 | 12× | let orgs = own_orgs(&app, &viewer, &owner).await; |
| 127 | 12× | let closed = query.state.as_deref() == Some("closed"); |
| 128 | 12× | Ok(Page(ProjectsPage::build(shell, owner, projects, r.edit, None, orgs, query.q, closed))) |
| 129 | 13× | } |
| 130 | |
|
| 131 | | #[derive(Deserialize)] |
| 132 | | pub struct NewProjectForm { |
| 133 | | #[serde(rename = "_csrf")] |
| 134 | | csrf_token: String, |
| 135 | | title: String, |
| 136 | | #[serde(default)] |
| 137 | | description: String, |
| 138 | | #[serde(default)] |
| 139 | | private: Option<String>, |
| 140 | | } |
| 141 | |
|
| 142 | 7× | pub async fn create( |
| 143 | 7× | State(app): State<Shared>, |
| 144 | 7× | viewer: Viewer, |
| 145 | 7× | Path(owner): Path<String>, |
| 146 | 7× | Form(f): Form<NewProjectForm>, |
| 147 | 7× | ) -> Result<Response, Error> { |
| 148 | 7× | let shell = Shell::new(&app, &format!("/projects/{owner}"), &viewer); |
| 149 | 7× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 150 | 7× | let r = rights(&app, &shell, &viewer, &owner).await?; |
| 151 | 7× | if !r.edit { |
| 152 | 0× | return Err(Error::not_found(shell)); |
| 153 | 7× | } |
| 154 | 7× | let title = f.title.trim(); |
| 155 | 7× | if title.is_empty() || title.len() > 100 { |
| 156 | 0× | let projects = pdb::of_owner(&app.db, &owner).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 157 | 0× | let orgs = own_orgs(&app, &viewer, &owner).await; |
| 158 | 0× | return Ok(Page(ProjectsPage::build( |
| 159 | 0× | shell, |
| 160 | 0× | owner, |
| 161 | 0× | projects, |
| 162 | 0× | true, |
| 163 | 0× | Some("A project needs a title of up to 100 characters.".into()), |
| 164 | 0× | orgs, |
| 165 | 0× | None, |
| 166 | 0× | false, |
| 167 | 0× | )) |
| 168 | 0× | .into_response()); |
| 169 | 7× | } |
| 170 | 7× | let p = pdb::create(&app.db, &owner, title, f.description.trim(), f.private.is_none()) |
| 171 | 7× | .await |
| 172 | 7× | .map_err(|e| Error::from_db(shell.clone(), e))?; |
| 173 | 7× | db::accounts::audit(&app.db, viewer.account(), "project.created", &format!("{owner}/{}", p.number), title).await.ok(); |
| 174 | 7× | Ok(Redirect::to(&format!("/projects/{owner}/{}", p.number)).into_response()) |
| 175 | 7× | } |
| 176 | |
|
| 177 | |
|
| 178 | | pub struct CardView { |
| 179 | | pub card: pdb::Card, |
| 180 | | |
| 181 | | pub reference: String, |
| 182 | | pub url: String, |
| 183 | | pub title: String, |
| 184 | | |
| 185 | | pub state: String, |
| 186 | | |
| 187 | | pub labels: Vec<idb::Label>, |
| 188 | | pub assignees: Vec<String>, |
| 189 | | } |
| 190 | |
|
| 191 | | pub struct ColumnView { |
| 192 | | pub column: pdb::Column, |
| 193 | | pub cards: Vec<CardView>, |
| 194 | | |
| 195 | | |
| 196 | | pub hidden: usize, |
| 197 | | pub total: usize, |
| 198 | | |
| 199 | | pub archived: usize, |
| 200 | | |
| 201 | | |
| 202 | | pub finished: bool, |
| 203 | | } |
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | | pub const COLUMN_SHOWN: usize = 30; |
| 208 | |
|
| 209 | |
|
| 210 | | #[derive(Deserialize, Default)] |
| 211 | | pub struct BoardQuery { |
| 212 | | |
| 213 | | #[serde(default)] |
| 214 | | all: Option<i64>, |
| 215 | | |
| 216 | | |
| 217 | | #[serde(default)] |
| 218 | | q: Option<String>, |
| 219 | | |
| 220 | | #[serde(default)] |
| 221 | | state: Option<String>, |
| 222 | | |
| 223 | | #[serde(default)] |
| 224 | | archived: Option<String>, |
| 225 | | } |
| 226 | |
|
| 227 | | impl BoardQuery { |
| 228 | 80× | fn text(&self) -> String { |
| 229 | 80× | self.q.as_deref().map(str::trim).unwrap_or("").to_string() |
| 230 | 80× | } |
| 231 | 80× | fn state(&self) -> String { |
| 232 | 80× | self.state.as_deref().map(str::trim).unwrap_or("").to_ascii_lowercase() |
| 233 | 80× | } |
| 234 | 41× | fn archived(&self) -> bool { |
| 235 | 41× | self.archived.as_deref().is_some_and(|a| !a.is_empty() && a != "0") |
| 236 | 41× | } |
| 237 | | } |
| 238 | |
|
| 239 | |
|
| 240 | | #[derive(Default)] |
| 241 | | struct CardFilter { |
| 242 | | labels: Vec<String>, |
| 243 | | assignees: Vec<String>, |
| 244 | | state: String, |
| 245 | | words: Vec<String>, |
| 246 | | } |
| 247 | |
|
| 248 | | impl CardFilter { |
| 249 | 40× | fn of(view: &BoardQuery) -> CardFilter { |
| 250 | 40× | let mut f = CardFilter { state: view.state(), ..Default::default() }; |
| 251 | 40× | for tok in view.text().split_whitespace() { |
| 252 | 9× | match tok.split_once(':') { |
| 253 | 6× | Some(("label", v)) if !v.is_empty() => f.labels.push(v.to_lowercase()), |
| 254 | 2× | Some(("assignee", v)) if !v.is_empty() => f.assignees.push(v.trim_start_matches('@').to_lowercase()), |
| 255 | 1× | Some(("state", v)) if !v.is_empty() => f.state = v.to_lowercase(), |
| 256 | 3× | _ => f.words.push(tok.to_lowercase()), |
| 257 | | } |
| 258 | | } |
| 259 | 40× | f |
| 260 | 40× | } |
| 261 | |
|
| 262 | 114× | fn is_empty(&self) -> bool { |
| 263 | 114× | self.labels.is_empty() && self.assignees.is_empty() && self.state.is_empty() && self.words.is_empty() |
| 264 | 114× | } |
| 265 | |
|
| 266 | | |
| 267 | | |
| 268 | 86× | fn keeps(&self, c: &CardView) -> bool { |
| 269 | 86× | let (title, reference) = (c.title.to_lowercase(), c.reference.to_lowercase()); |
| 270 | 86× | self.words.iter().all(|w| title.contains(w) || reference.contains(w)) |
| 271 | 50× | && self.labels.iter().all(|l| c.labels.iter().any(|x| x.name.eq_ignore_ascii_case(l))) |
| 272 | 47× | && self.assignees.iter().all(|a| c.assignees.iter().any(|x| x.eq_ignore_ascii_case(a))) |
| 273 | 45× | && (self.state.is_empty() || c.state.eq_ignore_ascii_case(&self.state)) |
| 274 | 86× | } |
| 275 | | } |
| 276 | |
|
| 277 | |
|
| 278 | 40× | fn shape(columns: &mut [ColumnView], view: &BoardQuery) { |
| 279 | 40× | let filter = CardFilter::of(view); |
| 280 | 114× | for col in columns.iter_mut() { |
| 281 | 114× | col.total = col.cards.len(); |
| 282 | 114× | if !filter.is_empty() { |
| 283 | 86× | col.cards.retain(|c| filter.keeps(c)); |
| 284 | 90× | } else if col.cards.len() > COLUMN_SHOWN && view.all != Some(col.column.id) && !view.archived() { |
| 285 | 1× | |
| 286 | 1× | let cut = col.cards.len() - COLUMN_SHOWN; |
| 287 | 1× | col.cards.drain(..cut); |
| 288 | 89× | } |
| 289 | 114× | col.hidden = col.total - col.cards.len(); |
| 290 | | } |
| 291 | 40× | } |
| 292 | |
|
| 293 | | #[derive(Template)] |
| 294 | | #[template(path = "project.html")] |
| 295 | | pub struct BoardPage { |
| 296 | | shell: Shell, |
| 297 | | project: pdb::Project, |
| 298 | | columns: Vec<ColumnView>, |
| 299 | | |
| 300 | | repos: Vec<String>, |
| 301 | | |
| 302 | | can_edit: bool, |
| 303 | | |
| 304 | | can_manage: bool, |
| 305 | | notice: Option<String>, |
| 306 | | error: Option<String>, |
| 307 | | |
| 308 | | |
| 309 | | transfer_to: Vec<String>, |
| 310 | | |
| 311 | | q: String, |
| 312 | | state: String, |
| 313 | | filtered: bool, |
| 314 | | |
| 315 | | archived: bool, |
| 316 | | |
| 317 | | archived_total: usize, |
| 318 | | } |
| 319 | |
|
| 320 | | #[allow(clippy::too_many_arguments)] |
| 321 | 42× | async fn board( |
| 322 | 42× | app: &Shared, |
| 323 | 42× | shell: Shell, |
| 324 | 42× | viewer: &Viewer, |
| 325 | 42× | owner: &str, |
| 326 | 42× | number: i64, |
| 327 | 42× | notice: Option<String>, |
| 328 | 42× | error: Option<String>, |
| 329 | 42× | view: &BoardQuery, |
| 330 | 42× | ) -> Result<Page<BoardPage>, Error> { |
| 331 | 42× | let e = |e| Error::from_db(shell.clone(), e); |
| 332 | 42× | let r = rights(app, &shell, viewer, owner).await?; |
| 333 | 42× | let project = pdb::get(&app.db, owner, number) |
| 334 | 42× | .await |
| 335 | 42× | .map_err(e)? |
| 336 | 42× | .filter(|p| p.public || r.member) |
| 337 | 42× | .ok_or_else(|| Error::not_found(shell.clone()))?; |
| 338 | 40× | let archived = view.archived(); |
| 339 | 40× | let (mut columns, repos) = board_columns(app, viewer.account(), owner, &project, archived).await.map_err(e)?; |
| 340 | 40× | shape(&mut columns, view); |
| 341 | 40× | let archived_total = columns.iter().map(|c| c.archived).sum(); |
| 342 | 40× | let can_edit = r.edit && !project.closed; |
| 343 | 40× | let mut transfer_to = Vec::new(); |
| 344 | 40× | if let (true, Some(a)) = (r.edit, viewer.account()) { |
| 345 | 39× | transfer_to.push(a.username.clone()); |
| 346 | 39× | let orgs = db::accounts::orgs_of(&app.db, a.id).await.map_err(e)?; |
| 347 | 39× | transfer_to.extend(orgs.into_iter().map(|(o, _)| o.name)); |
| 348 | 43× | transfer_to.retain(|o| o != owner); |
| 349 | 1× | } |
| 350 | 40× | let (q, state) = (view.text(), view.state()); |
| 351 | 40× | let filtered = !q.is_empty() || !state.is_empty(); |
| 352 | 40× | Ok(Page(BoardPage { |
| 353 | 40× | shell, |
| 354 | 40× | project, |
| 355 | 40× | columns, |
| 356 | 40× | repos, |
| 357 | 40× | can_edit, |
| 358 | 40× | can_manage: r.edit, |
| 359 | 40× | notice, |
| 360 | 40× | error, |
| 361 | 40× | transfer_to, |
| 362 | 40× | q, |
| 363 | 40× | state, |
| 364 | 40× | filtered, |
| 365 | 40× | archived, |
| 366 | 40× | archived_total, |
| 367 | 40× | })) |
| 368 | 42× | } |
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | 61× | pub async fn board_columns( |
| 375 | 61× | app: &Shared, |
| 376 | 61× | account: Option<&db::accounts::Account>, |
| 377 | 61× | owner: &str, |
| 378 | 61× | project: &pdb::Project, |
| 379 | 61× | archived: bool, |
| 380 | 61× | ) -> sqlx::Result<(Vec<ColumnView>, Vec<String>)> { |
| 381 | 61× | let mut readable: std::collections::HashMap<i64, db::repos::RepoRow> = Default::default(); |
| 382 | 67× | for row in db::repos::list_by_owner(&app.db, owner).await? { |
| 383 | 67× | let role = roles::role_for(&app.db, account, &row).await?; |
| 384 | 67× | if roles::allows(role, Access::Read) { |
| 385 | 59× | readable.insert(row.id, row); |
| 386 | 59× | } |
| 387 | | } |
| 388 | 61× | let all = pdb::cards(&app.db, project.id).await?; |
| 389 | 933× | let archived_in = |column: i64| all.iter().filter(|c| c.column_id == column && c.archived_ms.is_some()).count(); |
| 390 | 317× | let cards: Vec<&pdb::Card> = all.iter().filter(|c| c.archived_ms.is_some() == archived).collect(); |
| 391 | | |
| 392 | | |
| 393 | 61× | let mut shown: std::collections::HashMap<(i64, bool, i64), Shown> = Default::default(); |
| 394 | 61× | let mut repo_ids: Vec<(i64, bool)> = |
| 395 | 292× | cards.iter().filter_map(|c| Some((c.repo_id?, c.kind == "issue"))).filter(|(rid, _)| readable.contains_key(rid)).collect(); |
| 396 | 61× | repo_ids.sort(); |
| 397 | 61× | repo_ids.dedup(); |
| 398 | 61× | for (rid, issues) in repo_ids { |
| 399 | 110× | shown.extend(shown_in(app, rid, issues).await?.into_iter().map(|(n, s)| ((rid, issues, n), s))); |
| 400 | | } |
| 401 | 61× | let mut columns = Vec::new(); |
| 402 | 177× | for column in pdb::columns(&app.db, project.id).await? { |
| 403 | 177× | let mut views = Vec::new(); |
| 404 | 858× | for card in cards.iter().copied().filter(|c| c.column_id == column.id) { |
| 405 | 292× | match (card.kind.as_str(), card.repo_id, card.number) { |
| 406 | 292× | ("note", _, _) => views.push(CardView { |
| 407 | 202× | card: card.clone(), |
| 408 | 202× | reference: String::new(), |
| 409 | 202× | url: String::new(), |
| 410 | 202× | title: card.note.clone(), |
| 411 | 202× | state: String::new(), |
| 412 | 202× | labels: Vec::new(), |
| 413 | 202× | assignees: Vec::new(), |
| 414 | 202× | }), |
| 415 | 90× | (kind, Some(rid), Some(n)) => { |
| 416 | | |
| 417 | 90× | let Some(row) = readable.get(&rid) else { continue }; |
| 418 | 89× | let Some(s) = shown.get(&(rid, kind == "issue", n)) else { continue }; |
| 419 | 89× | let slug = format!("{}/{}", row.owner, row.name); |
| 420 | 89× | views.push(CardView { |
| 421 | 89× | card: card.clone(), |
| 422 | 89× | reference: format!("{slug}#{n}"), |
| 423 | 89× | url: format!("/{slug}/{kind}/{n}"), |
| 424 | 89× | title: s.title.clone(), |
| 425 | 89× | state: s.state.clone(), |
| 426 | 89× | labels: s.labels.clone(), |
| 427 | 89× | assignees: s.assignees.clone(), |
| 428 | 89× | }); |
| 429 | | } |
| 430 | 0× | _ => {} |
| 431 | | } |
| 432 | | } |
| 433 | 177× | let total = views.len(); |
| 434 | 177× | let archived = archived_in(column.id); |
| 435 | 291× | let items: Vec<&CardView> = views.iter().filter(|v| v.card.kind != "note").collect(); |
| 436 | 177× | let finished = !items.is_empty() && items.iter().all(|v| matches!(v.state.as_str(), "closed" | "landed")); |
| 437 | 177× | columns.push(ColumnView { column, cards: views, hidden: 0, total, archived, finished }); |
| 438 | | } |
| 439 | 61× | let mut repos: Vec<String> = readable.values().map(|r| r.name.clone()).collect(); |
| 440 | 61× | repos.sort(); |
| 441 | 61× | Ok((columns, repos)) |
| 442 | 61× | } |
| 443 | |
|
| 444 | |
|
| 445 | | struct Shown { |
| 446 | | title: String, |
| 447 | | state: String, |
| 448 | | labels: Vec<idb::Label>, |
| 449 | | assignees: Vec<String>, |
| 450 | | } |
| 451 | |
|
| 452 | |
|
| 453 | 59× | async fn shown_in(app: &Shared, repo_id: i64, issues: bool) -> sqlx::Result<Vec<(i64, Shown)>> { |
| 454 | | use std::collections::HashMap; |
| 455 | 59× | let mut out = Vec::new(); |
| 456 | 59× | if issues { |
| 457 | 48× | let mut labels: HashMap<i64, Vec<idb::Label>> = HashMap::new(); |
| 458 | 48× | for (id, l) in idb::labels_by_issue(&app.db, repo_id).await? { |
| 459 | 18× | labels.entry(id).or_default().push(l); |
| 460 | 18× | } |
| 461 | 48× | let mut assignees: HashMap<i64, Vec<String>> = HashMap::new(); |
| 462 | 48× | for (id, u) in idb::assignees_by_issue(&app.db, repo_id).await? { |
| 463 | 6× | assignees.entry(id).or_default().push(u); |
| 464 | 6× | } |
| 465 | 99× | for i in idb::list(&app.db, repo_id).await? { |
| 466 | 99× | let mut a = assignees.remove(&i.id).unwrap_or_default(); |
| 467 | 99× | a.sort(); |
| 468 | 99× | let s = Shown { labels: labels.remove(&i.id).unwrap_or_default(), assignees: a, title: i.title, state: i.state }; |
| 469 | 99× | out.push((i.number, s)); |
| 470 | 99× | } |
| 471 | | } else { |
| 472 | 11× | let mut labels: HashMap<i64, Vec<idb::Label>> = HashMap::new(); |
| 473 | 11× | for (id, l) in idb::labels_by_proposal(&app.db, repo_id).await? { |
| 474 | 0× | labels.entry(id).or_default().push(l); |
| 475 | 0× | } |
| 476 | 11× | for p in db::proposals::list(&app.db, repo_id, &[]).await? { |
| 477 | 11× | let s = Shown { labels: labels.remove(&p.id).unwrap_or_default(), assignees: Vec::new(), title: p.title, state: p.state }; |
| 478 | 11× | out.push((p.number, s)); |
| 479 | 11× | } |
| 480 | | } |
| 481 | 59× | Ok(out) |
| 482 | 59× | } |
| 483 | |
|
| 484 | 28× | pub async fn show( |
| 485 | 28× | State(app): State<Shared>, |
| 486 | 28× | viewer: Viewer, |
| 487 | 28× | Path((owner, number)): Path<(String, i64)>, |
| 488 | 28× | Query(view): Query<BoardQuery>, |
| 489 | 28× | ) -> Result<Response, Error> { |
| 490 | 28× | let shell = Shell::new(&app, &format!("/projects/{owner}/{number}"), &viewer); |
| 491 | | |
| 492 | 28× | let e = |e| Error::from_db(shell.clone(), e); |
| 493 | 28× | if pdb::get(&app.db, &owner, number).await.map_err(e)?.is_none() { |
| 494 | 2× | if let Some(p) = pdb::redirected(&app.db, &owner, number).await.map_err(e)? { |
| 495 | 1× | return Ok(Redirect::permanent(&format!("/projects/{}/{}", p.owner, p.number)).into_response()); |
| 496 | 1× | } |
| 497 | 26× | } |
| 498 | 27× | Ok(board(&app, shell, &viewer, &owner, number, None, None, &view).await?.into_response()) |
| 499 | 28× | } |
| 500 | |
|
| 501 | | #[derive(Deserialize)] |
| 502 | | pub struct BoardForm { |
| 503 | | #[serde(rename = "_csrf")] |
| 504 | | csrf_token: String, |
| 505 | | action: String, |
| 506 | | #[serde(default)] |
| 507 | | card: Option<i64>, |
| 508 | | #[serde(default)] |
| 509 | | column: Option<i64>, |
| 510 | | #[serde(default)] |
| 511 | | name: String, |
| 512 | | |
| 513 | | #[serde(default)] |
| 514 | | reference: String, |
| 515 | | #[serde(default)] |
| 516 | | repo: String, |
| 517 | | #[serde(default)] |
| 518 | | note: String, |
| 519 | | |
| 520 | | #[serde(default)] |
| 521 | | mode: String, |
| 522 | | #[serde(default)] |
| 523 | | title: String, |
| 524 | | #[serde(default)] |
| 525 | | description: String, |
| 526 | | #[serde(default)] |
| 527 | | private: Option<String>, |
| 528 | | #[serde(default)] |
| 529 | | closed: Option<String>, |
| 530 | | |
| 531 | | #[serde(default)] |
| 532 | | to: String, |
| 533 | | |
| 534 | | #[serde(default)] |
| 535 | | days: String, |
| 536 | | } |
| 537 | |
|
| 538 | |
|
| 539 | |
|
| 540 | |
|
| 541 | |
|
| 542 | 26× | fn parse_reference(reference: &str, repo: &str, owner: &str) -> Result<(String, i64), String> { |
| 543 | 26× | let bad = || "Name an issue or proposal as repo#12, or write a note.".to_string(); |
| 544 | 26× | let r = reference.trim(); |
| 545 | 26× | let (name, n) = r.split_once('#').ok_or_else(bad)?; |
| 546 | 25× | let name = if name.is_empty() { repo.trim() } else { name.trim() }; |
| 547 | 25× | let name = match name.split_once('/') { |
| 548 | 4× | Some((o, rest)) if o.eq_ignore_ascii_case(owner) => rest, |
| 549 | 2× | Some((o, _)) => return Err(format!("This board holds only {owner}'s issues and proposals, not {o}'s.")), |
| 550 | 21× | None => name, |
| 551 | | }; |
| 552 | 23× | let n: i64 = n.trim().parse().map_err(|_| bad())?; |
| 553 | 23× | if name.is_empty() || n <= 0 || name.contains('/') { |
| 554 | 2× | return Err(bad()); |
| 555 | 21× | } |
| 556 | 21× | Ok((name.to_string(), n)) |
| 557 | 26× | } |
| 558 | |
|
| 559 | |
|
| 560 | |
|
| 561 | |
|
| 562 | | #[allow(clippy::too_many_arguments)] |
| 563 | 22× | pub async fn add_card( |
| 564 | 22× | app: &Shared, |
| 565 | 22× | account: Option<&db::accounts::Account>, |
| 566 | 22× | owner: &str, |
| 567 | 22× | project_id: i64, |
| 568 | 22× | column_id: i64, |
| 569 | 22× | reference: &str, |
| 570 | 22× | repo: &str, |
| 571 | 22× | note: &str, |
| 572 | 22× | actor: &str, |
| 573 | 22× | ) -> sqlx::Result<Result<pdb::Card, String>> { |
| 574 | 22× | if !note.trim().is_empty() { |
| 575 | 4× | let note: String = note.trim().chars().take(500).collect(); |
| 576 | 4× | pdb::add_card( |
| 577 | 4× | &app.db, |
| 578 | 4× | project_id, |
| 579 | 4× | pdb::NewCard { column_id, kind: "note", repo_id: None, number: None, note: ¬e, added_by: actor }, |
| 580 | 4× | ) |
| 581 | 4× | .await?; |
| 582 | 4× | return Ok(Ok(newest_card(app, project_id).await?)); |
| 583 | 18× | } |
| 584 | 18× | let (repo, n) = match parse_reference(reference, repo, owner) { |
| 585 | 17× | Ok(r) => r, |
| 586 | 1× | Err(why) => return Ok(Err(why)), |
| 587 | | }; |
| 588 | 17× | let Some(row) = db::repos::get(&app.db, owner, &repo).await? else { return Ok(Err(format!("{owner} has no repository {repo}."))) }; |
| 589 | 16× | let role = roles::role_for(&app.db, account, &row).await?; |
| 590 | 16× | let (issues, proposals) = db::issues::numbers(&app.db, row.id).await?; |
| 591 | 16× | let kind = if issues.contains(&n) { |
| 592 | 13× | "issue" |
| 593 | 3× | } else if proposals.contains(&n) { |
| 594 | 2× | "proposal" |
| 595 | | } else { |
| 596 | 1× | return Ok(Err(format!("{repo} has no issue or proposal #{n}."))); |
| 597 | | }; |
| 598 | 15× | if !roles::allows(role, Access::Read) { |
| 599 | 0× | return Ok(Err(format!("{repo} has no issue or proposal #{n}."))); |
| 600 | 15× | } |
| 601 | 15× | if !pdb::add_card( |
| 602 | 15× | &app.db, |
| 603 | 15× | project_id, |
| 604 | 15× | pdb::NewCard { column_id, kind, repo_id: Some(row.id), number: Some(n), note: "", added_by: actor }, |
| 605 | 15× | ) |
| 606 | 15× | .await? |
| 607 | | { |
| 608 | 4× | return Ok(Err(format!("{repo}#{n} is already on this project."))); |
| 609 | 11× | } |
| 610 | 11× | Ok(Ok(newest_card(app, project_id).await?)) |
| 611 | 22× | } |
| 612 | |
|
| 613 | 15× | async fn newest_card(app: &Shared, project_id: i64) -> sqlx::Result<pdb::Card> { |
| 614 | 15× | sqlx::query_as("SELECT * FROM project_cards WHERE project_id = $1 ORDER BY id DESC LIMIT 1").bind(project_id).fetch_one(&app.db).await |
| 615 | 15× | } |
| 616 | |
|
| 617 | 31× | pub async fn act( |
| 618 | 31× | State(app): State<Shared>, |
| 619 | 31× | viewer: Viewer, |
| 620 | 31× | Path((owner, number)): Path<(String, i64)>, |
| 621 | 31× | Form(f): Form<BoardForm>, |
| 622 | 31× | ) -> Result<Response, Error> { |
| 623 | 31× | let shell = Shell::new(&app, &format!("/projects/{owner}/{number}"), &viewer); |
| 624 | 31× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 625 | 31× | let r = rights(&app, &shell, &viewer, &owner).await?; |
| 626 | 31× | let e = |e| Error::from_db(shell.clone(), e); |
| 627 | 31× | let project = pdb::get(&app.db, &owner, number) |
| 628 | 31× | .await |
| 629 | 31× | .map_err(e)? |
| 630 | 31× | .filter(|p| p.public || r.member) |
| 631 | 31× | .ok_or_else(|| Error::not_found(shell.clone()))?; |
| 632 | 31× | if !r.edit { |
| 633 | 1× | return Err(Error::not_found(shell)); |
| 634 | 30× | } |
| 635 | 30× | if project.closed && !matches!(f.action.as_str(), "settings" | "delete" | "transfer") { |
| 636 | 1× | return Ok(board( |
| 637 | 1× | &app, |
| 638 | 1× | shell, |
| 639 | 1× | &viewer, |
| 640 | 1× | &owner, |
| 641 | 1× | number, |
| 642 | 1× | None, |
| 643 | 1× | Some("This project is closed; reopen it to change it.".into()), |
| 644 | 1× | &BoardQuery::default(), |
| 645 | 1× | ) |
| 646 | 1× | .await? |
| 647 | 1× | .into_response()); |
| 648 | 29× | } |
| 649 | 29× | let actor = viewer.account().map(|a| a.username.clone()).unwrap_or_default(); |
| 650 | 29× | let columns = pdb::columns(&app.db, project.id).await.map_err(e)?; |
| 651 | 41× | let column = f.column.filter(|c| columns.iter().any(|x| x.id == *c)).or(columns.first().map(|c| c.id)); |
| 652 | 29× | let mut notice = None; |
| 653 | 29× | let mut error = None; |
| 654 | 29× | match f.action.as_str() { |
| 655 | 29× | "add" => { |
| 656 | 11× | let Some(column_id) = column else { return Err(Error::bad(shell, "no column")) }; |
| 657 | 3× | if let Err(msg) = |
| 658 | 11× | add_card(&app, viewer.account(), &owner, project.id, column_id, &f.reference, &f.repo, &f.note, &actor).await.map_err(e)? |
| 659 | 3× | { |
| 660 | 3× | error = Some(msg); |
| 661 | 8× | } |
| 662 | | } |
| 663 | 18× | "move" => { |
| 664 | 1× | if let (Some(card), Some(col)) = (f.card, f.column) { |
| 665 | 1× | pdb::move_card(&app.db, project.id, card, col).await.map_err(e)?; |
| 666 | 0× | } |
| 667 | | } |
| 668 | 17× | "up" | "down" => { |
| 669 | 0× | if let Some(card) = f.card { |
| 670 | 0× | pdb::shift_card(&app.db, project.id, card, if f.action == "up" { -1 } else { 1 }).await.map_err(e)?; |
| 671 | 0× | } |
| 672 | | } |
| 673 | 17× | "remove" => { |
| 674 | 0× | if let Some(card) = f.card { |
| 675 | 0× | pdb::remove_card(&app.db, project.id, card).await.map_err(e)?; |
| 676 | 0× | } |
| 677 | | } |
| 678 | 17× | "archive" => { |
| 679 | 1× | if let Some(card) = f.card { |
| 680 | 1× | pdb::archive_cards(&app.db, project.id, &[card]).await.map_err(e)?; |
| 681 | 0× | } |
| 682 | | } |
| 683 | 16× | "restore" => { |
| 684 | 1× | if let Some(card) = f.card { |
| 685 | 1× | pdb::restore_card(&app.db, project.id, card).await.map_err(e)?; |
| 686 | 0× | } |
| 687 | 1× | return Ok(Redirect::to(&format!("/projects/{owner}/{number}?archived=1")).into_response()); |
| 688 | | } |
| 689 | 15× | "takes_finished" => { |
| 690 | 2× | let Some(col) = f.column else { return Err(Error::bad(shell, "no column")) }; |
| 691 | 5× | let name = columns.iter().find(|c| c.id == col).map(|c| c.name.clone()).unwrap_or_default(); |
| 692 | 2× | let on = f.mode == "on"; |
| 693 | 2× | pdb::set_takes_finished(&app.db, project.id, on.then_some(col)).await.map_err(e)?; |
| 694 | 2× | notice = Some(if on { |
| 695 | 1× | format!("Cards move to {name} when their issue closes or their proposal lands or closes.") |
| 696 | | } else { |
| 697 | 1× | "Cards stay where they are when their issue closes or their proposal lands.".into() |
| 698 | | }); |
| 699 | | } |
| 700 | 13× | "archive_older" | "archive_after" => { |
| 701 | 5× | let Some(col) = f.column else { return Err(Error::bad(shell, "no column")) }; |
| 702 | 5× | let days = match (f.action.as_str(), f.mode.as_str(), f.days.trim()) { |
| 703 | 5× | ("archive_after", "finished", _) | ("archive_after", "", "") => None, |
| 704 | 1× | ("archive_after", "never", _) => Some(pdb::NEVER), |
| 705 | 3× | (_, _, d) => match d.parse::<i64>() { |
| 706 | 2× | Ok(n) if (0..=3650).contains(&n) => Some(n), |
| 707 | | _ => { |
| 708 | 1× | error = Some("Give a number of days, 0 to 3650.".into()); |
| 709 | 1× | None |
| 710 | | } |
| 711 | | }, |
| 712 | | }; |
| 713 | 15× | let name = columns.iter().find(|c| c.id == col).map(|c| c.name.clone()).unwrap_or_default(); |
| 714 | 5× | if error.is_none() && f.action == "archive_after" { |
| 715 | 3× | pdb::set_archive_after(&app.db, project.id, col, days).await.map_err(e)?; |
| 716 | 3× | notice = Some(match days { |
| 717 | 1× | Some(pdb::NEVER) => format!("{name} keeps its cards until you archive them."), |
| 718 | 1× | Some(n) => format!("{name} archives its cards {n} day{} after they reach it.", if n == 1 { "" } else { "s" }), |
| 719 | 1× | None => format!("{name} archives finished cards {} days after they reach it.", pdb::FINISHED_KEPT_DAYS), |
| 720 | | }); |
| 721 | 2× | } else if let (None, Some(days)) = (&error, days) { |
| 722 | 1× | let now = db::now_ms(); |
| 723 | 1× | let ids: Vec<i64> = pdb::cards(&app.db, project.id) |
| 724 | 1× | .await |
| 725 | 1× | .map_err(e)? |
| 726 | 1× | .into_iter() |
| 727 | 4× | .filter(|c| c.column_id == col && c.archived_ms.is_none() && pdb::older_than(c, days, now)) |
| 728 | 1× | .map(|c| c.id) |
| 729 | 1× | .collect(); |
| 730 | 1× | let n = pdb::archive_cards(&app.db, project.id, &ids).await.map_err(e)?; |
| 731 | 1× | let detail = format!("{n} card(s) of {name} older than {days} days"); |
| 732 | 1× | db::accounts::audit(&app.db, viewer.account(), "project.cards_archived", &format!("{owner}/{number}"), &detail).await.ok(); |
| 733 | 1× | notice = Some(format!("Archived {n} card{} from {name}.", if n == 1 { "" } else { "s" })); |
| 734 | 1× | } |
| 735 | | } |
| 736 | 8× | "add_column" => { |
| 737 | 0× | let name = f.name.trim(); |
| 738 | 0× | if name.is_empty() || name.len() > 40 { |
| 739 | 0× | error = Some("A column has a name of up to 40 characters.".into()); |
| 740 | 0× | } else { |
| 741 | 0× | pdb::add_column(&app.db, project.id, name).await.map_err(e)?; |
| 742 | | } |
| 743 | | } |
| 744 | 8× | "rename_column" => { |
| 745 | 0× | if let (Some(col), name) = (f.column, f.name.trim()) { |
| 746 | 0× | if !name.is_empty() && name.len() <= 40 { |
| 747 | 0× | pdb::rename_column(&app.db, project.id, col, name).await.map_err(e)?; |
| 748 | 0× | } |
| 749 | 0× | } |
| 750 | | } |
| 751 | 8× | "left" | "right" => { |
| 752 | 0× | if let Some(col) = f.column { |
| 753 | 0× | pdb::shift_column(&app.db, project.id, col, if f.action == "left" { -1 } else { 1 }).await.map_err(e)?; |
| 754 | 0× | } |
| 755 | | } |
| 756 | 8× | "remove_column" => { |
| 757 | 3× | if let Some(col) = f.column { |
| 758 | 3× | if !pdb::remove_column(&app.db, project.id, col).await.map_err(e)? { |
| 759 | 1× | error = Some("A project keeps at least one column.".into()); |
| 760 | 2× | } |
| 761 | 0× | } |
| 762 | | } |
| 763 | 5× | "settings" => { |
| 764 | 2× | let title = if f.title.trim().is_empty() { project.title.clone() } else { f.title.trim().chars().take(100).collect() }; |
| 765 | 2× | pdb::set(&app.db, project.id, &title, f.description.trim(), f.private.is_none(), f.closed.is_some()).await.map_err(e)?; |
| 766 | 2× | notice = Some("Saved.".into()); |
| 767 | | } |
| 768 | 3× | "transfer" => { |
| 769 | 2× | let to = f.to.trim(); |
| 770 | 2× | match rights_of(&app, viewer.account(), to).await.map_err(e)? { |
| 771 | 2× | _ if to == owner => error = Some(format!("It is {owner}'s already.")), |
| 772 | 1× | Some(t) if t.edit => { |
| 773 | 1× | let moved = pdb::transfer(&app.db, project.id, to).await.map_err(e)?; |
| 774 | 1× | let detail = format!("{owner}/{number} to {to}/{}", moved.number); |
| 775 | 1× | db::accounts::audit(&app.db, viewer.account(), "project.transferred", &detail, &project.title).await.ok(); |
| 776 | 1× | return Ok(Redirect::to(&format!("/projects/{to}/{}", moved.number)).into_response()); |
| 777 | | } |
| 778 | 1× | _ => error = Some(format!("You cannot make projects under {to}, so you cannot move this one there.")), |
| 779 | | } |
| 780 | | } |
| 781 | 1× | "delete" => { |
| 782 | 1× | pdb::delete(&app.db, project.id).await.map_err(e)?; |
| 783 | 1× | db::accounts::audit(&app.db, viewer.account(), "project.deleted", &format!("{owner}/{number}"), &project.title).await.ok(); |
| 784 | 1× | return Ok(Redirect::to(&format!("/projects/{owner}")).into_response()); |
| 785 | | } |
| 786 | 0× | _ => return Err(Error::bad(shell, "unknown action")), |
| 787 | | } |
| 788 | 26× | if notice.is_none() && error.is_none() { |
| 789 | | |
| 790 | 12× | return Ok(Redirect::to(&format!("/projects/{owner}/{number}")).into_response()); |
| 791 | 14× | } |
| 792 | 14× | Ok(board(&app, shell, &viewer, &owner, number, notice, error, &BoardQuery::default()).await?.into_response()) |
| 793 | 31× | } |
| 794 | |
|
| 795 | |
|
| 796 | |
|
| 797 | 104× | pub async fn placements_for(app: &Shared, viewer: &Viewer, repo: &db::repos::RepoRow, number: i64) -> Vec<(pdb::Project, String)> { |
| 798 | 104× | let Ok(all) = pdb::placements(&app.db, repo.id, number).await else { return vec![] }; |
| 799 | 104× | let mut out = Vec::new(); |
| 800 | 104× | for (p, col) in all { |
| 801 | 5× | let visible = p.public |
| 802 | 1× | || viewer.is_admin() |
| 803 | 1× | || match viewer.account() { |
| 804 | 0× | Some(v) if v.username == p.owner => true, |
| 805 | 0× | Some(v) => match db::accounts::org_by_name(&app.db, &p.owner).await.ok().flatten() { |
| 806 | 0× | Some(o) => db::accounts::org_role(&app.db, o.id, v.id).await.ok().flatten().is_some(), |
| 807 | 0× | None => false, |
| 808 | | }, |
| 809 | 1× | None => false, |
| 810 | | }; |
| 811 | 5× | if visible { |
| 812 | 4× | out.push((p, col)); |
| 813 | 4× | } |
| 814 | | } |
| 815 | 104× | out |
| 816 | 104× | } |
| 817 | |
|
| 818 | | #[cfg(test)] |
| 819 | | mod tests { |
| 820 | | use super::parse_reference; |
| 821 | |
|
| 822 | | #[test] |
| 823 | 1× | fn references() { |
| 824 | 7× | let p = |r: &str, repo: &str| parse_reference(r, repo, "andy").ok(); |
| 825 | 1× | assert_eq!(p("oxygen#12", ""), Some(("oxygen".into(), 12))); |
| 826 | 1× | assert_eq!(p("#7", "hub"), Some(("hub".into(), 7))); |
| 827 | 1× | assert_eq!(p(" hub # 3 ", ""), Some(("hub".into(), 3))); |
| 828 | 1× | assert_eq!(p("#7", ""), None, "a repository is needed"); |
| 829 | 1× | assert_eq!(p("andy/hub#4", ""), Some(("hub".into(), 4)), "the board's own owner, as it shows cards"); |
| 830 | 1× | assert!(parse_reference("other/repo#1", "", "andy").unwrap_err().contains("only andy's"), "another owner is refused, saying why"); |
| 831 | 1× | assert_eq!(p("hub#0", ""), None); |
| 832 | 1× | assert_eq!(p("hub", ""), None); |
| 833 | 1× | } |
| 834 | | } |
| 835 | |
|
| 836 | | #[derive(Deserialize)] |
| 837 | | pub struct PlaceForm { |
| 838 | | #[serde(rename = "_csrf")] |
| 839 | | csrf_token: String, |
| 840 | | |
| 841 | | project: i64, |
| 842 | | |
| 843 | | action: String, |
| 844 | | } |
| 845 | |
|
| 846 | |
|
| 847 | |
|
| 848 | 2× | async fn place( |
| 849 | 2× | app: Shared, |
| 850 | 2× | viewer: Viewer, |
| 851 | 2× | owner: String, |
| 852 | 2× | name: String, |
| 853 | 2× | kind: &'static str, |
| 854 | 2× | number: i64, |
| 855 | 2× | f: PlaceForm, |
| 856 | 2× | ) -> Result<Response, Error> { |
| 857 | 2× | let page = format!("/{owner}/{name}/{kind}/{number}"); |
| 858 | 2× | let shell = Shell::new(&app, &page, &viewer); |
| 859 | 2× | csrf(&viewer, &f.csrf_token, &shell)?; |
| 860 | 2× | let (_, row, _) = super::lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 861 | 2× | let r = rights(&app, &shell, &viewer, &row.owner).await?; |
| 862 | 2× | if !r.edit { |
| 863 | 0× | return Err(Error::not_found(shell)); |
| 864 | 2× | } |
| 865 | 2× | let e = |e| Error::from_db(shell.clone(), e); |
| 866 | 2× | let project = pdb::get(&app.db, &row.owner, f.project).await.map_err(e)?.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 867 | 2× | let actor = viewer.account().map(|a| a.username.clone()).unwrap_or_default(); |
| 868 | 2× | let mut error = None; |
| 869 | 2× | match f.action.as_str() { |
| 870 | 2× | "add" => { |
| 871 | 1× | let columns = pdb::columns(&app.db, project.id).await.map_err(e)?; |
| 872 | 1× | let Some(first) = columns.first() else { return Err(Error::bad(shell, "that project has no columns")) }; |
| 873 | 0× | if let Err(msg) = |
| 874 | 1× | add_card(&app, viewer.account(), &row.owner, project.id, first.id, &format!("{}#{number}", row.name), &row.name, "", &actor) |
| 875 | 1× | .await |
| 876 | 1× | .map_err(e)? |
| 877 | 0× | { |
| 878 | 0× | error = Some(msg); |
| 879 | 1× | } |
| 880 | | } |
| 881 | 1× | "remove" => { |
| 882 | 1× | for c in pdb::cards(&app.db, project.id).await.map_err(e)? { |
| 883 | 1× | if c.repo_id == Some(row.id) && c.number == Some(number) { |
| 884 | 1× | pdb::remove_card(&app.db, project.id, c.id).await.map_err(e)?; |
| 885 | 0× | } |
| 886 | | } |
| 887 | | } |
| 888 | 0× | _ => return Err(Error::bad(shell, "add or remove")), |
| 889 | | } |
| 890 | 2× | let to = match error { |
| 891 | 0× | Some(m) => format!("{page}?error={}", super::settings::urlencode(&m)), |
| 892 | 2× | None => page, |
| 893 | | }; |
| 894 | 2× | Ok(Redirect::to(&to).into_response()) |
| 895 | 2× | } |
| 896 | |
|
| 897 | |
|
| 898 | 2× | pub async fn place_proposal( |
| 899 | 2× | State(app): State<Shared>, |
| 900 | 2× | viewer: Viewer, |
| 901 | 2× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 902 | 2× | Form(f): Form<PlaceForm>, |
| 903 | 2× | ) -> Result<Response, Error> { |
| 904 | 2× | place(app, viewer, owner, name, "proposal", number, f).await |
| 905 | 2× | } |
| 906 | |
|
| 907 | |
|
| 908 | 0× | pub async fn place_issue( |
| 909 | 0× | State(app): State<Shared>, |
| 910 | 0× | viewer: Viewer, |
| 911 | 0× | Path((owner, name, number)): Path<(String, String, i64)>, |
| 912 | 0× | Form(f): Form<PlaceForm>, |
| 913 | 0× | ) -> Result<Response, Error> { |
| 914 | 0× | place(app, viewer, owner, name, "issue", number, f).await |
| 915 | 0× | } |
| 916 | |
|
| 917 | |
|
| 918 | |
|
| 919 | 104× | pub async fn project_choices( |
| 920 | 104× | app: &Shared, |
| 921 | 104× | viewer: &Viewer, |
| 922 | 104× | repo: &db::repos::RepoRow, |
| 923 | 104× | placed: &[(pdb::Project, String)], |
| 924 | 104× | ) -> Option<Vec<pdb::Project>> { |
| 925 | 104× | let Ok(Some(r)) = rights_of(app, viewer.account(), &repo.owner).await else { return None }; |
| 926 | 104× | if !r.edit { |
| 927 | 46× | return None; |
| 928 | 58× | } |
| 929 | | Some( |
| 930 | 58× | pdb::of_owner(&app.db, &repo.owner) |
| 931 | 58× | .await |
| 932 | 58× | .unwrap_or_default() |
| 933 | 58× | .into_iter() |
| 934 | 58× | .filter(|p| !p.closed && !placed.iter().any(|(q, _)| q.id == p.id)) |
| 935 | 58× | .collect(), |
| 936 | | ) |
| 937 | 104× | } |