| e1ec9fbf Andy McCall ↶ |
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, projects as pdb}; |
| 8 |
use crate::Shared; |
| 9 |
use askama::Template; |
| 7b26e611 Andy McCall ↶ |
10 |
use axum::extract::{Form, Path, Query, State}; |
| e1ec9fbf Andy McCall ↶ |
11 |
use axum::response::{IntoResponse, Redirect, Response}; |
| 12 |
use serde::Deserialize; |
| 13 |
|
| ef20fdc1 Andy McCall ↶ |
14 |
|
| 15 |
pub struct Rights { |
| e1ec9fbf Andy McCall ↶ |
16 |
|
| ef20fdc1 Andy McCall ↶ |
17 |
pub member: bool, |
| e1ec9fbf Andy McCall ↶ |
18 |
|
| ef20fdc1 Andy McCall ↶ |
19 |
pub edit: bool, |
| e1ec9fbf Andy McCall ↶ |
20 |
} |
| 21 |
|
| ef20fdc1 Andy McCall ↶ |
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 })); |
| e1ec9fbf Andy McCall ↶ |
29 |
} |
| ef20fdc1 Andy McCall ↶ |
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(), |
| e1ec9fbf Andy McCall ↶ |
33 |
None => false, |
| 34 |
}; |
| ef20fdc1 Andy McCall ↶ |
35 |
return Ok(Some(Rights { member: member || admin, edit: member || admin })); |
| e1ec9fbf Andy McCall ↶ |
36 |
} |
| ef20fdc1 Andy McCall ↶ |
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())) |
| e1ec9fbf Andy McCall ↶ |
45 |
} |
| 46 |
|
| 47 |
#[derive(Template)] |
| 48 |
#[template(path = "projects.html")] |
| 49 |
pub struct ProjectsPage { |
| 50 |
shell: Shell, |
| 51 |
owner: String, |
| 7b26e611 Andy McCall ↶ |
52 |
|
| e1ec9fbf Andy McCall ↶ |
53 |
projects: Vec<pdb::Project>, |
| 54 |
can_edit: bool, |
| 55 |
error: Option<String>, |
| a024e800 Andy McCall ↶ |
56 |
|
| 57 |
|
| 58 |
orgs: Vec<String>, |
| 7b26e611 Andy McCall ↶ |
59 |
|
| 60 |
q: String, |
| 61 |
closed: bool, |
| 62 |
open_count: usize, |
| 63 |
closed_count: usize, |
| e1ec9fbf Andy McCall ↶ |
64 |
} |
| 65 |
|
| 7b26e611 Andy McCall ↶ |
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 |
|
| a024e800 Andy McCall ↶ |
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 |
|
| 7b26e611 Andy McCall ↶ |
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> { |
| e1ec9fbf Andy McCall ↶ |
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(); |
| a024e800 Andy McCall ↶ |
126 |
let orgs = own_orgs(&app, &viewer, &owner).await; |
| 7b26e611 Andy McCall ↶ |
127 |
let closed = query.state.as_deref() == Some("closed"); |
| 128 |
Ok(Page(ProjectsPage::build(shell, owner, projects, r.edit, None, orgs, query.q, closed))) |
| e1ec9fbf Andy McCall ↶ |
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))?; |
| a024e800 Andy McCall ↶ |
157 |
let orgs = own_orgs(&app, &viewer, &owner).await; |
| 7b26e611 Andy McCall ↶ |
158 |
return Ok(Page(ProjectsPage::build( |
| e1ec9fbf Andy McCall ↶ |
159 |
shell, |
| 160 |
owner, |
| 161 |
projects, |
| 7b26e611 Andy McCall ↶ |
162 |
true, |
| 163 |
Some("A project needs a title of up to 100 characters.".into()), |
| a024e800 Andy McCall ↶ |
164 |
orgs, |
| 7b26e611 Andy McCall ↶ |
165 |
None, |
| 166 |
false, |
| 167 |
)) |
| e1ec9fbf Andy McCall ↶ |
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 |
|
| 188 |
pub struct ColumnView { |
| 189 |
pub column: pdb::Column, |
| 190 |
pub cards: Vec<CardView>, |
| 657e7d5c Andy McCall ↶ |
191 |
|
| 192 |
|
| 193 |
pub hidden: usize, |
| 194 |
pub total: usize, |
| e1ec9fbf Andy McCall ↶ |
195 |
} |
| 196 |
|
| 657e7d5c Andy McCall ↶ |
197 |
|
| 198 |
|
| 199 |
pub const COLUMN_SHOWN: usize = 30; |
| 200 |
|
| 201 |
|
| 202 |
#[derive(Deserialize, Default)] |
| 203 |
pub struct BoardQuery { |
| 204 |
|
| 205 |
#[serde(default)] |
| 206 |
all: Option<i64>, |
| 207 |
|
| 208 |
#[serde(default)] |
| 209 |
q: Option<String>, |
| 210 |
|
| 211 |
#[serde(default)] |
| 212 |
state: Option<String>, |
| 213 |
} |
| 214 |
|
| 215 |
impl BoardQuery { |
| 216 |
fn text(&self) -> String { |
| 217 |
self.q.as_deref().map(str::trim).unwrap_or("").to_string() |
| 218 |
} |
| 219 |
fn state(&self) -> String { |
| 220 |
self.state.as_deref().map(str::trim).unwrap_or("").to_ascii_lowercase() |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
fn shape(columns: &mut [ColumnView], view: &BoardQuery) { |
| 226 |
let (q, state) = (view.text().to_lowercase(), view.state()); |
| 227 |
for col in columns.iter_mut() { |
| 228 |
col.total = col.cards.len(); |
| 229 |
if !q.is_empty() || !state.is_empty() { |
| 230 |
col.cards.retain(|c| { |
| 231 |
(q.is_empty() || c.title.to_lowercase().contains(&q) || c.reference.to_lowercase().contains(&q)) |
| 232 |
&& (state.is_empty() || c.state.eq_ignore_ascii_case(&state)) |
| 233 |
}); |
| 234 |
} else if col.cards.len() > COLUMN_SHOWN && view.all != Some(col.column.id) { |
| 235 |
|
| 236 |
let cut = col.cards.len() - COLUMN_SHOWN; |
| 237 |
col.cards.drain(..cut); |
| 238 |
} |
| 239 |
col.hidden = col.total - col.cards.len(); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| e1ec9fbf Andy McCall ↶ |
243 |
#[derive(Template)] |
| 244 |
#[template(path = "project.html")] |
| 245 |
pub struct BoardPage { |
| 246 |
shell: Shell, |
| 247 |
project: pdb::Project, |
| 248 |
columns: Vec<ColumnView>, |
| 249 |
|
| 250 |
repos: Vec<String>, |
| 251 |
|
| 252 |
can_edit: bool, |
| 253 |
|
| 254 |
can_manage: bool, |
| 255 |
notice: Option<String>, |
| 256 |
error: Option<String>, |
| b1e92a62 Andy McCall ↶ |
257 |
|
| 258 |
|
| 259 |
transfer_to: Vec<String>, |
| 657e7d5c Andy McCall ↶ |
260 |
|
| 261 |
q: String, |
| 262 |
state: String, |
| 263 |
filtered: bool, |
| e1ec9fbf Andy McCall ↶ |
264 |
} |
| 265 |
|
| 657e7d5c Andy McCall ↶ |
266 |
#[allow(clippy::too_many_arguments)] |
| e1ec9fbf Andy McCall ↶ |
267 |
async fn board( |
| 268 |
app: &Shared, |
| 269 |
shell: Shell, |
| 270 |
viewer: &Viewer, |
| 271 |
owner: &str, |
| 272 |
number: i64, |
| 273 |
notice: Option<String>, |
| 274 |
error: Option<String>, |
| 657e7d5c Andy McCall ↶ |
275 |
view: &BoardQuery, |
| e1ec9fbf Andy McCall ↶ |
276 |
) -> Result<Page<BoardPage>, Error> { |
| 277 |
let e = |e| Error::from_db(shell.clone(), e); |
| 278 |
let r = rights(app, &shell, viewer, owner).await?; |
| 279 |
let project = pdb::get(&app.db, owner, number) |
| 280 |
.await |
| 281 |
.map_err(e)? |
| 282 |
.filter(|p| p.public || r.member) |
| 283 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 657e7d5c Andy McCall ↶ |
284 |
let (mut columns, repos) = board_columns(app, viewer.account(), owner, &project).await.map_err(e)?; |
| 285 |
shape(&mut columns, view); |
| ef20fdc1 Andy McCall ↶ |
286 |
let can_edit = r.edit && !project.closed; |
| b1e92a62 Andy McCall ↶ |
287 |
let mut transfer_to = Vec::new(); |
| 288 |
if let (true, Some(a)) = (r.edit, viewer.account()) { |
| 289 |
transfer_to.push(a.username.clone()); |
| 290 |
let orgs = db::accounts::orgs_of(&app.db, a.id).await.map_err(e)?; |
| 291 |
transfer_to.extend(orgs.into_iter().map(|(o, _)| o.name)); |
| 292 |
transfer_to.retain(|o| o != owner); |
| 293 |
} |
| 657e7d5c Andy McCall ↶ |
294 |
let (q, state) = (view.text(), view.state()); |
| 295 |
let filtered = !q.is_empty() || !state.is_empty(); |
| 296 |
Ok(Page(BoardPage { shell, project, columns, repos, can_edit, can_manage: r.edit, notice, error, transfer_to, q, state, filtered })) |
| ef20fdc1 Andy McCall ↶ |
297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
pub async fn board_columns( |
| 302 |
app: &Shared, |
| 303 |
account: Option<&db::accounts::Account>, |
| 304 |
owner: &str, |
| 305 |
project: &pdb::Project, |
| 306 |
) -> sqlx::Result<(Vec<ColumnView>, Vec<String>)> { |
| e1ec9fbf Andy McCall ↶ |
307 |
let mut readable: std::collections::HashMap<i64, db::repos::RepoRow> = Default::default(); |
| ef20fdc1 Andy McCall ↶ |
308 |
for row in db::repos::list_by_owner(&app.db, owner).await? { |
| 309 |
let role = roles::role_for(&app.db, account, &row).await?; |
| e1ec9fbf Andy McCall ↶ |
310 |
if roles::allows(role, Access::Read) { |
| 311 |
readable.insert(row.id, row); |
| 312 |
} |
| 313 |
} |
| ef20fdc1 Andy McCall ↶ |
314 |
let cards = pdb::cards(&app.db, project.id).await?; |
| e1ec9fbf Andy McCall ↶ |
315 |
let mut columns = Vec::new(); |
| ef20fdc1 Andy McCall ↶ |
316 |
for column in pdb::columns(&app.db, project.id).await? { |
| e1ec9fbf Andy McCall ↶ |
317 |
let mut views = Vec::new(); |
| 318 |
for card in cards.iter().filter(|c| c.column_id == column.id) { |
| 319 |
match (card.kind.as_str(), card.repo_id, card.number) { |
| 320 |
("note", _, _) => views.push(CardView { |
| 321 |
card: card.clone(), |
| 322 |
reference: String::new(), |
| 323 |
url: String::new(), |
| 324 |
title: card.note.clone(), |
| 325 |
state: String::new(), |
| 326 |
}), |
| 327 |
(kind, Some(rid), Some(n)) => { |
| 328 |
|
| 329 |
let Some(row) = readable.get(&rid) else { continue }; |
| 330 |
let (title, state) = if kind == "issue" { |
| ef20fdc1 Andy McCall ↶ |
331 |
match db::issues::get(&app.db, rid, n).await? { |
| e1ec9fbf Andy McCall ↶ |
332 |
Some(i) => (i.title, i.state), |
| 333 |
None => continue, |
| 334 |
} |
| 335 |
} else { |
| ef20fdc1 Andy McCall ↶ |
336 |
match db::proposals::get(&app.db, rid, n).await? { |
| e1ec9fbf Andy McCall ↶ |
337 |
Some(p) => (p.title, p.state), |
| 338 |
None => continue, |
| 339 |
} |
| 340 |
}; |
| 341 |
let slug = format!("{}/{}", row.owner, row.name); |
| 342 |
views.push(CardView { |
| 343 |
card: card.clone(), |
| 344 |
reference: format!("{slug}#{n}"), |
| 345 |
url: format!("/{slug}/{kind}/{n}"), |
| 346 |
title, |
| 347 |
state, |
| 348 |
}); |
| 349 |
} |
| 350 |
_ => {} |
| 351 |
} |
| 352 |
} |
| 657e7d5c Andy McCall ↶ |
353 |
let total = views.len(); |
| 354 |
columns.push(ColumnView { column, cards: views, hidden: 0, total }); |
| e1ec9fbf Andy McCall ↶ |
355 |
} |
| 356 |
let mut repos: Vec<String> = readable.values().map(|r| r.name.clone()).collect(); |
| 357 |
repos.sort(); |
| ef20fdc1 Andy McCall ↶ |
358 |
Ok((columns, repos)) |
| e1ec9fbf Andy McCall ↶ |
359 |
} |
| 360 |
|
| 657e7d5c Andy McCall ↶ |
361 |
pub async fn show( |
| 362 |
State(app): State<Shared>, |
| 363 |
viewer: Viewer, |
| 364 |
Path((owner, number)): Path<(String, i64)>, |
| 365 |
Query(view): Query<BoardQuery>, |
| 366 |
) -> Result<Response, Error> { |
| e1ec9fbf Andy McCall ↶ |
367 |
let shell = Shell::new(&app, &format!("/projects/{owner}/{number}"), &viewer); |
| b1e92a62 Andy McCall ↶ |
368 |
|
| 369 |
let e = |e| Error::from_db(shell.clone(), e); |
| 370 |
if pdb::get(&app.db, &owner, number).await.map_err(e)?.is_none() { |
| 371 |
if let Some(p) = pdb::redirected(&app.db, &owner, number).await.map_err(e)? { |
| 372 |
return Ok(Redirect::permanent(&format!("/projects/{}/{}", p.owner, p.number)).into_response()); |
| 373 |
} |
| 374 |
} |
| 657e7d5c Andy McCall ↶ |
375 |
Ok(board(&app, shell, &viewer, &owner, number, None, None, &view).await?.into_response()) |
| e1ec9fbf Andy McCall ↶ |
376 |
} |
| 377 |
|
| 378 |
#[derive(Deserialize)] |
| 379 |
pub struct BoardForm { |
| 380 |
#[serde(rename = "_csrf")] |
| 381 |
csrf_token: String, |
| 382 |
action: String, |
| 383 |
#[serde(default)] |
| 384 |
card: Option<i64>, |
| 385 |
#[serde(default)] |
| 386 |
column: Option<i64>, |
| 387 |
#[serde(default)] |
| 388 |
name: String, |
| 389 |
|
| 390 |
#[serde(default)] |
| 391 |
reference: String, |
| 392 |
#[serde(default)] |
| 393 |
repo: String, |
| 394 |
#[serde(default)] |
| 395 |
note: String, |
| 396 |
#[serde(default)] |
| 397 |
title: String, |
| 398 |
#[serde(default)] |
| 399 |
description: String, |
| 400 |
#[serde(default)] |
| 401 |
private: Option<String>, |
| 402 |
#[serde(default)] |
| 403 |
closed: Option<String>, |
| b1e92a62 Andy McCall ↶ |
404 |
|
| 405 |
#[serde(default)] |
| 406 |
to: String, |
| e1ec9fbf Andy McCall ↶ |
407 |
} |
| 408 |
|
| 409 |
|
| efb51b39 Andy McCall ↶ |
410 |
|
| 411 |
|
| 412 |
|
| 413 |
fn parse_reference(reference: &str, repo: &str, owner: &str) -> Result<(String, i64), String> { |
| 414 |
let bad = || "Name an issue or proposal as repo#12, or write a note.".to_string(); |
| e1ec9fbf Andy McCall ↶ |
415 |
let r = reference.trim(); |
| efb51b39 Andy McCall ↶ |
416 |
let (name, n) = r.split_once('#').ok_or_else(bad)?; |
| e1ec9fbf Andy McCall ↶ |
417 |
let name = if name.is_empty() { repo.trim() } else { name.trim() }; |
| efb51b39 Andy McCall ↶ |
418 |
let name = match name.split_once('/') { |
| 419 |
Some((o, rest)) if o.eq_ignore_ascii_case(owner) => rest, |
| 420 |
Some((o, _)) => return Err(format!("This board holds only {owner}'s issues and proposals, not {o}'s.")), |
| 421 |
None => name, |
| 422 |
}; |
| 423 |
let n: i64 = n.trim().parse().map_err(|_| bad())?; |
| 424 |
if name.is_empty() || n <= 0 || name.contains('/') { |
| 425 |
return Err(bad()); |
| 426 |
} |
| 427 |
Ok((name.to_string(), n)) |
| ef20fdc1 Andy McCall ↶ |
428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
#[allow(clippy::too_many_arguments)] |
| 434 |
pub async fn add_card( |
| 435 |
app: &Shared, |
| 436 |
account: Option<&db::accounts::Account>, |
| 437 |
owner: &str, |
| 438 |
project_id: i64, |
| 439 |
column_id: i64, |
| 440 |
reference: &str, |
| 441 |
repo: &str, |
| 442 |
note: &str, |
| 443 |
actor: &str, |
| 444 |
) -> sqlx::Result<Result<pdb::Card, String>> { |
| 445 |
if !note.trim().is_empty() { |
| 446 |
let note: String = note.trim().chars().take(500).collect(); |
| 447 |
pdb::add_card( |
| 448 |
&app.db, |
| 449 |
project_id, |
| 450 |
pdb::NewCard { column_id, kind: "note", repo_id: None, number: None, note: ¬e, added_by: actor }, |
| 451 |
) |
| 452 |
.await?; |
| 453 |
return Ok(Ok(newest_card(app, project_id).await?)); |
| 454 |
} |
| efb51b39 Andy McCall ↶ |
455 |
let (repo, n) = match parse_reference(reference, repo, owner) { |
| 456 |
Ok(r) => r, |
| 457 |
Err(why) => return Ok(Err(why)), |
| ef20fdc1 Andy McCall ↶ |
458 |
}; |
| 459 |
let Some(row) = db::repos::get(&app.db, owner, &repo).await? else { return Ok(Err(format!("{owner} has no repository {repo}."))) }; |
| 460 |
let role = roles::role_for(&app.db, account, &row).await?; |
| 461 |
let (issues, proposals) = db::issues::numbers(&app.db, row.id).await?; |
| 462 |
let kind = if issues.contains(&n) { |
| 463 |
"issue" |
| 464 |
} else if proposals.contains(&n) { |
| 465 |
"proposal" |
| 466 |
} else { |
| 467 |
return Ok(Err(format!("{repo} has no issue or proposal #{n}."))); |
| 468 |
}; |
| 469 |
if !roles::allows(role, Access::Read) { |
| 470 |
return Ok(Err(format!("{repo} has no issue or proposal #{n}."))); |
| 471 |
} |
| 472 |
if !pdb::add_card( |
| 473 |
&app.db, |
| 474 |
project_id, |
| 475 |
pdb::NewCard { column_id, kind, repo_id: Some(row.id), number: Some(n), note: "", added_by: actor }, |
| 476 |
) |
| 477 |
.await? |
| 478 |
{ |
| 479 |
return Ok(Err(format!("{repo}#{n} is already on this project."))); |
| 480 |
} |
| 481 |
Ok(Ok(newest_card(app, project_id).await?)) |
| 482 |
} |
| 483 |
|
| 484 |
async fn newest_card(app: &Shared, project_id: i64) -> sqlx::Result<pdb::Card> { |
| 5d90e0e0 Andy McCall |
485 |
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 |
| e1ec9fbf Andy McCall ↶ |
486 |
} |
| 487 |
|
| 488 |
pub async fn act( |
| 489 |
State(app): State<Shared>, |
| 490 |
viewer: Viewer, |
| 491 |
Path((owner, number)): Path<(String, i64)>, |
| 492 |
Form(f): Form<BoardForm>, |
| 493 |
) -> Result<Response, Error> { |
| 494 |
let shell = Shell::new(&app, &format!("/projects/{owner}/{number}"), &viewer); |
| 495 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 496 |
let r = rights(&app, &shell, &viewer, &owner).await?; |
| 497 |
let e = |e| Error::from_db(shell.clone(), e); |
| 498 |
let project = pdb::get(&app.db, &owner, number) |
| 499 |
.await |
| 500 |
.map_err(e)? |
| 501 |
.filter(|p| p.public || r.member) |
| 502 |
.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 503 |
if !r.edit { |
| 504 |
return Err(Error::not_found(shell)); |
| 505 |
} |
| b1e92a62 Andy McCall ↶ |
506 |
if project.closed && !matches!(f.action.as_str(), "settings" | "delete" | "transfer") { |
| 657e7d5c Andy McCall ↶ |
507 |
return Ok(board( |
| 508 |
&app, |
| 509 |
shell, |
| 510 |
&viewer, |
| 511 |
&owner, |
| 512 |
number, |
| 513 |
None, |
| 514 |
Some("This project is closed; reopen it to change it.".into()), |
| 515 |
&BoardQuery::default(), |
| 516 |
) |
| 517 |
.await? |
| 518 |
.into_response()); |
| e1ec9fbf Andy McCall ↶ |
519 |
} |
| 520 |
let actor = viewer.account().map(|a| a.username.clone()).unwrap_or_default(); |
| 521 |
let columns = pdb::columns(&app.db, project.id).await.map_err(e)?; |
| 522 |
let column = f.column.filter(|c| columns.iter().any(|x| x.id == *c)).or(columns.first().map(|c| c.id)); |
| 523 |
let mut notice = None; |
| 524 |
let mut error = None; |
| 525 |
match f.action.as_str() { |
| 526 |
"add" => { |
| 527 |
let Some(column_id) = column else { return Err(Error::bad(shell, "no column")) }; |
| ef20fdc1 Andy McCall ↶ |
528 |
if let Err(msg) = |
| 529 |
add_card(&app, viewer.account(), &owner, project.id, column_id, &f.reference, &f.repo, &f.note, &actor).await.map_err(e)? |
| 530 |
{ |
| 531 |
error = Some(msg); |
| e1ec9fbf Andy McCall ↶ |
532 |
} |
| 533 |
} |
| 534 |
"move" => { |
| 535 |
if let (Some(card), Some(col)) = (f.card, f.column) { |
| 536 |
pdb::move_card(&app.db, project.id, card, col).await.map_err(e)?; |
| 537 |
} |
| 538 |
} |
| 539 |
"up" | "down" => { |
| 540 |
if let Some(card) = f.card { |
| 541 |
pdb::shift_card(&app.db, project.id, card, if f.action == "up" { -1 } else { 1 }).await.map_err(e)?; |
| 542 |
} |
| 543 |
} |
| 544 |
"remove" => { |
| 545 |
if let Some(card) = f.card { |
| 546 |
pdb::remove_card(&app.db, project.id, card).await.map_err(e)?; |
| 547 |
} |
| 548 |
} |
| 549 |
"add_column" => { |
| 550 |
let name = f.name.trim(); |
| 551 |
if name.is_empty() || name.len() > 40 { |
| 552 |
error = Some("A column has a name of up to 40 characters.".into()); |
| 553 |
} else { |
| 554 |
pdb::add_column(&app.db, project.id, name).await.map_err(e)?; |
| 555 |
} |
| 556 |
} |
| 557 |
"rename_column" => { |
| 558 |
if let (Some(col), name) = (f.column, f.name.trim()) { |
| 559 |
if !name.is_empty() && name.len() <= 40 { |
| 560 |
pdb::rename_column(&app.db, project.id, col, name).await.map_err(e)?; |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
"left" | "right" => { |
| 565 |
if let Some(col) = f.column { |
| 566 |
pdb::shift_column(&app.db, project.id, col, if f.action == "left" { -1 } else { 1 }).await.map_err(e)?; |
| 567 |
} |
| 568 |
} |
| 569 |
"remove_column" => { |
| 570 |
if let Some(col) = f.column { |
| 571 |
if !pdb::remove_column(&app.db, project.id, col).await.map_err(e)? { |
| 572 |
error = Some("A project keeps at least one column.".into()); |
| 573 |
} |
| 574 |
} |
| 575 |
} |
| 576 |
"settings" => { |
| 577 |
let title = if f.title.trim().is_empty() { project.title.clone() } else { f.title.trim().chars().take(100).collect() }; |
| 578 |
pdb::set(&app.db, project.id, &title, f.description.trim(), f.private.is_none(), f.closed.is_some()).await.map_err(e)?; |
| 579 |
notice = Some("Saved.".into()); |
| b1e92a62 Andy McCall ↶ |
580 |
} |
| 581 |
"transfer" => { |
| 582 |
let to = f.to.trim(); |
| 583 |
match rights_of(&app, viewer.account(), to).await.map_err(e)? { |
| 584 |
_ if to == owner => error = Some(format!("It is {owner}'s already.")), |
| 585 |
Some(t) if t.edit => { |
| 586 |
let moved = pdb::transfer(&app.db, project.id, to).await.map_err(e)?; |
| 587 |
let detail = format!("{owner}/{number} to {to}/{}", moved.number); |
| 588 |
db::accounts::audit(&app.db, viewer.account(), "project.transferred", &detail, &project.title).await.ok(); |
| 589 |
return Ok(Redirect::to(&format!("/projects/{to}/{}", moved.number)).into_response()); |
| 590 |
} |
| 591 |
_ => error = Some(format!("You cannot make projects under {to}, so you cannot move this one there.")), |
| 592 |
} |
| e1ec9fbf Andy McCall ↶ |
593 |
} |
| 594 |
"delete" => { |
| 595 |
pdb::delete(&app.db, project.id).await.map_err(e)?; |
| 596 |
db::accounts::audit(&app.db, viewer.account(), "project.deleted", &format!("{owner}/{number}"), &project.title).await.ok(); |
| 597 |
return Ok(Redirect::to(&format!("/projects/{owner}")).into_response()); |
| 598 |
} |
| 599 |
_ => return Err(Error::bad(shell, "unknown action")), |
| 600 |
} |
| 601 |
if notice.is_none() && error.is_none() { |
| 602 |
|
| 603 |
return Ok(Redirect::to(&format!("/projects/{owner}/{number}")).into_response()); |
| 604 |
} |
| 657e7d5c Andy McCall ↶ |
605 |
Ok(board(&app, shell, &viewer, &owner, number, notice, error, &BoardQuery::default()).await?.into_response()) |
| e1ec9fbf Andy McCall ↶ |
606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
pub async fn placements_for(app: &Shared, viewer: &Viewer, repo: &db::repos::RepoRow, number: i64) -> Vec<(pdb::Project, String)> { |
| 611 |
let Ok(all) = pdb::placements(&app.db, repo.id, number).await else { return vec![] }; |
| 612 |
let mut out = Vec::new(); |
| 613 |
for (p, col) in all { |
| 614 |
let visible = p.public |
| 615 |
|| viewer.is_admin() |
| 616 |
|| match viewer.account() { |
| 617 |
Some(v) if v.username == p.owner => true, |
| 618 |
Some(v) => match db::accounts::org_by_name(&app.db, &p.owner).await.ok().flatten() { |
| 619 |
Some(o) => db::accounts::org_role(&app.db, o.id, v.id).await.ok().flatten().is_some(), |
| 620 |
None => false, |
| 621 |
}, |
| 622 |
None => false, |
| 623 |
}; |
| 624 |
if visible { |
| 625 |
out.push((p, col)); |
| 626 |
} |
| 627 |
} |
| 628 |
out |
| 629 |
} |
| 630 |
|
| 631 |
#[cfg(test)] |
| 632 |
mod tests { |
| 633 |
use super::parse_reference; |
| 634 |
|
| 635 |
#[test] |
| 636 |
fn references() { |
| efb51b39 Andy McCall ↶ |
637 |
let p = |r: &str, repo: &str| parse_reference(r, repo, "andy").ok(); |
| 638 |
assert_eq!(p("oxygen#12", ""), Some(("oxygen".into(), 12))); |
| 639 |
assert_eq!(p("#7", "hub"), Some(("hub".into(), 7))); |
| 640 |
assert_eq!(p(" hub # 3 ", ""), Some(("hub".into(), 3))); |
| 641 |
assert_eq!(p("#7", ""), None, "a repository is needed"); |
| 642 |
assert_eq!(p("andy/hub#4", ""), Some(("hub".into(), 4)), "the board's own owner, as it shows cards"); |
| 643 |
assert!(parse_reference("other/repo#1", "", "andy").unwrap_err().contains("only andy's"), "another owner is refused, saying why"); |
| 644 |
assert_eq!(p("hub#0", ""), None); |
| 645 |
assert_eq!(p("hub", ""), None); |
| e1ec9fbf Andy McCall ↶ |
646 |
} |
| 647 |
} |
| a1b98695 Andy McCall ↶ |
648 |
|
| 649 |
#[derive(Deserialize)] |
| 650 |
pub struct PlaceForm { |
| 651 |
#[serde(rename = "_csrf")] |
| 652 |
csrf_token: String, |
| 653 |
|
| 654 |
project: i64, |
| 655 |
|
| 656 |
action: String, |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
async fn place( |
| 662 |
app: Shared, |
| 663 |
viewer: Viewer, |
| 664 |
owner: String, |
| 665 |
name: String, |
| 666 |
kind: &'static str, |
| 667 |
number: i64, |
| 668 |
f: PlaceForm, |
| 669 |
) -> Result<Response, Error> { |
| 670 |
let page = format!("/{owner}/{name}/{kind}/{number}"); |
| 671 |
let shell = Shell::new(&app, &page, &viewer); |
| 672 |
csrf(&viewer, &f.csrf_token, &shell)?; |
| 673 |
let (_, row, _) = super::lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 674 |
let r = rights(&app, &shell, &viewer, &row.owner).await?; |
| 675 |
if !r.edit { |
| 676 |
return Err(Error::not_found(shell)); |
| 677 |
} |
| 678 |
let e = |e| Error::from_db(shell.clone(), e); |
| 679 |
let project = pdb::get(&app.db, &row.owner, f.project).await.map_err(e)?.ok_or_else(|| Error::not_found(shell.clone()))?; |
| 680 |
let actor = viewer.account().map(|a| a.username.clone()).unwrap_or_default(); |
| 681 |
let mut error = None; |
| 682 |
match f.action.as_str() { |
| 683 |
"add" => { |
| 684 |
let columns = pdb::columns(&app.db, project.id).await.map_err(e)?; |
| 685 |
let Some(first) = columns.first() else { return Err(Error::bad(shell, "that project has no columns")) }; |
| 686 |
if let Err(msg) = |
| 687 |
add_card(&app, viewer.account(), &row.owner, project.id, first.id, &format!("{}#{number}", row.name), &row.name, "", &actor) |
| 688 |
.await |
| 689 |
.map_err(e)? |
| 690 |
{ |
| 691 |
error = Some(msg); |
| 692 |
} |
| 693 |
} |
| 694 |
"remove" => { |
| 695 |
for c in pdb::cards(&app.db, project.id).await.map_err(e)? { |
| 696 |
if c.repo_id == Some(row.id) && c.number == Some(number) { |
| 697 |
pdb::remove_card(&app.db, project.id, c.id).await.map_err(e)?; |
| 698 |
} |
| 699 |
} |
| 700 |
} |
| 701 |
_ => return Err(Error::bad(shell, "add or remove")), |
| 702 |
} |
| 703 |
let to = match error { |
| 704 |
Some(m) => format!("{page}?error={}", super::settings::urlencode(&m)), |
| 705 |
None => page, |
| 706 |
}; |
| 707 |
Ok(Redirect::to(&to).into_response()) |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
pub async fn place_proposal( |
| 712 |
State(app): State<Shared>, |
| 713 |
viewer: Viewer, |
| 714 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 715 |
Form(f): Form<PlaceForm>, |
| 716 |
) -> Result<Response, Error> { |
| 717 |
place(app, viewer, owner, name, "proposal", number, f).await |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
pub async fn place_issue( |
| 722 |
State(app): State<Shared>, |
| 723 |
viewer: Viewer, |
| 724 |
Path((owner, name, number)): Path<(String, String, i64)>, |
| 725 |
Form(f): Form<PlaceForm>, |
| 726 |
) -> Result<Response, Error> { |
| 727 |
place(app, viewer, owner, name, "issue", number, f).await |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
pub async fn project_choices( |
| 733 |
app: &Shared, |
| 734 |
viewer: &Viewer, |
| 735 |
repo: &db::repos::RepoRow, |
| 736 |
placed: &[(pdb::Project, String)], |
| 737 |
) -> Option<Vec<pdb::Project>> { |
| 738 |
let Ok(Some(r)) = rights_of(app, viewer.account(), &repo.owner).await else { return None }; |
| 739 |
if !r.edit { |
| 740 |
return None; |
| 741 |
} |
| 742 |
Some( |
| 743 |
pdb::of_owner(&app.db, &repo.owner) |
| 744 |
.await |
| 745 |
.unwrap_or_default() |
| 746 |
.into_iter() |
| 747 |
.filter(|p| !p.closed && !placed.iter().any(|(q, _)| q.id == p.id)) |
| 748 |
.collect(), |
| 749 |
) |
| 750 |
} |