Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class MessageSendJob @AssistedInject constructor(
@Assisted val message: Message,
@Assisted val destination: Destination,
@Assisted val statusCallback: SendChannel<Result<Unit>>?,
/**
* This job copies a message that has already reached its recipient to our own swarm, so its
* progress is reported as syncing rather than sending. Note to Self also sends to our own swarm
* but is a first send, which is why this can't be inferred from [destination].
*/
@Assisted val isSyncOfSentMessage: Boolean,
private val attachmentUploadJobFactory: AttachmentUploadJob.Factory,
private val messageDataProvider: MessageDataProvider,
private val storage: StorageProtocol,
Expand All @@ -56,22 +62,28 @@ class MessageSendJob @AssistedInject constructor(
// Keys used for database storage
private const val MESSAGE_KEY = "message"
private const val DESTINATION_KEY = "destination"
private const val SYNC_OF_SENT_MESSAGE_KEY = "sync_of_sent_message"
}

override suspend fun execute(dispatcherName: String) {
val messageId = message.id
val message = message as? VisibleMessage

// do not attempt to send if the message is marked as deleted
if (messageId != null && messageDataProvider.isDeletedMessage(messageId)) {
finishForDeletedMessage(dispatcherName)
return
}

messageId?.let(storage::markAsSending)
if (messageId != null) {
if (isSyncOfSentMessage) storage.markAsSyncing(messageId) else storage.markAsSending(messageId)
}

if (message != null) {
val isOutgoing = messageId != null && messageDataProvider.isOutgoingMessage(messageId)
if (!isOutgoing && message.reaction == null) return // The message has been deleted
if (!isOutgoing && message.reaction == null) {
finishForDeletedMessage(dispatcherName)
return
}
val attachmentIDs = mutableListOf<Long>()
attachmentIDs.addAll(message.attachmentIDs)
message.quote?.let { it.attachmentID?.let { attachmentID -> attachmentIDs.add(attachmentID) } }
Expand Down Expand Up @@ -109,7 +121,7 @@ class MessageSendJob @AssistedInject constructor(
}
}

messageSender.sendNonDurably(this@MessageSendJob.message, destination, isSync)
messageSender.sendNonDurably(this@MessageSendJob.message, destination, isSync, isSyncOfSentMessage)

this.handleSuccess(dispatcherName)
statusCallback?.trySend(Result.success(Unit))
Expand Down Expand Up @@ -149,13 +161,22 @@ class MessageSendJob @AssistedInject constructor(
delegate?.handleJobFailedPermanently(this, dispatcherName, error)
}

/**
* A job whose message has been deleted has nothing left to send, but it must still be removed:
* left in storage, it is resumed at every launch only to stop here again.
*/
private fun finishForDeletedMessage(dispatcherName: String) {
delegate?.handleJobSucceeded(this, dispatcherName)
}

private fun handleFailure(dispatcherName: String, error: Exception) {
Log.w(TAG, "Failed to send ${message::class.simpleName}.", error)
val messageId = message.id
if (message is VisibleMessage && messageId != null) {
if (messageDataProvider.isDeletedMessage(messageId) ||
!messageDataProvider.isOutgoingMessage(messageId)) {
return // The message has been deleted
finishForDeletedMessage(dispatcherName)
return
}
}
delegate?.handleJobFailed(this, dispatcherName, error)
Expand All @@ -177,6 +198,7 @@ class MessageSendJob @AssistedInject constructor(
return Data.Builder()
.putByteArray(MESSAGE_KEY, serializedMessage)
.putByteArray(DESTINATION_KEY, serializedDestination)
.putBoolean(SYNC_OF_SENT_MESSAGE_KEY, isSyncOfSentMessage)
.build()
}

Expand All @@ -190,7 +212,8 @@ class MessageSendJob @AssistedInject constructor(
abstract fun create(
message: Message,
destination: Destination,
statusCallback: SendChannel<Result<Unit>>? = null
statusCallback: SendChannel<Result<Unit>>? = null,
isSyncOfSentMessage: Boolean = false,
): MessageSendJob

override fun create(data: Data): MessageSendJob? {
Expand Down Expand Up @@ -221,7 +244,8 @@ class MessageSendJob @AssistedInject constructor(
return create(
message = message,
destination = destination,
statusCallback = null
statusCallback = null,
isSyncOfSentMessage = data.getBooleanOrDefault(SYNC_OF_SENT_MESSAGE_KEY, false),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.session.libsession.messaging.sending_receiving

import androidx.annotation.VisibleForTesting
import com.google.protobuf.ByteString
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
Expand Down Expand Up @@ -124,12 +125,20 @@ class MessageSender @Inject constructor(
}
}

// Convenience
suspend fun sendNonDurably(message: Message, destination: Destination, isSyncMessage: Boolean) {
/**
* @param isSyncOfSentMessage [message] has already reached its recipient and this sends a copy to
* our own swarm, as opposed to a first send there such as Note to Self.
*/
suspend fun sendNonDurably(
message: Message,
destination: Destination,
isSyncMessage: Boolean,
isSyncOfSentMessage: Boolean = false,
) {
return if (destination is Destination.OpenGroup || destination is Destination.OpenGroupInbox) {
sendToOpenGroupDestination(destination, message)
} else {
sendToSnodeDestination(destination, message, isSyncMessage)
sendToSnodeDestination(destination, message, isSyncMessage, isSyncOfSentMessage)
}
}

Expand Down Expand Up @@ -259,7 +268,12 @@ class MessageSender @Inject constructor(
}

// One-on-One Chats & Closed Groups
private suspend fun sendToSnodeDestination(destination: Destination, message: Message, isSyncMessage: Boolean = false) {
private suspend fun sendToSnodeDestination(
destination: Destination,
message: Message,
isSyncMessage: Boolean = false,
isSyncOfSentMessage: Boolean = false,
) {
// Set the failure handler (need it here already for precondition failure handling)
fun handleFailure(error: Exception) {
handleFailedMessageSend(message, error, isSyncMessage)
Expand Down Expand Up @@ -305,7 +319,7 @@ class MessageSender @Inject constructor(

if (sendResult.isSuccess) {
message.serverHash = sendResult.getOrThrow().hash
handleSuccessfulMessageSend(message, destination, isSyncMessage)
handleSuccessfulMessageSend(message, destination, isSyncMessage, isSyncOfSentMessage = isSyncOfSentMessage)
} else {
throw sendResult.exceptionOrNull()!!
}
Expand Down Expand Up @@ -459,7 +473,14 @@ class MessageSender @Inject constructor(
}

// Result Handling
private fun handleSuccessfulMessageSend(message: Message, destination: Destination, isSyncMessage: Boolean = false, openGroupSentTimestamp: Long = -1) {
@VisibleForTesting
internal fun handleSuccessfulMessageSend(
message: Message,
destination: Destination,
isSyncMessage: Boolean = false,
openGroupSentTimestamp: Long = -1,
isSyncOfSentMessage: Boolean = false,
) {
val userPublicKey = storage.getUserPublicKey()!!
// Ignore future self-sends
storage.addReceivedMessageTimestamp(message.sentTimestamp!!)
Expand Down Expand Up @@ -500,7 +521,11 @@ class MessageSender @Inject constructor(
storage.updateSentTimestamp(messageId, message.sentTimestamp!!)

// Start the disappearing messages timer if needed
messageExpirationManager.onMessageSent(message)
if (isSyncOfSentMessage) {
messageExpirationManager.onMessageSynced(message)
} else {
messageExpirationManager.onMessageSent(message)
}
} ?: run {
storage.updateReactionIfNeeded(message, message.sender?:userPublicKey, openGroupSentTimestamp)
}
Expand All @@ -512,10 +537,37 @@ class MessageSender @Inject constructor(
if (message is VisibleMessage) message.syncTarget = destination.publicKey
if (message is ExpirationTimerUpdate) message.syncTarget = destination.publicKey

message.id?.let(storage::markAsSyncing)
scheduleSyncToSelf(message, userPublicKey)
}
}

/**
* A stored message's sync must be a persisted job rather than a coroutine. The message has
* already reached its recipient, so a failed sync leaves it showing "Failed to sync", and only a
* job is retried and resumed after a restart. A message with no stored record has no status to get
* stuck, and includes call signalling that must not be replayed late, so its sync stays best-effort.
*/
@VisibleForTesting
internal fun scheduleSyncToSelf(message: Message, userPublicKey: String) {
val messageId = message.id
if (messageId != null) {
storage.markAsSyncing(messageId)
jobQueue.get().add(
messageSendJobFactory.create(
message = message,
destination = Destination.Contact(userPublicKey),
isSyncOfSentMessage = true,
)
)
} else {
scope.launch {
try {
sendToSnodeDestination(Destination.Contact(userPublicKey), message, true)
sendToSnodeDestination(
Destination.Contact(userPublicKey),
message,
isSyncMessage = true,
isSyncOfSentMessage = true,
)
} catch (ec: Exception) {
Log.e("MessageSender", "Unable to send sync message", ec)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@ class ExpiringMessageManager @Inject constructor(
}
}

/**
* A copy of a message we already sent has reached our own swarm. Its timer here started when it
* was first sent, and must not restart: the copy can land long afterwards, after retries or a
* restart. Linked devices count a message from ourselves from its sent timestamp, so the copy is
* shortened to expire with theirs.
*/
fun onMessageSynced(message: Message) {
val hash = message.serverHash ?: return
val sentTimestamp = message.sentTimestamp ?: return
if (message.expiryMode == ExpiryMode.NONE) return

scope.launch { shortenTtl(hash, sentTimestamp, message.expiryMode.expiryMillis) }
}

override fun onMessageReceived(message: Message) {
val messageId = message.id ?: return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class JobKryoTest {
val restored = slot<Message>()
val factory = mockk<MessageSendJob.Factory> {
every { create(any<Data>()) } answers { callOriginal() }
every { create(capture(restored), any(), any()) } returns mockk(relaxed = true)
every { create(capture(restored), any(), any(), any()) } returns mockk(relaxed = true)
}

assertNotNull(factory.create(sendJob(message).serialize()))
Expand All @@ -59,6 +59,7 @@ class JobKryoTest {
message = message,
destination = Destination.ClosedGroup("03${"11".repeat(32)}"),
statusCallback = null,
isSyncOfSentMessage = false,
attachmentUploadJobFactory = mockk(relaxed = true),
messageDataProvider = mockk(relaxed = true),
storage = mockk(relaxed = true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class MessageSendJobTest {
),
destination = Destination.ClosedGroup(groupId),
statusCallback = statusChannel,
isSyncOfSentMessage = false,
attachmentUploadJobFactory = mockk(relaxed = true),
messageDataProvider = mockk(relaxed = true),
storage = mockk(relaxed = true),
Expand Down
Loading
Loading