From c9c9078c2e42d0d8c0c28b83f49fbc5081172cc4 Mon Sep 17 00:00:00 2001 From: ThomasSession Date: Mon, 21 Oct 2024 17:33:12 +1100 Subject: [PATCH] Not deleting a thread when it's empty - showing an empty snippet when thread is empty or only made up of 'marked as deleted' messages --- .../securesms/database/MmsDatabase.kt | 14 ++++---- .../securesms/database/SmsDatabase.java | 16 ++++----- .../securesms/database/Storage.kt | 2 +- .../securesms/database/ThreadDatabase.java | 36 ++++++++----------- 4 files changed, 31 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.kt index 60c4de6883..244f5db117 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/MmsDatabase.kt @@ -175,7 +175,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa ) get(context).groupReceiptDatabase() .update(ourAddress, id, status, timestamp) - get(context).threadDatabase().update(threadId, false, true) + get(context).threadDatabase().update(threadId, false) notifyConversationListeners(threadId) } } @@ -274,7 +274,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa " WHERE " + ID + " = ?", arrayOf(id.toString() + "") ) if (threadId.isPresent) { - get(context).threadDatabase().update(threadId.get(), false, true) + get(context).threadDatabase().update(threadId.get(), false) } } @@ -646,7 +646,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa ) if (!MmsSmsColumns.Types.isExpirationTimerUpdate(mailbox)) { if (runThreadUpdate) { - get(context).threadDatabase().update(threadId, true, true) + get(context).threadDatabase().update(threadId, true) } } notifyConversationListeners(threadId) @@ -791,7 +791,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa } setHasSent(threadId, true) if (runThreadUpdate) { - update(threadId, true, true) + update(threadId, true) } } return messageId @@ -930,7 +930,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa groupReceiptDatabase.deleteRowsForMessage(messageId) val database = databaseHelper.writableDatabase database!!.delete(TABLE_NAME, ID_WHERE, arrayOf(messageId.toString())) - val threadDeleted = get(context).threadDatabase().update(threadId, false, true) + val threadDeleted = get(context).threadDatabase().update(threadId, false) notifyConversationListeners(threadId) notifyStickerListeners() notifyStickerPackListeners() @@ -948,7 +948,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa argValues ) - val threadDeleted = get(context).threadDatabase().update(threadId, false, true) + val threadDeleted = get(context).threadDatabase().update(threadId, false) notifyConversationListeners(threadId) notifyStickerListeners() notifyStickerPackListeners() @@ -1145,7 +1145,7 @@ class MmsDatabase(context: Context, databaseHelper: SQLCipherOpenHelper) : Messa } val threadDb = get(context).threadDatabase() for (threadId in threadIds) { - val threadDeleted = threadDb.update(threadId, false, true) + val threadDeleted = threadDb.update(threadId, false) notifyConversationListeners(threadId) } notifyStickerListeners() diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java index ed7945e8a3..6deca0c939 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -158,7 +158,7 @@ public class SmsDatabase extends MessagingDatabase { long threadId = getThreadIdForMessage(id); - DatabaseComponent.get(context).threadDatabase().update(threadId, false, true); + DatabaseComponent.get(context).threadDatabase().update(threadId, false); notifyConversationListeners(threadId); } @@ -261,7 +261,7 @@ public class SmsDatabase extends MessagingDatabase { long threadId = getThreadIdForMessage(id); - DatabaseComponent.get(context).threadDatabase().update(threadId, false, true); + DatabaseComponent.get(context).threadDatabase().update(threadId, false); notifyConversationListeners(threadId); } @@ -346,7 +346,7 @@ public class SmsDatabase extends MessagingDatabase { ID + " = ?", new String[] {String.valueOf(cursor.getLong(cursor.getColumnIndexOrThrow(ID)))}); - DatabaseComponent.get(context).threadDatabase().update(threadId, false, true); + DatabaseComponent.get(context).threadDatabase().update(threadId, false); notifyConversationListeners(threadId); foundMessage = true; } @@ -429,7 +429,7 @@ public class SmsDatabase extends MessagingDatabase { long threadId = getThreadIdForMessage(messageId); - DatabaseComponent.get(context).threadDatabase().update(threadId, true, true); + DatabaseComponent.get(context).threadDatabase().update(threadId, true); notifyConversationListeners(threadId); notifyConversationListListeners(); @@ -504,7 +504,7 @@ public class SmsDatabase extends MessagingDatabase { long messageId = db.insert(TABLE_NAME, null, values); if (runThreadUpdate) { - DatabaseComponent.get(context).threadDatabase().update(threadId, true, true); + DatabaseComponent.get(context).threadDatabase().update(threadId, true); } if (message.getSubscriptionId() != -1) { @@ -596,7 +596,7 @@ public class SmsDatabase extends MessagingDatabase { } if (runThreadUpdate) { - DatabaseComponent.get(context).threadDatabase().update(threadId, true, true); + DatabaseComponent.get(context).threadDatabase().update(threadId, true); } long lastSeen = DatabaseComponent.get(context).threadDatabase().getLastSeenAndHasSent(threadId).first(); if (lastSeen < message.getSentTimestampMillis()) { @@ -656,7 +656,7 @@ public class SmsDatabase extends MessagingDatabase { long threadId = getThreadIdForMessage(messageId); db.delete(TABLE_NAME, ID_WHERE, new String[] {messageId+""}); notifyConversationListeners(threadId); - boolean threadDeleted = DatabaseComponent.get(context).threadDatabase().update(threadId, false, false); + boolean threadDeleted = DatabaseComponent.get(context).threadDatabase().update(threadId, false); return threadDeleted; } @@ -676,7 +676,7 @@ public class SmsDatabase extends MessagingDatabase { ID + " IN (" + StringUtils.join(argsArray, ',') + ")", argValues ); - boolean threadDeleted = DatabaseComponent.get(context).threadDatabase().update(threadId, false, true); + boolean threadDeleted = DatabaseComponent.get(context).threadDatabase().update(threadId, false); notifyConversationListeners(threadId); return threadDeleted; } diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt index 4fd9f97ede..fc27d8ebd8 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -287,7 +287,7 @@ open class Storage( override fun updateThread(threadId: Long, unarchive: Boolean) { val threadDb = DatabaseComponent.get(context).threadDatabase() - threadDb.update(threadId, unarchive, false) + threadDb.update(threadId, unarchive) } override fun persist(message: VisibleMessage, diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java index f48686aded..5cc8bd06f7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/ThreadDatabase.java @@ -188,6 +188,16 @@ public class ThreadDatabase extends Database { notifyConversationListListeners(); } + public void clearSnippet(long threadId){ + ContentValues contentValues = new ContentValues(1); + + contentValues.put(SNIPPET, ""); + + SQLiteDatabase db = databaseHelper.getWritableDatabase(); + db.update(TABLE_NAME, contentValues, ID + " = ?", new String[] {threadId + ""}); + notifyConversationListListeners(); + } + public void updateSnippet(long threadId, String snippet, @Nullable Uri attachment, long date, long type, boolean unarchive) { ContentValues contentValues = new ContentValues(4); @@ -281,7 +291,7 @@ public class ThreadDatabase extends Database { DatabaseComponent.get(context).smsDatabase().deleteMessagesInThreadBeforeDate(threadId, lastTweetDate); DatabaseComponent.get(context).mmsDatabase().deleteMessagesInThreadBeforeDate(threadId, lastTweetDate); - update(threadId, false, true); + update(threadId, false); notifyConversationListeners(threadId); } } finally { @@ -294,7 +304,7 @@ public class ThreadDatabase extends Database { Log.i("ThreadDatabase", "Trimming thread: " + threadId + " before :"+timestamp); DatabaseComponent.get(context).smsDatabase().deleteMessagesInThreadBeforeDate(threadId, timestamp); DatabaseComponent.get(context).mmsDatabase().deleteMessagesInThreadBeforeDate(threadId, timestamp); - update(threadId, false, true); + update(threadId, false); notifyConversationListeners(threadId); } @@ -722,18 +732,10 @@ public class ThreadDatabase extends Database { notifyConversationListListeners(); } - public boolean update(long threadId, boolean unarchive, boolean shouldDeleteOnEmpty) { + public boolean update(long threadId, boolean unarchive) { MmsSmsDatabase mmsSmsDatabase = DatabaseComponent.get(context).mmsSmsDatabase(); long count = mmsSmsDatabase.getConversationCount(threadId); - boolean shouldDeleteEmptyThread = shouldDeleteOnEmpty && possibleToDeleteThreadOnEmpty(threadId); - - if (count == 0 && shouldDeleteEmptyThread) { - deleteThread(threadId); - notifyConversationListListeners(); - return true; - } - try (MmsSmsDatabase.Reader reader = mmsSmsDatabase.readerFor(mmsSmsDatabase.getConversationSnippet(threadId))) { MessageRecord record = null; if (reader != null) { @@ -748,11 +750,8 @@ public class ThreadDatabase extends Database { record.getType(), unarchive, record.getExpiresIn(), record.getReadReceiptCount()); return false; } else { - if (shouldDeleteEmptyThread) { - deleteThread(threadId); - return true; - } - // todo: add empty snippet that clears existing data + // for empty threads or if there is only deleted messages, show an empty snippet + clearSnippet(threadId); return false; } } finally { @@ -800,11 +799,6 @@ public class ThreadDatabase extends Database { return setLastSeen(threadId, lastSeenTime); } - private boolean possibleToDeleteThreadOnEmpty(long threadId) { - Recipient threadRecipient = getRecipientForThreadId(threadId); - return threadRecipient != null && !threadRecipient.isCommunityRecipient(); - } - private @NonNull String getFormattedBodyFor(@NonNull MessageRecord messageRecord) { if (messageRecord.isMms()) { MmsMessageRecord record = (MmsMessageRecord) messageRecord;