-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: support remote search by handle * detekt * address PR comments Co-authored-by: Mohamad Jaara <[email protected]>
- Loading branch information
1 parent
c534b24
commit abbfd29
Showing
16 changed files
with
792 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/search/SearchByHandleUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.search | ||
|
||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.publicuser.ConversationMemberExcludedOptions | ||
import com.wire.kalium.logic.data.publicuser.SearchUserRepository | ||
import com.wire.kalium.logic.data.publicuser.SearchUsersOptions | ||
import com.wire.kalium.logic.data.publicuser.model.UserSearchDetails | ||
import com.wire.kalium.logic.data.user.UserId | ||
import com.wire.kalium.logic.functional.getOrElse | ||
import com.wire.kalium.logic.functional.map | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.coroutineScope | ||
|
||
/** | ||
* Result of a search by handle. | ||
*/ | ||
class SearchByHandleUseCase internal constructor( | ||
private val searchUserRepository: SearchUserRepository, | ||
private val selfUserId: UserId, | ||
private val maxRemoteSearchResultCount: Int | ||
) { | ||
suspend operator fun invoke( | ||
searchHandle: String, | ||
excludingConversation: ConversationId?, | ||
customDomain: String? | ||
): SearchUserResult = coroutineScope { | ||
val cleanSearchQuery = searchHandle | ||
.trim() | ||
.removePrefix("@") | ||
.lowercase() | ||
|
||
if (cleanSearchQuery.isBlank()) { | ||
return@coroutineScope SearchUserResult(emptyList(), emptyList()) | ||
} | ||
|
||
val remoteResultsDeferred = async { | ||
searchUserRepository.searchUserRemoteDirectory( | ||
cleanSearchQuery, | ||
customDomain ?: selfUserId.domain, | ||
maxRemoteSearchResultCount, | ||
SearchUsersOptions( | ||
conversationExcluded = excludingConversation?.let { ConversationMemberExcludedOptions.ConversationExcluded(it) } | ||
?: ConversationMemberExcludedOptions.None, | ||
selfUserIncluded = false | ||
) | ||
).map { userSearchResult -> | ||
userSearchResult.result.map { | ||
UserSearchDetails( | ||
id = it.id, | ||
name = it.name, | ||
completeAssetId = it.completePicture, | ||
previewAssetId = it.previewPicture, | ||
type = it.userType, | ||
connectionStatus = it.connectionStatus, | ||
handle = it.handle | ||
) | ||
} | ||
}.getOrElse(emptyList()) | ||
.associateBy { it.id } | ||
.toMutableMap() | ||
} | ||
|
||
val localSearchResultDeferred = async { | ||
searchUserRepository.searchLocalByHandle( | ||
cleanSearchQuery, | ||
excludingConversation | ||
).getOrElse(emptyList()) | ||
.associateBy { it.id } | ||
.toMutableMap() | ||
} | ||
|
||
val remoteResults = remoteResultsDeferred.await() | ||
val localSearchResult = localSearchResultDeferred.await() | ||
|
||
SearchUserResult.resolveLocalAndRemoteResult(localSearchResult, remoteResults) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/search/SearchUserResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2024 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.search | ||
|
||
import com.wire.kalium.logic.data.publicuser.model.UserSearchDetails | ||
import com.wire.kalium.logic.data.user.ConnectionState | ||
import com.wire.kalium.logic.data.user.UserId | ||
|
||
data class SearchUserResult( | ||
val connected: List<UserSearchDetails>, | ||
val notConnected: List<UserSearchDetails> | ||
) { | ||
internal companion object { | ||
inline fun resolveLocalAndRemoteResult( | ||
localResult: MutableMap<UserId, UserSearchDetails>, | ||
remoteSearch: MutableMap<UserId, UserSearchDetails> | ||
): SearchUserResult { | ||
val updatedUser = mutableListOf<UserId>() | ||
remoteSearch.forEach { (userId, remoteUser) -> | ||
if (localResult.contains(userId) || (remoteUser.connectionStatus == ConnectionState.ACCEPTED)) { | ||
localResult[userId] = remoteUser | ||
updatedUser.add(userId) | ||
} | ||
} | ||
|
||
updatedUser.forEach { userId -> | ||
remoteSearch.remove(userId) | ||
} | ||
|
||
return SearchUserResult( | ||
connected = localResult.values.toList(), | ||
notConnected = remoteSearch.values.toList() | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.