Improved attachment matching

This commit is contained in:
M66B 2019-09-06 13:21:07 +02:00
parent 3b81596d68
commit 843391db38
1 changed files with 31 additions and 9 deletions

View File

@ -883,20 +883,39 @@ public class MessageHelper {
List<EntityAttachment> remotes = getAttachments(); List<EntityAttachment> remotes = getAttachments();
// Some servers order attachments randomly // Some servers order attachments randomly
int index = -1; int index = -1;
boolean warning = false;
// Get attachment by position
if (local.sequence <= remotes.size()) {
EntityAttachment remote = remotes.get(local.sequence - 1);
if (Objects.equals(remote.name, local.name) &&
Objects.equals(remote.type, local.type) &&
Objects.equals(remote.disposition, local.disposition) &&
Objects.equals(remote.cid, local.cid) &&
Objects.equals(remote.size, local.size))
index = local.sequence - 1;
}
// Match attachment by name/cid // Match attachment by name/cid
for (int i = 0; i < remotes.size(); i++) { if (index < 0 && !(local.name == null && local.cid == null)) {
EntityAttachment remote = remotes.get(i); warning = true;
if (Objects.equals(remote.name, local.name) && Log.w("Matching attachment by name/cid");
Objects.equals(remote.cid, local.cid)) { for (int i = 0; i < remotes.size(); i++) {
index = i; EntityAttachment remote = remotes.get(i);
break; if (Objects.equals(remote.name, local.name) &&
Objects.equals(remote.cid, local.cid)) {
index = i;
break;
}
} }
} }
// Match attachment by type/size // Match attachment by type/size
if (index < 0) if (index < 0) {
warning = true;
Log.w("Matching attachment by type/size");
for (int i = 0; i < remotes.size(); i++) { for (int i = 0; i < remotes.size(); i++) {
EntityAttachment remote = remotes.get(i); EntityAttachment remote = remotes.get(i);
if (Objects.equals(remote.type, local.type) && if (Objects.equals(remote.type, local.type) &&
@ -905,8 +924,9 @@ public class MessageHelper {
break; break;
} }
} }
}
if (index < 0) { if (index < 0 || warning) {
Map<String, String> crumb = new HashMap<>(); Map<String, String> crumb = new HashMap<>();
crumb.put("local", local.toString()); crumb.put("local", local.toString());
Log.w("Attachment not found local=" + local); Log.w("Attachment not found local=" + local);
@ -916,9 +936,11 @@ public class MessageHelper {
Log.w("Attachment remote=" + remote); Log.w("Attachment remote=" + remote);
} }
Log.breadcrumb("attachments", crumb); Log.breadcrumb("attachments", crumb);
throw new IllegalArgumentException("Attachment not found");
} }
if (index < 0)
throw new IllegalArgumentException("Attachment not found");
downloadAttachment(context, index, local); downloadAttachment(context, index, local);
} }