mirror of
https://github.com/mjl-/mox.git
synced 2025-07-12 12:24:38 +03:00
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.
This commit is contained in:
39
import.go
39
import.go
@ -185,6 +185,20 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
var mdnewf, mdcurf *os.File
|
||||
var msgreader store.MsgSource
|
||||
|
||||
// Open account, creating a database file if it doesn't exist yet. It must be known
|
||||
// in the configuration file.
|
||||
a, err := store.OpenAccount(account)
|
||||
ctl.xcheck(err, "opening account")
|
||||
defer func() {
|
||||
if a != nil {
|
||||
err := a.Close()
|
||||
ctl.log.Check(err, "closing account after import")
|
||||
}
|
||||
}()
|
||||
|
||||
err = a.ThreadingWait(ctl.log)
|
||||
ctl.xcheck(err, "waiting for account thread upgrade")
|
||||
|
||||
defer func() {
|
||||
if mboxf != nil {
|
||||
err := mboxf.Close()
|
||||
@ -200,17 +214,6 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
}
|
||||
}()
|
||||
|
||||
// Open account, creating a database file if it doesn't exist yet. It must be known
|
||||
// in the configuration file.
|
||||
a, err := store.OpenAccount(account)
|
||||
ctl.xcheck(err, "opening account")
|
||||
defer func() {
|
||||
if a != nil {
|
||||
err := a.Close()
|
||||
ctl.log.Check(err, "closing account after import")
|
||||
}
|
||||
}()
|
||||
|
||||
// Messages don't always have a junk flag set. We'll assume anything in a mailbox
|
||||
// starting with junk or spam is junk mail.
|
||||
|
||||
@ -277,7 +280,8 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
const consumeFile = true
|
||||
const sync = false
|
||||
const notrain = true
|
||||
err := a.DeliverMessage(ctl.log, tx, m, mf, consumeFile, sync, notrain)
|
||||
const nothreads = true
|
||||
err := a.DeliverMessage(ctl.log, tx, m, mf, consumeFile, sync, notrain, nothreads)
|
||||
ctl.xcheck(err, "delivering message")
|
||||
deliveredIDs = append(deliveredIDs, m.ID)
|
||||
ctl.log.Debug("delivered message", mlog.Field("id", m.ID))
|
||||
@ -332,6 +336,11 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
m.ParsedBuf, err = json.Marshal(p)
|
||||
ctl.xcheck(err, "marshal parsed message structure")
|
||||
|
||||
// Set fields needed for future threading. By doing it now, DeliverMessage won't
|
||||
// have to parse the Part again.
|
||||
p.SetReaderAt(store.FileMsgReader(m.MsgPrefix, msgf))
|
||||
m.PrepareThreading(ctl.log, &p)
|
||||
|
||||
if m.Received.IsZero() {
|
||||
if p.Envelope != nil && !p.Envelope.Date.IsZero() {
|
||||
m.Received = p.Envelope.Date
|
||||
@ -385,6 +394,12 @@ func importctl(ctx context.Context, ctl *ctl, mbox bool) {
|
||||
process(m, msgf, origPath)
|
||||
}
|
||||
|
||||
// Match threads.
|
||||
if len(deliveredIDs) > 0 {
|
||||
err = a.AssignThreads(ctx, ctl.log, tx, deliveredIDs[0], 0, io.Discard)
|
||||
ctl.xcheck(err, "assigning messages to threads")
|
||||
}
|
||||
|
||||
// Get mailbox again, uidnext is likely updated.
|
||||
mc := mb.MailboxCounts
|
||||
err = tx.Get(&mb)
|
||||
|
Reference in New Issue
Block a user