From 9625f191c31d29183c7ad5ce6c0f1be57f203d41 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 2 May 2018 12:14:24 +0200 Subject: [PATCH] hide treatAsDownloadable in search results --- .../siacs/conversations/entities/Message.java | 25 ++--------------- .../services/MessageSearchTask.java | 20 ++++++++++--- .../conversations/utils/MessageUtils.java | 28 +++++++++++++++++++ 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index 70cbb7979..99d13de17 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -20,6 +20,7 @@ import eu.siacs.conversations.ui.adapter.MessageAdapter; import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.Emoticons; import eu.siacs.conversations.utils.GeoHelper; +import eu.siacs.conversations.utils.MessageUtils; import eu.siacs.conversations.utils.MimeUtils; import eu.siacs.conversations.utils.UIHelper; import rocks.xmpp.addr.Jid; @@ -709,29 +710,7 @@ public class Message extends AbstractEntity { public synchronized boolean treatAsDownloadable() { if (treatAsDownloadable == null) { - try { - final String[] lines = body.split("\n"); - if (lines.length == 0) { - treatAsDownloadable = false; - return false; - } - for (String line : lines) { - if (line.contains("\\s+")) { - treatAsDownloadable = false; - return false; - } - } - final URL url = new URL(lines[0]); - final String ref = url.getRef(); - final String protocol = url.getProtocol(); - final boolean encrypted = ref != null && AesGcmURLStreamHandler.IV_KEY.matcher(ref).matches(); - final boolean followedByDataUri = lines.length == 2 && lines[1].startsWith("data:"); - final boolean validAesGcm = AesGcmURLStreamHandler.PROTOCOL_NAME.equalsIgnoreCase(protocol) && encrypted && (lines.length == 1 || followedByDataUri); - final boolean validOob = ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) && (oob || encrypted) && lines.length == 1; - treatAsDownloadable = validAesGcm || validOob; - } catch (MalformedURLException e) { - treatAsDownloadable = false; - } + treatAsDownloadable = MessageUtils.treatAsDownloadable(this.body, this.oob); } return treatAsDownloadable; } diff --git a/src/main/java/eu/siacs/conversations/services/MessageSearchTask.java b/src/main/java/eu/siacs/conversations/services/MessageSearchTask.java index 91dc4feed..338af5cbe 100644 --- a/src/main/java/eu/siacs/conversations/services/MessageSearchTask.java +++ b/src/main/java/eu/siacs/conversations/services/MessageSearchTask.java @@ -47,6 +47,7 @@ import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.StubConversation; import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable; import eu.siacs.conversations.utils.Cancellable; +import eu.siacs.conversations.utils.MessageUtils; import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor; import rocks.xmpp.addr.Jid; @@ -94,17 +95,28 @@ public class MessageSearchTask implements Runnable, Cancellable { } if (cursor != null && cursor.getCount() > 0) { cursor.moveToLast(); + final int indexBody = cursor.getColumnIndex(Message.BODY); + final int indexOob = cursor.getColumnIndex(Message.OOB); + final int indexConversation = cursor.getColumnIndex(Message.CONVERSATION); + final int indexAccount = cursor.getColumnIndex(Conversation.ACCOUNT); + final int indexContact = cursor.getColumnIndex(Conversation.CONTACTJID); + final int indexMode = cursor.getColumnIndex(Conversation.MODE); do { if (isCancelled) { Log.d(Config.LOGTAG, "canceled search task"); return; } - final String conversationUuid = cursor.getString(cursor.getColumnIndex(Message.CONVERSATION)); + final String body = cursor.getString(indexBody); + final boolean oob = cursor.getInt(indexOob) > 0; + if (MessageUtils.treatAsDownloadable(body,oob)) { + continue; + } + final String conversationUuid = cursor.getString(indexConversation); Conversational conversation = conversationCache.get(conversationUuid); if (conversation == null) { - String accountUuid = cursor.getString(cursor.getColumnIndex(Conversation.ACCOUNT)); - String contactJid = cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID)); - int mode = cursor.getInt(cursor.getColumnIndex(Conversation.MODE)); + String accountUuid = cursor.getString(indexAccount); + String contactJid = cursor.getString(indexContact); + int mode = cursor.getInt(indexMode); conversation = findOrGenerateStub(conversationUuid, accountUuid, contactJid, mode); conversationCache.put(conversationUuid, conversation); } diff --git a/src/main/java/eu/siacs/conversations/utils/MessageUtils.java b/src/main/java/eu/siacs/conversations/utils/MessageUtils.java index 6e87d72db..6abfea0b2 100644 --- a/src/main/java/eu/siacs/conversations/utils/MessageUtils.java +++ b/src/main/java/eu/siacs/conversations/utils/MessageUtils.java @@ -29,7 +29,11 @@ package eu.siacs.conversations.utils; +import java.net.MalformedURLException; +import java.net.URL; + import eu.siacs.conversations.entities.Message; +import eu.siacs.conversations.http.AesGcmURLStreamHandler; public class MessageUtils { public static String prepareQuote(Message message) { @@ -51,4 +55,28 @@ public class MessageUtils { } return builder.toString(); } + + public static boolean treatAsDownloadable(final String body, final boolean oob) { + try { + final String[] lines = body.split("\n"); + if (lines.length == 0) { + return false; + } + for (String line : lines) { + if (line.contains("\\s+")) { + return false; + } + } + final URL url = new URL(lines[0]); + final String ref = url.getRef(); + final String protocol = url.getProtocol(); + final boolean encrypted = ref != null && AesGcmURLStreamHandler.IV_KEY.matcher(ref).matches(); + final boolean followedByDataUri = lines.length == 2 && lines[1].startsWith("data:"); + final boolean validAesGcm = AesGcmURLStreamHandler.PROTOCOL_NAME.equalsIgnoreCase(protocol) && encrypted && (lines.length == 1 || followedByDataUri); + final boolean validOob = ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) && (oob || encrypted) && lines.length == 1; + return validAesGcm || validOob; + } catch (MalformedURLException e) { + return false; + } + } }