From 7d186c198ebdb5355484b7b19e516e57a2b088ea Mon Sep 17 00:00:00 2001 From: Harris Date: Wed, 7 Sep 2022 14:41:39 +1000 Subject: [PATCH] Fix emoji notifications (#960) * fix: don't notify on group threads * fix: flip boolean and compile issue * build: update build number --- app/build.gradle | 4 ++-- .../conversation/v2/ConversationActivityV2.kt | 4 ++-- .../securesms/database/MessagingDatabase.java | 16 ++++++++----- .../securesms/database/ReactionDatabase.kt | 23 +++++++++++-------- .../securesms/database/Storage.kt | 9 ++++---- .../libsession/database/StorageProtocol.kt | 4 ++-- .../ReceivedMessageHandler.kt | 12 ++++++---- 7 files changed, 41 insertions(+), 31 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 4906743316..9ddaf98b94 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -156,8 +156,8 @@ dependencies { testImplementation 'org.robolectric:shadows-multidex:4.4' } -def canonicalVersionCode = 296 -def canonicalVersionName = "1.15.1" +def canonicalVersionCode = 297 +def canonicalVersionName = "1.15.2" def postFixSize = 10 def abiPostFix = ['armeabi-v7a' : 1, diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 5d48734385..dcf7f11cf6 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -1054,7 +1054,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe dateSent = emojiTimestamp, dateReceived = emojiTimestamp ) - reactionDb.addReaction(MessageId(originalMessage.id, originalMessage.isMms), reaction) + reactionDb.addReaction(MessageId(originalMessage.id, originalMessage.isMms), reaction, false) val originalAuthor = if (originalMessage.isOutgoing) { fromSerialized(viewModel.blindedPublicKey ?: textSecurePreferences.getLocalNumber()!!) } else originalMessage.individualRecipient.address @@ -1077,7 +1077,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe val emojiTimestamp = System.currentTimeMillis() message.sentTimestamp = emojiTimestamp val author = textSecurePreferences.getLocalNumber()!! - reactionDb.deleteReaction(emoji, MessageId(originalMessage.id, originalMessage.isMms), author) + reactionDb.deleteReaction(emoji, MessageId(originalMessage.id, originalMessage.isMms), author, false) val originalAuthor = if (originalMessage.isOutgoing) { fromSerialized(viewModel.blindedPublicKey ?: textSecurePreferences.getLocalNumber()!!) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/MessagingDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/MessagingDatabase.java index 022338687d..ffde5ca029 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/MessagingDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/MessagingDatabase.java @@ -13,10 +13,10 @@ import org.session.libsession.utilities.IdentityKeyMismatch; import org.session.libsession.utilities.IdentityKeyMismatchList; import org.session.libsignal.crypto.IdentityKey; import org.session.libsignal.utilities.JsonUtil; -import org.thoughtcrime.securesms.database.model.MessageRecord; -import org.thoughtcrime.securesms.util.SqlUtil; import org.session.libsignal.utilities.Log; import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper; +import org.thoughtcrime.securesms.database.model.MessageRecord; +import org.thoughtcrime.securesms.util.SqlUtil; import java.io.IOException; import java.util.ArrayList; @@ -67,15 +67,19 @@ public abstract class MessagingDatabase extends Database implements MmsSmsColumn } } - void updateReactionsUnread(SQLiteDatabase db, long messageId, boolean hasReactions, boolean isRemoval) { + void updateReactionsUnread(SQLiteDatabase db, long messageId, boolean hasReactions, boolean isRemoval, boolean notifyUnread) { try { MessageRecord message = getMessageRecord(messageId); ContentValues values = new ContentValues(); - if (!hasReactions) { + if (notifyUnread) { + if (!hasReactions) { + values.put(REACTIONS_UNREAD, 0); + } else if (!isRemoval) { + values.put(REACTIONS_UNREAD, 1); + } + } else { values.put(REACTIONS_UNREAD, 0); - } else if (!isRemoval) { - values.put(REACTIONS_UNREAD, 1); } if (message.isOutgoing() && hasReactions) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/ReactionDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/database/ReactionDatabase.kt index 9d5ec69c51..74e452db07 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/ReactionDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/ReactionDatabase.kt @@ -94,7 +94,7 @@ class ReactionDatabase(context: Context, helper: SQLCipherOpenHelper) : Database return reactions } - fun addReaction(messageId: MessageId, reaction: ReactionRecord) { + fun addReaction(messageId: MessageId, reaction: ReactionRecord, notifyUnread: Boolean) { writableDatabase.beginTransaction() try { @@ -113,9 +113,9 @@ class ReactionDatabase(context: Context, helper: SQLCipherOpenHelper) : Database writableDatabase.insert(TABLE_NAME, null, values) if (messageId.mms) { - DatabaseComponent.get(context).mmsDatabase().updateReactionsUnread(writableDatabase, messageId.id, hasReactions(messageId), false) + DatabaseComponent.get(context).mmsDatabase().updateReactionsUnread(writableDatabase, messageId.id, hasReactions(messageId), false, notifyUnread) } else { - DatabaseComponent.get(context).smsDatabase().updateReactionsUnread(writableDatabase, messageId.id, hasReactions(messageId), false) + DatabaseComponent.get(context).smsDatabase().updateReactionsUnread(writableDatabase, messageId.id, hasReactions(messageId), false, notifyUnread) } writableDatabase.setTransactionSuccessful() @@ -124,11 +124,12 @@ class ReactionDatabase(context: Context, helper: SQLCipherOpenHelper) : Database } } - fun deleteReaction(emoji: String, messageId: MessageId, author: String) { + fun deleteReaction(emoji: String, messageId: MessageId, author: String, notifyUnread: Boolean) { deleteReactions( messageId = messageId, query = "$MESSAGE_ID = ? AND $IS_MMS = ? AND $EMOJI = ? AND $AUTHOR_ID = ?", - args = arrayOf("${messageId.id}", "${if (messageId.mms) 1 else 0}", emoji, author) + args = arrayOf("${messageId.id}", "${if (messageId.mms) 1 else 0}", emoji, author), + notifyUnread ) } @@ -136,7 +137,8 @@ class ReactionDatabase(context: Context, helper: SQLCipherOpenHelper) : Database deleteReactions( messageId = messageId, query = "$MESSAGE_ID = ? AND $IS_MMS = ? AND $EMOJI = ?", - args = arrayOf("${messageId.id}", "${if (messageId.mms) 1 else 0}", emoji) + args = arrayOf("${messageId.id}", "${if (messageId.mms) 1 else 0}", emoji), + false ) } @@ -144,19 +146,20 @@ class ReactionDatabase(context: Context, helper: SQLCipherOpenHelper) : Database deleteReactions( messageId = messageId, query = "$MESSAGE_ID = ? AND $IS_MMS = ?", - args = arrayOf("${messageId.id}", "${if (messageId.mms) 1 else 0}") + args = arrayOf("${messageId.id}", "${if (messageId.mms) 1 else 0}"), + false ) } - private fun deleteReactions(messageId: MessageId, query: String, args: Array) { + private fun deleteReactions(messageId: MessageId, query: String, args: Array, notifyUnread: Boolean) { writableDatabase.beginTransaction() try { writableDatabase.delete(TABLE_NAME, query, args) if (messageId.mms) { - DatabaseComponent.get(context).mmsDatabase().updateReactionsUnread(writableDatabase, messageId.id, hasReactions(messageId), true) + DatabaseComponent.get(context).mmsDatabase().updateReactionsUnread(writableDatabase, messageId.id, hasReactions(messageId), true, notifyUnread) } else { - DatabaseComponent.get(context).smsDatabase().updateReactionsUnread(writableDatabase, messageId.id, hasReactions(messageId), true) + DatabaseComponent.get(context).smsDatabase().updateReactionsUnread(writableDatabase, messageId.id, hasReactions(messageId), true, notifyUnread) } writableDatabase.setTransactionSuccessful() 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 c374afdebd..ef14ed83e4 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/Storage.kt @@ -892,7 +892,7 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context, return mapping } - override fun addReaction(reaction: Reaction, messageSender: String) { + override fun addReaction(reaction: Reaction, messageSender: String, notifyUnread: Boolean) { val timestamp = reaction.timestamp val localId = reaction.localId val isMms = reaction.isMms @@ -914,14 +914,15 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context, sortId = reaction.index!!, dateSent = reaction.dateSent!!, dateReceived = reaction.dateReceived!! - ) + ), + notifyUnread ) } - override fun removeReaction(emoji: String, messageTimestamp: Long, author: String) { + override fun removeReaction(emoji: String, messageTimestamp: Long, author: String, notifyUnread: Boolean) { val messageRecord = DatabaseComponent.get(context).mmsSmsDatabase().getMessageForTimestamp(messageTimestamp) ?: return val messageId = MessageId(messageRecord.id, messageRecord.isMms) - DatabaseComponent.get(context).reactionDatabase().deleteReaction(emoji, messageId, author) + DatabaseComponent.get(context).reactionDatabase().deleteReaction(emoji, messageId, author, notifyUnread) } override fun updateReactionIfNeeded(message: Message, sender: String, openGroupSentTimestamp: Long) { diff --git a/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt b/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt index ec81e16767..8d9273a063 100644 --- a/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt +++ b/libsession/src/main/java/org/session/libsession/database/StorageProtocol.kt @@ -190,8 +190,8 @@ interface StorageProtocol { fun removeLastOutboxMessageId(server: String) fun getOrCreateBlindedIdMapping(blindedId: String, server: String, serverPublicKey: String, fromOutbox: Boolean = false): BlindedIdMapping - fun addReaction(reaction: Reaction, messageSender: String) - fun removeReaction(emoji: String, messageTimestamp: Long, author: String) + fun addReaction(reaction: Reaction, messageSender: String, notifyUnread: Boolean) + fun removeReaction(emoji: String, messageTimestamp: Long, author: String, notifyUnread: Boolean) fun updateReactionIfNeeded(message: Message, sender: String, openGroupSentTimestamp: Long) fun deleteReactions(messageId: Long, mms: Boolean) } diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt index 152b5aff72..1322da58d2 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/ReceivedMessageHandler.kt @@ -230,6 +230,7 @@ fun MessageReceiver.handleVisibleMessage(message: VisibleMessage, // Thread doesn't exist; should only be reached in a case where we are processing open group messages for a no longer existent thread throw MessageReceiver.Error.NoThread } + val threadRecipient = storage.getRecipientForThread(threadID) // Update profile if needed val recipient = Recipient.from(context, Address.fromSerialized(messageSender!!), false) if (runProfileUpdate) { @@ -295,14 +296,15 @@ fun MessageReceiver.handleVisibleMessage(message: VisibleMessage, } } // Parse reaction if needed + val threadIsGroup = threadRecipient?.isGroupRecipient == true message.reaction?.let { reaction -> if (reaction.react == true) { reaction.serverId = message.openGroupServerMessageID?.toString() ?: message.serverHash.orEmpty() reaction.dateSent = message.sentTimestamp ?: 0 reaction.dateReceived = message.receivedTimestamp ?: 0 - storage.addReaction(reaction, messageSender) + storage.addReaction(reaction, messageSender, !threadIsGroup) } else { - storage.removeReaction(reaction.emoji!!, reaction.timestamp!!, reaction.publicKey!!) + storage.removeReaction(reaction.emoji!!, reaction.timestamp!!, reaction.publicKey!!, threadIsGroup) } } ?: run { // Persist the message @@ -357,7 +359,7 @@ fun MessageReceiver.handleOpenGroupReactions( serverId = "$openGroupMessageServerID", count = count, index = reaction.index - ), reactor) + ), reactor, false) } // Add all other reactions @@ -373,7 +375,7 @@ fun MessageReceiver.handleOpenGroupReactions( serverId = "$openGroupMessageServerID", count = 0, // Only want this on the first reaction index = reaction.index - ), reactor) + ), reactor, false) } // Add the current user reaction (if applicable and not already included) @@ -387,7 +389,7 @@ fun MessageReceiver.handleOpenGroupReactions( serverId = "$openGroupMessageServerID", count = 1, index = reaction.index - ), userPublicKey) + ), userPublicKey, false) } } }