notification_listing.rs 2.4 KB

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