From 637b7f8249efda3cd7964c73b1d7909f119651e3 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 9 Feb 2024 11:13:10 +1030 Subject: [PATCH] Fix expiry mode application to Message --- .../conversation/v2/ConversationActivityV2.kt | 2 +- .../securesms/webrtc/CallManager.kt | 34 +++++++++++-------- .../libsession/messaging/messages/Message.kt | 6 ---- .../messaging/messages/control/CallMessage.kt | 6 ++-- .../sending_receiving/MessageSender.kt | 2 +- 5 files changed, 25 insertions(+), 25 deletions(-) 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 6078572406..9019322a6c 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 @@ -1617,7 +1617,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe val sentTimestamp = SnodeAPI.nowWithOffset processMessageRequestApproval() // Create the message - val message = VisibleMessage().applyExpiryMode() + val message = VisibleMessage().applyExpiryMode(viewModel.threadId) message.sentTimestamp = sentTimestamp message.text = body val quote = quotedMessage?.let { diff --git a/app/src/main/java/org/thoughtcrime/securesms/webrtc/CallManager.kt b/app/src/main/java/org/thoughtcrime/securesms/webrtc/CallManager.kt index 6a441b2152..272f2c12db 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/webrtc/CallManager.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/webrtc/CallManager.kt @@ -26,6 +26,7 @@ import org.session.libsession.utilities.Util import org.session.libsession.utilities.recipients.Recipient import org.session.libsignal.protos.SignalServiceProtos.CallMessage.Type.ICE_CANDIDATES import org.session.libsignal.utilities.Log +import org.thoughtcrime.securesms.dependencies.DatabaseComponent import org.thoughtcrime.securesms.webrtc.CallManager.StateEvent.AudioDeviceUpdate import org.thoughtcrime.securesms.webrtc.CallManager.StateEvent.AudioEnabled import org.thoughtcrime.securesms.webrtc.CallManager.StateEvent.RecipientUpdate @@ -299,6 +300,7 @@ class CallManager( currentPendings.add(pendingOutgoingIceUpdates.pop()) } + val thread = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(expectedRecipient) CallMessage( ICE_CANDIDATES, sdps = currentPendings.map(IceCandidate::sdp), @@ -306,7 +308,7 @@ class CallManager( sdpMids = currentPendings.map(IceCandidate::sdpMid), currentCallId ) - .applyExpiryMode() + .applyExpiryMode(thread) .also { MessageSender.sendNonDurably(it, currentRecipient.address, isSyncMessage = currentRecipient.isLocalNumber) } } } @@ -423,6 +425,7 @@ class CallManager( fun onNewOffer(offer: String, callId: UUID, recipient: Recipient): Promise { if (callId != this.callId) return Promise.ofFail(NullPointerException("No callId")) if (recipient != this.recipient) return Promise.ofFail(NullPointerException("No recipient")) + val thread = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(recipient) val connection = peerConnection ?: return Promise.ofFail(NullPointerException("No peer connection wrapper")) @@ -435,11 +438,9 @@ class CallManager( mandatory.add(MediaConstraints.KeyValuePair("IceRestart", "true")) }) connection.setLocalDescription(answer) - pendingIncomingIceUpdates.toList().forEach { update -> - connection.addIceCandidate(update) - } + pendingIncomingIceUpdates.toList().forEach(connection::addIceCandidate) pendingIncomingIceUpdates.clear() - val answerMessage = CallMessage.answer(answer.description, callId) + val answerMessage = CallMessage.answer(answer.description, callId).applyExpiryMode(thread) Log.i("Loki", "Posting new answer") MessageSender.sendNonDurably(answerMessage, recipient.address, isSyncMessage = recipient.isLocalNumber) } else { @@ -483,13 +484,14 @@ class CallManager( connection.setRemoteDescription(SessionDescription(SessionDescription.Type.OFFER, offer)) val answer = connection.createAnswer(MediaConstraints()) connection.setLocalDescription(answer) - val answerMessage = CallMessage.answer(answer.description, callId) + val thread = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(recipient) + val answerMessage = CallMessage.answer(answer.description, callId).applyExpiryMode(thread) val userAddress = storage.getUserPublicKey() ?: return Promise.ofFail(NullPointerException("No user public key")) MessageSender.sendNonDurably(answerMessage, Address.fromSerialized(userAddress), isSyncMessage = true) val sendAnswerMessage = MessageSender.sendNonDurably(CallMessage.answer( answer.description, callId - ), recipient.address, isSyncMessage = recipient.isLocalNumber) + ).applyExpiryMode(thread), recipient.address, isSyncMessage = recipient.isLocalNumber) insertCallMessage(recipient.address.serialize(), CallMessageType.CALL_INCOMING, false) @@ -537,15 +539,16 @@ class CallManager( connection.setLocalDescription(offer) Log.d("Loki", "Sending pre-offer") + val thread = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(recipient) return MessageSender.sendNonDurably(CallMessage.preOffer( callId - ), recipient.address, isSyncMessage = recipient.isLocalNumber).bind { + ).applyExpiryMode(thread), recipient.address, isSyncMessage = recipient.isLocalNumber).bind { Log.d("Loki", "Sent pre-offer") Log.d("Loki", "Sending offer") MessageSender.sendNonDurably(CallMessage.offer( offer.description, callId - ), recipient.address, isSyncMessage = recipient.isLocalNumber).success { + ).applyExpiryMode(thread), recipient.address, isSyncMessage = recipient.isLocalNumber).success { Log.d("Loki", "Sent offer") }.fail { Log.e("Loki", "Failed to send offer", it) @@ -559,8 +562,9 @@ class CallManager( val recipient = recipient ?: return val userAddress = storage.getUserPublicKey() ?: return stateProcessor.processEvent(Event.DeclineCall) { - MessageSender.sendNonDurably(CallMessage.endCall(callId), Address.fromSerialized(userAddress), isSyncMessage = true) - MessageSender.sendNonDurably(CallMessage.endCall(callId), recipient.address, isSyncMessage = recipient.isLocalNumber) + val thread = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(recipient) + MessageSender.sendNonDurably(CallMessage.endCall(callId).applyExpiryMode(thread), Address.fromSerialized(userAddress), isSyncMessage = true) + MessageSender.sendNonDurably(CallMessage.endCall(callId).applyExpiryMode(thread), recipient.address, isSyncMessage = recipient.isLocalNumber) insertCallMessage(recipient.address.serialize(), CallMessageType.CALL_MISSED) } } @@ -579,7 +583,9 @@ class CallManager( val buffer = DataChannel.Buffer(ByteBuffer.wrap(HANGUP_JSON.toString().encodeToByteArray()), false) channel.send(buffer) } - MessageSender.sendNonDurably(CallMessage.endCall(callId), recipient.address, isSyncMessage = recipient.isLocalNumber) + + val thread = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(recipient) + MessageSender.sendNonDurably(CallMessage.endCall(callId).applyExpiryMode(thread), recipient.address, isSyncMessage = recipient.isLocalNumber) } } @@ -729,8 +735,8 @@ class CallManager( mandatory.add(MediaConstraints.KeyValuePair("IceRestart", "true")) }) connection.setLocalDescription(offer) - - MessageSender.sendNonDurably(CallMessage.offer(offer.description, callId), recipient.address, isSyncMessage = recipient.isLocalNumber) + val thread = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(recipient) + MessageSender.sendNonDurably(CallMessage.offer(offer.description, callId).applyExpiryMode(thread), recipient.address, isSyncMessage = recipient.isLocalNumber) } } diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt index 690cfc509e..0827a1a5b3 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt @@ -94,12 +94,6 @@ fun SignalServiceProtos.Content.expiryMode(): ExpiryMode = /** * Apply ExpiryMode from the current setting. */ -inline fun M.applyExpiryMode(): M { - val address = Address.fromSerialized(sender ?: return this) - MessagingModuleConfiguration.shared.storage.getThreadId(address)?.let(::applyExpiryMode) - return this -} - inline fun M.applyExpiryMode(thread: Long): M { val storage = MessagingModuleConfiguration.shared.storage expiryMode = storage.getExpirationConfiguration(thread)?.expiryMode?.coerceSendToRead(coerceDisappearAfterSendToRead) ?: ExpiryMode.NONE diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/control/CallMessage.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/control/CallMessage.kt index 4ae99e7605..f678271030 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/CallMessage.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/control/CallMessage.kt @@ -42,21 +42,21 @@ class CallMessage(): ControlMessage() { listOf(), listOf(), callId - ).applyExpiryMode() + ) fun preOffer(callId: UUID) = CallMessage(PRE_OFFER, listOf(), listOf(), listOf(), callId - ).applyExpiryMode() + ) fun offer(sdp: String, callId: UUID) = CallMessage(OFFER, listOf(sdp), listOf(), listOf(), callId - ).applyExpiryMode() + ) fun endCall(callId: UUID) = CallMessage(END_CALL, emptyList(), emptyList(), emptyList(), callId) diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt index d68c8f5e37..4199e69486 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageSender.kt @@ -471,8 +471,8 @@ object MessageSender { @JvmStatic fun send(message: Message, address: Address) { - message.applyExpiryMode() val threadID = MessagingModuleConfiguration.shared.storage.getThreadId(address) + threadID?.let(message::applyExpiryMode) message.threadID = threadID val destination = Destination.from(address) val job = MessageSendJob(message, destination)