| 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 |
pub async fn rights_of(app: &Shared, account: Option<&db::accounts::Account>, owner: &str) -> sqlx::Result<Option<Rights>> { |
| 25 |
let admin = account.is_some_and(|a| a.is_admin); |
| 26 |
if let Some(a) = db::accounts::account_by_name(&app.db, owner).await? { |
| 27 |
let me = account.is_some_and(|v| v.id == a.id); |
| 28 |
return Ok(Some(Rights { member: me || admin, edit: me || admin })); |
| 29 |
} |
| 30 |
if let Some(o) = db::accounts::org_by_name(&app.db, owner).await? { |
| 31 |
let member = match account { |
| 32 |
Some(v) => db::accounts::org_role(&app.db, o.id, v.id).await?.is_some(), |
| 33 |
None => false, |
| 34 |
}; |
| 35 |
return Ok(Some(Rights { member: member || admin, edit: member || admin })); |
| 36 |
} |
| 37 |
Ok(None) |
| 38 |
} |
| 39 |
|
| 40 |
async fn rights(app: &Shared, shell: &Shell, viewer: &Viewer, owner: &str) -> Result<Rights, Error> { |
| 41 |
rights_of(app, viewer.account(), owner) |
| 42 |
.await |
| 43 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 44 |
.ok_or_else(|| Error::not_found(shell.clone())) |
| 45 |
} |
| 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 |
fn build( |
| 71 |
shell: Shell, |
| 72 |
owner: String, |
| 73 |
all: Vec<pdb::Project>, |
| 74 |
can_edit: bool, |
| 75 |
error: Option<String>, |
| 76 |
orgs: Vec<String>, |
| 77 |
q: Option<String>, |
| 78 |
closed: bool, |
| 79 |
) -> ProjectsPage { |
| 80 |
let q = q.unwrap_or_default().trim().to_string(); |
| 81 |
let needle = q.to_lowercase(); |
| 82 |
let matching: Vec<pdb::Project> = all |
| 83 |
.into_iter() |
| 84 |
.filter(|p| needle.is_empty() || p.title.to_lowercase().contains(&needle) || p.description.to_lowercase().contains(&needle)) |
| 85 |
.collect(); |
| 86 |
let closed_count = matching.iter().filter(|p| p.closed).count(); |
| 87 |
let open_count = matching.len() - closed_count; |
| 88 |
let projects = matching.into_iter().filter(|p| p.closed == closed).collect(); |
| 89 |
ProjectsPage { shell, owner, projects, can_edit, error, orgs, q, closed, open_count, closed_count } |
| 90 |
} |
| 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 |
async fn own_orgs(app: &Shared, viewer: &Viewer, owner: &str) -> Vec<String> { |
| 104 |
match viewer.account() { |
| 105 |
Some(a) if a.username == owner => { |
| 106 |
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 |
_ => vec![], |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
pub async fn list( |
| 113 |
State(app): State<Shared>, |
| 114 |
viewer: Viewer, |
| 115 |
Path(owner): Path<String>, |
| 116 |
Query(query): Query<ProjectsQuery>, |
| 117 |
) -> Result<Page<ProjectsPage>, Error> { |
| 118 |
let shell = Shell::new(&app, &format!("/projects/{owner}"), &viewer); |
| 119 |
let r = rights(&app, &shell, &viewer, &owner).await?; |
| 120 |
let projects = pdb::of_owner(&app.db, &owner) |
| 121 |
.await |
| 122 |
.map_err(|e| Error::from_db(shell.clone(), e))? |
| 123 |
.into_iter() |
| 124 |
.filter(|p| p.public || r.member) |
| 125 |
.collect(); |
| 126 |
let orgs = own_orgs(&app, &viewer, &owner).await; |
| 127 |
let closed = query.state.as_deref() == Some("closed"); |
| 128 |
Ok(Page(ProjectsPage::build(shell, owner, projects, r.edit, None, orgs, query.q, closed))) |
| 129 |
} |
| 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 |
pub async fn create( |
| 143 |
State(app): State<Shared>, |
| 144 |
viewer: Viewer, |
| 145 |
Path(owner): Path<String>, |
| 146 |
Form(f): Form<NewProjectForm>, |
| 147 |
) -> Result<Response, Error> { |
| 148 |
let shell = Shell::new(&app, &format!("/projects/{owner}"), &viewer); |
| 149 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 150 |
let r = rights(&app, &shell, &viewer, &owner).await?; |
| 151 |
if !r.edit { |
| 152 |
return Err(Error::not_found(shell)); |
| 153 |
} |
| 154 |
let title = f.title.trim(); |
| 155 |
if title.is_empty() || title.len() > 100 { |
| 156 |
let projects = pdb::of_owner(&app.db, &owner).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 157 |
let orgs = own_orgs(&app, &viewer, &owner).await; |
| 158 |
return Ok(Page(ProjectsPage::build( |
| 159 |
shell, |
| 160 |
owner, |
| 161 |
projects, |
| 162 |
true, |
| 163 |
Some("A project needs a title of up to 100 characters.".into()), |
| 164 |
orgs, |
| 165 |
None, |
| 166 |
false, |
| 167 |
)) |
| 168 |
.into_response()); |
| 169 |
} |
| 170 |
let p = pdb::create(&app.db, &owner, title, f.description.trim(), f.private.is_none()) |
| 171 |
.await |
| 172 |
.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 173 |
db::accounts::audit(&app.db, viewer.account(), "project.created", &format!("{owner}/{}", p.number), title).await.ok(); |
| 174 |
Ok(Redirect::to(&format!("/projects/{owner}/{}", p.number)).into_response()) |
| 175 |
} |
| 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 |
#[serde(default)] |
| 227 |
iteration: Option<i64>, |
| 228 |
} |
| 229 |
|
| 230 |
impl BoardQuery { |
| 231 |
fn text(&self) -> String { |
| 232 |
self.q.as_deref().map(str::trim).unwrap_or("").to_string() |
| 233 |
} |
| 234 |
fn state(&self) -> String { |
| 235 |
self.state.as_deref().map(str::trim).unwrap_or("").to_ascii_lowercase() |
| 236 |
} |
| 237 |
fn archived(&self) -> bool { |
| 238 |
self.archived.as_deref().is_some_and(|a| !a.is_empty() && a != "0") |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
#[derive(Default)] |
| 244 |
struct CardFilter { |
| 245 |
labels: Vec<String>, |
| 246 |
assignees: Vec<String>, |
| 247 |
state: String, |
| 248 |
words: Vec<String>, |
| 249 |
} |
| 250 |
|
| 251 |
impl CardFilter { |
| 252 |
fn of(view: &BoardQuery) -> CardFilter { |
| 253 |
let mut f = CardFilter { state: view.state(), ..Default::default() }; |
| 254 |
for tok in view.text().split_whitespace() { |
| 255 |
match tok.split_once(':') { |
| 256 |
Some(("label", v)) if !v.is_empty() => f.labels.push(v.to_lowercase()), |
| 257 |
Some(("assignee", v)) if !v.is_empty() => f.assignees.push(v.trim_start_matches('@').to_lowercase()), |
| 258 |
Some(("state", v)) if !v.is_empty() => f.state = v.to_lowercase(), |
| 259 |
_ => f.words.push(tok.to_lowercase()), |
| 260 |
} |
| 261 |
} |
| 262 |
f |
| 263 |
} |
| 264 |
|
| 265 |
fn is_empty(&self) -> bool { |
| 266 |
self.labels.is_empty() && self.assignees.is_empty() && self.state.is_empty() && self.words.is_empty() |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
fn keeps(&self, c: &CardView) -> bool { |
| 272 |
let (title, reference) = (c.title.to_lowercase(), c.reference.to_lowercase()); |
| 273 |
self.words.iter().all(|w| title.contains(w) || reference.contains(w)) |
| 274 |
&& self.labels.iter().all(|l| c.labels.iter().any(|x| x.name.eq_ignore_ascii_case(l))) |
| 275 |
&& self.assignees.iter().all(|a| c.assignees.iter().any(|x| x.eq_ignore_ascii_case(a))) |
| 276 |
&& (self.state.is_empty() || c.state.eq_ignore_ascii_case(&self.state)) |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
fn shape(columns: &mut [ColumnView], view: &BoardQuery) { |
| 282 |
let filter = CardFilter::of(view); |
| 283 |
for col in columns.iter_mut() { |
| 284 |
col.total = col.cards.len(); |
| 285 |
if !filter.is_empty() { |
| 286 |
col.cards.retain(|c| filter.keeps(c)); |
| 287 |
} else if col.cards.len() > COLUMN_SHOWN && view.all != Some(col.column.id) && !view.archived() && view.iteration.is_none() { |
| 288 |
|
| 289 |
let cut = col.cards.len() - COLUMN_SHOWN; |
| 290 |
col.cards.drain(..cut); |
| 291 |
} |
| 292 |
col.hidden = col.total - col.cards.len(); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
pub struct IterationView { |
| 298 |
pub it: pdb::Iteration, |
| 299 |
|
| 300 |
pub first: String, |
| 301 |
pub last: String, |
| 302 |
pub days: i64, |
| 303 |
|
| 304 |
pub so_far: usize, |
| 305 |
|
| 306 |
pub done: usize, |
| 307 |
|
| 308 |
|
| 309 |
pub points: String, |
| 310 |
pub top: usize, |
| 311 |
} |
| 312 |
|
| 313 |
pub const BURN_W: i64 = 280; |
| 314 |
pub const BURN_H: i64 = 64; |
| 315 |
|
| 316 |
|
| 317 |
const PAST_SHOWN: usize = 12; |
| 318 |
|
| 319 |
const DAY_MS: i64 = 86_400_000; |
| 320 |
|
| 321 |
|
| 322 |
fn day_ms(s: &str) -> Option<i64> { |
| 323 |
let mut parts = s.trim().splitn(3, '-').map(|p| p.parse::<i32>().ok()); |
| 324 |
let (y, m, d) = (parts.next()??, parts.next()??, parts.next()??); |
| 325 |
let month = time::Month::try_from(u8::try_from(m).ok()?).ok()?; |
| 326 |
let date = time::Date::from_calendar_date(y, month, u8::try_from(d).ok()?).ok()?; |
| 327 |
Some(date.midnight().assume_utc().unix_timestamp() * 1000) |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
fn day_of(ms: i64) -> String { |
| 332 |
let t = time::OffsetDateTime::from_unix_timestamp(ms.div_euclid(1000)).unwrap_or(time::OffsetDateTime::UNIX_EPOCH); |
| 333 |
format!("{:04}-{:02}-{:02}", t.year(), u8::from(t.month()), t.day()) |
| 334 |
} |
| 335 |
|
| 336 |
async fn iteration_view(app: &Shared, it: pdb::Iteration, done: Option<i64>, now: i64) -> sqlx::Result<IterationView> { |
| 337 |
let times = pdb::done_times(&app.db, &it, done).await?; |
| 338 |
let days = pdb::iteration_days(&it); |
| 339 |
let up = pdb::burn_up(×, it.start_ms, it.end_ms, it.closed_ms.unwrap_or(now)); |
| 340 |
let top = up.last().copied().unwrap_or(0).max(1); |
| 341 |
|
| 342 |
let points = std::iter::once((0, 0)) |
| 343 |
.chain(up.iter().enumerate().map(|(d, &n)| (d as i64 + 1, n))) |
| 344 |
.map(|(d, n)| format!("{},{}", d * BURN_W / days, BURN_H - n as i64 * BURN_H / top as i64)) |
| 345 |
.collect::<Vec<_>>() |
| 346 |
.join(" "); |
| 347 |
Ok(IterationView { |
| 348 |
first: day_of(it.start_ms), |
| 349 |
last: day_of(it.end_ms - 1), |
| 350 |
days, |
| 351 |
so_far: up.len(), |
| 352 |
done: times.len(), |
| 353 |
points, |
| 354 |
top, |
| 355 |
it, |
| 356 |
}) |
| 357 |
} |
| 358 |
|
| 359 |
#[derive(Template)] |
| 360 |
#[template(path = "project.html")] |
| 361 |
pub struct BoardPage { |
| 362 |
shell: Shell, |
| 363 |
project: pdb::Project, |
| 364 |
columns: Vec<ColumnView>, |
| 365 |
|
| 366 |
repos: Vec<String>, |
| 367 |
|
| 368 |
can_edit: bool, |
| 369 |
|
| 370 |
can_manage: bool, |
| 371 |
notice: Option<String>, |
| 372 |
error: Option<String>, |
| 373 |
|
| 374 |
|
| 375 |
transfer_to: Vec<String>, |
| 376 |
|
| 377 |
q: String, |
| 378 |
state: String, |
| 379 |
filtered: bool, |
| 380 |
|
| 381 |
archived: bool, |
| 382 |
|
| 383 |
archived_total: usize, |
| 384 |
|
| 385 |
|
| 386 |
current: Option<IterationView>, |
| 387 |
past: Vec<IterationView>, |
| 388 |
shown_iteration: Option<IterationView>, |
| 389 |
|
| 390 |
next_first: String, |
| 391 |
next_last: String, |
| 392 |
} |
| 393 |
|
| 394 |
#[allow(clippy::too_many_arguments)] |
| 395 |
async fn board( |
| 396 |
app: &Shared, |
| 397 |
shell: Shell, |
| 398 |
viewer: &Viewer, |
| 399 |
owner: &str, |
| 400 |
number: i64, |
| 401 |
notice: Option<String>, |
| 402 |
error: Option<String>, |
| 403 |
view: &BoardQuery, |
| 404 |
) -> Result<Page<BoardPage>, Error> { |
| 405 |
let e = |e| Error::from_db(shell.clone(), e); |
| 406 |
let r = rights(app, &shell, viewer, owner).await?; |
| 407 |
let project = pdb::get(&app.db, owner, number) |
| 408 |
.await |
| 409 |
.map_err(e)? |
| 410 |
.filter(|p| p.public || r.member) |
| 411 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 412 |
let now = db::now_ms(); |
| 413 |
let iterations = pdb::iterations(&app.db, project.id).await.map_err(e)?; |
| 414 |
let shown = view.iteration.and_then(|id| iterations.iter().find(|i| i.id == id && i.closed_ms.is_some()).cloned()); |
| 415 |
|
| 416 |
let archived = view.archived() || shown.is_some(); |
| 417 |
let (mut columns, repos) = board_columns(app, viewer.account(), owner, &project, archived).await.map_err(e)?; |
| 418 |
if let Some(it) = &shown { |
| 419 |
for col in columns.iter_mut() { |
| 420 |
col.cards.retain(|c| c.card.iteration_id == Some(it.id)); |
| 421 |
} |
| 422 |
} |
| 423 |
shape(&mut columns, view); |
| 424 |
let archived_total = columns.iter().map(|c| c.archived).sum(); |
| 425 |
let done = pdb::done_column(&columns.iter().map(|c| c.column.clone()).collect::<Vec<_>>()); |
| 426 |
let (mut current, mut past) = (None, Vec::new()); |
| 427 |
for it in iterations { |
| 428 |
if it.closed_ms.is_none() { |
| 429 |
current = Some(iteration_view(app, it, done, now).await.map_err(e)?); |
| 430 |
} else if past.len() < PAST_SHOWN { |
| 431 |
past.push(iteration_view(app, it, done, now).await.map_err(e)?); |
| 432 |
} |
| 433 |
} |
| 434 |
let shown_iteration = match shown { |
| 435 |
Some(it) => Some(iteration_view(app, it, done, now).await.map_err(e)?), |
| 436 |
None => None, |
| 437 |
}; |
| 438 |
let can_edit = r.edit && !project.closed; |
| 439 |
let mut transfer_to = Vec::new(); |
| 440 |
if let (true, Some(a)) = (r.edit, viewer.account()) { |
| 441 |
transfer_to.push(a.username.clone()); |
| 442 |
let orgs = db::accounts::orgs_of(&app.db, a.id).await.map_err(e)?; |
| 443 |
transfer_to.extend(orgs.into_iter().map(|(o, _)| o.name)); |
| 444 |
transfer_to.retain(|o| o != owner); |
| 445 |
} |
| 446 |
let (q, state) = (view.text(), view.state()); |
| 447 |
let filtered = !q.is_empty() || !state.is_empty(); |
| 448 |
Ok(Page(BoardPage { |
| 449 |
shell, |
| 450 |
project, |
| 451 |
columns, |
| 452 |
repos, |
| 453 |
can_edit, |
| 454 |
can_manage: r.edit, |
| 455 |
notice, |
| 456 |
error, |
| 457 |
transfer_to, |
| 458 |
q, |
| 459 |
state, |
| 460 |
filtered, |
| 461 |
archived, |
| 462 |
archived_total, |
| 463 |
current, |
| 464 |
past, |
| 465 |
shown_iteration, |
| 466 |
next_first: day_of(now), |
| 467 |
next_last: day_of(now + 13 * DAY_MS), |
| 468 |
})) |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
pub async fn board_columns( |
| 476 |
app: &Shared, |
| 477 |
account: Option<&db::accounts::Account>, |
| 478 |
owner: &str, |
| 479 |
project: &pdb::Project, |
| 480 |
archived: bool, |
| 481 |
) -> sqlx::Result<(Vec<ColumnView>, Vec<String>)> { |
| 482 |
let mut readable: std::collections::HashMap<i64, db::repos::RepoRow> = Default::default(); |
| 483 |
for row in db::repos::list_by_owner(&app.db, owner).await? { |
| 484 |
let role = roles::role_for(&app.db, account, &row).await?; |
| 485 |
if roles::allows(role, Access::Read) { |
| 486 |
readable.insert(row.id, row); |
| 487 |
} |
| 488 |
} |
| 489 |
let all = pdb::cards(&app.db, project.id).await?; |
| 490 |
let archived_in = |column: i64| all.iter().filter(|c| c.column_id == column && c.archived_ms.is_some()).count(); |
| 491 |
let cards: Vec<&pdb::Card> = all.iter().filter(|c| c.archived_ms.is_some() == archived).collect(); |
| 492 |
|
| 493 |
|
| 494 |
let mut shown: std::collections::HashMap<(i64, bool, i64), Shown> = Default::default(); |
| 495 |
let mut repo_ids: Vec<(i64, bool)> = |
| 496 |
cards.iter().filter_map(|c| Some((c.repo_id?, c.kind == "issue"))).filter(|(rid, _)| readable.contains_key(rid)).collect(); |
| 497 |
repo_ids.sort(); |
| 498 |
repo_ids.dedup(); |
| 499 |
for (rid, issues) in repo_ids { |
| 500 |
shown.extend(shown_in(app, rid, issues).await?.into_iter().map(|(n, s)| ((rid, issues, n), s))); |
| 501 |
} |
| 502 |
let mut columns = Vec::new(); |
| 503 |
for column in pdb::columns(&app.db, project.id).await? { |
| 504 |
let mut views = Vec::new(); |
| 505 |
for card in cards.iter().copied().filter(|c| c.column_id == column.id) { |
| 506 |
match (card.kind.as_str(), card.repo_id, card.number) { |
| 507 |
("note", _, _) => views.push(CardView { |
| 508 |
card: card.clone(), |
| 509 |
reference: String::new(), |
| 510 |
url: String::new(), |
| 511 |
title: card.note.clone(), |
| 512 |
state: String::new(), |
| 513 |
labels: Vec::new(), |
| 514 |
assignees: Vec::new(), |
| 515 |
}), |
| 516 |
(kind, Some(rid), Some(n)) => { |
| 517 |
|
| 518 |
let Some(row) = readable.get(&rid) else { continue }; |
| 519 |
let Some(s) = shown.get(&(rid, kind == "issue", n)) else { continue }; |
| 520 |
let slug = format!("{}/{}", row.owner, row.name); |
| 521 |
views.push(CardView { |
| 522 |
card: card.clone(), |
| 523 |
reference: format!("{slug}#{n}"), |
| 524 |
url: format!("/{slug}/{kind}/{n}"), |
| 525 |
title: s.title.clone(), |
| 526 |
state: s.state.clone(), |
| 527 |
labels: s.labels.clone(), |
| 528 |
assignees: s.assignees.clone(), |
| 529 |
}); |
| 530 |
} |
| 531 |
_ => {} |
| 532 |
} |
| 533 |
} |
| 534 |
let total = views.len(); |
| 535 |
let archived = archived_in(column.id); |
| 536 |
let items: Vec<&CardView> = views.iter().filter(|v| v.card.kind != "note").collect(); |
| 537 |
let finished = !items.is_empty() && items.iter().all(|v| matches!(v.state.as_str(), "closed" | "landed")); |
| 538 |
columns.push(ColumnView { column, cards: views, hidden: 0, total, archived, finished }); |
| 539 |
} |
| 540 |
let mut repos: Vec<String> = readable.values().map(|r| r.name.clone()).collect(); |
| 541 |
repos.sort(); |
| 542 |
Ok((columns, repos)) |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
struct Shown { |
| 547 |
title: String, |
| 548 |
state: String, |
| 549 |
labels: Vec<idb::Label>, |
| 550 |
assignees: Vec<String>, |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
async fn shown_in(app: &Shared, repo_id: i64, issues: bool) -> sqlx::Result<Vec<(i64, Shown)>> { |
| 555 |
use std::collections::HashMap; |
| 556 |
let mut out = Vec::new(); |
| 557 |
if issues { |
| 558 |
let mut labels: HashMap<i64, Vec<idb::Label>> = HashMap::new(); |
| 559 |
for (id, l) in idb::labels_by_issue(&app.db, repo_id).await? { |
| 560 |
labels.entry(id).or_default().push(l); |
| 561 |
} |
| 562 |
let mut assignees: HashMap<i64, Vec<String>> = HashMap::new(); |
| 563 |
for (id, u) in idb::assignees_by_issue(&app.db, repo_id).await? { |
| 564 |
assignees.entry(id).or_default().push(u); |
| 565 |
} |
| 566 |
for i in idb::list(&app.db, repo_id).await? { |
| 567 |
let mut a = assignees.remove(&i.id).unwrap_or_default(); |
| 568 |
a.sort(); |
| 569 |
let s = Shown { labels: labels.remove(&i.id).unwrap_or_default(), assignees: a, title: i.title, state: i.state }; |
| 570 |
out.push((i.number, s)); |
| 571 |
} |
| 572 |
} else { |
| 573 |
let mut labels: HashMap<i64, Vec<idb::Label>> = HashMap::new(); |
| 574 |
for (id, l) in idb::labels_by_proposal(&app.db, repo_id).await? { |
| 575 |
labels.entry(id).or_default().push(l); |
| 576 |
} |
| 577 |
for p in db::proposals::list(&app.db, repo_id, &[]).await? { |
| 578 |
let s = Shown { labels: labels.remove(&p.id).unwrap_or_default(), assignees: Vec::new(), title: p.title, state: p.state }; |
| 579 |
out.push((p.number, s)); |
| 580 |
} |
| 581 |
} |
| 582 |
Ok(out) |
| 583 |
} |
| 584 |
|
| 585 |
pub async fn show( |
| 586 |
State(app): State<Shared>, |
| 587 |
viewer: Viewer, |
| 588 |
Path((owner, number)): Path<(String, i64)>, |
| 589 |
Query(view): Query<BoardQuery>, |
| 590 |
) -> Result<Response, Error> { |
| 591 |
let shell = Shell::new(&app, &format!("/projects/{owner}/{number}"), &viewer); |
| 592 |
|
| 593 |
let e = |e| Error::from_db(shell.clone(), e); |
| 594 |
if pdb::get(&app.db, &owner, number).await.map_err(e)?.is_none() { |
| 595 |
if let Some(p) = pdb::redirected(&app.db, &owner, number).await.map_err(e)? { |
| 596 |
return Ok(Redirect::permanent(&format!("/projects/{}/{}", p.owner, p.number)).into_response()); |
| 597 |
} |
| 598 |
} |
| 599 |
Ok(board(&app, shell, &viewer, &owner, number, None, None, &view).await?.into_response()) |
| 600 |
} |
| 601 |
|
| 602 |
#[derive(Deserialize)] |
| 603 |
pub struct BoardForm { |
| 604 |
#[serde(rename = "_csrf")] |
| 605 |
csrf_token: String, |
| 606 |
action: String, |
| 607 |
#[serde(default)] |
| 608 |
card: Option<i64>, |
| 609 |
#[serde(default)] |
| 610 |
column: Option<i64>, |
| 611 |
#[serde(default)] |
| 612 |
name: String, |
| 613 |
|
| 614 |
#[serde(default)] |
| 615 |
reference: String, |
| 616 |
#[serde(default)] |
| 617 |
repo: String, |
| 618 |
#[serde(default)] |
| 619 |
note: String, |
| 620 |
|
| 621 |
#[serde(default)] |
| 622 |
mode: String, |
| 623 |
#[serde(default)] |
| 624 |
title: String, |
| 625 |
#[serde(default)] |
| 626 |
description: String, |
| 627 |
#[serde(default)] |
| 628 |
private: Option<String>, |
| 629 |
#[serde(default)] |
| 630 |
closed: Option<String>, |
| 631 |
|
| 632 |
#[serde(default)] |
| 633 |
to: String, |
| 634 |
|
| 635 |
#[serde(default)] |
| 636 |
days: String, |
| 637 |
|
| 638 |
|
| 639 |
#[serde(default)] |
| 640 |
iteration: Option<i64>, |
| 641 |
#[serde(default)] |
| 642 |
start: String, |
| 643 |
#[serde(default)] |
| 644 |
end: String, |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
fn parse_reference(reference: &str, repo: &str, owner: &str) -> Result<(String, i64), String> { |
| 652 |
let bad = || "Name an issue or proposal as repo#12, or write a note.".to_string(); |
| 653 |
let r = reference.trim(); |
| 654 |
let (name, n) = r.split_once('#').ok_or_else(bad)?; |
| 655 |
let name = if name.is_empty() { repo.trim() } else { name.trim() }; |
| 656 |
let name = match name.split_once('/') { |
| 657 |
Some((o, rest)) if o.eq_ignore_ascii_case(owner) => rest, |
| 658 |
Some((o, _)) => return Err(format!("This board holds only {owner}'s issues and proposals, not {o}'s.")), |
| 659 |
None => name, |
| 660 |
}; |
| 661 |
let n: i64 = n.trim().parse().map_err(|_| bad())?; |
| 662 |
if name.is_empty() || n <= 0 || name.contains('/') { |
| 663 |
return Err(bad()); |
| 664 |
} |
| 665 |
Ok((name.to_string(), n)) |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
#[allow(clippy::too_many_arguments)] |
| 672 |
pub async fn add_card( |
| 673 |
app: &Shared, |
| 674 |
account: Option<&db::accounts::Account>, |
| 675 |
owner: &str, |
| 676 |
project_id: i64, |
| 677 |
column_id: i64, |
| 678 |
reference: &str, |
| 679 |
repo: &str, |
| 680 |
note: &str, |
| 681 |
actor: &str, |
| 682 |
) -> sqlx::Result<Result<pdb::Card, String>> { |
| 683 |
if !note.trim().is_empty() { |
| 684 |
let note: String = note.trim().chars().take(500).collect(); |
| 685 |
pdb::add_card( |
| 686 |
&app.db, |
| 687 |
project_id, |
| 688 |
pdb::NewCard { column_id, kind: "note", repo_id: None, number: None, note: ¬e, added_by: actor }, |
| 689 |
) |
| 690 |
.await?; |
| 691 |
return Ok(Ok(newest_card(app, project_id).await?)); |
| 692 |
} |
| 693 |
let (repo, n) = match parse_reference(reference, repo, owner) { |
| 694 |
Ok(r) => r, |
| 695 |
Err(why) => return Ok(Err(why)), |
| 696 |
}; |
| 697 |
let Some(row) = db::repos::get(&app.db, owner, &repo).await? else { return Ok(Err(format!("{owner} has no repository {repo}."))) }; |
| 698 |
let role = roles::role_for(&app.db, account, &row).await?; |
| 699 |
let (issues, proposals) = db::issues::numbers(&app.db, row.id).await?; |
| 700 |
let kind = if issues.contains(&n) { |
| 701 |
"issue" |
| 702 |
} else if proposals.contains(&n) { |
| 703 |
"proposal" |
| 704 |
} else { |
| 705 |
return Ok(Err(format!("{repo} has no issue or proposal #{n}."))); |
| 706 |
}; |
| 707 |
if !roles::allows(role, Access::Read) { |
| 708 |
return Ok(Err(format!("{repo} has no issue or proposal #{n}."))); |
| 709 |
} |
| 710 |
if !pdb::add_card( |
| 711 |
&app.db, |
| 712 |
project_id, |
| 713 |
pdb::NewCard { column_id, kind, repo_id: Some(row.id), number: Some(n), note: "", added_by: actor }, |
| 714 |
) |
| 715 |
.await? |
| 716 |
{ |
| 717 |
return Ok(Err(format!("{repo}#{n} is already on this project."))); |
| 718 |
} |
| 719 |
Ok(Ok(newest_card(app, project_id).await?)) |
| 720 |
} |
| 721 |
|
| 722 |
async fn newest_card(app: &Shared, project_id: i64) -> sqlx::Result<pdb::Card> { |
| 723 |
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 |
| 724 |
} |
| 725 |
|
| 726 |
pub async fn act( |
| 727 |
State(app): State<Shared>, |
| 728 |
viewer: Viewer, |
| 729 |
Path((owner, number)): Path<(String, i64)>, |
| 730 |
Form(f): Form<BoardForm>, |
| 731 |
) -> Result<Response, Error> { |
| 732 |
let shell = Shell::new(&app, &format!("/projects/{owner}/{number}"), &viewer); |
| 733 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 734 |
let r = rights(&app, &shell, &viewer, &owner).await?; |
| 735 |
let e = |e| Error::from_db(shell.clone(), e); |
| 736 |
let project = pdb::get(&app.db, &owner, number) |
| 737 |
.await |
| 738 |
.map_err(e)? |
| 739 |
.filter(|p| p.public || r.member) |
| 740 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 741 |
if !r.edit { |
| 742 |
return Err(Error::not_found(shell)); |
| 743 |
} |
| 744 |
if project.closed && !matches!(f.action.as_str(), "settings" | "delete" | "transfer") { |
| 745 |
return Ok(board( |
| 746 |
&app, |
| 747 |
shell, |
| 748 |
&viewer, |
| 749 |
&owner, |
| 750 |
number, |
| 751 |
None, |
| 752 |
Some("This project is closed; reopen it to change it.".into()), |
| 753 |
&BoardQuery::default(), |
| 754 |
) |
| 755 |
.await? |
| 756 |
.into_response()); |
| 757 |
} |
| 758 |
let actor = viewer.account().map(|a| a.username.clone()).unwrap_or_default(); |
| 759 |
let columns = pdb::columns(&app.db, project.id).await.map_err(e)?; |
| 760 |
let column = f.column.filter(|c| columns.iter().any(|x| x.id == *c)).or(columns.first().map(|c| c.id)); |
| 761 |
let mut notice = None; |
| 762 |
let mut error = None; |
| 763 |
match f.action.as_str() { |
| 764 |
"add" => { |
| 765 |
let Some(column_id) = column else { return Err(Error::bad(shell, "no column")) }; |
| 766 |
if let Err(msg) = |
| 767 |
add_card(&app, viewer.account(), &owner, project.id, column_id, &f.reference, &f.repo, &f.note, &actor).await.map_err(e)? |
| 768 |
{ |
| 769 |
error = Some(msg); |
| 770 |
} |
| 771 |
} |
| 772 |
"move" => { |
| 773 |
if let (Some(card), Some(col)) = (f.card, f.column) { |
| 774 |
pdb::move_card(&app.db, project.id, card, col).await.map_err(e)?; |
| 775 |
} |
| 776 |
} |
| 777 |
"up" | "down" => { |
| 778 |
if let Some(card) = f.card { |
| 779 |
pdb::shift_card(&app.db, project.id, card, if f.action == "up" { -1 } else { 1 }).await.map_err(e)?; |
| 780 |
} |
| 781 |
} |
| 782 |
"remove" => { |
| 783 |
if let Some(card) = f.card { |
| 784 |
pdb::remove_card(&app.db, project.id, card).await.map_err(e)?; |
| 785 |
} |
| 786 |
} |
| 787 |
"archive" => { |
| 788 |
if let Some(card) = f.card { |
| 789 |
pdb::archive_cards(&app.db, project.id, &[card]).await.map_err(e)?; |
| 790 |
} |
| 791 |
} |
| 792 |
"restore" => { |
| 793 |
if let Some(card) = f.card { |
| 794 |
pdb::restore_card(&app.db, project.id, card).await.map_err(e)?; |
| 795 |
} |
| 796 |
return Ok(Redirect::to(&format!("/projects/{owner}/{number}?archived=1")).into_response()); |
| 797 |
} |
| 798 |
"takes_finished" => { |
| 799 |
let Some(col) = f.column else { return Err(Error::bad(shell, "no column")) }; |
| 800 |
let name = columns.iter().find(|c| c.id == col).map(|c| c.name.clone()).unwrap_or_default(); |
| 801 |
let on = f.mode == "on"; |
| 802 |
pdb::set_takes_finished(&app.db, project.id, on.then_some(col)).await.map_err(e)?; |
| 803 |
notice = Some(if on { |
| 804 |
format!("Cards move to {name} when their issue closes or their proposal lands or closes.") |
| 805 |
} else { |
| 806 |
"Cards stay where they are when their issue closes or their proposal lands.".into() |
| 807 |
}); |
| 808 |
} |
| 809 |
"archive_older" | "archive_after" => { |
| 810 |
let Some(col) = f.column else { return Err(Error::bad(shell, "no column")) }; |
| 811 |
let days = match (f.action.as_str(), f.mode.as_str(), f.days.trim()) { |
| 812 |
("archive_after", "finished", _) | ("archive_after", "", "") => None, |
| 813 |
("archive_after", "never", _) => Some(pdb::NEVER), |
| 814 |
(_, _, d) => match d.parse::<i64>() { |
| 815 |
Ok(n) if (0..=3650).contains(&n) => Some(n), |
| 816 |
_ => { |
| 817 |
error = Some("Give a number of days, 0 to 3650.".into()); |
| 818 |
None |
| 819 |
} |
| 820 |
}, |
| 821 |
}; |
| 822 |
let name = columns.iter().find(|c| c.id == col).map(|c| c.name.clone()).unwrap_or_default(); |
| 823 |
if error.is_none() && f.action == "archive_after" { |
| 824 |
pdb::set_archive_after(&app.db, project.id, col, days).await.map_err(e)?; |
| 825 |
notice = Some(match days { |
| 826 |
Some(pdb::NEVER) => format!("{name} keeps its cards until you archive them."), |
| 827 |
Some(n) => format!("{name} archives its cards {n} day{} after they reach it.", if n == 1 { "" } else { "s" }), |
| 828 |
None => format!("{name} archives finished cards {} days after they reach it.", pdb::FINISHED_KEPT_DAYS), |
| 829 |
}); |
| 830 |
} else if let (None, Some(days)) = (&error, days) { |
| 831 |
let now = db::now_ms(); |
| 832 |
let ids: Vec<i64> = pdb::cards(&app.db, project.id) |
| 833 |
.await |
| 834 |
.map_err(e)? |
| 835 |
.into_iter() |
| 836 |
.filter(|c| c.column_id == col && c.archived_ms.is_none() && pdb::older_than(c, days, now)) |
| 837 |
.map(|c| c.id) |
| 838 |
.collect(); |
| 839 |
let n = pdb::archive_cards(&app.db, project.id, &ids).await.map_err(e)?; |
| 840 |
let detail = format!("{n} card(s) of {name} older than {days} days"); |
| 841 |
db::accounts::audit(&app.db, viewer.account(), "project.cards_archived", &format!("{owner}/{number}"), &detail).await.ok(); |
| 842 |
notice = Some(format!("Archived {n} card{} from {name}.", if n == 1 { "" } else { "s" })); |
| 843 |
} |
| 844 |
} |
| 845 |
"start_iteration" => { |
| 846 |
let name: String = f.name.trim().chars().take(60).collect(); |
| 847 |
match (day_ms(&f.start), day_ms(&f.end)) { |
| 848 |
_ if name.is_empty() => error = Some("Name the iteration.".into()), |
| 849 |
(Some(start), Some(last)) if last >= start && last - start < 366 * DAY_MS => { |
| 850 |
match pdb::start_iteration(&app.db, project.id, &name, start, last + DAY_MS).await.map_err(e)? { |
| 851 |
Some(_) => notice = Some(format!("{name} started.")), |
| 852 |
None => error = Some("Close the open iteration before starting another.".into()), |
| 853 |
} |
| 854 |
} |
| 855 |
(Some(_), Some(_)) => error = Some("An iteration ends on or after its first day, within a year.".into()), |
| 856 |
_ => error = Some("Give the first and last days as dates.".into()), |
| 857 |
} |
| 858 |
} |
| 859 |
"close_iteration" => { |
| 860 |
let Some(id) = f.iteration else { return Err(Error::bad(shell, "no iteration")) }; |
| 861 |
match pdb::close_iteration(&app.db, project.id, id, pdb::done_column(&columns)).await.map_err(e)? { |
| 862 |
Some(n) => { |
| 863 |
let detail = format!("iteration {id}: {n} done card(s) archived"); |
| 864 |
db::accounts::audit(&app.db, viewer.account(), "project.iteration_closed", &format!("{owner}/{number}"), &detail) |
| 865 |
.await |
| 866 |
.ok(); |
| 867 |
notice = Some(format!("Closed: {n} done card{} archived under it.", if n == 1 { "" } else { "s" })); |
| 868 |
} |
| 869 |
None => error = Some("That iteration is not open.".into()), |
| 870 |
} |
| 871 |
} |
| 872 |
"add_column" => { |
| 873 |
let name = f.name.trim(); |
| 874 |
if name.is_empty() || name.len() > 40 { |
| 875 |
error = Some("A column has a name of up to 40 characters.".into()); |
| 876 |
} else { |
| 877 |
pdb::add_column(&app.db, project.id, name).await.map_err(e)?; |
| 878 |
} |
| 879 |
} |
| 880 |
"rename_column" => { |
| 881 |
if let (Some(col), name) = (f.column, f.name.trim()) { |
| 882 |
if !name.is_empty() && name.len() <= 40 { |
| 883 |
pdb::rename_column(&app.db, project.id, col, name).await.map_err(e)?; |
| 884 |
} |
| 885 |
} |
| 886 |
} |
| 887 |
"left" | "right" => { |
| 888 |
if let Some(col) = f.column { |
| 889 |
pdb::shift_column(&app.db, project.id, col, if f.action == "left" { -1 } else { 1 }).await.map_err(e)?; |
| 890 |
} |
| 891 |
} |
| 892 |
"remove_column" => { |
| 893 |
if let Some(col) = f.column { |
| 894 |
if !pdb::remove_column(&app.db, project.id, col).await.map_err(e)? { |
| 895 |
error = Some("A project keeps at least one column.".into()); |
| 896 |
} |
| 897 |
} |
| 898 |
} |
| 899 |
"settings" => { |
| 900 |
let title = if f.title.trim().is_empty() { project.title.clone() } else { f.title.trim().chars().take(100).collect() }; |
| 901 |
pdb::set(&app.db, project.id, &title, f.description.trim(), f.private.is_none(), f.closed.is_some()).await.map_err(e)?; |
| 902 |
notice = Some("Saved.".into()); |
| 903 |
} |
| 904 |
"transfer" => { |
| 905 |
let to = f.to.trim(); |
| 906 |
match rights_of(&app, viewer.account(), to).await.map_err(e)? { |
| 907 |
_ if to == owner => error = Some(format!("It is {owner}'s already.")), |
| 908 |
Some(t) if t.edit => { |
| 909 |
let moved = pdb::transfer(&app.db, project.id, to).await.map_err(e)?; |
| 910 |
let detail = format!("{owner}/{number} to {to}/{}", moved.number); |
| 911 |
db::accounts::audit(&app.db, viewer.account(), "project.transferred", &detail, &project.title).await.ok(); |
| 912 |
return Ok(Redirect::to(&format!("/projects/{to}/{}", moved.number)).into_response()); |
| 913 |
} |
| 914 |
_ => error = Some(format!("You cannot make projects under {to}, so you cannot move this one there.")), |
| 915 |
} |
| 916 |
} |
| 917 |
"delete" => { |
| 918 |
pdb::delete(&app.db, project.id).await.map_err(e)?; |
| 919 |
db::accounts::audit(&app.db, viewer.account(), "project.deleted", &format!("{owner}/{number}"), &project.title).await.ok(); |
| 920 |
return Ok(Redirect::to(&format!("/projects/{owner}")).into_response()); |
| 921 |
} |
| 922 |
_ => return Err(Error::bad(shell, "unknown action")), |
| 923 |
} |
| 924 |
if notice.is_none() && error.is_none() { |
| 925 |
|
| 926 |
return Ok(Redirect::to(&format!("/projects/{owner}/{number}")).into_response()); |
| 927 |
} |
| 928 |
Ok(board(&app, shell, &viewer, &owner, number, notice, error, &BoardQuery::default()).await?.into_response()) |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
pub async fn placements_for(app: &Shared, viewer: &Viewer, repo: &db::repos::RepoRow, number: i64) -> Vec<(pdb::Project, String)> { |
| 934 |
let Ok(all) = pdb::placements(&app.db, repo.id, number).await else { return vec![] }; |
| 935 |
let mut out = Vec::new(); |
| 936 |
for (p, col) in all { |
| 937 |
let visible = p.public |
| 938 |
|| viewer.is_admin() |
| 939 |
|| match viewer.account() { |
| 940 |
Some(v) if v.username == p.owner => true, |
| 941 |
Some(v) => match db::accounts::org_by_name(&app.db, &p.owner).await.ok().flatten() { |
| 942 |
Some(o) => db::accounts::org_role(&app.db, o.id, v.id).await.ok().flatten().is_some(), |
| 943 |
None => false, |
| 944 |
}, |
| 945 |
None => false, |
| 946 |
}; |
| 947 |
if visible { |
| 948 |
out.push((p, col)); |
| 949 |
} |
| 950 |
} |
| 951 |
out |
| 952 |
} |
| 953 |
|
| 954 |
#[cfg(test)] |
| 955 |
mod tests { |
| 956 |
use super::parse_reference; |
| 957 |
|
| 958 |
#[test] |
| 959 |
fn references() { |
| 960 |
let p = |r: &str, repo: &str| parse_reference(r, repo, "andy").ok(); |
| 961 |
assert_eq!(p("oxygen#12", ""), Some(("oxygen".into(), 12))); |
| 962 |
assert_eq!(p("#7", "hub"), Some(("hub".into(), 7))); |
| 963 |
assert_eq!(p(" hub # 3 ", ""), Some(("hub".into(), 3))); |
| 964 |
assert_eq!(p("#7", ""), None, "a repository is needed"); |
| 965 |
assert_eq!(p("andy/hub#4", ""), Some(("hub".into(), 4)), "the board's own owner, as it shows cards"); |
| 966 |
assert!(parse_reference("other/repo#1", "", "andy").unwrap_err().contains("only andy's"), "another owner is refused, saying why"); |
| 967 |
assert_eq!(p("hub#0", ""), None); |
| 968 |
assert_eq!(p("hub", ""), None); |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
#[derive(Deserialize)] |
| 973 |
pub struct PlaceForm { |
| 974 |
#[serde(rename = "_csrf")] |
| 975 |
csrf_token: String, |
| 976 |
|
| 977 |
project: i64, |
| 978 |
|
| 979 |
action: String, |
| 980 |
} |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
async fn place( |
| 985 |
app: Shared, |
| 986 |
viewer: Viewer, |
| 987 |
owner: String, |
| 988 |
name: String, |
| 989 |
kind: &'static str, |
| 990 |
number: i64, |
| 991 |
f: PlaceForm, |
| 992 |
) -> Result<Response, Error> { |
| 993 |
let page = format!("/{owner}/{name}/{kind}/{number}"); |
| 994 |
let shell = Shell::new(&app, &page, &viewer); |
| 995 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 996 |
let (_, row, _) = super::lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 997 |
let r = rights(&app, &shell, &viewer, &row.owner).await?; |
| 998 |
if !r.edit { |
| 999 |
return Err(Error::not_found(shell)); |
| 1000 |
} |
| 1001 |
let e = |e| Error::from_db(shell.clone(), e); |
| 1002 |
let project = pdb::get(&app.db, &row.owner, f.project).await.map_err(e)?.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 1003 |
let actor = viewer.account().map(|a| a.username.clone()).unwrap_or_default(); |
| 1004 |
let mut error = None; |
| 1005 |
match f.action.as_str() { |
| 1006 |
"add" => { |
| 1007 |
let columns = pdb::columns(&app.db, project.id).await.map_err(e)?; |
| 1008 |
let Some(first) = columns.first() else { return Err(Error::bad(shell, "that project has no columns")) }; |
| 1009 |
if let Err(msg) = |
| 1010 |
add_card(&app, viewer.account(), &row.owner, project.id, first.id, &format!("{}#{number}", row.name), &row.name, "", &actor) |
| 1011 |
.await |
| 1012 |
.map_err(e)? |
| 1013 |
{ |
| 1014 |
error = Some(msg); |
| 1015 |
} |
| 1016 |
} |
| 1017 |
"remove" => { |
| 1018 |
for c in pdb::cards(&app.db, project.id).await.map_err(e)? { |
| 1019 |
if c.repo_id == Some(row.id) && c.number == Some(number) { |
| 1020 |
pdb::remove_card(&app.db, project.id, c.id).await.map_err(e)?; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
} |
| 1024 |
_ => return Err(Error::bad(shell, "add or remove")), |
| 1025 |
} |
| 1026 |
let to = match error { |
| 1027 |
Some(m) => format!("{page}?error={}", super::settings::urlencode(&m)), |
| 1028 |
None => page, |
| 1029 |
}; |
| 1030 |
Ok(Redirect::to(&to).into_response()) |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
pub async fn place_proposal( |
| 1035 |
State(app): State<Shared>, |
| 1036 |
viewer: Viewer, |
| 1037 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 1038 |
Form(f): Form<PlaceForm>, |
| 1039 |
) -> Result<Response, Error> { |
| 1040 |
place(app, viewer, owner, name, "proposal", number, f).await |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
pub async fn place_issue( |
| 1045 |
State(app): State<Shared>, |
| 1046 |
viewer: Viewer, |
| 1047 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 1048 |
Form(f): Form<PlaceForm>, |
| 1049 |
) -> Result<Response, Error> { |
| 1050 |
place(app, viewer, owner, name, "issue", number, f).await |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
pub async fn project_choices( |
| 1056 |
app: &Shared, |
| 1057 |
viewer: &Viewer, |
| 1058 |
repo: &db::repos::RepoRow, |
| 1059 |
placed: &[(pdb::Project, String)], |
| 1060 |
) -> Option<Vec<pdb::Project>> { |
| 1061 |
let Ok(Some(r)) = rights_of(app, viewer.account(), &repo.owner).await else { return None }; |
| 1062 |
if !r.edit { |
| 1063 |
return None; |
| 1064 |
} |
| 1065 |
Some( |
| 1066 |
pdb::of_owner(&app.db, &repo.owner) |
| 1067 |
.await |
| 1068 |
.unwrap_or_default() |
| 1069 |
.into_iter() |
| 1070 |
.filter(|p| !p.closed && !placed.iter().any(|(q, _)| q.id == p.id)) |
| 1071 |
.collect(), |
| 1072 |
) |
| 1073 |
} |