| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | use std::time::{SystemTime, UNIX_EPOCH}; |
| 27 | |
| 28 | use topcoat::{ |
| 29 | Result, |
| 30 | context::Cx, |
| 31 | icon::{icon, iconify::iconify_icon}, |
| 32 | router::{ |
| 33 | Body, Response, |
| 34 | error::{RouterErrorExt, not_found}, |
| 35 | header::CONTENT_TYPE, |
| 36 | path_param, query_params, route, |
| 37 | }, |
| 38 | view::{attributes, component, view}, |
| 39 | }; |
| 40 | |
| 41 | use crate::{ |
| 42 | application::{ |
| 43 | ActivityEntry, IssueEntry, activity_feed, commit_feed, issue_feed, port::GitQueryError, |
| 44 | }, |
| 45 | domain::{CommitEntry, OrgName, RefName, RepoName}, |
| 46 | }; |
| 47 | |
| 48 | use super::{ |
| 49 | browse::encode, |
| 50 | commit::{commit_url, compare_url}, |
| 51 | context::{ |
| 52 | current_actor, issues, memberships, orgs, public_origin, push_events, queries, repos, |
| 53 | server_error, users, |
| 54 | }, |
| 55 | markdown, |
| 56 | profile::handle_param, |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | #[path_param] |
| 61 | struct Name(str); |
| 62 | |
| 63 | |
| 64 | #[query_params(error = bad_request)] |
| 65 | struct Query { |
| 66 | rev: Option<String>, |
| 67 | } |
| 68 | |
| 69 | |
| 70 | const ATOM: &str = "application/atom+xml; charset=utf-8"; |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | const ATOM_LINK: &str = "application/atom+xml"; |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | #[route(GET "/{handle}/repos/{name}/commits.atom")] |
| 85 | async fn commits_feed(cx: &Cx) -> Result<Response<Body>> { |
| 86 | let handle = handle_param(cx)?; |
| 87 | let name = repo_param(cx)?; |
| 88 | let rev = requested_rev(cx)?; |
| 89 | |
| 90 | let entries = commit_feed( |
| 91 | &handle, |
| 92 | &name, |
| 93 | rev.as_ref(), |
| 94 | ¤t_actor(cx).await?, |
| 95 | &orgs(cx), |
| 96 | &memberships(cx), |
| 97 | &repos(cx), |
| 98 | &queries(cx), |
| 99 | ) |
| 100 | .await |
| 101 | |
| 102 | |
| 103 | .map_err(unreadable)? |
| 104 | .ok_or_not_found()?; |
| 105 | |
| 106 | let origin = public_origin(cx); |
| 107 | let handle = handle.as_str(); |
| 108 | let name = name.as_str(); |
| 109 | let path = commits_feed_path(handle, name); |
| 110 | let alternate = match &rev { |
| 111 | Some(rev) => super::browse::log_url(handle, name, rev.as_str()), |
| 112 | None => format!("/{handle}/repos/{name}"), |
| 113 | }; |
| 114 | |
| 115 | let feed = Feed { |
| 116 | id: absolute(&origin, &path), |
| 117 | title: match &rev { |
| 118 | Some(rev) => format!("{handle}/{name} commits on {}", rev.as_str()), |
| 119 | None => format!("{handle}/{name} commits"), |
| 120 | }, |
| 121 | self_url: absolute(&origin, &path), |
| 122 | alternate: absolute(&origin, &alternate), |
| 123 | author: handle.to_owned(), |
| 124 | updated: entries.first().map(|entry| entry.commit.committed_at), |
| 125 | entries: entries |
| 126 | .iter() |
| 127 | .map(|entry| commit_entry(&origin, handle, name, entry)) |
| 128 | .collect(), |
| 129 | }; |
| 130 | |
| 131 | served(feed.render()) |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | #[route(GET "/{handle}/repos/{name}/issues.atom")] |
| 139 | async fn issues_feed(cx: &Cx) -> Result<Response<Body>> { |
| 140 | let handle = handle_param(cx)?; |
| 141 | let name = repo_param(cx)?; |
| 142 | |
| 143 | let entries = issue_feed( |
| 144 | ¤t_actor(cx).await?, |
| 145 | &handle, |
| 146 | &name, |
| 147 | &orgs(cx), |
| 148 | &memberships(cx), |
| 149 | &repos(cx), |
| 150 | &issues(cx), |
| 151 | &users(cx), |
| 152 | ) |
| 153 | .await |
| 154 | .map_err(server_error)? |
| 155 | .ok_or_not_found()?; |
| 156 | |
| 157 | let origin = public_origin(cx); |
| 158 | let handle = handle.as_str(); |
| 159 | let name = name.as_str(); |
| 160 | let path = issues_feed_path(handle, name); |
| 161 | |
| 162 | let feed = Feed { |
| 163 | id: absolute(&origin, &path), |
| 164 | title: format!("{handle}/{name} issues"), |
| 165 | self_url: absolute(&origin, &path), |
| 166 | alternate: absolute(&origin, &format!("/{handle}/repos/{name}/issues")), |
| 167 | author: handle.to_owned(), |
| 168 | |
| 169 | |
| 170 | updated: entries.first().map(|entry| entry.updated_at), |
| 171 | entries: entries |
| 172 | .iter() |
| 173 | .map(|entry| issue_entry(&origin, handle, name, entry)) |
| 174 | .collect(), |
| 175 | }; |
| 176 | |
| 177 | served(feed.render()) |
| 178 | } |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | #[route(GET "/{handle}/feed.atom")] |
| 192 | async fn profile_feed(cx: &Cx) -> Result<Response<Body>> { |
| 193 | let handle = handle_param(cx)?; |
| 194 | |
| 195 | let entries = activity_feed( |
| 196 | &handle, |
| 197 | ¤t_actor(cx).await?, |
| 198 | &orgs(cx), |
| 199 | &memberships(cx), |
| 200 | &repos(cx), |
| 201 | &push_events(cx), |
| 202 | &users(cx), |
| 203 | ) |
| 204 | .await |
| 205 | .map_err(server_error)? |
| 206 | .ok_or_not_found()?; |
| 207 | |
| 208 | let origin = public_origin(cx); |
| 209 | let handle = handle.as_str(); |
| 210 | let path = profile_feed_path(handle); |
| 211 | |
| 212 | let feed = Feed { |
| 213 | id: absolute(&origin, &path), |
| 214 | title: format!("@{handle}"), |
| 215 | self_url: absolute(&origin, &path), |
| 216 | alternate: absolute(&origin, &format!("/{handle}")), |
| 217 | author: handle.to_owned(), |
| 218 | updated: entries.first().map(|entry| entry.event.created_at), |
| 219 | entries: entries |
| 220 | .iter() |
| 221 | .map(|entry| activity_item(&origin, handle, entry)) |
| 222 | .collect(), |
| 223 | }; |
| 224 | |
| 225 | served(feed.render()) |
| 226 | } |
| 227 | |
| 228 | |
| 229 | fn repo_param(cx: &Cx) -> Result<RepoName> { |
| 230 | Ok(RepoName::new(path_param::<Name>(cx)).map_err(|_| not_found())?) |
| 231 | } |
| 232 | |
| 233 | |
| 234 | fn requested_rev(cx: &Cx) -> Result<Option<RefName>> { |
| 235 | query_params::<Query>(cx)? |
| 236 | .rev |
| 237 | .as_deref() |
| 238 | .filter(|rev| !rev.is_empty()) |
| 239 | .map(|rev| RefName::new(rev).map_err(|_| not_found().into())) |
| 240 | .transpose() |
| 241 | } |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | fn unreadable(error: crate::application::Error) -> topcoat::Error { |
| 249 | match &error { |
| 250 | crate::application::Error::GitQuery(GitQueryError { .. }) => not_found().into(), |
| 251 | _ => server_error(error), |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | |
| 256 | fn served(body: String) -> Result<Response<Body>> { |
| 257 | Response::builder() |
| 258 | .header(CONTENT_TYPE, ATOM) |
| 259 | .body(Body::from(body)) |
| 260 | .map_err(server_error) |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | fn commit_entry(origin: &str, handle: &str, name: &str, entry: &CommitEntry) -> Entry { |
| 267 | let link = absolute(origin, &commit_url(handle, name, entry.commit.id.as_str())); |
| 268 | |
| 269 | Entry { |
| 270 | |
| 271 | |
| 272 | id: link.clone(), |
| 273 | title: entry.commit.summary.clone(), |
| 274 | link, |
| 275 | updated: entry.commit.committed_at, |
| 276 | author: Some(entry.commit.author_name.clone()), |
| 277 | content: (!entry.body.is_empty()).then(|| Content::Text(entry.body.clone())), |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | |
| 282 | fn issue_entry(origin: &str, handle: &str, name: &str, entry: &IssueEntry) -> Entry { |
| 283 | let link = absolute( |
| 284 | origin, |
| 285 | &format!("/{handle}/repos/{name}/issues/{}", entry.number), |
| 286 | ); |
| 287 | |
| 288 | Entry { |
| 289 | id: link.clone(), |
| 290 | |
| 291 | |
| 292 | |
| 293 | title: format!( |
| 294 | "#{} {}{}", |
| 295 | entry.number, |
| 296 | entry.title, |
| 297 | if entry.state.is_open() { |
| 298 | "" |
| 299 | } else { |
| 300 | " (closed)" |
| 301 | } |
| 302 | ), |
| 303 | link, |
| 304 | |
| 305 | |
| 306 | updated: entry.updated_at, |
| 307 | author: Some( |
| 308 | entry |
| 309 | .author |
| 310 | .as_ref() |
| 311 | .map(|handle| handle.as_str().to_owned()) |
| 312 | .unwrap_or_else(|| "someone".to_owned()), |
| 313 | ), |
| 314 | content: (!entry.body.is_empty()).then(|| { |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | Content::Html( |
| 320 | markdown::render( |
| 321 | &entry.body, |
| 322 | Some(markdown::Crossref { |
| 323 | handle, |
| 324 | name, |
| 325 | origin: Some(origin), |
| 326 | }), |
| 327 | ) |
| 328 | .to_string(), |
| 329 | ) |
| 330 | }), |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | |
| 335 | fn activity_item(origin: &str, handle: &str, entry: &ActivityEntry) -> Entry { |
| 336 | let name = entry.repo.as_str(); |
| 337 | let event = &entry.event; |
| 338 | let short = event.short_ref(); |
| 339 | let kind = event.ref_kind(); |
| 340 | |
| 341 | let (title, path) = if event.is_delete() { |
| 342 | ( |
| 343 | format!("deleted {kind} {short} in {name}"), |
| 344 | format!("/{handle}/repos/{name}"), |
| 345 | ) |
| 346 | } else if event.is_create() { |
| 347 | ( |
| 348 | format!("created {kind} {short} in {name}"), |
| 349 | format!("/{handle}/repos/{name}/tree/{}", encode(short, false)), |
| 350 | ) |
| 351 | } else if kind == "tag" { |
| 352 | ( |
| 353 | format!("moved tag {short} in {name}"), |
| 354 | format!("/{handle}/repos/{name}/tree/{}", encode(short, false)), |
| 355 | ) |
| 356 | } else { |
| 357 | ( |
| 358 | format!("pushed to {name}/{short}"), |
| 359 | compare_url(handle, name, event.before.as_str(), event.after.as_str()), |
| 360 | ) |
| 361 | }; |
| 362 | |
| 363 | Entry { |
| 364 | |
| 365 | |
| 366 | id: format!("urn:uuid:{}", event.id), |
| 367 | title, |
| 368 | link: absolute(origin, &path), |
| 369 | updated: event.created_at, |
| 370 | author: entry |
| 371 | .pusher |
| 372 | .as_ref() |
| 373 | .map(|handle| handle.as_str().to_owned()), |
| 374 | |
| 375 | |
| 376 | content: Some(Content::Text(if event.is_delete() { |
| 377 | format!("{} was deleted", event.before.short()) |
| 378 | } else if event.is_create() { |
| 379 | format!("created at {}", event.after.short()) |
| 380 | } else { |
| 381 | format!("{} \u{2192} {}", event.before.short(), event.after.short()) |
| 382 | })), |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | enum Content { |
| 394 | Text(String), |
| 395 | Html(String), |
| 396 | } |
| 397 | |
| 398 | |
| 399 | struct Entry { |
| 400 | |
| 401 | |
| 402 | id: String, |
| 403 | title: String, |
| 404 | link: String, |
| 405 | updated: SystemTime, |
| 406 | author: Option<String>, |
| 407 | content: Option<Content>, |
| 408 | } |
| 409 | |
| 410 | |
| 411 | struct Feed { |
| 412 | id: String, |
| 413 | title: String, |
| 414 | |
| 415 | self_url: String, |
| 416 | |
| 417 | alternate: String, |
| 418 | |
| 419 | |
| 420 | author: String, |
| 421 | |
| 422 | updated: Option<SystemTime>, |
| 423 | entries: Vec<Entry>, |
| 424 | } |
| 425 | |
| 426 | impl Feed { |
| 427 | fn render(&self) -> String { |
| 428 | let mut out = String::with_capacity(2048 + self.entries.len() * 512); |
| 429 | |
| 430 | out.push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); |
| 431 | out.push_str("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n"); |
| 432 | element(&mut out, 1, "id", &self.id); |
| 433 | element(&mut out, 1, "title", &self.title); |
| 434 | |
| 435 | |
| 436 | element( |
| 437 | &mut out, |
| 438 | 1, |
| 439 | "updated", |
| 440 | &rfc3339(self.updated.unwrap_or(UNIX_EPOCH)), |
| 441 | ); |
| 442 | link(&mut out, 1, "self", ATOM_LINK, &self.self_url); |
| 443 | link(&mut out, 1, "alternate", "text/html", &self.alternate); |
| 444 | out.push_str(" <author><name>"); |
| 445 | out.push_str(&escape(&self.author)); |
| 446 | out.push_str("</name></author>\n"); |
| 447 | |
| 448 | for entry in &self.entries { |
| 449 | out.push_str(" <entry>\n"); |
| 450 | element(&mut out, 2, "id", &entry.id); |
| 451 | element(&mut out, 2, "title", &entry.title); |
| 452 | element(&mut out, 2, "updated", &rfc3339(entry.updated)); |
| 453 | link(&mut out, 2, "alternate", "text/html", &entry.link); |
| 454 | |
| 455 | if let Some(author) = &entry.author { |
| 456 | out.push_str(" <author><name>"); |
| 457 | out.push_str(&escape(author)); |
| 458 | out.push_str("</name></author>\n"); |
| 459 | } |
| 460 | |
| 461 | match &entry.content { |
| 462 | Some(Content::Text(text)) => { |
| 463 | out.push_str(" <content type=\"text\">"); |
| 464 | out.push_str(&escape(text)); |
| 465 | out.push_str("</content>\n"); |
| 466 | } |
| 467 | Some(Content::Html(html)) => { |
| 468 | out.push_str(" <content type=\"html\">"); |
| 469 | out.push_str(&escape(html)); |
| 470 | out.push_str("</content>\n"); |
| 471 | } |
| 472 | None => {} |
| 473 | } |
| 474 | |
| 475 | out.push_str(" </entry>\n"); |
| 476 | } |
| 477 | |
| 478 | out.push_str("</feed>\n"); |
| 479 | out |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | |
| 484 | fn element(out: &mut String, depth: usize, name: &str, text: &str) { |
| 485 | for _ in 0..depth { |
| 486 | out.push_str(" "); |
| 487 | } |
| 488 | out.push('<'); |
| 489 | out.push_str(name); |
| 490 | out.push('>'); |
| 491 | out.push_str(&escape(text)); |
| 492 | out.push_str("</"); |
| 493 | out.push_str(name); |
| 494 | out.push_str(">\n"); |
| 495 | } |
| 496 | |
| 497 | |
| 498 | |
| 499 | fn link(out: &mut String, depth: usize, rel: &str, kind: &str, href: &str) { |
| 500 | for _ in 0..depth { |
| 501 | out.push_str(" "); |
| 502 | } |
| 503 | out.push_str("<link rel=\""); |
| 504 | out.push_str(rel); |
| 505 | out.push_str("\" type=\""); |
| 506 | out.push_str(kind); |
| 507 | out.push_str("\" href=\""); |
| 508 | out.push_str(&escape(href)); |
| 509 | out.push_str("\" />\n"); |
| 510 | } |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | pub(super) fn escape(value: &str) -> String { |
| 523 | let mut out = String::with_capacity(value.len() + value.len() / 8); |
| 524 | |
| 525 | for character in value.chars() { |
| 526 | match character { |
| 527 | '&' => out.push_str("&"), |
| 528 | '<' => out.push_str("<"), |
| 529 | '>' => out.push_str(">"), |
| 530 | '"' => out.push_str("""), |
| 531 | '\'' => out.push_str("'"), |
| 532 | '\t' | '\n' | '\r' => out.push(character), |
| 533 | |
| 534 | |
| 535 | character if character >= ' ' => out.push(character), |
| 536 | _ => {} |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | out |
| 541 | } |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | fn rfc3339(time: SystemTime) -> String { |
| 547 | let seconds = time |
| 548 | .duration_since(UNIX_EPOCH) |
| 549 | .map(|since| since.as_secs() as i64) |
| 550 | .unwrap_or(0); |
| 551 | |
| 552 | let (year, month, day) = super::browse::civil_from_days(seconds.div_euclid(86_400)); |
| 553 | let rest = seconds.rem_euclid(86_400); |
| 554 | |
| 555 | format!( |
| 556 | "{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z", |
| 557 | rest / 3600, |
| 558 | (rest % 3600) / 60, |
| 559 | rest % 60 |
| 560 | ) |
| 561 | } |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | fn absolute(origin: &str, path: &str) -> String { |
| 568 | format!("{origin}{path}") |
| 569 | } |
| 570 | |
| 571 | |
| 572 | |
| 573 | pub(super) fn commits_feed_path(handle: &str, name: &str) -> String { |
| 574 | format!("/{handle}/repos/{name}/commits.atom") |
| 575 | } |
| 576 | |
| 577 | pub(super) fn issues_feed_path(handle: &str, name: &str) -> String { |
| 578 | format!("/{handle}/repos/{name}/issues.atom") |
| 579 | } |
| 580 | |
| 581 | pub(super) fn profile_feed_path(handle: &str) -> String { |
| 582 | format!("/{handle}/feed.atom") |
| 583 | } |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | pub(super) fn discovered(path: &str) -> Option<(String, String)> { |
| 597 | let segments: Vec<&str> = path.trim_matches('/').split('/').collect(); |
| 598 | |
| 599 | match segments[..] { |
| 600 | [handle] if OrgName::new(handle).is_ok() => { |
| 601 | Some((profile_feed_path(handle), format!("@{handle} activity"))) |
| 602 | } |
| 603 | [handle, "repos", name] if OrgName::new(handle).is_ok() && RepoName::new(name).is_ok() => { |
| 604 | Some(( |
| 605 | commits_feed_path(handle, name), |
| 606 | format!("{handle}/{name} commits"), |
| 607 | )) |
| 608 | } |
| 609 | [handle, "repos", name, "issues"] |
| 610 | if OrgName::new(handle).is_ok() && RepoName::new(name).is_ok() => |
| 611 | { |
| 612 | Some(( |
| 613 | issues_feed_path(handle, name), |
| 614 | format!("{handle}/{name} issues"), |
| 615 | )) |
| 616 | } |
| 617 | _ => None, |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | #[component] |
| 626 | pub(super) async fn feed_link(href: &str) -> Result { |
| 627 | view! { |
| 628 | <a |
| 629 | href=(href) |
| 630 | title="Atom feed" |
| 631 | class="inline-flex shrink-0 items-center gap-1 text-xs text-muted-foreground hover:text-foreground" |
| 632 | > |
| 633 | icon(data: iconify_icon!("feather:rss"), attrs: attributes! { class="size-3.5" }) |
| 634 | "Feed" |
| 635 | </a> |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | #[cfg(test)] |
| 640 | mod tests { |
| 641 | use super::*; |
| 642 | |
| 643 | #[test] |
| 644 | fn the_five_predefined_entities_are_escaped() { |
| 645 | assert_eq!( |
| 646 | escape("a < b & c > d \"quoted\" 'single'"), |
| 647 | "a < b & c > d "quoted" 'single'" |
| 648 | ); |
| 649 | } |
| 650 | |
| 651 | #[test] |
| 652 | fn markup_cannot_be_smuggled_through_a_title() { |
| 653 | assert_eq!( |
| 654 | escape("</title><script>alert(1)</script>"), |
| 655 | "</title><script>alert(1)</script>" |
| 656 | ); |
| 657 | } |
| 658 | |
| 659 | #[test] |
| 660 | fn a_non_ascii_title_passes_through_unchanged() { |
| 661 | |
| 662 | |
| 663 | assert_eq!( |
| 664 | escape("Übergrößenträger — 日本語 · café"), |
| 665 | "Übergrößenträger — 日本語 · café" |
| 666 | ); |
| 667 | } |
| 668 | |
| 669 | #[test] |
| 670 | fn characters_xml_cannot_carry_are_dropped_rather_than_escaped() { |
| 671 | |
| 672 | |
| 673 | assert_eq!(escape("before\u{0}\u{7}after"), "beforeafter"); |
| 674 | assert_eq!( |
| 675 | escape("keep\tthe\nreal\rwhitespace"), |
| 676 | "keep\tthe\nreal\rwhitespace" |
| 677 | ); |
| 678 | } |
| 679 | |
| 680 | #[test] |
| 681 | fn timestamps_are_rfc_3339_in_utc() { |
| 682 | assert_eq!( |
| 683 | rfc3339(UNIX_EPOCH + std::time::Duration::from_secs(1_700_000_000)), |
| 684 | "2023-11-14T22:13:20Z" |
| 685 | ); |
| 686 | assert_eq!(rfc3339(UNIX_EPOCH), "1970-01-01T00:00:00Z"); |
| 687 | } |
| 688 | |
| 689 | fn feed_with(entries: Vec<Entry>) -> Feed { |
| 690 | Feed { |
| 691 | id: "https://example.com/ada/repos/steid/commits.atom".to_owned(), |
| 692 | title: "ada/steid commits".to_owned(), |
| 693 | self_url: "https://example.com/ada/repos/steid/commits.atom".to_owned(), |
| 694 | alternate: "https://example.com/ada/repos/steid".to_owned(), |
| 695 | author: "ada".to_owned(), |
| 696 | updated: entries.first().map(|entry| entry.updated), |
| 697 | entries, |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | #[test] |
| 702 | fn an_empty_feed_is_still_a_document_with_an_updated() { |
| 703 | let rendered = feed_with(Vec::new()).render(); |
| 704 | |
| 705 | assert!(rendered.starts_with("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<feed")); |
| 706 | assert!(rendered.contains("<updated>1970-01-01T00:00:00Z</updated>")); |
| 707 | assert!(rendered.ends_with("</feed>\n")); |
| 708 | assert!(!rendered.contains("<entry>")); |
| 709 | } |
| 710 | |
| 711 | #[test] |
| 712 | fn every_value_in_an_entry_is_escaped() { |
| 713 | let rendered = feed_with(vec![Entry { |
| 714 | id: "https://example.com/x?a=1&b=2".to_owned(), |
| 715 | title: "fix <T> & co".to_owned(), |
| 716 | link: "https://example.com/ada/repos/steid/tree/feat%2F<x>".to_owned(), |
| 717 | updated: UNIX_EPOCH + std::time::Duration::from_secs(86_400), |
| 718 | author: Some("Ada & Co".to_owned()), |
| 719 | content: Some(Content::Html("<p>a & b</p>".to_owned())), |
| 720 | }]) |
| 721 | .render(); |
| 722 | |
| 723 | assert!(rendered.contains("<title>fix <T> & co</title>")); |
| 724 | assert!(rendered.contains("<id>https://example.com/x?a=1&b=2</id>")); |
| 725 | assert!( |
| 726 | rendered.contains("href=\"https://example.com/ada/repos/steid/tree/feat%2F<x>\"") |
| 727 | ); |
| 728 | assert!(rendered.contains("<name>Ada & Co</name>")); |
| 729 | assert!( |
| 730 | rendered.contains("<content type=\"html\"><p>a &amp; b</p></content>"), |
| 731 | "rendered markdown is escaped once more, which is what type=\"html\" means" |
| 732 | ); |
| 733 | assert!(rendered.contains("<updated>1970-01-02T00:00:00Z</updated>")); |
| 734 | } |
| 735 | |
| 736 | #[test] |
| 737 | fn only_the_three_page_shapes_advertise_a_feed() { |
| 738 | assert_eq!( |
| 739 | discovered("/ada"), |
| 740 | Some(("/ada/feed.atom".to_owned(), "@ada activity".to_owned())) |
| 741 | ); |
| 742 | assert_eq!( |
| 743 | discovered("/ada/repos/steid"), |
| 744 | Some(( |
| 745 | "/ada/repos/steid/commits.atom".to_owned(), |
| 746 | "ada/steid commits".to_owned() |
| 747 | )) |
| 748 | ); |
| 749 | assert_eq!( |
| 750 | discovered("/ada/repos/steid/issues"), |
| 751 | Some(( |
| 752 | "/ada/repos/steid/issues.atom".to_owned(), |
| 753 | "ada/steid issues".to_owned() |
| 754 | )) |
| 755 | ); |
| 756 | } |
| 757 | |
| 758 | #[test] |
| 759 | fn nothing_else_advertises_one() { |
| 760 | for path in [ |
| 761 | "/", |
| 762 | "/ada/repos", |
| 763 | "/ada/settings", |
| 764 | "/ada/repos/new", |
| 765 | "/ada/repos/steid/log", |
| 766 | "/ada/repos/steid/issues/4", |
| 767 | "/ada/repos/steid/tree/main", |
| 768 | "/not a handle", |
| 769 | ] { |
| 770 | assert_eq!(discovered(path), None, "{path} should advertise no feed"); |
| 771 | } |
| 772 | } |
| 773 | } |