Refactored POP3 purge

pull/184/head
M66B 4 years ago
parent 07f3a1248b
commit de76322f8d

@ -1259,7 +1259,7 @@ class Core {
else else
msgid = helper.getMessageID(); msgid = helper.getMessageID();
Log.i("POP searching=" + message.msgid + " iterate=" + msgid); Log.i(folder.name + " POP searching=" + message.msgid + " iterate=" + msgid);
if (msgid != null && if (msgid != null &&
(msgid.equals(message.uidl) || msgid.equals(message.msgid))) { (msgid.equals(message.uidl) || msgid.equals(message.msgid))) {
found = true; found = true;
@ -1836,7 +1836,8 @@ class Core {
imessages = Arrays.copyOfRange(imessages, imessages = Arrays.copyOfRange(imessages,
imessages.length - account.max_messages, imessages.length); imessages.length - account.max_messages, imessages.length);
if (caps.containsKey("UIDL")) { boolean hasUidl = caps.containsKey("UIDL");
if (hasUidl) {
FetchProfile ifetch = new FetchProfile(); FetchProfile ifetch = new FetchProfile();
ifetch.add(UIDFolder.FetchProfileItem.UID); ifetch.add(UIDFolder.FetchProfileItem.UID);
ifolder.fetch(imessages, ifetch); ifolder.fetch(imessages, ifetch);
@ -1845,16 +1846,52 @@ class Core {
db.folder().setFolderSyncState(folder.id, "downloading"); db.folder().setFolderSyncState(folder.id, "downloading");
List<TupleUidl> ids = db.message().getUidls(folder.id); List<TupleUidl> ids = db.message().getUidls(folder.id);
Log.i(folder.name + " POP existing=" + ids.size()); Log.i(folder.name + " POP existing=" + ids.size() + " uidl=" + hasUidl);
// Map identifiers // Index UIDLs
Map<String, TupleUidl> uidls = new HashMap<>(); Map<String, String> uidlMsgId = new HashMap<>();
Map<String, TupleUidl> msgids = new HashMap<>();
for (TupleUidl id : ids) for (TupleUidl id : ids)
if (id.uidl != null) if (id.uidl != null && id.msgid != null)
uidls.put(id.uidl, id); uidlMsgId.put(id.uidl, id.msgid);
else if (id.msgid != null)
msgids.put(id.msgid, id); if (!account.leave_on_device) {
if (hasUidl) {
Map<String, TupleUidl> known = new HashMap<>();
for (TupleUidl id : ids)
if (id.uidl != null)
known.put(id.uidl, id);
for (Message imessage : imessages) {
String uidl = ifolder.getUID(imessage);
if (TextUtils.isEmpty(uidl))
known.clear(); // better safe than sorry
else
known.remove(uidl);
}
for (TupleUidl uidl : known.values()) {
Log.i(folder.name + " POP purging uidl=" + uidl.uidl);
db.message().deleteMessage(uidl.id);
}
} else {
Map<String, TupleUidl> known = new HashMap<>();
for (TupleUidl id : ids)
if (id.msgid != null)
known.put(id.msgid, id);
for (Message imessage : imessages) {
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
String msgid = helper.getMessageID(); // expensive!
if (!TextUtils.isEmpty(msgid))
known.remove(msgid);
}
for (TupleUidl uidl : known.values()) {
Log.i(folder.name + " POP purging msgid=" + uidl.msgid);
db.message().deleteMessage(uidl.id);
}
}
}
for (Message imessage : imessages) for (Message imessage : imessages)
try { try {
@ -1862,32 +1899,40 @@ class Core {
return; return;
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context); MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
String uidl = caps.containsKey("UIDL")
? ifolder.getUID(imessage)
: helper.getMessageID();
if (TextUtils.isEmpty(uidl)) { String uidl;
Log.w(folder.name + " POP no message ID"); String msgid;
continue; if (hasUidl) {
} uidl = ifolder.getUID(imessage);
if (TextUtils.isEmpty(uidl)) {
Log.w(folder.name + " POP no uidl");
continue;
}
if (uidls.containsKey(uidl)) { msgid = uidlMsgId.get(uidl);
uidls.remove(uidl); if (msgid == null)
Log.i(folder.name + " POP having uidl=" + uidl); msgid = helper.getMessageID();
continue; else {
} Log.i(folder.name + " POP having uidl=" + uidl);
continue;
}
} else {
uidl = null;
msgid = helper.getMessageID();
if (caps.containsKey("UIDL")) { if (db.message().countMessageByMsgId(folder.id, msgid) > 0) {
String msgid = helper.getMessageID();
if (msgids.containsKey(msgid)) {
Log.i(folder.name + " POP having msgid=" + msgid); Log.i(folder.name + " POP having msgid=" + msgid);
db.message().setMessageUidl(msgids.get(msgid).id, uidl);
continue; continue;
} }
} }
if (TextUtils.isEmpty(msgid)) {
Log.w(folder.name + " POP no msgid");
continue;
}
try { try {
Log.i(folder.name + " POP sync=" + uidl); Log.i(folder.name + " POP sync=" + msgid);
Long sent = helper.getSent(); Long sent = helper.getSent();
Long received = helper.getReceivedHeader(); Long received = helper.getReceivedHeader();
@ -1904,7 +1949,7 @@ class Core {
message.folder = folder.id; message.folder = folder.id;
message.uid = null; message.uid = null;
message.uidl = uidl; message.uidl = uidl;
message.msgid = helper.getMessageID(); message.msgid = msgid;
message.hash = helper.getHash(); message.hash = helper.getHash();
message.references = TextUtils.join(" ", helper.getReferences()); message.references = TextUtils.join(" ", helper.getReferences());
message.inreplyto = helper.getInReplyTo(); message.inreplyto = helper.getInReplyTo();
@ -2010,12 +2055,6 @@ class Core {
((POP3Message) imessage).invalidate(true); ((POP3Message) imessage).invalidate(true);
} }
if (!account.leave_on_device)
for (TupleUidl id : uidls.values()) {
Log.i(folder.name + " POP deleting=" + id.msgid);
db.message().deleteMessage(folder.id, id.msgid);
}
Log.i(folder.name + " POP done"); Log.i(folder.name + " POP done");
} finally { } finally {
db.folder().setFolderSyncState(folder.id, null); db.folder().setFolderSyncState(folder.id, null);

Loading…
Cancel
Save