Prechádzať zdrojové kódy

Dump acknowledged notifications to Zulip

This is primarily useful when someone else is acknolwedging for you, in which
case you may want to know what they've deleted from your queue. Previously, that
was essentially impossible to find out, now you have a nice log.
Mark Rousskov 5 rokov pred
rodič
commit
eece170ddc
2 zmenil súbory, kde vykonal 65 pridanie a 21 odobranie
  1. 48 17
      src/db/notifications.rs
  2. 17 4
      src/zulip.rs

+ 48 - 17
src/db/notifications.rs

@@ -46,19 +46,36 @@ pub async fn delete_ping(
     db: &mut DbClient,
     user_id: i64,
     identifier: Identifier<'_>,
-) -> anyhow::Result<()> {
+) -> anyhow::Result<Vec<NotificationData>> {
     match identifier {
         Identifier::Url(origin_url) => {
-            let count = db
-                .execute(
-                    "DELETE FROM notifications WHERE user_id = $1 and origin_url = $2",
+            let rows = db
+                .query(
+                    "DELETE FROM notifications WHERE user_id = $1 and origin_url = $2
+                    RETURNING origin_html, time, short_description, metadata",
                     &[&user_id, &origin_url],
                 )
                 .await
                 .context("delete notification query")?;
-            if count == 0 {
-                anyhow::bail!("Did not delete any rows");
+            if rows.is_empty() {
+                anyhow::bail!("Did not delete any notifications");
             }
+            Ok(rows
+                .into_iter()
+                .map(|row| {
+                    let origin_text: String = row.get(0);
+                    let time: DateTime<FixedOffset> = row.get(1);
+                    let short_description: Option<String> = row.get(2);
+                    let metadata: Option<String> = row.get(3);
+                    NotificationData {
+                        origin_url: origin_url.to_owned(),
+                        origin_text,
+                        time,
+                        short_description,
+                        metadata,
+                    }
+                })
+                .collect())
         }
         Identifier::Index(idx) => loop {
             let t = db
@@ -84,15 +101,30 @@ pub async fn delete_ping(
                 .ok_or_else(|| anyhow::anyhow!("No such notification with index {}", idx.get()))?
                 .get(0);
 
-            t.execute(
-                "DELETE FROM notifications WHERE notification_id = $1",
-                &[&notification_id],
-            )
-            .await
-            .context(format!(
-                "Failed to delete notification with id {}",
-                notification_id
-            ))?;
+            let row = t
+                .query_one(
+                    "DELETE FROM notifications WHERE notification_id = $1
+                RETURNING origin_url, origin_html, time, short_description, metadata",
+                    &[&notification_id],
+                )
+                .await
+                .context(format!(
+                    "Failed to delete notification with id {}",
+                    notification_id
+                ))?;
+
+            let origin_url: String = row.get(0);
+            let origin_text: String = row.get(1);
+            let time: DateTime<FixedOffset> = row.get(2);
+            let short_description: Option<String> = row.get(3);
+            let metadata: Option<String> = row.get(4);
+            let deleted_notification = NotificationData {
+                origin_url,
+                origin_text,
+                time,
+                short_description,
+                metadata,
+            };
 
             if let Err(e) = t.commit().await {
                 if e.code().map_or(false, |c| {
@@ -104,11 +136,10 @@ pub async fn delete_ping(
                     return Err(e).context("transaction commit failure");
                 }
             } else {
-                break;
+                return Ok(vec![deleted_notification]);
             }
         },
     }
-    Ok(())
 }
 
 #[derive(Debug)]

+ 17 - 4
src/zulip.rs

@@ -332,10 +332,23 @@ async fn acknowledge(
     )
     .await
     {
-        Ok(()) => Ok(serde_json::to_string(&Response {
-            content: &format!("Acknowledged {}.", url),
-        })
-        .unwrap()),
+        Ok(deleted) => {
+            let mut resp = format!("Acknowledged:\n");
+            for deleted in deleted {
+                resp.push_str(&format!(
+                    " * [{}]({}){}\n",
+                    deleted
+                        .short_description
+                        .as_deref()
+                        .unwrap_or(&deleted.origin_url),
+                    deleted.origin_url,
+                    deleted
+                        .metadata
+                        .map_or(String::new(), |m| format!(" ({})", m)),
+                ));
+            }
+            Ok(serde_json::to_string(&Response { content: &resp }).unwrap())
+        }
         Err(e) => Ok(serde_json::to_string(&Response {
             content: &format!("Failed to acknowledge {}: {:?}.", url, e),
         })