Search thread in references messages

pull/153/head
M66B 6 years ago
parent 28ba618b32
commit 12d3882101

@ -48,6 +48,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -755,7 +756,7 @@ class Core {
private static void onSynchronizeMessages( private static void onSynchronizeMessages(
Context context, JSONArray jargs, Context context, JSONArray jargs,
EntityAccount account, final EntityFolder folder, EntityAccount account, final EntityFolder folder,
IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException { final IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
final DB db = DB.getInstance(context); final DB db = DB.getInstance(context);
try { try {
// Legacy // Legacy
@ -824,6 +825,18 @@ class Core {
long fetch = SystemClock.elapsedRealtime(); long fetch = SystemClock.elapsedRealtime();
Log.i(folder.name + " remote fetched=" + (SystemClock.elapsedRealtime() - fetch) + " ms"); Log.i(folder.name + " remote fetched=" + (SystemClock.elapsedRealtime() - fetch) + " ms");
// Sort for finding references messages
Arrays.sort(imessages, new Comparator<Message>() {
@Override
public int compare(Message m1, Message m2) {
try {
return Long.compare(ifolder.getUID(m1), ifolder.getUID(m2));
} catch (MessagingException ex) {
return 0;
}
}
});
for (int i = 0; i < imessages.length && state.running(); i++) for (int i = 0; i < imessages.length && state.running(); i++)
try { try {
uids.remove(ifolder.getUID(imessages[i])); uids.remove(ifolder.getUID(imessages[i]));
@ -1047,7 +1060,7 @@ class Core {
if (dup.folder.equals(folder.id) || if (dup.folder.equals(folder.id) ||
(EntityFolder.OUTBOX.equals(dfolder.type) && EntityFolder.SENT.equals(folder.type))) { (EntityFolder.OUTBOX.equals(dfolder.type) && EntityFolder.SENT.equals(folder.type))) {
String thread = helper.getThreadId(uid); String thread = helper.getThreadId(context, account.id, uid);
Log.i(folder.name + " found as id=" + dup.id + Log.i(folder.name + " found as id=" + dup.id +
" uid=" + dup.uid + "/" + uid + " uid=" + dup.uid + "/" + uid +
" msgid=" + msgid + " thread=" + thread); " msgid=" + msgid + " thread=" + thread);
@ -1123,7 +1136,7 @@ class Core {
message.references = TextUtils.join(" ", helper.getReferences()); message.references = TextUtils.join(" ", helper.getReferences());
message.inreplyto = helper.getInReplyTo(); message.inreplyto = helper.getInReplyTo();
message.deliveredto = delivered; message.deliveredto = delivered;
message.thread = helper.getThreadId(uid); message.thread = helper.getThreadId(context, account.id, uid);
message.from = froms; message.from = froms;
message.to = tos; message.to = tos;
message.cc = ccs; message.cc = ccs;

@ -428,14 +428,26 @@ public class MessageHelper {
return imessage.getHeader("In-Reply-To", null); return imessage.getHeader("In-Reply-To", null);
} }
String getThreadId(long uid) throws MessagingException { String getThreadId(Context context, long account, long uid) throws MessagingException {
List<String> refs = new ArrayList<>();
for (String ref : getReferences()) for (String ref : getReferences())
if (!TextUtils.isEmpty(ref)) if (!TextUtils.isEmpty(ref))
return ref; refs.add(ref);
String inreplyto = getInReplyTo(); String inreplyto = getInReplyTo();
if (inreplyto != null) if (!TextUtils.isEmpty(inreplyto) && !refs.contains(inreplyto))
return inreplyto; refs.add(inreplyto);
DB db = DB.getInstance(context);
for (String ref : refs) {
List<EntityMessage> messages = db.message().getMessageByMsgId(account, ref);
if (messages.size() > 0)
return messages.get(0).thread;
}
if (refs.size() > 0)
return refs.get(0);
String msgid = getMessageID(); String msgid = getMessageID();
return (TextUtils.isEmpty(msgid) ? Long.toString(uid) : msgid); return (TextUtils.isEmpty(msgid) ? Long.toString(uid) : msgid);

Loading…
Cancel
Save