From fdd70ec6d0a6e6afb2acfcff404481d1f92da299 Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Wed, 22 Feb 2023 13:56:18 +1100 Subject: [PATCH] feat: add the last seen --- libsession-util/libsession-util | 2 +- .../messaging/jobs/BatchMessageReceiveJob.kt | 34 +++++++++++-------- .../messages/visible/VisibleMessage.kt | 1 - .../ReceivedMessageHandler.kt | 17 +++++----- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/libsession-util/libsession-util b/libsession-util/libsession-util index 943dc8a32f..4930086a34 160000 --- a/libsession-util/libsession-util +++ b/libsession-util/libsession-util @@ -1 +1 @@ -Subproject commit 943dc8a32fccd0de02028f9ff7344a5effbce0d2 +Subproject commit 4930086a348d175cff4a41e804c84f23d048951e diff --git a/libsession/src/main/java/org/session/libsession/messaging/jobs/BatchMessageReceiveJob.kt b/libsession/src/main/java/org/session/libsession/messaging/jobs/BatchMessageReceiveJob.kt index bd1a5ab545..9e876fd329 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/jobs/BatchMessageReceiveJob.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/jobs/BatchMessageReceiveJob.kt @@ -123,26 +123,27 @@ class BatchMessageReceiveJob( async { // The LinkedHashMap should preserve insertion order val messageIds = linkedMapOf>() - var myLastSeen = storage.getLastSeen(threadId) + val myLastSeen = storage.getLastSeen(threadId) + var newLastSeen = myLastSeen messages.forEach { (parameters, message, proto) -> try { when (message) { is VisibleMessage -> { - val messageId = MessageReceiver.handleVisibleMessage(message, proto, openGroupID, - runIncrement = false, - runThreadUpdate = false, - runProfileUpdate = true + val isUserBlindedSender = message.sender == serverPublicKey?.let { SodiumUtilities.blindedKeyPair(it, MessagingModuleConfiguration.shared.getUserED25519KeyPair()!!) }?.let { SessionId( + IdPrefix.BLINDED, it.publicKey.asBytes).hexString } + val sentTimestamp = message.sentTimestamp!! + if (message.sender == localUserPublicKey || isUserBlindedSender) { + if (sentTimestamp > newLastSeen) { + newLastSeen = sentTimestamp // use sent timestamp here since that is technically the last one we have + } + } + val messageId = MessageReceiver.handleVisibleMessage( + message, proto, openGroupID, + runThreadUpdate = false, + runProfileUpdate = true ) if (messageId != null && message.reaction == null) { - val isUserBlindedSender = message.sender == serverPublicKey?.let { SodiumUtilities.blindedKeyPair(it, MessagingModuleConfiguration.shared.getUserED25519KeyPair()!!) }?.let { SessionId( - IdPrefix.BLINDED, it.publicKey.asBytes).hexString } - if (message.sender == localUserPublicKey || isUserBlindedSender) { - val sentTimestamp = message.sentTimestamp - if (sentTimestamp != null && sentTimestamp > myLastSeen) { - myLastSeen = sentTimestamp // use sent timestamp here since that is technically the last one we have - } - } messageIds[messageId] = Pair( (message.sender == localUserPublicKey || isUserBlindedSender), message.hasMention @@ -176,7 +177,12 @@ class BatchMessageReceiveJob( } // increment unreads, notify, and update thread // last seen will be the current last seen if not changed (re-computes the read counts for thread record) - storage.markConversationAsRead(threadId, myLastSeen) + // might have been updated from a different thread at this point + val currentLastSeen = storage.getLastSeen(threadId) + if (currentLastSeen > newLastSeen) { + newLastSeen = currentLastSeen + } + storage.markConversationAsRead(threadId, newLastSeen) storage.updateThread(threadId, true) SSKEnvironment.shared.notificationManager.updateNotification(context, threadId) } diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/visible/VisibleMessage.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/visible/VisibleMessage.kt index e66147da18..ff85d3211c 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/visible/VisibleMessage.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/visible/VisibleMessage.kt @@ -25,7 +25,6 @@ class VisibleMessage : Message() { var openGroupInvitation: OpenGroupInvitation? = null var reaction: Reaction? = null var hasMention: Boolean = false - override val isSelfSendValid: Boolean = true // region Validation 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 2a18b07fa6..5adedc47ac 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 @@ -67,8 +67,8 @@ fun MessageReceiver.handle(message: Message, proto: SignalServiceProtos.Content, is ConfigurationMessage -> handleConfigurationMessage(message) is UnsendRequest -> handleUnsendRequest(message) is MessageRequestResponse -> handleMessageRequestResponse(message) - is VisibleMessage -> handleVisibleMessage(message, proto, openGroupID, - runIncrement = true, + is VisibleMessage -> handleVisibleMessage( + message, proto, openGroupID, runThreadUpdate = true, runProfileUpdate = true ) @@ -214,12 +214,13 @@ fun handleMessageRequestResponse(message: MessageRequestResponse) { } //endregion -fun MessageReceiver.handleVisibleMessage(message: VisibleMessage, - proto: SignalServiceProtos.Content, - openGroupID: String?, - runIncrement: Boolean, - runThreadUpdate: Boolean, - runProfileUpdate: Boolean): Long? { +fun MessageReceiver.handleVisibleMessage( + message: VisibleMessage, + proto: SignalServiceProtos.Content, + openGroupID: String?, + runThreadUpdate: Boolean, + runProfileUpdate: Boolean +): Long? { val storage = MessagingModuleConfiguration.shared.storage val context = MessagingModuleConfiguration.shared.context val userPublicKey = storage.getUserPublicKey()