Browse Source

Store an optional short description

This describes a given notification better than the URL. If it's not available,
then the URL is used.
Mark Rousskov 5 years ago
parent
commit
2e17bbc20c
5 changed files with 22 additions and 6 deletions
  1. 1 0
      src/db.rs
  2. 9 4
      src/db/notifications.rs
  3. 1 1
      src/github.rs
  4. 6 0
      src/handlers/notification.rs
  5. 5 1
      src/notification_listing.rs

+ 1 - 0
src/db.rs

@@ -67,4 +67,5 @@ CREATE TABLE users (
     username TEXT NOT NULL
 );
 ",
+    "ALTER TABLE notifications ADD COLUMN short_description TEXT;",
 ];

+ 9 - 4
src/db/notifications.rs

@@ -7,6 +7,7 @@ pub struct Notification {
     pub username: String,
     pub origin_url: String,
     pub origin_html: String,
+    pub short_description: Option<String>,
     pub time: DateTime<FixedOffset>,
 }
 
@@ -18,8 +19,8 @@ pub async fn record_ping(db: &DbClient, notification: &Notification) -> anyhow::
     .await
     .context("inserting user id / username")?;
 
-    db.execute("INSERT INTO notifications (user_id, origin_url, origin_html, time) VALUES ($1, $2, $3, $4)",
-        &[&notification.user_id, &notification.origin_url, &notification.origin_html, &notification.time],
+    db.execute("INSERT INTO notifications (user_id, origin_url, origin_html, time, short_description) VALUES ($1, $2, $3, $4, $5)",
+        &[&notification.user_id, &notification.origin_url, &notification.origin_html, &notification.time, &notification.short_description],
         ).await.context("inserting notification")?;
 
     Ok(())
@@ -36,10 +37,11 @@ pub async fn delete_ping(db: &DbClient, user_id: i64, origin_url: &str) -> anyho
     Ok(())
 }
 
-#[derive(Debug, serde::Serialize)]
+#[derive(Debug)]
 pub struct NotificationData {
     pub origin_url: String,
     pub origin_text: String,
+    pub short_description: Option<String>,
     pub time: DateTime<FixedOffset>,
 }
 
@@ -50,7 +52,8 @@ pub async fn get_notifications(
     let notifications = db
         .query(
             "
-        select username, origin_url, origin_html, time from notifications
+        select username, origin_url, origin_html, time, short_description
+        from notifications
         join users on notifications.user_id = users.user_id
         where username = $1
         order by time desc;",
@@ -64,10 +67,12 @@ pub async fn get_notifications(
         let origin_url: String = notification.get(1);
         let origin_text: String = notification.get(2);
         let time: DateTime<FixedOffset> = notification.get(3);
+        let short_description: Option<String> = notification.get(4);
 
         data.push(NotificationData {
             origin_url,
             origin_text,
+            short_description,
             time,
         });
     }

+ 1 - 1
src/github.rs

@@ -124,7 +124,7 @@ pub struct Issue {
     pub number: u64,
     pub body: String,
     created_at: chrono::DateTime<Utc>,
-    title: String,
+    pub title: String,
     html_url: String,
     pub user: User,
     labels: Vec<Label>,

+ 6 - 0
src/handlers/notification.rs

@@ -76,6 +76,11 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
         }
     }
 
+    let short_description = match event {
+        Event::Issue(e) => e.issue.title.clone(),
+        Event::IssueComment(e) => format!("Comment on {}", e.issue.title),
+    };
+
     let caps = PING_RE
         .captures_iter(body)
         .map(|c| c.get(1).unwrap().as_str().to_owned())
@@ -104,6 +109,7 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
                 origin_url: event.html_url().unwrap().to_owned(),
                 origin_html: body.to_owned(),
                 time: event.time(),
+                short_description: Some(short_description.clone()),
             },
         )
         .await

+ 5 - 1
src/notification_listing.rs

@@ -22,7 +22,11 @@ pub async fn render(db: &DbClient, user: &str) -> String {
     for notification in notifications {
         out.push_str(&format!(
             "<li><a href='{}'>{}</a></li>",
-            notification.origin_url, notification.origin_url,
+            notification.origin_url,
+            notification
+                .short_description
+                .as_ref()
+                .unwrap_or(&notification.origin_url),
         ));
     }
     out.push_str("</ul>");