Skip to content

Commit

Permalink
Fix emoji notifications (#960)
Browse files Browse the repository at this point in the history
* fix: don't notify on group threads

* fix: flip boolean and compile issue

* build: update build number
  • Loading branch information
hjubb authored Sep 7, 2022
1 parent 361ff83 commit 7d186c1
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
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;
Expand Down Expand Up @@ -67,15 +67,19 @@ public void removeMismatchedIdentity(long messageId, Address address, IdentityKe
}
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -124,39 +124,42 @@ 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
)
}

fun deleteEmojiReactions(emoji: String, messageId: MessageId) {
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
)
}

fun deleteMessageReactions(messageId: MessageId) {
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<String>) {
private fun deleteReactions(messageId: MessageId, query: String, args: Array<String>, 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -357,7 +359,7 @@ fun MessageReceiver.handleOpenGroupReactions(
serverId = "$openGroupMessageServerID",
count = count,
index = reaction.index
), reactor)
), reactor, false)
}

// Add all other reactions
Expand All @@ -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)
Expand All @@ -387,7 +389,7 @@ fun MessageReceiver.handleOpenGroupReactions(
serverId = "$openGroupMessageServerID",
count = 1,
index = reaction.index
), userPublicKey)
), userPublicKey, false)
}
}
}
Expand Down

0 comments on commit 7d186c1

Please sign in to comment.