# E-Mail Threading in ML-Mode does not work in Thunderbird

**URL:** https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657
**Category:** Feature
**Created:** [February 2, 2018, 5:38pm UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657 "2018-02-02T17:38:18Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![fwolfst](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fwolfst/32/88859_2.png) [@fwolfst](https://meta.discourse.org/u/fwolfst)
#### Post date: [February 2, 2018, 5:38pm UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/1 "2018-02-02T17:38:18Z")

</div>

I have one of these “dinosaur”-Settings, with the wish to replicate a mailing list.  
However, the threading in thunderbird does not work the way it should (with the latest Docker installation).  
Assume “A” and “B” are users and they send mails that should result in following thread:

```
A: message1
  B: reply1 to message1
    A: reply1 to reply1 to message1
      B: reply1 to reply1 to reply1 to message1
  A: reply2 to message1

```

How I see them in my thunderbird is the following

```
A: message1
  B: reply1 to message1
  A: reply1 to reply1 to message1
  B: reply1 to reply1 to reply1 to message1
  A: reply2 to message1

```

Its flat, like in only one level deep.  
I wonder a bit that nobody has these issues - probably its related to the fact that we do not use reply-key mail-addresses and tough maillinglist-setting everywhere possible.

I compared the Headers (`in-reply-to` and `references` are the relevant ones) to threads that fold well and found that I can “fix” the issue manually in my inbox by manipulating the headers such that the `references` is sorted such that the reply-to - message-id is **last**.

That would mean the bug lies in ordering of the references-header. This is found here:

> <https://github.com/discourse/discourse/blob/e8559f222c856455f38bf3edb62985b464581acb/lib/email/sender.rb#L112>

Unfortunately I will not soon find the time to setup a dev-environment and test these carefully. I suspect that the `uniq` kicks out the actual topic\_message\_id, so _maybe_ something along these lines would fix it:

```
[referenced_post_message_ids - topic_message_id].flatten.compact.uniq + [topic_message_id]

```

Although it looks quiet ugly, but you get the point.

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [February 2, 2018, 10:53pm UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/2 "2018-02-02T22:53:02Z")

</div>

Interesting 🤔 and good 🕵️‍♂️ work. I’ll fix it.

I wish email threading was documented so we could stop guessing and write headers the way they’re actually used…

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [February 3, 2018, 12:50am UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/4 "2018-02-03T00:50:33Z")

</div>

Wouldn’t that still put the topic message ID last? You’d want it to be first, instead right?

---

<div class="post-metadata">

### Author: ![fwolfst](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fwolfst/32/88859_2.png) [@fwolfst](https://meta.discourse.org/u/fwolfst)
#### Post date: [February 3, 2018, 7:10am UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/5 "2018-02-03T07:10:39Z")

</div>

Nope. The references in mails that are displayed well in my thunderbird traverse “from the root to the last message”. So, original “post” first, then the first answer (in that branch), the answer to that answer, … and so on.

From:

> **[message threading](https://www.jwz.org/doc/threading.html)**

_[…] it asserts that this header contain a list of Message-IDs listing the parent, grandparent, great-grandparent, and so on, of this message, oldest first. That is, the direct parent of this message will be the last element of the References header._

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 3, 2018, 7:12am UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/6 "2018-02-03T07:12:48Z")

</div>

That’s not how topics work in Discourse, they are mostly flat with some light, **one level only** attachment of replies .. so I am afraid this is an irreconcilable difference.

This is a correct representation of the discussion from Discourse’s standpoint:

```
A: message1
  B: reply1 to message1
  A: reply1 to reply1 to message1
  B: reply1 to reply1 to reply1 to message1
  A: reply2 to message1

```

---

<div class="post-metadata">

### Author: ![fwolfst](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fwolfst/32/88859_2.png) [@fwolfst](https://meta.discourse.org/u/fwolfst)
#### Post date: [February 3, 2018, 7:23am UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/7 "2018-02-03T07:23:22Z")

</div>

@codinghorror: ? Sometimes here on the discourse frontend I see something like “3 Replies” after a reply and can unfold that (which is **superhandy** btw). Are these only on the first level?  
Anyways, the change is cheap, the benefit huge - I find threaded mail views enormously helpful, especially when the original “discussion” branches into several topics.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 3, 2018, 8:09am UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/8 "2018-02-03T08:09:46Z")

</div>

> [@fwolfst](#):
>
> Are these only on the first level?

Correct, only one level. There is no way to expand grandchildren or great-grandchildren etc.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [February 3, 2018, 8:28am UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/9 "2018-02-03T08:28:16Z")

</div>

If you understand “code speak”, the field `reply_to_post_number` is an integer, _not_ an array. So even if some type of recursive iterator was coded up, the deeper level threading would be limited to one per post. i.e. when a post quotes (refers to) more than one other post, only one of them is stored in the table.

---

<div class="post-metadata">

### Author: ![fwolfst](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fwolfst/32/88859_2.png) [@fwolfst](https://meta.discourse.org/u/fwolfst)
#### Post date: [February 3, 2018, 1:23pm UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/10 "2018-02-03T13:23:57Z")

</div>

Sometimes I speak code better than english 😉

> [@Mittineague](#):
>
> i.e. when a post quotes (refers to) more than one other post, only one of them is stored in the table.

Thats totally fine from the point of view of algorithmic thread calculation, from my interpretation of

> **[message threading](https://www.jwz.org/doc/threading.html)**

Apparently, the real length of the `References`-Array does not matter. From my experiments however the **order** matters. The last referenced (“`reply-to’”) post should be the last.

It seems to me that there is some doubt of whether it makes sense to take advantage of email-readers ability to reflect “hierarchies” and the (simplified) tree-nature of replies visually (which can often be switched off by the email-client anyway). I dont have that doubts 🙂

@zogstrip wanted to look into this and I would be glad if we help the email-clients to visualize what happened - especially when reply-by-mail is offered imho it makes sense to give the users the possibility to see to which post/mail was replied to. Again, the change should be extremely cheap code-wise and results in an increased usabilty. And users who dislike to see deeper threads in their mail-client (if such users exist in a larger number that would be a surprise for me) can usually switch threaded-views off.

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [February 3, 2018, 10:02pm UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/11 "2018-02-03T22:02:30Z")

</div>

> [@fwolfst](#):
>
> The last referenced (“`reply-to’”) post should be the last

> [@fwolfst](#):
>
> `[referenced_post_message_ids - topic_message_id].flatten.compact.uniq + [topic_message_id]`

> [@riking](#):
>
> Wouldn’t that still put the topic message ID last? You’d want it to be first, instead right?

So, I repeat. Wouldn’t something more like this be more correct?

`[referenced_post_message_ids, topic_message_id].flatten.compact.uniq.reverse`

---

<div class="post-metadata">

### Author: ![fwolfst](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fwolfst/32/88859_2.png) [@fwolfst](https://meta.discourse.org/u/fwolfst)
#### Post date: [February 4, 2018, 7:36am UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/12 "2018-02-04T07:36:12Z")

</div>

@riking I see I have to dig in the code and check where the stuff from - I cannot tell you exactly what the “topic message” is. But if you consider the thread - tree

```
  MSG1
    MSG1R1
    MSG1R2
      MSG1R2R1
        MSG1R2R1R1
    MSG1R3

```

then **for MSG1R2R1R1** the `reply-to` should be _MSG1R2R1_ and the `references` would list the path from the root node to itself (in that order, old-to-new), so  
`MSG1, MSG1R2, MSG1R2R1`. In my impression, the “later” references are the more important ones (so, the parent is more important than the grand-grand-grand-grand-parent).

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [February 4, 2018, 7:39am UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/13 "2018-02-04T07:39:22Z")

</div>

topic\_message\_id is MSG1 in your diagram.

Note that Discourse’s view of the thread is more like this:

```plaintext
  MSG1
    MSG2 (← ø)
    MSG3 (← MSG1)
      MSG4 (← MSG3)
        MSG6 (← MSG4, MSG5)
    MSG5 (← ø)

```

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 5, 2018, 5:01am UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/14 "2018-02-05T05:01:42Z")

</div>

Also in Discourse’s view a single post can be in reply to three other upstream posts. Just quote three different posts above you, in a single reply..

---

<div class="post-metadata">

### Author: ![fwolfst](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fwolfst/32/88859_2.png) [@fwolfst](https://meta.discourse.org/u/fwolfst)
#### Post date: [February 5, 2018, 1:34pm UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/15 "2018-02-05T13:34:21Z")

</div>

> [@riking](#):
>
> [referenced\_post\_message\_ids, topic\_message\_id].flatten.compact.uniq.reverse

Actually

```
[topic_message_id, referenced_post_message_ids].flatten.compact.uniq.reverse

```

solves the issue. I prepare a PR on github .  
@zogstrip, @riking, @codinghorror : How are the chances of that PR (will link it from here) getting merged upstream?

---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [February 5, 2018, 1:47pm UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/16 "2018-02-05T13:47:58Z")

</div>

> [@fwolfst](#):
>
> How are the chances of that PR (will link it from here) getting merged upstream?

Very high since it’s what I was thinking of doing 😉

---

<div class="post-metadata">

### Author: ![fwolfst](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fwolfst/32/88859_2.png) [@fwolfst](https://meta.discourse.org/u/fwolfst)
#### Post date: [February 5, 2018, 1:51pm UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/17 "2018-02-05T13:51:12Z")

</div>

Cool. Here you go 🙂

[https://github.com/discourse/discourse/pull/5567](https://github.com/discourse/discourse/pull/5567)

I am not sure whether further optimizations are needed or possible, but the referenced change fixes the issue for me afaics.

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [June 29, 2018, 3:46pm UTC](https://meta.discourse.org/t/e-mail-threading-in-ml-mode-does-not-work-in-thunderbird/79657/18 "2018-06-29T15:46:35Z")

</div>


