Browse Source

Transactional synchronize message

main
M66B 6 years ago
parent
commit
f0cf9dafa4
1 changed files with 82 additions and 69 deletions
  1. +82
    -69
      app/src/main/java/eu/faircode/email/ServiceSynchronize.java

+ 82
- 69
app/src/main/java/eu/faircode/email/ServiceSynchronize.java View File

@ -1153,85 +1153,98 @@ public class ServiceSynchronize extends LifecycleService {
boolean seen = helper.getSeen(); boolean seen = helper.getSeen();
DB db = DB.getInstance(this); DB db = DB.getInstance(this);
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
try {
db.beginTransaction();
// Find message by uid (fast, no headers required)
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
// Find message by Message-ID (slow, headers required)
// - messages in inbox have same id as message sent to self
// - messages in archive have same id as original
if (message == null &&
!EntityFolder.SENT.equals(folder.type) &&
!EntityFolder.ARCHIVE.equals(folder.type)) {
// Will fetch headers within database transaction
String msgid = imessage.getMessageID();
message = db.message().getMessageByMsgId(msgid);
if (message != null) {
Log.i(Helper.TAG, folder.name + " found as id=" + message.id + " uid=" + message.uid + " msgid=" + msgid);
message.folder = folder.id;
message.uid = uid;
db.message().updateMessage(message);
}
}
// Find by Message-ID
// - messages in inbox have same id as message sent to self
// - messages in archive have same id as original
if (message == null &&
!EntityFolder.SENT.equals(folder.type) &&
!EntityFolder.ARCHIVE.equals(folder.type)) {
String msgid = imessage.getMessageID(); // Will fetch headers
message = db.message().getMessageByMsgId(msgid);
if (message != null) { if (message != null) {
Log.i(Helper.TAG, folder.name + " found as id=" + message.id + " uid=" + message.uid + " msgid=" + msgid);
message.folder = folder.id;
message.uid = uid;
db.message().updateMessage(message);
if (message.seen != seen) {
message.seen = seen;
message.ui_seen = seen;
db.message().updateMessage(message);
Log.v(Helper.TAG, folder.name + " updated id=" + message.id + " uid=" + message.uid);
return -1;
} else {
Log.v(Helper.TAG, folder.name + " unchanged id=" + message.id + " uid=" + message.uid);
return 0;
}
} }
}
if (message == null) {
FetchProfile fp1 = new FetchProfile();
fp1.add(FetchProfile.Item.ENVELOPE);
fp1.add(FetchProfile.Item.CONTENT_INFO);
fp1.add(IMAPFolder.FetchProfileItem.HEADERS);
fp1.add(IMAPFolder.FetchProfileItem.MESSAGE);
ifolder.fetch(new Message[]{imessage}, fp1);
db.setTransactionSuccessful();
message = new EntityMessage();
message.account = folder.account;
message.folder = folder.id;
message.uid = uid;
} finally {
db.endTransaction();
}
if (!EntityFolder.SENT.equals(folder.type) &&
!EntityFolder.ARCHIVE.equals(folder.type)) {
message.msgid = helper.getMessageID();
if (TextUtils.isEmpty(message.msgid))
Log.w(Helper.TAG, "No Message-ID id=" + message.id + " uid=" + message.uid);
}
FetchProfile fp1 = new FetchProfile();
fp1.add(FetchProfile.Item.ENVELOPE);
fp1.add(FetchProfile.Item.CONTENT_INFO);
fp1.add(IMAPFolder.FetchProfileItem.HEADERS);
fp1.add(IMAPFolder.FetchProfileItem.MESSAGE);
ifolder.fetch(new Message[]{imessage}, fp1);
message.references = TextUtils.join(" ", helper.getReferences());
message.inreplyto = helper.getInReplyTo();
message.thread = helper.getThreadId(uid);
message.from = helper.getFrom();
message.to = helper.getTo();
message.cc = helper.getCc();
message.bcc = helper.getBcc();
message.reply = helper.getReply();
message.subject = imessage.getSubject();
message.body = helper.getHtml();
message.received = imessage.getReceivedDate().getTime();
message.sent = (imessage.getSentDate() == null ? null : imessage.getSentDate().getTime());
message.seen = seen;
message.ui_seen = seen;
message.ui_hide = false;
message.id = db.message().insertMessage(message);
Log.v(Helper.TAG, folder.name + " added id=" + message.id + " uid=" + message.uid);
int sequence = 0;
for (EntityAttachment attachment : helper.getAttachments()) {
sequence++;
Log.i(Helper.TAG, "attachment seq=" + sequence +
" name=" + attachment.name + " type=" + attachment.type);
attachment.message = message.id;
attachment.sequence = sequence;
attachment.id = db.attachment().insertAttachment(attachment);
}
EntityMessage message = new EntityMessage();
message.account = folder.account;
message.folder = folder.id;
message.uid = uid;
return 1;
} else if (message.seen != seen) {
message.seen = seen;
message.ui_seen = seen;
db.message().updateMessage(message);
Log.v(Helper.TAG, folder.name + " updated id=" + message.id + " uid=" + message.uid);
return -1;
} else {
Log.v(Helper.TAG, folder.name + " unchanged id=" + message.id + " uid=" + message.uid);
return 0;
if (!EntityFolder.SENT.equals(folder.type) &&
!EntityFolder.ARCHIVE.equals(folder.type)) {
message.msgid = helper.getMessageID();
if (TextUtils.isEmpty(message.msgid))
Log.w(Helper.TAG, "No Message-ID id=" + message.id + " uid=" + message.uid);
} }
message.references = TextUtils.join(" ", helper.getReferences());
message.inreplyto = helper.getInReplyTo();
message.thread = helper.getThreadId(uid);
message.from = helper.getFrom();
message.to = helper.getTo();
message.cc = helper.getCc();
message.bcc = helper.getBcc();
message.reply = helper.getReply();
message.subject = imessage.getSubject();
message.body = helper.getHtml();
message.received = imessage.getReceivedDate().getTime();
message.sent = (imessage.getSentDate() == null ? null : imessage.getSentDate().getTime());
message.seen = seen;
message.ui_seen = seen;
message.ui_hide = false;
message.id = db.message().insertMessage(message);
Log.v(Helper.TAG, folder.name + " added id=" + message.id + " uid=" + message.uid);
int sequence = 0;
for (EntityAttachment attachment : helper.getAttachments()) {
sequence++;
Log.i(Helper.TAG, "attachment seq=" + sequence +
" name=" + attachment.name + " type=" + attachment.type);
attachment.message = message.id;
attachment.sequence = sequence;
attachment.id = db.attachment().insertAttachment(attachment);
}
return 1;
} finally { } finally {
Log.v(Helper.TAG, folder.name + " end sync uid=" + uid); Log.v(Helper.TAG, folder.name + " end sync uid=" + uid);
} }


Loading…
Cancel
Save