implement message threading in backend and webmail
we match messages to their parents based on the "references" and "in-reply-to"
headers (requiring the same base subject), and in absense of those headers we
also by only base subject (against messages received max 4 weeks ago).
we store a threadid with messages. all messages in a thread have the same
threadid. messages also have a "thread parent ids", which holds all id's of
parent messages up to the thread root. then there is "thread missing link",
which is set when a referenced immediate parent wasn't found (but possibly
earlier ancestors can still be found and will be in thread parent ids".
threads can be muted: newly delivered messages are automatically marked as
read/seen. threads can be marked as collapsed: if set, the webmail collapses
the thread to a single item in the basic threading view (default is to expand
threads). the muted and collapsed fields are copied from their parent on
message delivery.
the threading is implemented in the webmail. the non-threading mode still works
as before. the new default threading mode "unread" automatically expands only
the threads with at least one unread (not seen) meessage. the basic threading
mode "on" expands all threads except when explicitly collapsed (as saved in the
thread collapsed field). new shortcuts for navigation/interaction threads have
been added, e.g. go to previous/next thread root, toggle collapse/expand of
thread (or double click), toggle mute of thread. some previous shortcuts have
changed, see the help for details.
the message threading are added with an explicit account upgrade step,
automatically started when an account is opened. the upgrade is done in the
background because it will take too long for large mailboxes to block account
operations. the upgrade takes two steps: 1. updating all message records in the
database to add a normalized message-id and thread base subject (with "re:",
"fwd:" and several other schemes stripped). 2. going through all messages in
the database again, reading the "references" and "in-reply-to" headers from
disk, and matching against their parents. this second step is also done at the
end of each import of mbox/maildir mailboxes. new deliveries are matched
immediately against other existing messages, currently no attempt is made to
rematch previously delivered messages (which could be useful for related
messages being delivered out of order).
the threading is not yet exposed over imap.
2023-09-13 09:51:50 +03:00
|
|
|
package message
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ThreadSubject returns the base subject to use for matching against other
|
|
|
|
// messages, to see if they belong to the same thread. A matching subject is
|
|
|
|
// always required to match to an existing thread, both if
|
|
|
|
// References/In-Reply-To header(s) are present, and if not.
|
|
|
|
//
|
2023-12-12 17:47:26 +03:00
|
|
|
// isResponse indicates if this message is a response, such as a reply or a
|
|
|
|
// forward.
|
|
|
|
//
|
implement message threading in backend and webmail
we match messages to their parents based on the "references" and "in-reply-to"
headers (requiring the same base subject), and in absense of those headers we
also by only base subject (against messages received max 4 weeks ago).
we store a threadid with messages. all messages in a thread have the same
threadid. messages also have a "thread parent ids", which holds all id's of
parent messages up to the thread root. then there is "thread missing link",
which is set when a referenced immediate parent wasn't found (but possibly
earlier ancestors can still be found and will be in thread parent ids".
threads can be muted: newly delivered messages are automatically marked as
read/seen. threads can be marked as collapsed: if set, the webmail collapses
the thread to a single item in the basic threading view (default is to expand
threads). the muted and collapsed fields are copied from their parent on
message delivery.
the threading is implemented in the webmail. the non-threading mode still works
as before. the new default threading mode "unread" automatically expands only
the threads with at least one unread (not seen) meessage. the basic threading
mode "on" expands all threads except when explicitly collapsed (as saved in the
thread collapsed field). new shortcuts for navigation/interaction threads have
been added, e.g. go to previous/next thread root, toggle collapse/expand of
thread (or double click), toggle mute of thread. some previous shortcuts have
changed, see the help for details.
the message threading are added with an explicit account upgrade step,
automatically started when an account is opened. the upgrade is done in the
background because it will take too long for large mailboxes to block account
operations. the upgrade takes two steps: 1. updating all message records in the
database to add a normalized message-id and thread base subject (with "re:",
"fwd:" and several other schemes stripped). 2. going through all messages in
the database again, reading the "references" and "in-reply-to" headers from
disk, and matching against their parents. this second step is also done at the
end of each import of mbox/maildir mailboxes. new deliveries are matched
immediately against other existing messages, currently no attempt is made to
rematch previously delivered messages (which could be useful for related
messages being delivered out of order).
the threading is not yet exposed over imap.
2023-09-13 09:51:50 +03:00
|
|
|
// Subject should already be q/b-word-decoded.
|
|
|
|
//
|
|
|
|
// If allowNull is true, base subjects with a \0 can be returned. If not set,
|
|
|
|
// an empty string is returned if a base subject would have a \0.
|
|
|
|
func ThreadSubject(subject string, allowNull bool) (threadSubject string, isResponse bool) {
|
|
|
|
subject = strings.ToLower(subject)
|
|
|
|
|
|
|
|
// ../rfc/5256:101, Step 1.
|
|
|
|
var s string
|
|
|
|
for _, c := range subject {
|
|
|
|
if c == '\r' {
|
|
|
|
continue
|
|
|
|
} else if c == ' ' || c == '\n' || c == '\t' {
|
|
|
|
if !strings.HasSuffix(s, " ") {
|
|
|
|
s += " "
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s += string(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ../rfc/5256:107 ../rfc/5256:811, removing mailing list tag "[...]" and reply/forward "re"/"fwd" prefix.
|
|
|
|
removeBlob := func(s string) string {
|
|
|
|
for i, c := range s {
|
|
|
|
if i == 0 {
|
|
|
|
if c != '[' {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
} else if c == '[' {
|
|
|
|
return s
|
|
|
|
} else if c == ']' {
|
|
|
|
s = s[i+1:] // Past [...].
|
|
|
|
s = strings.TrimRight(s, " \t") // *WSP
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
// ../rfc/5256:107 ../rfc/5256:811
|
|
|
|
removeLeader := func(s string) string {
|
|
|
|
if strings.HasPrefix(s, " ") || strings.HasPrefix(s, "\t") {
|
|
|
|
s = s[1:] // WSP
|
|
|
|
}
|
|
|
|
|
|
|
|
orig := s
|
|
|
|
|
|
|
|
// Remove zero or more subj-blob
|
|
|
|
for {
|
|
|
|
prevs := s
|
|
|
|
s = removeBlob(s)
|
|
|
|
if prevs == s {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(s, "re") {
|
|
|
|
s = s[2:]
|
|
|
|
} else if strings.HasPrefix(s, "fwd") {
|
|
|
|
s = s[3:]
|
|
|
|
} else if strings.HasPrefix(s, "fw") {
|
|
|
|
s = s[2:]
|
|
|
|
} else {
|
|
|
|
return orig
|
|
|
|
}
|
|
|
|
s = strings.TrimLeft(s, " \t") // *WSP
|
|
|
|
s = removeBlob(s)
|
|
|
|
if !strings.HasPrefix(s, ":") {
|
|
|
|
return orig
|
|
|
|
}
|
|
|
|
s = s[1:]
|
|
|
|
isResponse = true
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
// ../rfc/5256:104 ../rfc/5256:817, remove trailing "(fwd)" or WSP, Step 2.
|
|
|
|
for {
|
|
|
|
prevs := s
|
|
|
|
s = strings.TrimRight(s, " \t")
|
|
|
|
if strings.HasSuffix(s, "(fwd)") {
|
|
|
|
s = strings.TrimSuffix(s, "(fwd)")
|
|
|
|
isResponse = true
|
|
|
|
}
|
|
|
|
if s == prevs {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
prevs := s
|
|
|
|
s = removeLeader(s) // Step 3.
|
|
|
|
if ns := removeBlob(s); ns != "" {
|
|
|
|
s = ns // Step 4.
|
|
|
|
}
|
|
|
|
// Step 5, ../rfc/5256:123
|
|
|
|
if s == prevs {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 6. ../rfc/5256:128 ../rfc/5256:805
|
|
|
|
if strings.HasPrefix(s, "[fwd:") && strings.HasSuffix(s, "]") {
|
|
|
|
s = s[len("[fwd:") : len(s)-1]
|
|
|
|
isResponse = true
|
|
|
|
continue // From step 2 again.
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !allowNull && strings.ContainsRune(s, 0) {
|
|
|
|
s = ""
|
|
|
|
}
|
|
|
|
return s, isResponse
|
|
|
|
}
|