[PATCH 6/8] CLI: refactor dumping of tags.
[notmuch-archives.git] / fd / e961b80ca542f565b0077f223f740f0f103f84
1 Return-Path: <dkg@fifthhorseman.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5  by arlo.cworth.org (Postfix) with ESMTP id EB2486DE0928\r
6  for <notmuch@notmuchmail.org>; Fri,  8 Apr 2016 18:03:09 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12  autolearn=disabled\r
13 Received: from arlo.cworth.org ([127.0.0.1])\r
14  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
15  with ESMTP id aj-8SuSvODWY for <notmuch@notmuchmail.org>;\r
16  Fri,  8 Apr 2016 18:03:02 -0700 (PDT)\r
17 Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
18  by arlo.cworth.org (Postfix) with ESMTP id 8747B6DE02DB\r
19  for <notmuch@notmuchmail.org>; Fri,  8 Apr 2016 18:02:46 -0700 (PDT)\r
20 Received: from fifthhorseman.net (unknown [201.140.212.132])\r
21  by che.mayfirst.org (Postfix) with ESMTPSA id 5982010080\r
22  for <notmuch@notmuchmail.org>; Fri,  8 Apr 2016 21:02:39 -0400 (EDT)\r
23 Received: by fifthhorseman.net (Postfix, from userid 1000)\r
24  id 0A92C200B0; Fri,  8 Apr 2016 22:02:35 -0300 (ART)\r
25 From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
26 To: Notmuch Mail <notmuch@notmuchmail.org>\r
27 Subject: [PATCH v3 6/7] On deletion,\r
28  replace with ghost when other active messages in thread\r
29 Date: Fri,  8 Apr 2016 22:02:33 -0300\r
30 Message-Id: <1460163754-22994-6-git-send-email-dkg@fifthhorseman.net>\r
31 X-Mailer: git-send-email 2.8.0.rc3\r
32 In-Reply-To: <1460163754-22994-1-git-send-email-dkg@fifthhorseman.net>\r
33 References: <1459445693-3900-1-git-send-email-dkg@fifthhorseman.net>\r
34  <1460163754-22994-1-git-send-email-dkg@fifthhorseman.net>\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.20\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39  <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
41  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
46  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Sat, 09 Apr 2016 01:03:10 -0000\r
48 \r
49 There is no need to add a ghost message upon deletion if there are no\r
50 other active messages in the thread.\r
51 \r
52 Also, if the message being deleted was a ghost already, we can just go\r
53 ahead and delete it.\r
54 ---\r
55  lib/message.cc | 58 ++++++++++++++++++++++++++++++++++++++++++----------------\r
56  1 file changed, 42 insertions(+), 16 deletions(-)\r
57 \r
58 diff --git a/lib/message.cc b/lib/message.cc\r
59 index 2399ab3..1b423b0 100644\r
60 --- a/lib/message.cc\r
61 +++ b/lib/message.cc\r
62 @@ -1044,11 +1044,14 @@ _notmuch_message_delete (notmuch_message_t *message)\r
63  {\r
64      notmuch_status_t status;\r
65      Xapian::WritableDatabase *db;\r
66 -    const char *mid, *tid;\r
67 +    const char *mid, *tid, *query_string;\r
68      notmuch_message_t *ghost;\r
69      notmuch_private_status_t private_status;\r
70      notmuch_database_t *notmuch;\r
71 -           \r
72 +    notmuch_query_t *query;\r
73 +    unsigned int count = 0;\r
74 +    notmuch_bool_t is_ghost;\r
75 +\r
76      mid = notmuch_message_get_message_id (message);\r
77      tid = notmuch_message_get_thread_id (message);\r
78      notmuch = message->notmuch;\r
79 @@ -1059,22 +1062,45 @@ _notmuch_message_delete (notmuch_message_t *message)\r
80  \r
81      db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
82      db->delete_document (message->doc_id);\r
83 -           \r
84 -    /* and reintroduce a ghost in its place */\r
85 -    ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);\r
86 -    if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
87 -       private_status = _notmuch_message_initialize_ghost (ghost, tid);\r
88 -       if (! private_status)\r
89 -           _notmuch_message_sync (ghost);\r
90 -    } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {\r
91 -       /* this is deeply weird, and we should not have gotten into\r
92 -          this state.  is there a better error message to return\r
93 -          here? */\r
94 -       return NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
95 +\r
96 +    /* if this was a ghost to begin with, we are done */\r
97 +    private_status = _notmuch_message_has_term (message, "type", "ghost", &is_ghost);\r
98 +    if (private_status)\r
99 +       return COERCE_STATUS (private_status,\r
100 +                             "Error trying to determine whether message was a ghost");\r
101 +    if (is_ghost)\r
102 +       return NOTMUCH_STATUS_SUCCESS;\r
103 +    \r
104 +    query_string = talloc_asprintf (message, "thread:%s", tid);\r
105 +    query = notmuch_query_create (notmuch, query_string);\r
106 +    if (query == NULL)\r
107 +       return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
108 +    status = notmuch_query_count_messages_st (query, &count);\r
109 +    if (status) {\r
110 +       notmuch_query_destroy (query);\r
111 +       return status;\r
112      }\r
113  \r
114 -    notmuch_message_destroy (ghost);\r
115 -    return COERCE_STATUS (private_status, "Error converting to ghost message");\r
116 +    if (count > 0) {\r
117 +       /* reintroduce a ghost in its place because there are still\r
118 +        * other active messages in this thread: */\r
119 +       ghost = _notmuch_message_create_for_message_id (notmuch, mid, &private_status);\r
120 +       if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
121 +           private_status = _notmuch_message_initialize_ghost (ghost, tid);\r
122 +           if (! private_status)\r
123 +               _notmuch_message_sync (ghost);\r
124 +       } else if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {\r
125 +           /* this is deeply weird, and we should not have gotten\r
126 +              into this state.  is there a better error message to\r
127 +              return here? */\r
128 +           status = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;\r
129 +       }\r
130 +\r
131 +       notmuch_message_destroy (ghost);\r
132 +       status = COERCE_STATUS (private_status, "Error converting to ghost message");\r
133 +    }\r
134 +    notmuch_query_destroy (query);\r
135 +    return status;\r
136  }\r
137  \r
138  /* Transform a blank message into a ghost message.  The caller must\r
139 -- \r
140 2.8.0.rc3\r
141 \r