Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature get newer replies #5256

Merged
merged 5 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
### ⬆️ Improved

### ✅ Added
- Added `ChatClient.getNewerReplies` to fetch newer replies for a message in a thread. [#5256](https://github.com/GetStream/stream-chat-android/pull/5256)

### ⚠️ Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public final class io/getstream/chat/android/client/ChatClient {
public final fun getLogicRegistry ()Lio/getstream/chat/android/client/channel/state/ChannelStateLogicProvider;
public final fun getMessage (Ljava/lang/String;)Lio/getstream/result/call/Call;
public final fun getMessagesWithAttachments (Ljava/lang/String;Ljava/lang/String;IILjava/util/List;)Lio/getstream/result/call/Call;
public final fun getNewerReplies (Ljava/lang/String;ILjava/lang/String;)Lio/getstream/result/call/Call;
public static synthetic fun getNewerReplies$default (Lio/getstream/chat/android/client/ChatClient;Ljava/lang/String;ILjava/lang/String;ILjava/lang/Object;)Lio/getstream/result/call/Call;
public static final fun getOFFLINE_SUPPORT_ENABLED ()Z
public final fun getPinnedMessages (Ljava/lang/String;Ljava/lang/String;ILio/getstream/chat/android/models/querysort/QuerySorter;Lio/getstream/chat/android/client/api/models/PinnedMessagesPagination;)Lio/getstream/result/call/Call;
public final fun getReactions (Ljava/lang/String;II)Lio/getstream/result/call/Call;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import io.getstream.chat.android.client.api.models.identifier.SendMessageIdentif
import io.getstream.chat.android.client.api.models.identifier.SendReactionIdentifier
import io.getstream.chat.android.client.api.models.identifier.ShuffleGiphyIdentifier
import io.getstream.chat.android.client.api.models.identifier.UpdateMessageIdentifier
import io.getstream.chat.android.client.api.models.identifier.getNewerRepliesIdentifier
import io.getstream.chat.android.client.api2.model.dto.AttachmentDto
import io.getstream.chat.android.client.api2.model.dto.DownstreamChannelDto
import io.getstream.chat.android.client.api2.model.dto.DownstreamMessageDto
Expand Down Expand Up @@ -1549,6 +1550,28 @@ internal constructor(
.share(userScope) { GetRepliesIdentifier(messageId, limit) }
}

/**
* Fetch replies to the specified message with id [parentId] that are newer than the message with [lastId].
* If [lastId] is null, the oldest replies are returned.
*
* @param parentId The id of the parent message.
* @param limit The number of replies to fetch.
* @param lastId The id of the last message to fetch.
JcMinarro marked this conversation as resolved.
Show resolved Hide resolved
*
* @return Executable async [Call] responsible for fetching newer replies.
*/
@CheckResult
public fun getNewerReplies(
parentId: String,
limit: Int,
lastId: String? = null,
): Call<List<Message>> {
logger.d { "[getNewerReplies] parentId: $parentId, limit: $limit, lastId: $lastId" }

return api.getNewerReplies(parentId, limit, lastId)
.share(userScope) { getNewerRepliesIdentifier(parentId, limit, lastId) }
}

@CheckResult
public fun getRepliesMore(
messageId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ internal interface ChatApi {
@CheckResult
fun getReplies(messageId: String, limit: Int): Call<List<Message>>

@CheckResult
fun getNewerReplies(
parentId: String,
limit: Int,
lastId: String?,
): Call<List<Message>>

@CheckResult
fun getReactions(
messageId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import io.getstream.chat.android.client.api.models.PinnedMessagesPagination
import io.getstream.chat.android.client.api.models.QueryChannelRequest
import io.getstream.chat.android.client.api.models.QueryChannelsRequest
import io.getstream.chat.android.client.api2.optimisation.hash.ChannelQueryKey
import io.getstream.chat.android.client.api2.optimisation.hash.GetNewerRepliesHash
import io.getstream.chat.android.client.api2.optimisation.hash.GetPinnedMessagesHash
import io.getstream.chat.android.client.api2.optimisation.hash.GetReactionsHash
import io.getstream.chat.android.client.api2.optimisation.hash.GetRepliesHash
Expand Down Expand Up @@ -78,6 +79,16 @@ internal class DistinctChatApi(
}
}

override fun getNewerReplies(parentId: String, limit: Int, lastId: String?): Call<List<Message>> {
val uniqueKey = GetNewerRepliesHash(parentId, limit, lastId).hashCode()
StreamLog.d(TAG) {
"[getNewerReplies] parentId: $parentId, limit: $limit, lastId: $lastId, uniqueKey: $uniqueKey"
}
return getOrCreate(uniqueKey) {
delegate.getNewerReplies(parentId, limit, lastId)
}
}

override fun getReactions(messageId: String, offset: Int, limit: Int): Call<List<Reaction>> {
val uniqueKey = GetReactionsHash(messageId, offset, limit).hashCode()
StreamLog.d(TAG) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ internal class DistinctChatApiEnabler(
return getApi().getReplies(messageId, limit)
}

override fun getNewerReplies(parentId: String, limit: Int, lastId: String?): Call<List<Message>> {
return getApi().getNewerReplies(parentId, limit, lastId)
}

override fun getReactions(messageId: String, offset: Int, limit: Int): Call<List<Reaction>> {
return getApi().getReactions(messageId, offset, limit)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ internal fun GetRepliesIdentifier(
return result
}

/**
* Identifier for a [ChatClient.getNewerReplies] call.
*/
@Suppress("FunctionName", "MagicNumber")
internal fun getNewerRepliesIdentifier(
parentId: String,
limit: Int,
lastId: String? = null,
): Int {
var result = "GetOlderReplies".hashCode()
result = 31 * result + parentId.hashCode()
result = 31 * result + limit.hashCode()
result = 31 * result + (lastId?.hashCode() ?: 0)
return result
}

/**
* Identifier for a [ChatClient.getRepliesMore] call.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,16 @@ constructor(
}
}

override fun getNewerReplies(
parentId: String,
limit: Int,
lastId: String?,
): Call<List<Message>> = messageApi.getNewerReplies(
parentId = parentId,
limit = limit,
lastId = lastId,
).map { response -> response.messages.map(DownstreamMessageDto::toDomain) }

override fun getReplies(messageId: String, limit: Int): Call<List<Message>> {
return messageApi.getReplies(
messageId = messageId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ internal interface MessageApi {
@Query("limit") limit: Int,
): RetrofitCall<MessagesResponse>

@GET("/messages/{parent_id}/replies?sort=[{\"field\":\"created_at\",\"direction\":1}]")
fun getNewerReplies(
@Path("parent_id") parentId: String,
@Query("limit") limit: Int,
@Query("id_gt") lastId: String?,
): RetrofitCall<MessagesResponse>

@GET("/messages/{parent_id}/replies")
fun getRepliesMore(
@Path("parent_id") messageId: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2014-2022 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-chat-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.getstream.chat.android.client.api2.optimisation.hash

internal data class GetNewerRepliesHash(
val parentId: String,
val limit: Int,
val lastId: String?,
)
Loading