From 3a7d2e81c7fdc8c2e4b9810065028f4906fc28b3 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 18 Aug 2017 19:04:54 -0600 Subject: Implemented thread merging It can happen that thread messages are not delivered in order, which means we will have to merge threads once all messages are available. --- common/mail/threadindexer.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'common/mail/threadindexer.cpp') diff --git a/common/mail/threadindexer.cpp b/common/mail/threadindexer.cpp index 4171d85..1401fc8 100644 --- a/common/mail/threadindexer.cpp +++ b/common/mail/threadindexer.cpp @@ -34,9 +34,37 @@ void ThreadIndexer::updateThreadingIndex(const QByteArray &identifier, const App QVector thread; - //a child already registered our thread. + //check if a child already registered our thread. thread = index().secondaryLookup(messageId); + if (!thread.isEmpty()) { + //A child already registered our thread so we merge the childs thread + //* check if we have a parent thread, if not just continue as usual + //* get all messages that have the same threadid as the child + //* switch all to the parents thread + auto parentThread = index().secondaryLookup(parentMessageId); + if (!parentThread.isEmpty()) { + auto childThreadId = thread.first(); + auto parentThreadId = parentThread.first(); + SinkTrace() << "Merging child thread: " << childThreadId << " into parent thread: " << parentThreadId; + + //Ensure this mail ends up in the correct thread + index().unindex(messageId, childThreadId, transaction); + //We have to copy the id here, otherwise it doesn't survive the subsequent writes + thread = QVector() << QByteArray{parentThreadId.data(), parentThreadId.size()}; + + //Merge all child messages into the correct thread + auto childThreadMessageIds = index().secondaryLookup(childThreadId); + for (const auto &msgId : childThreadMessageIds) { + SinkTrace() << "Merging child message: " << msgId; + index().unindex(msgId, childThreadId, transaction); + index().unindex(childThreadId, msgId, transaction); + index().index(msgId, parentThreadId, transaction); + index().index(parentThreadId, msgId, transaction); + } + } + } + //If parent is already available, add to thread of parent if (thread.isEmpty() && parentMessageId.isValid()) { thread = index().secondaryLookup(parentMessageId); -- cgit v1.2.3