From e84a65bc8690f73807bcfbcbcfacf3857257975e Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 9 May 2018 11:09:01 +0200 Subject: [PATCH] do not load conversations with null jid --- .../siacs/conversations/entities/Conversation.java | 11 +++-------- .../conversations/persistance/DatabaseBackend.java | 12 ++++++++++-- .../java/eu/siacs/conversations/utils/JidHelper.java | 9 +++++++++ .../java/eu/siacs/conversations/xmpp/InvalidJid.java | 1 + 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index 4c689ab74..a4cc0a6a7 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -23,6 +23,8 @@ import eu.siacs.conversations.Config; import eu.siacs.conversations.crypto.OmemoSetting; import eu.siacs.conversations.crypto.PgpDecryptionService; import eu.siacs.conversations.crypto.axolotl.AxolotlService; +import eu.siacs.conversations.utils.JidHelper; +import eu.siacs.conversations.xmpp.InvalidJid; import eu.siacs.conversations.xmpp.chatstate.ChatState; import eu.siacs.conversations.xmpp.mam.MamReference; import rocks.xmpp.addr.Jid; @@ -104,18 +106,11 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl } public static Conversation fromCursor(Cursor cursor) { - Jid jid; - try { - jid = Jid.of(cursor.getString(cursor.getColumnIndex(CONTACTJID))); - } catch (final IllegalArgumentException e) { - // Borked DB.. - jid = null; - } return new Conversation(cursor.getString(cursor.getColumnIndex(UUID)), cursor.getString(cursor.getColumnIndex(NAME)), cursor.getString(cursor.getColumnIndex(CONTACT)), cursor.getString(cursor.getColumnIndex(ACCOUNT)), - jid, + JidHelper.parseOrFallbackToInvalid(cursor.getString(cursor.getColumnIndex(CONTACTJID))), cursor.getLong(cursor.getColumnIndex(CREATED)), cursor.getInt(cursor.getColumnIndex(STATUS)), cursor.getInt(cursor.getColumnIndex(MODE)), diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index 9bb35ebbd..55cf52027 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -53,6 +53,7 @@ import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.FtsUtils; import eu.siacs.conversations.utils.MimeUtils; import eu.siacs.conversations.utils.Resolver; +import eu.siacs.conversations.xmpp.InvalidJid; import eu.siacs.conversations.xmpp.mam.MamReference; import rocks.xmpp.addr.Jid; @@ -680,10 +681,14 @@ public class DatabaseBackend extends SQLiteOpenHelper { SQLiteDatabase db = this.getReadableDatabase(); String[] selectionArgs = {Integer.toString(status)}; Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME - + " where " + Conversation.STATUS + " = ? order by " + + " where " + Conversation.STATUS + " = ? and "+Conversation.CONTACTJID+" is not null order by " + Conversation.CREATED + " desc", selectionArgs); while (cursor.moveToNext()) { - list.add(Conversation.fromCursor(cursor)); + final Conversation conversation = Conversation.fromCursor(cursor); + if (conversation.getJid() instanceof InvalidJid) { + continue; + } + list.add(conversation); } cursor.close(); return list; @@ -780,6 +785,9 @@ public class DatabaseBackend extends SQLiteOpenHelper { cursor.moveToFirst(); Conversation conversation = Conversation.fromCursor(cursor); cursor.close(); + if (conversation.getJid() instanceof InvalidJid) { + return null; + } return conversation; } diff --git a/src/main/java/eu/siacs/conversations/utils/JidHelper.java b/src/main/java/eu/siacs/conversations/utils/JidHelper.java index 1d3df0994..562e179ab 100644 --- a/src/main/java/eu/siacs/conversations/utils/JidHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/JidHelper.java @@ -34,6 +34,7 @@ import java.util.Arrays; import java.util.List; import java.util.Locale; +import eu.siacs.conversations.xmpp.InvalidJid; import rocks.xmpp.addr.Jid; public class JidHelper { @@ -50,4 +51,12 @@ public class JidHelper { } } + public static Jid parseOrFallbackToInvalid(String jid) { + try { + return Jid.of(jid); + } catch (IllegalArgumentException e) { + return InvalidJid.of(jid, true); + } + } + } diff --git a/src/main/java/eu/siacs/conversations/xmpp/InvalidJid.java b/src/main/java/eu/siacs/conversations/xmpp/InvalidJid.java index de35ac73d..c98da2a16 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/InvalidJid.java +++ b/src/main/java/eu/siacs/conversations/xmpp/InvalidJid.java @@ -41,6 +41,7 @@ public class InvalidJid implements Jid { private InvalidJid(String jid) { this.value = jid; } + public static Jid of(String jid, boolean fallback) { final int pos = jid.indexOf('/'); if (fallback && pos >= 0 && jid.length() >= pos + 1) {