Forráskód Böngészése

Add notification listing page

Mark Rousskov 5 éve
szülő
commit
984aa71468
4 módosított fájl, 95 hozzáadás és 1 törlés
  1. 39 0
      src/db/notifications.rs
  2. 1 0
      src/lib.rs
  3. 21 1
      src/main.rs
  4. 34 0
      src/notification_listing.rs

+ 39 - 0
src/db/notifications.rs

@@ -35,3 +35,42 @@ pub async fn delete_ping(db: &DbClient, user_id: i64, origin_url: &str) -> anyho
 
     Ok(())
 }
+
+#[derive(Debug, serde::Serialize)]
+pub struct NotificationData {
+    pub origin_url: String,
+    pub origin_text: String,
+    pub time: DateTime<FixedOffset>,
+}
+
+pub async fn get_notifications(
+    db: &DbClient,
+    username: &str,
+) -> anyhow::Result<Vec<NotificationData>> {
+    let notifications = db
+        .query(
+            "
+        select username, origin_url, origin_html, time from notifications
+        join users on notifications.user_id = users.user_id
+        where username = $1
+        order by time desc;",
+            &[&username],
+        )
+        .await
+        .context("Getting notification data")?;
+
+    let mut data = Vec::new();
+    for notification in notifications {
+        let origin_url: String = notification.get(1);
+        let origin_text: String = notification.get(2);
+        let time: DateTime<FixedOffset> = notification.get(3);
+
+        data.push(NotificationData {
+            origin_url,
+            origin_text,
+            time,
+        });
+    }
+
+    Ok(data)
+}

+ 1 - 0
src/lib.rs

@@ -10,6 +10,7 @@ pub mod db;
 pub mod github;
 pub mod handlers;
 pub mod interactions;
+pub mod notification_listing;
 pub mod payload;
 pub mod team;
 

+ 21 - 1
src/main.rs

@@ -7,7 +7,7 @@ use native_tls::{Certificate, TlsConnector};
 use postgres_native_tls::MakeTlsConnector;
 use reqwest::Client;
 use std::{env, net::SocketAddr, sync::Arc};
-use triagebot::{db, github, handlers::Context, payload, EventName};
+use triagebot::{db, github, handlers::Context, notification_listing, payload, EventName};
 use uuid::Uuid;
 
 mod logger;
@@ -21,6 +21,26 @@ async fn serve_req(req: Request<Body>, ctx: Arc<Context>) -> Result<Response<Bod
             .body(Body::from("Triagebot is awaiting triage."))
             .unwrap());
     }
+    if req.uri.path() == "/notifications" {
+        if let Some(query) = req.uri.query() {
+            let user = url::form_urlencoded::parse(query.as_bytes()).find(|(k, _)| k == "user");
+            if let Some((_, name)) = user {
+                return Ok(Response::builder()
+                    .status(StatusCode::OK)
+                    .body(Body::from(
+                        notification_listing::render(&ctx.db, &*name).await,
+                    ))
+                    .unwrap());
+            }
+        }
+
+        return Ok(Response::builder()
+            .status(StatusCode::OK)
+            .body(Body::from(String::from(
+                "Please provide `?user=<username>` query param on URL.",
+            )))
+            .unwrap());
+    }
     if req.uri.path() != "/github-hook" {
         return Ok(Response::builder()
             .status(StatusCode::NOT_FOUND)

+ 34 - 0
src/notification_listing.rs

@@ -0,0 +1,34 @@
+use crate::db::notifications::get_notifications;
+use crate::db::DbClient;
+
+pub async fn render(db: &DbClient, user: &str) -> String {
+    let notifications = match get_notifications(db, user).await {
+        Ok(n) => n,
+        Err(e) => {
+            return format!("{:?}", e.context("getting notifications"));
+        }
+    };
+
+    let mut out = String::new();
+    out.push_str("<html>");
+    out.push_str("<head>");
+    out.push_str("<title>Triagebot Notification Data</title>");
+    out.push_str("</head>");
+    out.push_str("<body>");
+
+    out.push_str(&format!("<h3>Pending notifications for {}</h3>", user));
+
+    out.push_str("<ul>");
+    for notification in notifications {
+        out.push_str(&format!(
+            "<li><a href='{}'>{}</a></li>",
+            notification.origin_url, notification.origin_url,
+        ));
+    }
+    out.push_str("</ul>");
+
+    out.push_str("</body>");
+    out.push_str("</html>");
+
+    out
+}