[PATCH] emacs: Function to query the list of labels from a thread-id

Subject: [PATCH] emacs: Function to query the list of labels from a thread-id

Date: Sun, 28 Oct 2012 20:11:18 +0100

To: notmuch@notmuchmail.org

Cc:

From: Damien Cassou


This patch adds `notmuch-query-thread-labels-from-id' and
corresponding test. This function returns the labels of thread whose
id is passed as argument. The thread labels are the union of the
labels of emails in the thread. I need this function to integrate
notmuch-labeler.

Signed-off-by: Damien Cassou <damien.cassou@gmail.com>
---
 emacs/notmuch-query.el |   16 ++++++++++++++++
 test/emacs             |   23 +++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/emacs/notmuch-query.el b/emacs/notmuch-query.el
index d66baea..8b9a41d 100644
--- a/emacs/notmuch-query.el
+++ b/emacs/notmuch-query.el
@@ -81,4 +81,20 @@ See the function notmuch-query-get-threads for more information."
    (lambda (msg) (plist-get msg :id))
    (notmuch-query-get-threads search-terms)))

+(defun notmuch-query-thread-labels-from-id (thread-id)
+  "Return the labels of thread whose id is THREAD-ID.
+The thread labels are the union of the labels of emails in the
+thread."
+  (let ((label-lists
+	 (notmuch-query-map-forest
+	  (lambda (msg) (plist-get msg :tags))
+	  (car (notmuch-query-get-threads
+		(list (concat "thread:" thread-id)))))))
+    (case (length label-lists)
+      (0 nil)
+      (1 (car label-lists))
+      (otherwise (reduce (lambda (l1 l2)
+			   (union l1 l2 :test 'string=))
+			 label-lists)))))
+
 (provide 'notmuch-query)
diff --git a/test/emacs b/test/emacs
index 44f641e..3e8bdb8 100755
--- a/test/emacs
+++ b/test/emacs
@@ -820,5 +820,28 @@ Date: Fri, 05 Jan 2001 15:43:57 +0000
 EOF
 test_expect_equal_file OUTPUT EXPECTED

+test_begin_subtest "Extracting all labels from a thread"
+add_message \
+    '[subject]="Extracting all labels from a thread"' \
+    '[body]="body 1"'
+first=${gen_msg_cnt}
+parent=${gen_msg_id}
+add_message \
+    '[subject]="Extracting all labels from a thread"' \
+    '[body]="body 2"' \
+    "[in-reply-to]=\<$parent\>"
+add_message \
+    '[subject]="Extracting all labels from a thread"' \
+    '[body]="body 3"' \
+    "[in-reply-to]=\<$parent\>"
+latest=${gen_msg_id}
+# Extract the thread-id from one of the emails
+thread_id=$(notmuch search id:${latest} | sed -e "s/thread:\([a-f0-9]*\).*/\1/")
+# Add tag "mytagfoo" to one of the emails
+notmuch tag +mytagfoo id:${latest}
+test_emacs_expect_t \
+    "(let ((output (notmuch-query-thread-labels-from-id \"${thread_id}\"))
+           (expected '(\"inbox\" \"mytagfoo\" \"unread\")))
+      (notmuch-test-expect-equal output expected))"

 test_done
--
1.7.10.4

Thread: