Merge pull request #797 from vector-im/feature/cjs/hide-self-in-search
This commit is contained in:
commit
e0d5096afb
7 changed files with 75 additions and 17 deletions
|
|
@ -100,7 +100,7 @@ class RoomMemberDetailsPresenter @AssistedInject constructor(
|
||||||
avatarUrl = userAvatar,
|
avatarUrl = userAvatar,
|
||||||
isBlocked = isBlocked.value,
|
isBlocked = isBlocked.value,
|
||||||
displayConfirmationDialog = confirmationDialog,
|
displayConfirmationDialog = confirmationDialog,
|
||||||
isCurrentUser = roomMember?.userId == client.sessionId,
|
isCurrentUser = client.isMe(roomMember?.userId),
|
||||||
eventSink = ::handleEvents
|
eventSink = ::handleEvents
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ class DefaultRoomLastMessageFormatter @Inject constructor(
|
||||||
) : RoomLastMessageFormatter {
|
) : RoomLastMessageFormatter {
|
||||||
|
|
||||||
override fun format(event: EventTimelineItem, isDmRoom: Boolean): CharSequence? {
|
override fun format(event: EventTimelineItem, isDmRoom: Boolean): CharSequence? {
|
||||||
val isOutgoing = event.sender == matrixClient.sessionId
|
val isOutgoing = matrixClient.isMe(event.sender)
|
||||||
val senderDisplayName = (event.senderProfile as? ProfileTimelineDetails.Ready)?.displayName ?: event.sender.value
|
val senderDisplayName = (event.senderProfile as? ProfileTimelineDetails.Ready)?.displayName ?: event.sender.value
|
||||||
return when (val content = event.content) {
|
return when (val content = event.content) {
|
||||||
is MessageContent -> processMessageContents(content, senderDisplayName, isDmRoom)
|
is MessageContent -> processMessageContents(content, senderDisplayName, isDmRoom)
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ class DefaultTimelineEventFormatter @Inject constructor(
|
||||||
) : TimelineEventFormatter {
|
) : TimelineEventFormatter {
|
||||||
|
|
||||||
override fun format(event: EventTimelineItem): CharSequence? {
|
override fun format(event: EventTimelineItem): CharSequence? {
|
||||||
val isOutgoing = event.sender == matrixClient.sessionId
|
val isOutgoing = matrixClient.isMe(event.sender)
|
||||||
val senderDisplayName = (event.senderProfile as? ProfileTimelineDetails.Ready)?.displayName ?: event.sender.value
|
val senderDisplayName = (event.senderProfile as? ProfileTimelineDetails.Ready)?.displayName ?: event.sender.value
|
||||||
return when (val content = event.content) {
|
return when (val content = event.content) {
|
||||||
is RoomMembershipContent -> {
|
is RoomMembershipContent -> {
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ class RoomMembershipContentFormatter @Inject constructor(
|
||||||
senderIsYou: Boolean,
|
senderIsYou: Boolean,
|
||||||
): CharSequence? {
|
): CharSequence? {
|
||||||
val userId = membershipContent.userId
|
val userId = membershipContent.userId
|
||||||
val memberIsYou = userId == matrixClient.sessionId
|
val memberIsYou = matrixClient.isMe(userId)
|
||||||
return when (val change = membershipContent.change) {
|
return when (val change = membershipContent.change) {
|
||||||
MembershipChange.JOINED -> if (memberIsYou) {
|
MembershipChange.JOINED -> if (memberIsYou) {
|
||||||
sp.getString(R.string.state_event_room_join_by_you)
|
sp.getString(R.string.state_event_room_join_by_you)
|
||||||
|
|
|
||||||
|
|
@ -62,4 +62,6 @@ interface MatrixClient : Closeable {
|
||||||
suspend fun loadUserAvatarURLString(): Result<String?>
|
suspend fun loadUserAvatarURLString(): Result<String?>
|
||||||
suspend fun uploadMedia(mimeType: String, data: ByteArray, progressCallback: ProgressCallback?): Result<String>
|
suspend fun uploadMedia(mimeType: String, data: ByteArray, progressCallback: ProgressCallback?): Result<String>
|
||||||
fun roomMembershipObserver(): RoomMembershipObserver
|
fun roomMembershipObserver(): RoomMembershipObserver
|
||||||
|
|
||||||
|
fun isMe(userId: UserId?) = userId == sessionId
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ package io.element.android.libraries.usersearch.impl
|
||||||
|
|
||||||
import com.squareup.anvil.annotations.ContributesBinding
|
import com.squareup.anvil.annotations.ContributesBinding
|
||||||
import io.element.android.libraries.di.SessionScope
|
import io.element.android.libraries.di.SessionScope
|
||||||
|
import io.element.android.libraries.matrix.api.MatrixClient
|
||||||
import io.element.android.libraries.matrix.api.core.MatrixPatterns
|
import io.element.android.libraries.matrix.api.core.MatrixPatterns
|
||||||
import io.element.android.libraries.matrix.api.core.UserId
|
import io.element.android.libraries.matrix.api.core.UserId
|
||||||
import io.element.android.libraries.matrix.api.user.MatrixUser
|
import io.element.android.libraries.matrix.api.user.MatrixUser
|
||||||
|
|
@ -31,13 +32,14 @@ import javax.inject.Inject
|
||||||
|
|
||||||
@ContributesBinding(SessionScope::class)
|
@ContributesBinding(SessionScope::class)
|
||||||
class MatrixUserRepository @Inject constructor(
|
class MatrixUserRepository @Inject constructor(
|
||||||
|
private val client: MatrixClient,
|
||||||
private val dataSource: UserListDataSource
|
private val dataSource: UserListDataSource
|
||||||
) : UserRepository {
|
) : UserRepository {
|
||||||
|
|
||||||
override suspend fun search(query: String): Flow<List<UserSearchResult>> = flow {
|
override suspend fun search(query: String): Flow<List<UserSearchResult>> = flow {
|
||||||
// Manually add a fake result with the matrixId, if any
|
// If the search term is a MXID that's not ours, we'll show a 'fake' result for that user, then update it when we get search results.
|
||||||
val isUserId = MatrixPatterns.isUserId(query)
|
val shouldQueryProfile = MatrixPatterns.isUserId(query) && !client.isMe(UserId(query))
|
||||||
if (isUserId) {
|
if (shouldQueryProfile) {
|
||||||
emit(listOf(UserSearchResult(MatrixUser(UserId(query)))))
|
emit(listOf(UserSearchResult(MatrixUser(UserId(query)))))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -45,10 +47,14 @@ class MatrixUserRepository @Inject constructor(
|
||||||
// Debounce
|
// Debounce
|
||||||
delay(DEBOUNCE_TIME_MILLIS)
|
delay(DEBOUNCE_TIME_MILLIS)
|
||||||
|
|
||||||
val results = dataSource.search(query, MAXIMUM_SEARCH_RESULTS).map { UserSearchResult(it) }.toMutableList()
|
val results = dataSource
|
||||||
|
.search(query, MAXIMUM_SEARCH_RESULTS)
|
||||||
|
.filter { !client.isMe(it.userId) }
|
||||||
|
.map { UserSearchResult(it) }
|
||||||
|
.toMutableList()
|
||||||
|
|
||||||
// If the query is a user ID and the result doesn't contain that user ID, query the profile information explicitly
|
// If the query is another user's MXID and the result doesn't contain that user ID, query the profile information explicitly
|
||||||
if (isUserId && results.none { it.matrixUser.userId.value == query }) {
|
if (shouldQueryProfile && results.none { it.matrixUser.userId.value == query }) {
|
||||||
results.add(
|
results.add(
|
||||||
0,
|
0,
|
||||||
dataSource.getProfile(UserId(query))
|
dataSource.getProfile(UserId(query))
|
||||||
|
|
|
||||||
|
|
@ -18,22 +18,26 @@ package io.element.android.libraries.usersearch.impl
|
||||||
|
|
||||||
import app.cash.turbine.test
|
import app.cash.turbine.test
|
||||||
import com.google.common.truth.Truth.assertThat
|
import com.google.common.truth.Truth.assertThat
|
||||||
|
import io.element.android.libraries.matrix.api.core.SessionId
|
||||||
import io.element.android.libraries.matrix.api.core.UserId
|
import io.element.android.libraries.matrix.api.core.UserId
|
||||||
import io.element.android.libraries.matrix.api.user.MatrixUser
|
import io.element.android.libraries.matrix.api.user.MatrixUser
|
||||||
import io.element.android.libraries.matrix.test.A_USER_ID
|
import io.element.android.libraries.matrix.test.A_USER_ID
|
||||||
import io.element.android.libraries.matrix.test.A_USER_NAME
|
import io.element.android.libraries.matrix.test.A_USER_NAME
|
||||||
|
import io.element.android.libraries.matrix.test.FakeMatrixClient
|
||||||
import io.element.android.libraries.matrix.ui.components.aMatrixUserList
|
import io.element.android.libraries.matrix.ui.components.aMatrixUserList
|
||||||
import io.element.android.libraries.usersearch.api.UserSearchResult
|
import io.element.android.libraries.usersearch.api.UserSearchResult
|
||||||
import io.element.android.libraries.usersearch.test.FakeUserListDataSource
|
import io.element.android.libraries.usersearch.test.FakeUserListDataSource
|
||||||
import kotlinx.coroutines.test.runTest
|
import kotlinx.coroutines.test.runTest
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
|
private val SESSION_ID = SessionId("@current-user:example.com")
|
||||||
|
|
||||||
internal class MatrixUserRepositoryTest {
|
internal class MatrixUserRepositoryTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `search - emits nothing if the search query is too short`() = runTest {
|
fun `search - emits nothing if the search query is too short`() = runTest {
|
||||||
val dataSource = FakeUserListDataSource()
|
val dataSource = FakeUserListDataSource()
|
||||||
val repository = MatrixUserRepository(dataSource)
|
val repository = MatrixUserRepository(FakeMatrixClient(SESSION_ID), dataSource)
|
||||||
|
|
||||||
val result = repository.search("x")
|
val result = repository.search("x")
|
||||||
|
|
||||||
|
|
@ -45,7 +49,7 @@ internal class MatrixUserRepositoryTest {
|
||||||
@Test
|
@Test
|
||||||
fun `search - returns empty list if no results are found`() = runTest {
|
fun `search - returns empty list if no results are found`() = runTest {
|
||||||
val dataSource = FakeUserListDataSource()
|
val dataSource = FakeUserListDataSource()
|
||||||
val repository = MatrixUserRepository(dataSource)
|
val repository = MatrixUserRepository(FakeMatrixClient(SESSION_ID), dataSource)
|
||||||
|
|
||||||
val result = repository.search("some query")
|
val result = repository.search("some query")
|
||||||
|
|
||||||
|
|
@ -59,7 +63,7 @@ internal class MatrixUserRepositoryTest {
|
||||||
fun `search - returns users if results are found`() = runTest {
|
fun `search - returns users if results are found`() = runTest {
|
||||||
val dataSource = FakeUserListDataSource()
|
val dataSource = FakeUserListDataSource()
|
||||||
dataSource.givenSearchResult(aMatrixUserList())
|
dataSource.givenSearchResult(aMatrixUserList())
|
||||||
val repository = MatrixUserRepository(dataSource)
|
val repository = MatrixUserRepository(FakeMatrixClient(SESSION_ID), dataSource)
|
||||||
|
|
||||||
val result = repository.search("some query")
|
val result = repository.search("some query")
|
||||||
|
|
||||||
|
|
@ -72,7 +76,7 @@ internal class MatrixUserRepositoryTest {
|
||||||
@Test
|
@Test
|
||||||
fun `search - immediately returns placeholder if search is mxid`() = runTest {
|
fun `search - immediately returns placeholder if search is mxid`() = runTest {
|
||||||
val dataSource = FakeUserListDataSource()
|
val dataSource = FakeUserListDataSource()
|
||||||
val repository = MatrixUserRepository(dataSource)
|
val repository = MatrixUserRepository(FakeMatrixClient(SESSION_ID), dataSource)
|
||||||
|
|
||||||
val result = repository.search(A_USER_ID.value)
|
val result = repository.search(A_USER_ID.value)
|
||||||
|
|
||||||
|
|
@ -83,12 +87,40 @@ internal class MatrixUserRepositoryTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `search - doesn't return placeholder if search is the local user's mxid`() = runTest {
|
||||||
|
val dataSource = FakeUserListDataSource()
|
||||||
|
val repository = MatrixUserRepository(FakeMatrixClient(SESSION_ID), dataSource)
|
||||||
|
|
||||||
|
val result = repository.search(SESSION_ID.value)
|
||||||
|
|
||||||
|
result.test {
|
||||||
|
assertThat(awaitItem()).isEmpty()
|
||||||
|
awaitComplete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `search - filters out results with the local user's mxid`() = runTest {
|
||||||
|
val searchResults = aMatrixUserList() + MatrixUser(userId = SESSION_ID, displayName = A_USER_NAME)
|
||||||
|
val dataSource = FakeUserListDataSource()
|
||||||
|
dataSource.givenSearchResult(searchResults)
|
||||||
|
val repository = MatrixUserRepository(FakeMatrixClient(SESSION_ID), dataSource)
|
||||||
|
|
||||||
|
val result = repository.search("some text")
|
||||||
|
|
||||||
|
result.test {
|
||||||
|
assertThat(awaitItem()).isEqualTo(aMatrixUserList().toUserSearchResults())
|
||||||
|
awaitComplete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `search - does not change results if they contain searched mxid`() = runTest {
|
fun `search - does not change results if they contain searched mxid`() = runTest {
|
||||||
val searchResults = aMatrixUserListWithoutUserId(A_USER_ID) + MatrixUser(userId = A_USER_ID, displayName = A_USER_NAME)
|
val searchResults = aMatrixUserListWithoutUserId(A_USER_ID) + MatrixUser(userId = A_USER_ID, displayName = A_USER_NAME)
|
||||||
val dataSource = FakeUserListDataSource()
|
val dataSource = FakeUserListDataSource()
|
||||||
dataSource.givenSearchResult(searchResults)
|
dataSource.givenSearchResult(searchResults)
|
||||||
val repository = MatrixUserRepository(dataSource)
|
val repository = MatrixUserRepository(FakeMatrixClient(SESSION_ID), dataSource)
|
||||||
|
|
||||||
val result = repository.search(A_USER_ID.value)
|
val result = repository.search(A_USER_ID.value)
|
||||||
|
|
||||||
|
|
@ -107,7 +139,7 @@ internal class MatrixUserRepositoryTest {
|
||||||
val dataSource = FakeUserListDataSource()
|
val dataSource = FakeUserListDataSource()
|
||||||
dataSource.givenSearchResult(searchResults)
|
dataSource.givenSearchResult(searchResults)
|
||||||
dataSource.givenUserProfile(userProfile)
|
dataSource.givenUserProfile(userProfile)
|
||||||
val repository = MatrixUserRepository(dataSource)
|
val repository = MatrixUserRepository(FakeMatrixClient(SESSION_ID), dataSource)
|
||||||
|
|
||||||
val result = repository.search(A_USER_ID.value)
|
val result = repository.search(A_USER_ID.value)
|
||||||
|
|
||||||
|
|
@ -118,6 +150,24 @@ internal class MatrixUserRepositoryTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `search - doesn't add profile results if searched mxid is local user and not in results`() = runTest {
|
||||||
|
val userProfile = MatrixUser(userId = A_USER_ID, displayName = A_USER_NAME)
|
||||||
|
val searchResults = aMatrixUserListWithoutUserId(SESSION_ID)
|
||||||
|
|
||||||
|
val dataSource = FakeUserListDataSource()
|
||||||
|
dataSource.givenSearchResult(searchResults)
|
||||||
|
dataSource.givenUserProfile(userProfile)
|
||||||
|
val repository = MatrixUserRepository(FakeMatrixClient(SESSION_ID), dataSource)
|
||||||
|
|
||||||
|
val result = repository.search(SESSION_ID.value)
|
||||||
|
|
||||||
|
result.test {
|
||||||
|
assertThat(awaitItem()).isEqualTo(searchResults.toUserSearchResults())
|
||||||
|
awaitComplete()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `search - returns unresolved user if profile can't be loaded`() = runTest {
|
fun `search - returns unresolved user if profile can't be loaded`() = runTest {
|
||||||
val searchResults = aMatrixUserListWithoutUserId(A_USER_ID)
|
val searchResults = aMatrixUserListWithoutUserId(A_USER_ID)
|
||||||
|
|
@ -125,7 +175,7 @@ internal class MatrixUserRepositoryTest {
|
||||||
val dataSource = FakeUserListDataSource()
|
val dataSource = FakeUserListDataSource()
|
||||||
dataSource.givenSearchResult(searchResults)
|
dataSource.givenSearchResult(searchResults)
|
||||||
dataSource.givenUserProfile(null)
|
dataSource.givenUserProfile(null)
|
||||||
val repository = MatrixUserRepository(dataSource)
|
val repository = MatrixUserRepository(FakeMatrixClient(SESSION_ID), dataSource)
|
||||||
|
|
||||||
val result = repository.search(A_USER_ID.value)
|
val result = repository.search(A_USER_ID.value)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue