From d9723387ebb7be282aeb9fb0749bd39b3340fa2c Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 29 Oct 2020 11:00:01 +0100 Subject: [PATCH] We only need the roomId in many cases, so update the API --- .../org/matrix/android/sdk/api/session/room/RoomService.kt | 5 ++++- .../android/sdk/internal/session/room/DefaultRoomService.kt | 2 +- .../matrix/android/sdk/internal/session/room/RoomGetter.kt | 5 ++--- .../crypto/verification/VerificationBottomSheetViewModel.kt | 2 +- .../app/features/home/room/detail/RoomDetailViewModel.kt | 6 +++--- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomService.kt index 9bd7738af4..0e42e36d86 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomService.kt @@ -129,5 +129,8 @@ interface RoomService { */ fun getChangeMembershipsLive(): LiveData> - fun getExistingDirectRoomWithUser(otherUserId: String): Room? + /** + * Return the roomId of an existing DM with the other user, or null if such room does not exist + */ + fun getExistingDirectRoomWithUser(otherUserId: String): String? } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomService.kt index 171e90703c..d49c2f120c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/DefaultRoomService.kt @@ -61,7 +61,7 @@ internal class DefaultRoomService @Inject constructor( return roomGetter.getRoom(roomId) } - override fun getExistingDirectRoomWithUser(otherUserId: String): Room? { + override fun getExistingDirectRoomWithUser(otherUserId: String): String? { return roomGetter.getDirectRoomWith(otherUserId) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomGetter.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomGetter.kt index 2947518605..ebf2b402a4 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomGetter.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomGetter.kt @@ -31,7 +31,7 @@ import javax.inject.Inject internal interface RoomGetter { fun getRoom(roomId: String): Room? - fun getDirectRoomWith(otherUserId: String): Room? + fun getDirectRoomWith(otherUserId: String): String? } @SessionScope @@ -46,7 +46,7 @@ internal class DefaultRoomGetter @Inject constructor( } } - override fun getDirectRoomWith(otherUserId: String): Room? { + override fun getDirectRoomWith(otherUserId: String): String? { return realmSessionProvider.withRealm { realm -> RoomSummaryEntity.where(realm) .equalTo(RoomSummaryEntityFields.IS_DIRECT, true) @@ -55,7 +55,6 @@ internal class DefaultRoomGetter @Inject constructor( .filter { dm -> dm.otherMemberIds.contains(otherUserId) } .map { it.roomId } .firstOrNull { roomId -> otherUserId in RoomMemberHelper(realm, roomId).getActiveRoomMemberIds() } - ?.let { roomId -> createRoom(realm, roomId) } } } diff --git a/vector/src/main/java/im/vector/app/features/crypto/verification/VerificationBottomSheetViewModel.kt b/vector/src/main/java/im/vector/app/features/crypto/verification/VerificationBottomSheetViewModel.kt index 22711108f8..2d09974687 100644 --- a/vector/src/main/java/im/vector/app/features/crypto/verification/VerificationBottomSheetViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/crypto/verification/VerificationBottomSheetViewModel.kt @@ -232,7 +232,7 @@ class VerificationBottomSheetViewModel @AssistedInject constructor( override fun handle(action: VerificationAction) = withState { state -> val otherUserId = state.otherUserMxItem?.id ?: return@withState val roomId = state.roomId - ?: session.getExistingDirectRoomWithUser(otherUserId)?.roomId + ?: session.getExistingDirectRoomWithUser(otherUserId) when (action) { is VerificationAction.RequestVerificationByDM -> { diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt index ca387db1f6..5d978ed6a4 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailViewModel.kt @@ -279,8 +279,8 @@ class RoomDetailViewModel @AssistedInject constructor( } private fun handleOpenOrCreateDm(action: RoomDetailAction.OpenOrCreateDm) { - val existingDm = session.getExistingDirectRoomWithUser(action.userId) - if (existingDm == null) { + val existingDmRoomId = session.getExistingDirectRoomWithUser(action.userId) + if (existingDmRoomId == null) { // First create a direct room viewModelScope.launch(Dispatchers.IO) { val roomId = awaitCallback { @@ -289,7 +289,7 @@ class RoomDetailViewModel @AssistedInject constructor( _viewEvents.post(RoomDetailViewEvents.OpenRoom(roomId)) } } else { - _viewEvents.post(RoomDetailViewEvents.OpenRoom(existingDm.roomId)) + _viewEvents.post(RoomDetailViewEvents.OpenRoom(existingDmRoomId)) } }