diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/user/UserService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/user/UserService.kt index 2cfc4b731f..ab85f979bf 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/user/UserService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/user/UserService.kt @@ -35,6 +35,11 @@ interface UserService { */ fun getUser(userId: String): User? + /** + * Try to resolve user from known users, or using profile api + */ + fun resolveUser(userId: String, callback: MatrixCallback) + /** * Search list of users on server directory. * @param search the searched term diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/DefaultUserService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/DefaultUserService.kt index d2eb7a14ef..1740956915 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/DefaultUserService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/DefaultUserService.kt @@ -19,10 +19,13 @@ package org.matrix.android.sdk.internal.session.user import androidx.lifecycle.LiveData import androidx.paging.PagedList import org.matrix.android.sdk.api.MatrixCallback +import org.matrix.android.sdk.api.session.profile.ProfileService import org.matrix.android.sdk.api.session.user.UserService import org.matrix.android.sdk.api.session.user.model.User import org.matrix.android.sdk.api.util.Cancelable +import org.matrix.android.sdk.api.util.JsonDict import org.matrix.android.sdk.api.util.Optional +import org.matrix.android.sdk.internal.session.profile.GetProfileInfoTask import org.matrix.android.sdk.internal.session.user.accountdata.UpdateIgnoredUserIdsTask import org.matrix.android.sdk.internal.session.user.model.SearchUserTask import org.matrix.android.sdk.internal.task.TaskExecutor @@ -32,12 +35,40 @@ import javax.inject.Inject internal class DefaultUserService @Inject constructor(private val userDataSource: UserDataSource, private val searchUserTask: SearchUserTask, private val updateIgnoredUserIdsTask: UpdateIgnoredUserIdsTask, + private val getProfileInfoTask: GetProfileInfoTask, private val taskExecutor: TaskExecutor) : UserService { override fun getUser(userId: String): User? { return userDataSource.getUser(userId) } + override fun resolveUser(userId: String, callback: MatrixCallback) { + val known = getUser(userId) + if (known != null) { + callback.onSuccess(known) + } else { + val params = GetProfileInfoTask.Params(userId) + getProfileInfoTask + .configureWith(params) { + this.callback = object : MatrixCallback { + override fun onSuccess(data: JsonDict) { + callback.onSuccess( + User( + userId, + data[ProfileService.DISPLAY_NAME_KEY] as? String, + data[ProfileService.AVATAR_URL_KEY] as? String) + ) + } + + override fun onFailure(failure: Throwable) { + callback.onFailure(failure) + } + } + } + .executeBy(taskExecutor) + } + } + override fun getUserLive(userId: String): LiveData> { return userDataSource.getUserLive(userId) } diff --git a/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetState.kt b/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetState.kt index 0080b28c66..9ec2071a94 100644 --- a/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetState.kt +++ b/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetState.kt @@ -22,6 +22,14 @@ import com.airbnb.mvrx.Uninitialized import org.matrix.android.sdk.api.util.MatrixItem data class MatrixToBottomSheetState( + val userId: String? = null, + val deepLink: String? = null, val matrixItem: Async = Uninitialized, val startChattingState: Async = Uninitialized -) : MvRxState +) : MvRxState { + + constructor(args: MatrixToBottomSheet.MatrixToArgs) : this( + userId = args.userId, + deepLink = args.matrixToLink + ) +} diff --git a/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetViewModel.kt b/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetViewModel.kt index 06cae3218b..5f06bc6edd 100644 --- a/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/matrixto/MatrixToBottomSheetViewModel.kt @@ -39,24 +39,20 @@ import org.matrix.android.sdk.api.raw.RawService import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.permalinks.PermalinkData import org.matrix.android.sdk.api.session.permalinks.PermalinkParser -import org.matrix.android.sdk.api.session.profile.ProfileService import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams import org.matrix.android.sdk.api.session.user.model.User -import org.matrix.android.sdk.api.util.JsonDict import org.matrix.android.sdk.api.util.toMatrixItem import org.matrix.android.sdk.internal.util.awaitCallback class MatrixToBottomSheetViewModel @AssistedInject constructor( @Assisted initialState: MatrixToBottomSheetState, - @Assisted val args: MatrixToBottomSheet.MatrixToArgs, private val session: Session, private val stringProvider: StringProvider, private val rawService: RawService) : VectorViewModel(initialState) { @AssistedInject.Factory interface Factory { - fun create(initialState: MatrixToBottomSheetState, - args: MatrixToBottomSheet.MatrixToArgs): MatrixToBottomSheetViewModel + fun create(initialState: MatrixToBottomSheetState): MatrixToBottomSheetViewModel } init { @@ -64,14 +60,14 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor( copy(matrixItem = Loading()) } viewModelScope.launch(Dispatchers.IO) { - resolveLink() + resolveLink(initialState) } } - private suspend fun resolveLink() { + private suspend fun resolveLink(initialState: MatrixToBottomSheetState) { when { - args.matrixToLink != null -> { - val linkedId = PermalinkParser.parse(args.matrixToLink) + initialState.deepLink != null -> { + val linkedId = PermalinkParser.parse(initialState.deepLink) if (linkedId is PermalinkData.FallbackLink) { setState { copy( @@ -92,7 +88,9 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor( ) } } - is PermalinkData.RoomLink -> TODO() + is PermalinkData.RoomLink -> { + // not yet supported + } is PermalinkData.GroupLink -> { // not yet supported } @@ -100,8 +98,8 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor( } } } - args.userId != null -> { - val user = resolveUser(args.userId) + initialState.userId != null -> { + val user = resolveUser(initialState.userId) setState { copy( @@ -110,7 +108,7 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor( ) } } - else -> { + else -> { setState { copy( matrixItem = Fail(IllegalArgumentException(stringProvider.getString(R.string.unexpected_error))), @@ -122,24 +120,20 @@ class MatrixToBottomSheetViewModel @AssistedInject constructor( } private suspend fun resolveUser(userId: String): User { - return (session.getUser(userId) - ?: tryOrNull { - awaitCallback { - session.getProfile(userId, it) + return tryOrNull { + awaitCallback { + session.resolveUser(userId, it) } - }?.let { - User(userId, it[ProfileService.DISPLAY_NAME_KEY] as? String, it[ProfileService.AVATAR_URL_KEY] as? String) } - // Create raw Uxid in case the user is not searchable - ?: User(userId, null, null)) + // Create raw user in case the user is not searchable + ?: User(userId, null, null) } companion object : MvRxViewModelFactory { override fun create(viewModelContext: ViewModelContext, state: MatrixToBottomSheetState): MatrixToBottomSheetViewModel? { val fragment: MatrixToBottomSheet = (viewModelContext as FragmentViewModelContext).fragment() - val args: MatrixToBottomSheet.MatrixToArgs = viewModelContext.args() - return fragment.matrixToBottomSheetViewModelFactory.create(state, args) + return fragment.matrixToBottomSheetViewModelFactory.create(state) } } diff --git a/vector/src/main/java/im/vector/app/features/usercode/UserCodeSharedViewModel.kt b/vector/src/main/java/im/vector/app/features/usercode/UserCodeSharedViewModel.kt index 93b198f9d7..98acab147e 100644 --- a/vector/src/main/java/im/vector/app/features/usercode/UserCodeSharedViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/usercode/UserCodeSharedViewModel.kt @@ -35,10 +35,8 @@ import org.matrix.android.sdk.api.raw.RawService import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.permalinks.PermalinkData import org.matrix.android.sdk.api.session.permalinks.PermalinkParser -import org.matrix.android.sdk.api.session.profile.ProfileService import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams import org.matrix.android.sdk.api.session.user.model.User -import org.matrix.android.sdk.api.util.JsonDict import org.matrix.android.sdk.api.util.toMatrixItem import org.matrix.android.sdk.internal.util.awaitCallback @@ -147,15 +145,12 @@ class UserCodeSharedViewModel @AssistedInject constructor( _viewEvents.post(UserCodeShareViewEvents.ToastMessage(stringProvider.getString(R.string.not_implemented))) } is PermalinkData.UserLink -> { - val user = session.getUser(linkedId.userId) - ?: tryOrNull { - awaitCallback { - session.getProfile(linkedId.userId, it) - } - }?.let { - User(linkedId.userId, it[ProfileService.DISPLAY_NAME_KEY] as? String, it[ProfileService.AVATAR_URL_KEY] as? String) - } - // Create raw Uxid in case the user is not searchable + val user = tryOrNull { + awaitCallback { + session.resolveUser(linkedId.userId, it) + } + } + // Create raw Uxid in case the user is not searchable ?: User(linkedId.userId, null, null) setState {