From 7a1053691c344044be5f5bfbf95c05b8f119cba6 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 13 Sep 2018 06:05:47 +0000 Subject: [PATCH] Workaround for providers erasing the message ID Fixes #109 --- .../main/java/eu/faircode/email/DaoMessage.java | 5 +++-- .../java/eu/faircode/email/MessageHelper.java | 16 ++++++++-------- .../eu/faircode/email/ServiceSynchronize.java | 14 +++++++------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoMessage.java b/app/src/main/java/eu/faircode/email/DaoMessage.java index b1c560ae..fcba86fe 100644 --- a/app/src/main/java/eu/faircode/email/DaoMessage.java +++ b/app/src/main/java/eu/faircode/email/DaoMessage.java @@ -109,8 +109,9 @@ public interface DaoMessage { @Query("SELECT message.* FROM message" + " JOIN folder ON folder.id = message.folder" + " WHERE message.account = :account" + - " AND message.msgid = :msgid") - List getMessageByMsgId(long account, String msgid); + " AND (message.msgid = :msgid" + + " OR message.msgid = :reference)") + List getMessageByMsgId(long account, String msgid, String reference); @Query("SELECT message.* FROM message" + " JOIN folder ON folder.id = message.folder" + diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index cef054a6..181fc3db 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -136,9 +136,16 @@ public class MessageHelper { return props; } - static MimeMessageEx from(Context context, EntityMessage message, List attachments, Session isession) throws MessagingException, IOException { + static MimeMessageEx from(Context context, EntityMessage message, EntityMessage reply, List attachments, Session isession) throws MessagingException, IOException { MimeMessageEx imessage = new MimeMessageEx(isession, message.msgid); + if (reply == null) + imessage.addHeader("References", message.msgid); + else { + imessage.addHeader("In-Reply-To", reply.msgid); + imessage.addHeader("References", (reply.references == null ? "" : reply.references + " ") + reply.msgid); + } + imessage.setFlag(Flags.Flag.SEEN, message.seen); if (message.from != null && message.from.length > 0) @@ -198,13 +205,6 @@ public class MessageHelper { return imessage; } - static MimeMessageEx from(Context context, EntityMessage message, EntityMessage reply, List attachments, Session isession) throws MessagingException, IOException { - MimeMessageEx imessage = from(context, message, attachments, isession); - imessage.addHeader("In-Reply-To", reply.msgid); - imessage.addHeader("References", (reply.references == null ? "" : reply.references + " ") + reply.msgid); - return imessage; - } - MessageHelper(MimeMessage message) { this.imessage = message; } diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 5290adca..a473b55a 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -974,7 +974,7 @@ public class ServiceSynchronize extends LifecycleService { private void doAdd(EntityFolder folder, Session isession, IMAPFolder ifolder, EntityMessage message, JSONArray jargs, DB db) throws MessagingException, JSONException, IOException { // Append message List attachments = db.attachment().getAttachments(message.id); - MimeMessage imessage = MessageHelper.from(this, message, attachments, isession); + MimeMessage imessage = MessageHelper.from(this, message, null, attachments, isession); AppendUID[] uid = ifolder.appendUIDMessages(new Message[]{imessage}); db.message().setMessageUid(message.id, uid[0].uid); Log.i(Helper.TAG, "Appended uid=" + uid[0].uid); @@ -1014,7 +1014,7 @@ public class ServiceSynchronize extends LifecycleService { ifolder.expunge(); } - MimeMessageEx icopy = MessageHelper.from(this, message, attachments, isession); + MimeMessageEx icopy = MessageHelper.from(this, message, null, attachments, isession); Folder itarget = istore.getFolder(target.name); itarget.appendMessages(new Message[]{icopy}); } @@ -1054,10 +1054,7 @@ public class ServiceSynchronize extends LifecycleService { MimeMessage imessage; EntityMessage reply = (message.replying == null ? null : db.message().getMessage(message.replying)); List attachments = db.attachment().getAttachments(message.id); - if (reply == null) - imessage = MessageHelper.from(this, message, attachments, isession); - else - imessage = MessageHelper.from(this, message, reply, attachments, isession); + imessage = MessageHelper.from(this, message, reply, attachments, isession); if (ident.replyto != null) imessage.setReplyTo(new Address[]{new InternetAddress(ident.replyto)}); @@ -1385,7 +1382,10 @@ public class ServiceSynchronize extends LifecycleService { if (message == null) { // Will fetch headers within database transaction String msgid = helper.getMessageID(); - for (EntityMessage dup : db.message().getMessageByMsgId(folder.account, msgid)) { + String[] refs = helper.getReferences(); + String reference = (refs.length == 1 && refs[0].indexOf(BuildConfig.APPLICATION_ID) > 0 ? refs[0] : msgid); + Log.i(Helper.TAG, "Searching for " + msgid + " / " + reference); + for (EntityMessage dup : db.message().getMessageByMsgId(folder.account, msgid, reference)) { EntityFolder dfolder = db.folder().getFolder(dup.folder); boolean outbox = EntityFolder.OUTBOX.equals(dfolder.type); Log.i(Helper.TAG, folder.name + " found as id=" + dup.id +