]> git.notmuchmail.org Git - notmuch/blobdiff - database.cc
add_message: Pull the thread-stitching portion out into new _notmuch_database_link_me...
[notmuch] / database.cc
index b392914190bb2f6247d89af371e0a86d56301938..d94755780b88eebdce68303e5980417883807dbe 100644 (file)
@@ -124,8 +124,7 @@ _find_prefix (const char *name)
        if (strcmp (name, BOOLEAN_PREFIX_EXTERNAL[i].name) == 0)
            return BOOLEAN_PREFIX_EXTERNAL[i].prefix;
 
-    fprintf (stderr, "Internal error: No prefix exists for '%s'\n", name);
-    exit (1);
+    INTERNAL_ERROR ("No prefix exists for '%s'\n", name);
 
     return "";
 }
@@ -284,7 +283,7 @@ notmuch_database_find_message (notmuch_database_t *notmuch,
     if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
        return NULL;
 
-    return _notmuch_message_create (notmuch, notmuch, doc_id);
+    return _notmuch_message_create (notmuch, notmuch, doc_id, NULL);
 }
 
 /* Return one or more thread_ids, (as a GPtrArray of strings), for the
@@ -700,6 +699,70 @@ notmuch_database_get_timestamp (notmuch_database_t *notmuch, const char *key)
     return ret;
 }
 
+/* Given a (mostly empty) 'message' and its corresponding
+ * 'message_file' link it to existing threads in the database.
+ *
+ * We first looke at 'message_file' and its link-relevant headers
+ * (References and In-Reply-To) for message IDs. We also look in the
+ * database for existing message that reference 'message'.p
+ *
+ * The end result is to call _notmuch_message_add_thread_id with one
+ * or more thread IDs to which this message belongs, (including
+ * generating a new thread ID if necessary if the message doesn't
+ * connect to any existing threads).
+ */
+static notmuch_status_t
+_notmuch_database_link_message (notmuch_database_t *notmuch,
+                               notmuch_message_t *message,
+                               notmuch_message_file_t *message_file)
+{
+    GPtrArray *parents, *thread_ids;
+    const char *refs, *in_reply_to;
+    const char *message_id = notmuch_message_get_message_id (message);
+    unsigned int i;
+
+    parents = g_ptr_array_new ();
+
+    refs = notmuch_message_file_get_header (message_file, "references");
+    parse_references (parents, refs);
+
+    in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to");
+    parse_references (parents, in_reply_to);
+
+    for (i = 0; i < parents->len; i++)
+       _notmuch_message_add_term (message, "ref",
+                                  (char *) g_ptr_array_index (parents, i));
+
+    thread_ids = find_thread_ids (notmuch, parents, message_id);
+
+    for (i = 0; i < parents->len; i++)
+       g_free (g_ptr_array_index (parents, i));
+    g_ptr_array_free (parents, TRUE);
+
+    if (thread_ids->len) {
+       GString *thread_id;
+       char *id;
+
+       for (i = 0; i < thread_ids->len; i++) {
+           id = (char *) thread_ids->pdata[i];
+           _notmuch_message_add_thread_id (message, id);
+           if (i == 0)
+               thread_id = g_string_new (id);
+           else
+               g_string_append_printf (thread_id, ",%s", id);
+
+           free (id);
+       }
+       g_string_free (thread_id, TRUE);
+    } else {
+       _notmuch_message_ensure_thread_id (message);
+    }
+
+    g_ptr_array_free (thread_ids, TRUE);
+
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 notmuch_status_t
 notmuch_database_add_message (notmuch_database_t *notmuch,
                              const char *filename)
@@ -708,14 +771,10 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
     notmuch_message_t *message;
     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
 
-    GPtrArray *parents, *thread_ids;
-
-    const char *refs, *in_reply_to, *date, *header;
+    const char *date, *header;
     const char *from, *to, *subject, *old_filename;
     char *message_id;
 
-    unsigned int i;
-
     message_file = notmuch_message_file_open (filename);
     if (message_file == NULL) {
        ret = NOTMUCH_STATUS_FILE_ERROR;
@@ -762,17 +821,14 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
         * database). */
 
        /* Use NULL for owner since we want to free this locally. */
+       message = _notmuch_message_create_for_message_id (NULL,
+                                                         notmuch,
+                                                         message_id,
+                                                         &ret);
+       free (message_id);
 
-       /* XXX: This call can fail by either out-of-memory or an
-        * "impossible" Xapian exception. We should rewrite it to
-        * allow us to propagate the error status. */
-       message = _notmuch_message_create_for_message_id (NULL, notmuch,
-                                                         message_id);
-       if (message == NULL) {
-           fprintf (stderr, "Internal error. This shouldn't happen.\n\n");
-           fprintf (stderr, "I mean, it's possible you ran out of memory, but then this code path is still an internal error since it should have detected that and propagated the status value up the stack.\n");
-           exit (1);
-       }
+       if (message == NULL)
+           goto DONE;
 
        /* Has a message previously been added with the same ID? */
        old_filename = notmuch_message_get_filename (message);
@@ -784,48 +840,9 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
            _notmuch_message_add_term (message, "type", "mail");
        }
 
-       /* Next, find the thread(s) to which this message belongs. */
-       parents = g_ptr_array_new ();
-
-       refs = notmuch_message_file_get_header (message_file, "references");
-       parse_references (parents, refs);
-
-       in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to");
-       parse_references (parents, in_reply_to);
-
-       for (i = 0; i < parents->len; i++)
-           _notmuch_message_add_term (message, "ref",
-                                      (char *) g_ptr_array_index (parents, i));
-
-       thread_ids = find_thread_ids (notmuch, parents, message_id);
-
-       free (message_id);
-
-       for (i = 0; i < parents->len; i++)
-           g_free (g_ptr_array_index (parents, i));
-       g_ptr_array_free (parents, TRUE);
-
-       if (thread_ids->len) {
-           unsigned int i;
-           GString *thread_id;
-           char *id;
-
-           for (i = 0; i < thread_ids->len; i++) {
-               id = (char *) thread_ids->pdata[i];
-               _notmuch_message_add_thread_id (message, id);
-               if (i == 0)
-                   thread_id = g_string_new (id);
-               else
-                   g_string_append_printf (thread_id, ",%s", id);
-
-               free (id);
-           }
-           g_string_free (thread_id, TRUE);
-       } else {
-           _notmuch_message_ensure_thread_id (message);
-       }
-
-       g_ptr_array_free (thread_ids, TRUE);
+       ret = _notmuch_database_link_message (notmuch, message, message_file);
+       if (ret)
+           goto DONE;
 
        date = notmuch_message_file_get_header (message_file, "date");
        _notmuch_message_set_date (message, date);