Skip to content

Commit

Permalink
[PBE-3738] Ensure message has id before starting the send process (#5266
Browse files Browse the repository at this point in the history
)

* Ensure message has id before starting the send process

* Update CHANGELOG.md
  • Loading branch information
JcMinarro authored May 23, 2024
1 parent e417e33 commit 55488b1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
## stream-chat-android-client
### 🐞 Fixed
- Fix crash when parsing users from query user endpoint. [#5257](https://github.com/GetStream/stream-chat-android/pull/5257)
- Fix `ChatClient.sendMessage()` method to be able to send multiple message in parallel. [#5266](https://github.com/GetStream/stream-chat-android/pull/5266)

### ⬆️ Improved

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ import io.getstream.chat.android.client.utils.ProgressCallback
import io.getstream.chat.android.client.utils.TokenUtils
import io.getstream.chat.android.client.utils.internal.toggle.ToggleService
import io.getstream.chat.android.client.utils.mergePartially
import io.getstream.chat.android.client.utils.message.ensureId
import io.getstream.chat.android.client.utils.observable.ChatEventsObservable
import io.getstream.chat.android.client.utils.observable.Disposable
import io.getstream.chat.android.client.utils.retry.NoRetryPolicy
Expand Down Expand Up @@ -1721,6 +1722,7 @@ internal constructor(
isRetrying: Boolean = false,
): Call<Message> {
return message.copy(createdLocallyAt = message.createdLocallyAt ?: Date())
.ensureId(getCurrentUser() ?: getStoredUser())
.let { processedMessage ->
CoroutineCall(userScope) {
val debugger = clientDebugger.debugSendMessage(channelType, channelId, processedMessage, isRetrying)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import io.getstream.chat.android.client.extensions.uploadId
import io.getstream.chat.android.client.interceptor.message.PrepareMessageLogic
import io.getstream.chat.android.client.setup.state.ClientState
import io.getstream.chat.android.client.utils.internal.getMessageType
import io.getstream.chat.android.client.utils.message.ensureId
import io.getstream.chat.android.models.Attachment
import io.getstream.chat.android.models.Message
import io.getstream.chat.android.models.SyncStatus
Expand Down Expand Up @@ -61,8 +62,7 @@ internal class PrepareMessageLogicImpl(
)
}
}
return message.copy(
id = message.id.takeIf { it.isNotBlank() } ?: generateMessageId(user.id),
return message.ensureId(user).copy(
user = user,
attachments = attachments,
type = getMessageType(message),
Expand Down Expand Up @@ -92,13 +92,6 @@ internal class PrepareMessageLogicImpl(
}
}

/**
* Returns a unique message id prefixed with user id.
*/
private fun generateMessageId(userId: String): String {
return "$userId-${UUID.randomUUID()}"
}

private fun generateUploadId(): String {
return "upload_id_${UUID.randomUUID()}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

@file:Suppress("TooManyFunctions")
@file:JvmName("MessageUtils")

package io.getstream.chat.android.client.utils.message
Expand All @@ -25,6 +26,8 @@ import io.getstream.chat.android.models.Message
import io.getstream.chat.android.models.MessageModerationAction
import io.getstream.chat.android.models.MessageType
import io.getstream.chat.android.models.SyncStatus
import io.getstream.chat.android.models.User
import java.util.UUID

private const val ITEM_COUNT_OF_TWO: Int = 2

Expand Down Expand Up @@ -146,3 +149,18 @@ public fun Message.isModerationFlag(): Boolean = moderationDetails?.action == Me
*/
public fun Message.isModerationError(currentUserId: String?): Boolean = isMine(currentUserId) &&
(isError() && isModerationBounce())

/**
* Ensures the message has an id.
* If the message doesn't have an id, a unique message id is generated.
* @return the message with an id.
*/
internal fun Message.ensureId(currentUser: User?): Message =
copy(id = id.takeIf { it.isNotBlank() } ?: generateMessageId(currentUser))

/**
* Returns a unique message id prefixed with user id.
*/
private fun generateMessageId(user: User?): String {
return "${user?.id}-${UUID.randomUUID()}"
}

0 comments on commit 55488b1

Please sign in to comment.