Skip to content

Commit

Permalink
Only take up to 100 suggestions for the picker.
Browse files Browse the repository at this point in the history
This was broken before.
  • Loading branch information
jmartinesp committed Nov 20, 2023
1 parent cb33a13 commit 8d7f24e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.element.android.features.messages.impl.mentions

import io.element.android.features.messages.impl.messagecomposer.MentionSuggestion
import io.element.android.libraries.core.data.filterUpTo
import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.libraries.matrix.api.room.MatrixRoomMembersState
import io.element.android.libraries.matrix.api.room.RoomMember
Expand Down Expand Up @@ -48,8 +49,6 @@ object MentionSuggestionsProcessor {
canSendRoomMention: suspend () -> Boolean,
): List<MentionSuggestion> {
val members = roomMembersState.roomMembers()
// Take the first MAX_BATCH_ITEMS only
?.take(MAX_BATCH_ITEMS)
return when {
members.isNullOrEmpty() || suggestion == null -> {
// Clear suggestions
Expand All @@ -61,7 +60,7 @@ object MentionSuggestionsProcessor {
// Replace suggestions
val matchingMembers = getMemberSuggestions(
query = suggestion.text,
roomMembers = roomMembersState.roomMembers(),
roomMembers = members,
currentUserId = currentUserId,
canSendRoomMention = canSendRoomMention()
)
Expand Down Expand Up @@ -95,8 +94,8 @@ object MentionSuggestionsProcessor {
}

val matchingMembers = roomMembers
// Search only in joined members, exclude the current user
.filter { member ->
// Search only in joined members, up to MAX_BATCH_ITEMS, exclude the current user
.filterUpTo(MAX_BATCH_ITEMS) { member ->
isJoinedMemberAndNotSelf(member) && memberMatchesQuery(member, query)
}
.map(MentionSuggestion::Member)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.element.android.libraries.core.data

/**
* Returns a list containing first [count] elements matching the given [predicate].
* If the list contains less elements matching the [predicate], then all of them are returned.
*
* @param count the maximum number of elements to take.
* @param predicate the predicate used to match elements.
* @return a list containing first [count] elements matching the given [predicate].
*/
inline fun <T> Iterable<T>.filterUpTo(count: Int, predicate: (T) -> Boolean): List<T> {
val result = mutableListOf<T>()
for (element in this) {
if (predicate(element)) {
result.add(element)
if (result.size == count) {
break

Check warning on line 33 in libraries/core/src/main/kotlin/io/element/android/libraries/core/data/FilterUpTo.kt

View check run for this annotation

Codecov / codecov/patch

libraries/core/src/main/kotlin/io/element/android/libraries/core/data/FilterUpTo.kt#L33

Added line #L33 was not covered by tests
}
}
}
return result
}

0 comments on commit 8d7f24e

Please sign in to comment.