]> git.notmuchmail.org Git - notmuch/blobdiff - lib/thread.cc
lib: Clean up error handling in _notmuch_thread_create
[notmuch] / lib / thread.cc
index ace5ce7fd178fbde7358982bacb7c20034a378d2..aed87b1e61d009288b905dc2d872647e2b1f0b0a 100644 (file)
@@ -24,7 +24,7 @@
 #include <gmime/gmime.h>
 #include <glib.h> /* GHashTable */
 
-struct _notmuch_thread {
+struct visible _notmuch_thread {
     notmuch_database_t *notmuch;
     char *thread_id;
     char *subject;
@@ -214,7 +214,8 @@ _thread_cleanup_author (notmuch_thread_t *thread,
  */
 static void
 _thread_add_message (notmuch_thread_t *thread,
-                    notmuch_message_t *message)
+                    notmuch_message_t *message,
+                    notmuch_string_list_t *exclude_terms)
 {
     notmuch_tags_t *tags;
     const char *tag;
@@ -262,6 +263,15 @@ _thread_add_message (notmuch_thread_t *thread,
         notmuch_tags_move_to_next (tags))
     {
        tag = notmuch_tags_get (tags);
+       /* Mark excluded messages. */
+       for (notmuch_string_node_t *term = exclude_terms->head; term;
+            term = term->next) {
+           /* We ignore initial 'K'. */
+           if (strcmp(tag, (term->string + 1)) == 0) {
+               notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, TRUE);
+               break;
+           }
+       }
        g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
     }
 }
@@ -321,7 +331,8 @@ _thread_add_matched_message (notmuch_thread_t *thread,
            _thread_set_subject_from_message (thread, message);
     }
 
-    thread->matched_messages++;
+    if (!notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED))
+       thread->matched_messages++;
 
     if (g_hash_table_lookup_extended (thread->message_hash,
                            notmuch_message_get_message_id (message), NULL,
@@ -392,9 +403,11 @@ _notmuch_thread_create (void *ctx,
                        notmuch_database_t *notmuch,
                        unsigned int seed_doc_id,
                        notmuch_doc_id_set_t *match_set,
+                       notmuch_string_list_t *exclude_terms,
                        notmuch_sort_t sort)
 {
-    notmuch_thread_t *thread;
+    void *local = talloc_new (ctx);
+    notmuch_thread_t *thread = NULL;
     notmuch_message_t *seed_message;
     const char *thread_id;
     char *thread_id_query_string;
@@ -403,24 +416,23 @@ _notmuch_thread_create (void *ctx,
     notmuch_messages_t *messages;
     notmuch_message_t *message;
 
-    seed_message = _notmuch_message_create (ctx, notmuch, seed_doc_id, NULL);
+    seed_message = _notmuch_message_create (local, notmuch, seed_doc_id, NULL);
     if (! seed_message)
        INTERNAL_ERROR ("Thread seed message %u does not exist", seed_doc_id);
 
     thread_id = notmuch_message_get_thread_id (seed_message);
-    thread_id_query_string = talloc_asprintf (ctx, "thread:%s", thread_id);
+    thread_id_query_string = talloc_asprintf (local, "thread:%s", thread_id);
     if (unlikely (thread_id_query_string == NULL))
-       return NULL;
+       goto DONE;
 
-    thread_id_query = notmuch_query_create (notmuch, thread_id_query_string);
+    thread_id_query = talloc_steal (
+       local, notmuch_query_create (notmuch, thread_id_query_string));
     if (unlikely (thread_id_query == NULL))
-       return NULL;
-
-    talloc_free (thread_id_query_string);
+       goto DONE;
 
-    thread = talloc (ctx, notmuch_thread_t);
+    thread = talloc (local, notmuch_thread_t);
     if (unlikely (thread == NULL))
-       return NULL;
+       goto DONE;
 
     talloc_set_destructor (thread, _notmuch_thread_destructor);
 
@@ -439,8 +451,10 @@ _notmuch_thread_create (void *ctx,
                                          free, NULL);
 
     thread->message_list = _notmuch_message_list_create (thread);
-    if (unlikely (thread->message_list == NULL))
-       return NULL;
+    if (unlikely (thread->message_list == NULL)) {
+       thread = NULL;
+       goto DONE;
+    }
 
     thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                  free, NULL);
@@ -467,7 +481,7 @@ _notmuch_thread_create (void *ctx,
        if (doc_id == seed_doc_id)
            message = seed_message;
 
-       _thread_add_message (thread, message);
+       _thread_add_message (thread, message, exclude_terms);
 
        if ( _notmuch_doc_id_set_contains (match_set, doc_id)) {
            _notmuch_doc_id_set_remove (match_set, doc_id);
@@ -477,12 +491,15 @@ _notmuch_thread_create (void *ctx,
        _notmuch_message_close (message);
     }
 
-    notmuch_query_destroy (thread_id_query);
-
     _resolve_thread_authors_string (thread);
 
     _resolve_thread_relationships (thread);
 
+    /* Commit to returning thread. */
+    talloc_steal (ctx, thread);
+
+  DONE:
+    talloc_free (local);
     return thread;
 }