]> git.notmuchmail.org Git - notmuch/blob - lib/thread.cc
Merge tag 'debian/0.29.2-2'
[notmuch] / lib / thread.cc
1 /* thread.cc - Results of thread-based searches from a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see https://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22 #include "database-private.h"
23
24 #include <gmime/gmime.h>
25 #include <glib.h>                                       /* GHashTable */
26
27 #ifdef DEBUG_THREADING
28 #define THREAD_DEBUG(format, ...) fprintf (stderr, format " (%s).\n", ##__VA_ARGS__, __location__)
29 #else
30 #define THREAD_DEBUG(format, ...) do {} while (0)       /* ignored */
31 #endif
32
33 struct _notmuch_thread {
34     notmuch_database_t *notmuch;
35     char *thread_id;
36     char *subject;
37     GHashTable *authors_hash;
38     GPtrArray *authors_array;
39     GHashTable *matched_authors_hash;
40     GPtrArray *matched_authors_array;
41     char *authors;
42     GHashTable *tags;
43
44     /* All messages, oldest first. */
45     notmuch_message_list_t *message_list;
46     /* Top-level messages, oldest first. */
47     notmuch_message_list_t *toplevel_list;
48
49     GHashTable *message_hash;
50     int total_messages;
51     int total_files;
52     int matched_messages;
53     time_t oldest;
54     time_t newest;
55 };
56
57 static int
58 _notmuch_thread_destructor (notmuch_thread_t *thread)
59 {
60     g_hash_table_unref (thread->authors_hash);
61     g_hash_table_unref (thread->matched_authors_hash);
62     g_hash_table_unref (thread->tags);
63     g_hash_table_unref (thread->message_hash);
64
65     if (thread->authors_array) {
66         g_ptr_array_free (thread->authors_array, true);
67         thread->authors_array = NULL;
68     }
69
70     if (thread->matched_authors_array) {
71         g_ptr_array_free (thread->matched_authors_array, true);
72         thread->matched_authors_array = NULL;
73     }
74
75     return 0;
76 }
77
78 /* Add each author of the thread to the thread's authors_hash and to
79  * the thread's authors_array. */
80 static void
81 _thread_add_author (notmuch_thread_t *thread,
82                     const char *author)
83 {
84     char *author_copy;
85
86     if (author == NULL)
87         return;
88
89     if (g_hash_table_lookup_extended (thread->authors_hash,
90                                       author, NULL, NULL))
91         return;
92
93     author_copy = talloc_strdup (thread, author);
94
95     g_hash_table_insert (thread->authors_hash, author_copy, NULL);
96
97     g_ptr_array_add (thread->authors_array, author_copy);
98 }
99
100 /* Add each matched author of the thread to the thread's
101  * matched_authors_hash and to the thread's matched_authors_array. */
102 static void
103 _thread_add_matched_author (notmuch_thread_t *thread,
104                             const char *author)
105 {
106     char *author_copy;
107
108     if (author == NULL)
109         return;
110
111     if (g_hash_table_lookup_extended (thread->matched_authors_hash,
112                                       author, NULL, NULL))
113         return;
114
115     author_copy = talloc_strdup (thread, author);
116
117     g_hash_table_insert (thread->matched_authors_hash, author_copy, NULL);
118
119     g_ptr_array_add (thread->matched_authors_array, author_copy);
120 }
121
122 /* Construct an authors string from matched_authors_array and
123  * authors_array. The string contains matched authors first, then
124  * non-matched authors (with the two groups separated by '|'). Within
125  * each group, authors are listed in date order. */
126 static void
127 _resolve_thread_authors_string (notmuch_thread_t *thread)
128 {
129     unsigned int i;
130     char *author;
131     int first_non_matched_author = 1;
132
133     /* First, list all matched authors in date order. */
134     for (i = 0; i < thread->matched_authors_array->len; i++) {
135         author = (char *) g_ptr_array_index (thread->matched_authors_array, i);
136         if (thread->authors)
137             thread->authors = talloc_asprintf (thread, "%s, %s",
138                                                thread->authors,
139                                                author);
140         else
141             thread->authors = author;
142     }
143
144     /* Next, append any non-matched authors that haven't already appeared. */
145     for (i = 0; i < thread->authors_array->len; i++) {
146         author = (char *) g_ptr_array_index (thread->authors_array, i);
147         if (g_hash_table_lookup_extended (thread->matched_authors_hash,
148                                           author, NULL, NULL))
149             continue;
150         if (first_non_matched_author) {
151             thread->authors = talloc_asprintf (thread, "%s| %s",
152                                                thread->authors,
153                                                author);
154         } else {
155             thread->authors = talloc_asprintf (thread, "%s, %s",
156                                                thread->authors,
157                                                author);
158         }
159
160         first_non_matched_author = 0;
161     }
162
163     g_ptr_array_free (thread->authors_array, true);
164     thread->authors_array = NULL;
165     g_ptr_array_free (thread->matched_authors_array, true);
166     thread->matched_authors_array = NULL;
167
168     if (! thread->authors)
169         thread->authors = talloc_strdup (thread, "");
170 }
171
172 /* clean up the ugly "Lastname, Firstname" format that some mail systems
173  * (most notably, Exchange) are creating to be "Firstname Lastname"
174  * To make sure that we don't change other potential situations where a
175  * comma is in the name, we check that we match one of these patterns
176  * "Last, First" <first.last@company.com>
177  * "Last, First MI" <first.mi.last@company.com>
178  */
179 static char *
180 _thread_cleanup_author (notmuch_thread_t *thread,
181                         const char *author, const char *from)
182 {
183     char *clean_author, *test_author;
184     const char *comma;
185     char *blank;
186     int fname, lname;
187
188     if (author == NULL)
189         return NULL;
190     clean_author = talloc_strdup (thread, author);
191     if (clean_author == NULL)
192         return NULL;
193     /* check if there's a comma in the name and that there's a
194      * component of the name behind it (so the name doesn't end with
195      * the comma - in which case the string that strchr finds is just
196      * one character long ",\0").
197      * Otherwise just return the copy of the original author name that
198      * we just made*/
199     comma = strchr (author, ',');
200     if (comma && strlen (comma) > 1) {
201         /* let's assemble what we think is the correct name */
202         lname = comma - author;
203
204         /* Skip all the spaces after the comma */
205         fname = strlen (author) - lname - 1;
206         comma += 1;
207         while (*comma == ' ') {
208             fname -= 1;
209             comma += 1;
210         }
211         strncpy (clean_author, comma, fname);
212
213         *(clean_author + fname) = ' ';
214         strncpy (clean_author + fname + 1, author, lname);
215         *(clean_author + fname + 1 + lname) = '\0';
216         /* make a temporary copy and see if it matches the email */
217         test_author = talloc_strdup (thread, clean_author);
218
219         blank = strchr (test_author, ' ');
220         while (blank != NULL) {
221             *blank = '.';
222             blank = strchr (test_author, ' ');
223         }
224         if (strcasestr (from, test_author) == NULL)
225             /* we didn't identify this as part of the email address
226              * so let's punt and return the original author */
227             strcpy (clean_author, author);
228     }
229     return clean_author;
230 }
231
232 /* Add 'message' as a message that belongs to 'thread'.
233  *
234  * The 'thread' will talloc_steal the 'message' and hold onto a
235  * reference to it.
236  */
237 static void
238 _thread_add_message (notmuch_thread_t *thread,
239                      notmuch_message_t *message,
240                      notmuch_string_list_t *exclude_terms,
241                      notmuch_exclude_t omit_exclude)
242 {
243     notmuch_tags_t *tags;
244     const char *tag;
245     InternetAddressList *list = NULL;
246     InternetAddress *address;
247     const char *from, *author;
248     char *clean_author;
249     bool message_excluded = false;
250
251     if (omit_exclude != NOTMUCH_EXCLUDE_FALSE) {
252         for (tags = notmuch_message_get_tags (message);
253              notmuch_tags_valid (tags);
254              notmuch_tags_move_to_next (tags)) {
255             tag = notmuch_tags_get (tags);
256             /* Is message excluded? */
257             for (notmuch_string_node_t *term = exclude_terms->head;
258                  term != NULL;
259                  term = term->next) {
260                 /* Check for an empty string, and then ignore initial 'K'. */
261                 if (*(term->string) && strcmp (tag, (term->string + 1)) == 0) {
262                     message_excluded = true;
263                     break;
264                 }
265             }
266         }
267     }
268
269     if (message_excluded && omit_exclude == NOTMUCH_EXCLUDE_ALL)
270         return;
271
272     _notmuch_message_list_add_message (thread->message_list,
273                                        talloc_steal (thread, message));
274     thread->total_messages++;
275     thread->total_files += notmuch_message_count_files (message);
276
277     g_hash_table_insert (thread->message_hash,
278                          xstrdup (notmuch_message_get_message_id (message)),
279                          message);
280
281     from = notmuch_message_get_header (message, "from");
282     if (from)
283         list = internet_address_list_parse (NULL, from);
284
285     if (list) {
286         address = internet_address_list_get_address (list, 0);
287         if (address) {
288             author = internet_address_get_name (address);
289             /* We treat quoted empty names as if they were empty. */
290             if (author == NULL || author[0] == '\0') {
291                 InternetAddressMailbox *mailbox;
292                 mailbox = INTERNET_ADDRESS_MAILBOX (address);
293                 author = internet_address_mailbox_get_addr (mailbox);
294             }
295             clean_author = _thread_cleanup_author (thread, author, from);
296             _thread_add_author (thread, clean_author);
297             _notmuch_message_set_author (message, clean_author);
298         }
299         g_object_unref (G_OBJECT (list));
300     }
301
302     if (! thread->subject) {
303         const char *subject;
304         subject = notmuch_message_get_header (message, "subject");
305         thread->subject = talloc_strdup (thread, subject ? subject : "");
306     }
307
308     for (tags = notmuch_message_get_tags (message);
309          notmuch_tags_valid (tags);
310          notmuch_tags_move_to_next (tags)) {
311         tag = notmuch_tags_get (tags);
312         g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
313     }
314
315     /* Mark excluded messages. */
316     if (message_excluded)
317         notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, true);
318 }
319
320 static void
321 _thread_set_subject_from_message (notmuch_thread_t *thread,
322                                   notmuch_message_t *message)
323 {
324     const char *subject;
325     const char *cleaned_subject;
326
327     subject = notmuch_message_get_header (message, "subject");
328     if (! subject)
329         return;
330
331     if ((strncasecmp (subject, "Re: ", 4) == 0) ||
332         (strncasecmp (subject, "Aw: ", 4) == 0) ||
333         (strncasecmp (subject, "Vs: ", 4) == 0) ||
334         (strncasecmp (subject, "Sv: ", 4) == 0)) {
335
336         cleaned_subject = talloc_strndup (thread,
337                                           subject + 4,
338                                           strlen (subject) - 4);
339     } else {
340         cleaned_subject = talloc_strdup (thread, subject);
341     }
342
343     if (! EMPTY_STRING (cleaned_subject)) {
344         if (thread->subject)
345             talloc_free (thread->subject);
346
347         thread->subject = talloc_strdup (thread, cleaned_subject);
348     }
349 }
350
351 /* Add a message to this thread which is known to match the original
352  * search specification. The 'sort' parameter controls whether the
353  * oldest or newest matching subject is applied to the thread as a
354  * whole. */
355 static void
356 _thread_add_matched_message (notmuch_thread_t *thread,
357                              notmuch_message_t *message,
358                              notmuch_sort_t sort)
359 {
360     time_t date;
361     notmuch_message_t *hashed_message;
362
363     date = notmuch_message_get_date (message);
364
365     if (date < thread->oldest || ! thread->matched_messages) {
366         thread->oldest = date;
367         if (sort == NOTMUCH_SORT_OLDEST_FIRST)
368             _thread_set_subject_from_message (thread, message);
369     }
370
371     if (date > thread->newest || ! thread->matched_messages) {
372         thread->newest = date;
373         const char *cur_subject = notmuch_thread_get_subject (thread);
374         if (sort != NOTMUCH_SORT_OLDEST_FIRST || EMPTY_STRING (cur_subject))
375             _thread_set_subject_from_message (thread, message);
376     }
377
378     if (! notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED))
379         thread->matched_messages++;
380
381     if (g_hash_table_lookup_extended (thread->message_hash,
382                                       notmuch_message_get_message_id (message), NULL,
383                                       (void **) &hashed_message)) {
384         notmuch_message_set_flag (hashed_message,
385                                   NOTMUCH_MESSAGE_FLAG_MATCH, 1);
386     }
387
388     _thread_add_matched_author (thread, _notmuch_message_get_author (hashed_message));
389 }
390
391 static bool
392 _parent_via_in_reply_to (notmuch_thread_t *thread, notmuch_message_t *message)
393 {
394     notmuch_message_t *parent;
395     const char *in_reply_to;
396
397     in_reply_to = _notmuch_message_get_in_reply_to (message);
398     THREAD_DEBUG ("checking message = %s in_reply_to=%s\n",
399                   notmuch_message_get_message_id (message), in_reply_to);
400
401     if (in_reply_to && (! EMPTY_STRING (in_reply_to)) &&
402         g_hash_table_lookup_extended (thread->message_hash,
403                                       in_reply_to, NULL,
404                                       (void **) &parent)) {
405         _notmuch_message_add_reply (parent, message);
406         return true;
407     } else {
408         return false;
409     }
410 }
411
412 static void
413 _parent_or_toplevel (notmuch_thread_t *thread, notmuch_message_t *message)
414 {
415     size_t max_depth = 0;
416     notmuch_message_t *new_parent;
417     notmuch_message_t *parent = NULL;
418     const notmuch_string_list_t *references =
419         _notmuch_message_get_references (message);
420
421     THREAD_DEBUG ("trying to reparent via references: %s\n",
422                   notmuch_message_get_message_id (message));
423
424     for (notmuch_string_node_t *ref_node = references->head;
425          ref_node; ref_node = ref_node->next) {
426         THREAD_DEBUG ("checking reference=%s\n", ref_node->string);
427         if ((g_hash_table_lookup_extended (thread->message_hash,
428                                            ref_node->string, NULL,
429                                            (void **) &new_parent))) {
430             size_t new_depth = _notmuch_message_get_thread_depth (new_parent);
431             THREAD_DEBUG ("got depth %lu\n", new_depth);
432             if (new_depth > max_depth || ! parent) {
433                 THREAD_DEBUG ("adding at depth %lu parent=%s\n", new_depth, ref_node->string);
434                 max_depth = new_depth;
435                 parent = new_parent;
436             }
437         }
438     }
439     if (parent) {
440         THREAD_DEBUG ("adding reply %s to parent=%s\n",
441                       notmuch_message_get_message_id (message),
442                       notmuch_message_get_message_id (parent));
443         _notmuch_message_add_reply (parent, message);
444     } else {
445         THREAD_DEBUG ("adding as toplevel %s\n",
446                       notmuch_message_get_message_id (message));
447         _notmuch_message_list_add_message (thread->toplevel_list, message);
448     }
449 }
450
451 static void
452 _resolve_thread_relationships (notmuch_thread_t *thread)
453 {
454     notmuch_message_node_t *node, *first_node;
455     notmuch_message_t *message;
456     void *local;
457     notmuch_message_list_t *maybe_toplevel_list;
458
459     first_node = thread->message_list->head;
460     if (! first_node)
461         return;
462
463     local = talloc_new (thread);
464     maybe_toplevel_list = _notmuch_message_list_create (local);
465
466     for (node = first_node->next; node; node = node->next) {
467         message = node->message;
468         if (! _parent_via_in_reply_to (thread, message))
469             _notmuch_message_list_add_message (maybe_toplevel_list, message);
470     }
471
472     /*
473      * if we reach the end of the list without finding a top-level
474      * message, that means the thread is a cycle (or set of cycles)
475      * and any message can be considered top-level.  Choose the oldest
476      * message, which happens to be first in our list.
477      */
478     if (first_node) {
479         message = first_node->message;
480         THREAD_DEBUG ("checking first message  %s\n",
481                       notmuch_message_get_message_id (message));
482
483         if (_notmuch_message_list_empty (maybe_toplevel_list) ||
484             ! _parent_via_in_reply_to (thread, message)) {
485
486             THREAD_DEBUG ("adding first message as toplevel = %s\n",
487                           notmuch_message_get_message_id (message));
488             _notmuch_message_list_add_message (maybe_toplevel_list, message);
489         }
490     }
491
492     for (notmuch_messages_t *messages = _notmuch_messages_create (maybe_toplevel_list);
493          notmuch_messages_valid (messages);
494          notmuch_messages_move_to_next (messages)) {
495         notmuch_message_t *message = notmuch_messages_get (messages);
496         _notmuch_message_label_depths (message, 0);
497     }
498
499     for (notmuch_messages_t *roots = _notmuch_messages_create (maybe_toplevel_list);
500          notmuch_messages_valid (roots);
501          notmuch_messages_move_to_next (roots)) {
502         notmuch_message_t *message = notmuch_messages_get (roots);
503         if (_notmuch_messages_has_next (roots) || ! _notmuch_message_list_empty (thread->toplevel_list))
504             _parent_or_toplevel (thread, message);
505         else
506             _notmuch_message_list_add_message (thread->toplevel_list, message);
507     }
508
509     /* XXX this could be made conditional on messages being inserted
510      * (out of order) in later passes
511      */
512     thread->toplevel_list = _notmuch_message_sort_subtrees (thread, thread->toplevel_list);
513
514     talloc_free (local);
515 }
516
517 /* Create a new notmuch_thread_t object by finding the thread
518  * containing the message with the given doc ID, treating any messages
519  * contained in match_set as "matched".  Remove all messages in the
520  * thread from match_set.
521  *
522  * Creating the thread will perform a database search to get all
523  * messages belonging to the thread and will get the first subject
524  * line, the total count of messages, and all authors in the thread.
525  * Each message in the thread is checked against match_set to allow
526  * for a separate count of matched messages, and to allow a viewer to
527  * display these messages differently.
528  *
529  * Here, 'ctx' is talloc context for the resulting thread object.
530  *
531  * This function returns NULL in the case of any error.
532  */
533 notmuch_thread_t *
534 _notmuch_thread_create (void *ctx,
535                         notmuch_database_t *notmuch,
536                         unsigned int seed_doc_id,
537                         notmuch_doc_id_set_t *match_set,
538                         notmuch_string_list_t *exclude_terms,
539                         notmuch_exclude_t omit_excluded,
540                         notmuch_sort_t sort)
541 {
542     void *local = talloc_new (ctx);
543     notmuch_thread_t *thread = NULL;
544     notmuch_message_t *seed_message;
545     const char *thread_id;
546     char *thread_id_query_string;
547     notmuch_query_t *thread_id_query;
548
549     notmuch_messages_t *messages;
550     notmuch_message_t *message;
551     notmuch_status_t status;
552
553     seed_message = _notmuch_message_create (local, notmuch, seed_doc_id, NULL);
554     if (! seed_message)
555         INTERNAL_ERROR ("Thread seed message %u does not exist", seed_doc_id);
556
557     thread_id = notmuch_message_get_thread_id (seed_message);
558     thread_id_query_string = talloc_asprintf (local, "thread:%s", thread_id);
559     if (unlikely (thread_id_query_string == NULL))
560         goto DONE;
561
562     thread_id_query = talloc_steal (
563         local, notmuch_query_create (notmuch, thread_id_query_string));
564     if (unlikely (thread_id_query == NULL))
565         goto DONE;
566
567     thread = talloc (local, notmuch_thread_t);
568     if (unlikely (thread == NULL))
569         goto DONE;
570
571     talloc_set_destructor (thread, _notmuch_thread_destructor);
572
573     thread->notmuch = notmuch;
574     thread->thread_id = talloc_strdup (thread, thread_id);
575     thread->subject = NULL;
576     thread->authors_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
577                                                   NULL, NULL);
578     thread->authors_array = g_ptr_array_new ();
579     thread->matched_authors_hash = g_hash_table_new_full (g_str_hash,
580                                                           g_str_equal,
581                                                           NULL, NULL);
582     thread->matched_authors_array = g_ptr_array_new ();
583     thread->authors = NULL;
584     thread->tags = g_hash_table_new_full (g_str_hash, g_str_equal,
585                                           free, NULL);
586
587     thread->message_list = _notmuch_message_list_create (thread);
588     thread->toplevel_list = _notmuch_message_list_create (thread);
589     if (unlikely (thread->message_list == NULL ||
590                   thread->toplevel_list == NULL)) {
591         thread = NULL;
592         goto DONE;
593     }
594
595     thread->message_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
596                                                   free, NULL);
597
598     thread->total_messages = 0;
599     thread->total_files = 0;
600     thread->matched_messages = 0;
601     thread->oldest = 0;
602     thread->newest = 0;
603
604     /* We use oldest-first order unconditionally here to obtain the
605      * proper author ordering for the thread. The 'sort' parameter
606      * passed to this function is used only to indicate whether the
607      * oldest or newest subject is desired. */
608     notmuch_query_set_sort (thread_id_query, NOTMUCH_SORT_OLDEST_FIRST);
609
610     status = notmuch_query_search_messages (thread_id_query, &messages);
611     if (status)
612         goto DONE;
613
614     for (;
615          notmuch_messages_valid (messages);
616          notmuch_messages_move_to_next (messages)) {
617         unsigned int doc_id;
618
619         message = notmuch_messages_get (messages);
620         doc_id = _notmuch_message_get_doc_id (message);
621         if (doc_id == seed_doc_id)
622             message = seed_message;
623
624         _thread_add_message (thread, message, exclude_terms, omit_excluded);
625
626         if ( _notmuch_doc_id_set_contains (match_set, doc_id)) {
627             _notmuch_doc_id_set_remove (match_set, doc_id);
628             _thread_add_matched_message (thread, message, sort);
629         }
630
631         _notmuch_message_close (message);
632     }
633
634     _resolve_thread_authors_string (thread);
635
636     _resolve_thread_relationships (thread);
637
638     /* Commit to returning thread. */
639     (void) talloc_steal (ctx, thread);
640
641   DONE:
642     talloc_free (local);
643     return thread;
644 }
645
646 notmuch_messages_t *
647 notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread)
648 {
649     return _notmuch_messages_create (thread->toplevel_list);
650 }
651
652 notmuch_messages_t *
653 notmuch_thread_get_messages (notmuch_thread_t *thread)
654 {
655     return _notmuch_messages_create (thread->message_list);
656 }
657
658 const char *
659 notmuch_thread_get_thread_id (notmuch_thread_t *thread)
660 {
661     return thread->thread_id;
662 }
663
664 int
665 notmuch_thread_get_total_messages (notmuch_thread_t *thread)
666 {
667     return thread->total_messages;
668 }
669
670 int
671 notmuch_thread_get_total_files (notmuch_thread_t *thread)
672 {
673     return thread->total_files;
674 }
675
676 int
677 notmuch_thread_get_matched_messages (notmuch_thread_t *thread)
678 {
679     return thread->matched_messages;
680 }
681
682 const char *
683 notmuch_thread_get_authors (notmuch_thread_t *thread)
684 {
685     return thread->authors;
686 }
687
688 const char *
689 notmuch_thread_get_subject (notmuch_thread_t *thread)
690 {
691     return thread->subject;
692 }
693
694 time_t
695 notmuch_thread_get_oldest_date (notmuch_thread_t *thread)
696 {
697     return thread->oldest;
698 }
699
700 time_t
701 notmuch_thread_get_newest_date (notmuch_thread_t *thread)
702 {
703     return thread->newest;
704 }
705
706 notmuch_tags_t *
707 notmuch_thread_get_tags (notmuch_thread_t *thread)
708 {
709     notmuch_string_list_t *tags;
710     GList *keys, *l;
711
712     tags = _notmuch_string_list_create (thread);
713     if (unlikely (tags == NULL))
714         return NULL;
715
716     keys = g_hash_table_get_keys (thread->tags);
717
718     for (l = keys; l; l = l->next)
719         _notmuch_string_list_append (tags, (char *) l->data);
720
721     g_list_free (keys);
722
723     _notmuch_string_list_sort (tags);
724
725     return _notmuch_tags_create (thread, tags);
726 }
727
728 void
729 notmuch_thread_destroy (notmuch_thread_t *thread)
730 {
731     talloc_free (thread);
732 }