From ba93fdd6496652d65db58daaf4536c6cb3a4f49b Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 5 Dec 2019 12:59:37 +1100 Subject: [PATCH] Only show one session reset prompt at a time --- .../securesms/database/MmsSmsColumns.java | 7 +++++++ .../securesms/database/SmsDatabase.java | 20 +++++++++++++++++++ .../securesms/jobs/PushDecryptJob.java | 14 ++++++------- .../securesms/loki/FriendRequestHandler.kt | 2 ++ .../securesms/util/TextSecurePreferences.java | 8 ++++++++ 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/MmsSmsColumns.java b/src/org/thoughtcrime/securesms/database/MmsSmsColumns.java index 3872472a6d..d053ccc74e 100644 --- a/src/org/thoughtcrime/securesms/database/MmsSmsColumns.java +++ b/src/org/thoughtcrime/securesms/database/MmsSmsColumns.java @@ -82,6 +82,9 @@ public interface MmsSmsColumns { protected static final long ENCRYPTION_REMOTE_DUPLICATE_BIT = 0x04000000; protected static final long ENCRYPTION_REMOTE_LEGACY_BIT = 0x02000000; + // Loki + protected static final long ENCRYPTION_LOKI_SESSION_RESTORE_SENT_BIT = 0x09000000; // Type that determines if we've sent a session reset after receiving `ENCRYPTION_REMOTE_NO_SESSION_BIT` + public static boolean isDraftMessageType(long type) { return (type & BASE_TYPE_MASK) == BASE_DRAFT_TYPE; } @@ -230,6 +233,10 @@ public interface MmsSmsColumns { return (type & ENCRYPTION_REMOTE_NO_SESSION_BIT) != 0; } + public static boolean isLokiSessionRestoreSentType(long type) { + return (type & ENCRYPTION_LOKI_SESSION_RESTORE_SENT_BIT) != 0; + } + public static boolean isLegacyType(long type) { return (type & ENCRYPTION_REMOTE_LEGACY_BIT) != 0 || (type & ENCRYPTION_REMOTE_BIT) != 0; diff --git a/src/org/thoughtcrime/securesms/database/SmsDatabase.java b/src/org/thoughtcrime/securesms/database/SmsDatabase.java index 112ddc463b..35086df927 100644 --- a/src/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -47,6 +47,7 @@ import org.whispersystems.libsignal.util.guava.Optional; import java.io.IOException; import java.security.SecureRandom; +import java.util.ArrayList; import java.util.HashSet; import java.util.LinkedList; import java.util.List; @@ -251,6 +252,10 @@ public class SmsDatabase extends MessagingDatabase { updateTypeBitmask(id, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_NO_SESSION_BIT); } + public void markAsLokiSessionRestoreSent(long id) { + updateTypeBitmask(id, Types.ENCRYPTION_MASK, Types.ENCRYPTION_LOKI_SESSION_RESTORE_SENT_BIT); + } + public void markAsLegacyVersion(long id) { updateTypeBitmask(id, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_LEGACY_BIT); } @@ -727,6 +732,21 @@ public class SmsDatabase extends MessagingDatabase { else return record; } + public List getMessages(long threadId) { + SQLiteDatabase db = databaseHelper.getReadableDatabase(); + Cursor cursor = db.query(TABLE_NAME, MESSAGE_PROJECTION, THREAD_ID + " = ?", new String[]{ threadId + "" }, null, null, null); + Reader reader = new Reader(cursor); + List records = new ArrayList<>(); + SmsMessageRecord record = reader.getNext(); + while (record != null) { + records.add(record); + record = reader.getNext(); + } + + reader.close(); + return records; + } + public Cursor getMessageCursor(long messageId) { SQLiteDatabase db = databaseHelper.getReadableDatabase(); Cursor cursor = db.query(TABLE_NAME, MESSAGE_PROJECTION, ID_WHERE, new String[] {messageId + ""}, null, null, null); diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 1c259cc95f..1270fe6909 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -42,7 +42,6 @@ import org.thoughtcrime.securesms.crypto.storage.SignalProtocolStoreImpl; import org.thoughtcrime.securesms.crypto.storage.TextSecureSessionStore; import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.database.AttachmentDatabase; -import org.thoughtcrime.securesms.database.Database; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.GroupDatabase; import org.thoughtcrime.securesms.database.GroupReceiptDatabase; @@ -131,7 +130,6 @@ import org.whispersystems.signalservice.api.messages.shared.SharedContact; import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.loki.api.DeviceLinkingSession; import org.whispersystems.signalservice.loki.api.LokiAPI; -import org.whispersystems.signalservice.loki.api.LokiPublicChatAPI; import org.whispersystems.signalservice.loki.api.LokiStorageAPI; import org.whispersystems.signalservice.loki.api.PairingAuthorisation; import org.whispersystems.signalservice.loki.crypto.LokiServiceCipher; @@ -148,7 +146,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; -import java.util.Set; import javax.inject.Inject; @@ -1404,11 +1401,14 @@ public class PushDecryptJob extends BaseJob implements InjectableType { SmsDatabase smsDatabase = DatabaseFactory.getSmsDatabase(context); if (!smsMessageId.isPresent()) { - Optional insertResult = insertPlaceholder(sender, senderDevice, timestamp); + if (!TextSecurePreferences.isShowingSessionRestorePrompt(context, sender)) { + Optional insertResult = insertPlaceholder(sender, senderDevice, timestamp); - if (insertResult.isPresent()) { - smsDatabase.markAsNoSession(insertResult.get().getMessageId()); - MessageNotifier.updateNotification(context, insertResult.get().getThreadId()); + if (insertResult.isPresent()) { + smsDatabase.markAsNoSession(insertResult.get().getMessageId()); + TextSecurePreferences.setShowingSessionRestorePrompt(context, sender, true); + //MessageNotifier.updateNotification(context, insertResult.get().getThreadId()); + } } } else { smsDatabase.markAsNoSession(smsMessageId.get()); diff --git a/src/org/thoughtcrime/securesms/loki/FriendRequestHandler.kt b/src/org/thoughtcrime/securesms/loki/FriendRequestHandler.kt index 050f9a1bb6..0ecffedb55 100644 --- a/src/org/thoughtcrime/securesms/loki/FriendRequestHandler.kt +++ b/src/org/thoughtcrime/securesms/loki/FriendRequestHandler.kt @@ -56,6 +56,8 @@ object FriendRequestHandler { @JvmStatic fun updateLastFriendRequestMessage(context: Context, threadId: Long, status: LokiMessageFriendRequestStatus) { if (threadId < 0) { return } + val recipient = DatabaseFactory.getThreadDatabase(context).getRecipientForThreadId(threadId) ?: return + if (!recipient.address.isPhone) { return } val messages = DatabaseFactory.getSmsDatabase(context).getAllMessageIDs(threadId) val lokiMessageDatabase = DatabaseFactory.getLokiMessageDatabase(context) diff --git a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java index 0868ae8005..463efdb21b 100644 --- a/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java +++ b/src/org/thoughtcrime/securesms/util/TextSecurePreferences.java @@ -1228,6 +1228,14 @@ public class TextSecurePreferences { public static long getRestorationTime(Context context) { return getLongPreference(context, "restoration_time", 0); } + + public static void setShowingSessionRestorePrompt(Context context, String sender, boolean showingPrompt) { + setBooleanPreference(context, sender + "_showing_session_reset", showingPrompt); + } + + public static boolean isShowingSessionRestorePrompt(Context context, String sender) { + return getBooleanPreference(context, sender + "_showing_session_reset", false); + } // endregion public static void clearAll(Context context) {