notification_listing.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. use crate::db::notifications::get_notifications;
  2. use crate::db::DbClient;
  3. pub async fn render(db: &DbClient, user: &str) -> String {
  4. let notifications = match get_notifications(db, user).await {
  5. Ok(n) => n,
  6. Err(e) => {
  7. return format!("{:?}", e.context("getting notifications"));
  8. }
  9. };
  10. let mut out = String::new();
  11. out.push_str("<html>");
  12. out.push_str("<head>");
  13. out.push_str("<meta charset=\"utf-8\">");
  14. out.push_str("<title>Triagebot Notification Data</title>");
  15. out.push_str("</head>");
  16. out.push_str("<body>");
  17. out.push_str(&format!("<h3>Pending notifications for {}</h3>", user));
  18. if notifications.is_empty() {
  19. out.push_str("<p><em>You have no pending notifications! :)</em></p>");
  20. } else {
  21. out.push_str("<ol>");
  22. for notification in notifications {
  23. out.push_str("<li>");
  24. out.push_str(&format!(
  25. "<a href='{}'>{}</a>",
  26. notification.origin_url,
  27. notification
  28. .short_description
  29. .as_ref()
  30. .unwrap_or(&notification.origin_url)
  31. .replace('&', "&amp;")
  32. .replace('<', "&lt;")
  33. .replace('>', "&gt;")
  34. .replace('"', "&quot;")
  35. .replace('\'', "&#39;"),
  36. ));
  37. if let Some(metadata) = &notification.metadata {
  38. out.push_str(&format!(
  39. "<ul><li>{}</li></ul>",
  40. metadata
  41. .replace('&', "&amp;")
  42. .replace('<', "&lt;")
  43. .replace('>', "&gt;")
  44. .replace('"', "&quot;")
  45. .replace('\'', "&#39;"),
  46. ));
  47. }
  48. out.push_str("</li>");
  49. }
  50. out.push_str("</ol>");
  51. out.push_str(
  52. "<p><em>You can acknowledge a notification by sending </em><code>ack &lt;idx&gt;</code><em> \
  53. to </em><strong><code>@triagebot</code></strong><em> on Zulip, or you can acknowledge \
  54. all notifications by sending </em><code>ack all</code><em>. Read about the other notification commands \
  55. <a href=\"https://forge.rust-lang.org/platforms/zulip/triagebot.html#issue-notifications\">here</a>.</em></p>"
  56. );
  57. }
  58. out.push_str("</body>");
  59. out.push_str("</html>");
  60. out
  61. }