hide treatAsDownloadable in search results

This commit is contained in:
Daniel Gultsch 2018-05-02 12:14:24 +02:00
parent 90135131c0
commit 9625f191c3
3 changed files with 46 additions and 27 deletions

View file

@ -20,6 +20,7 @@ import eu.siacs.conversations.ui.adapter.MessageAdapter;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.Emoticons; import eu.siacs.conversations.utils.Emoticons;
import eu.siacs.conversations.utils.GeoHelper; import eu.siacs.conversations.utils.GeoHelper;
import eu.siacs.conversations.utils.MessageUtils;
import eu.siacs.conversations.utils.MimeUtils; import eu.siacs.conversations.utils.MimeUtils;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import rocks.xmpp.addr.Jid; import rocks.xmpp.addr.Jid;
@ -709,29 +710,7 @@ public class Message extends AbstractEntity {
public synchronized boolean treatAsDownloadable() { public synchronized boolean treatAsDownloadable() {
if (treatAsDownloadable == null) { if (treatAsDownloadable == null) {
try { treatAsDownloadable = MessageUtils.treatAsDownloadable(this.body, this.oob);
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;
}
} }
return treatAsDownloadable; return treatAsDownloadable;
} }

View file

@ -47,6 +47,7 @@ import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.StubConversation; import eu.siacs.conversations.entities.StubConversation;
import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable; import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable;
import eu.siacs.conversations.utils.Cancellable; import eu.siacs.conversations.utils.Cancellable;
import eu.siacs.conversations.utils.MessageUtils;
import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor; import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor;
import rocks.xmpp.addr.Jid; import rocks.xmpp.addr.Jid;
@ -94,17 +95,28 @@ public class MessageSearchTask implements Runnable, Cancellable {
} }
if (cursor != null && cursor.getCount() > 0) { if (cursor != null && cursor.getCount() > 0) {
cursor.moveToLast(); 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 { do {
if (isCancelled) { if (isCancelled) {
Log.d(Config.LOGTAG, "canceled search task"); Log.d(Config.LOGTAG, "canceled search task");
return; 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); Conversational conversation = conversationCache.get(conversationUuid);
if (conversation == null) { if (conversation == null) {
String accountUuid = cursor.getString(cursor.getColumnIndex(Conversation.ACCOUNT)); String accountUuid = cursor.getString(indexAccount);
String contactJid = cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID)); String contactJid = cursor.getString(indexContact);
int mode = cursor.getInt(cursor.getColumnIndex(Conversation.MODE)); int mode = cursor.getInt(indexMode);
conversation = findOrGenerateStub(conversationUuid, accountUuid, contactJid, mode); conversation = findOrGenerateStub(conversationUuid, accountUuid, contactJid, mode);
conversationCache.put(conversationUuid, conversation); conversationCache.put(conversationUuid, conversation);
} }

View file

@ -29,7 +29,11 @@
package eu.siacs.conversations.utils; package eu.siacs.conversations.utils;
import java.net.MalformedURLException;
import java.net.URL;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.http.AesGcmURLStreamHandler;
public class MessageUtils { public class MessageUtils {
public static String prepareQuote(Message message) { public static String prepareQuote(Message message) {
@ -51,4 +55,28 @@ public class MessageUtils {
} }
return builder.toString(); 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;
}
}
} }