From 7699628959c32b3743ca039f3e5c6f89abffc7f3 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 14 Oct 2022 12:25:55 +0200 Subject: [PATCH] Fix other potential issue when using Session.getUser() --- .../timeline/helper/LocationPinProvider.kt | 8 +++--- .../app/features/html/PillsPostProcessor.kt | 4 +-- .../location/LocationSharingViewModel.kt | 4 +-- .../map/UserLiveLocationViewStateMapper.kt | 26 +++++++++---------- .../notifications/NotifiableEventResolver.kt | 4 +-- .../NotificationDrawerManager.kt | 8 +++--- .../signout/soft/SoftLogoutViewModel.kt | 6 +++-- .../invite/SpaceInviteBottomSheetViewModel.kt | 6 ++--- .../usercode/UserCodeSharedViewModel.kt | 6 ++--- 9 files changed, 36 insertions(+), 36 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/LocationPinProvider.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/LocationPinProvider.kt index 8ef910c931..7f276f2f73 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/LocationPinProvider.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/LocationPinProvider.kt @@ -29,7 +29,7 @@ import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.glide.GlideApp import im.vector.app.core.utils.DimensionConverter import im.vector.app.features.home.AvatarRenderer -import org.matrix.android.sdk.api.session.getUser +import org.matrix.android.sdk.api.session.getUserOrDefault import org.matrix.android.sdk.api.util.toMatrixItem import timber.log.Timber import javax.inject.Inject @@ -67,9 +67,9 @@ class LocationPinProvider @Inject constructor( activeSessionHolder .getActiveSession() - .getUser(userId) - ?.toMatrixItem() - ?.let { userItem -> + .getUserOrDefault(userId) + .toMatrixItem() + .let { userItem -> val size = dimensionConverter.dpToPx(44) val bgTintColor = matrixItemColorProvider.getColor(userItem) avatarRenderer.render(glideRequests, userItem, object : CustomTarget(size, size) { diff --git a/vector/src/main/java/im/vector/app/features/html/PillsPostProcessor.kt b/vector/src/main/java/im/vector/app/features/html/PillsPostProcessor.kt index 5f20b7278e..85cfb76ff7 100644 --- a/vector/src/main/java/im/vector/app/features/html/PillsPostProcessor.kt +++ b/vector/src/main/java/im/vector/app/features/html/PillsPostProcessor.kt @@ -27,7 +27,7 @@ import im.vector.app.core.glide.GlideApp import im.vector.app.features.home.AvatarRenderer import io.noties.markwon.core.spans.LinkSpan import org.matrix.android.sdk.api.session.getRoomSummary -import org.matrix.android.sdk.api.session.getUser +import org.matrix.android.sdk.api.session.getUserOrDefault 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.room.model.RoomSummary @@ -101,7 +101,7 @@ class PillsPostProcessor @AssistedInject constructor( private fun PermalinkData.UserLink.toMatrixItem(roomId: String?): MatrixItem? = if (roomId == null) { - sessionHolder.getSafeActiveSession()?.getUser(userId)?.toMatrixItem() + sessionHolder.getSafeActiveSession()?.getUserOrDefault(userId)?.toMatrixItem() } else { sessionHolder.getSafeActiveSession()?.roomService()?.getRoomMember(userId, roomId)?.toMatrixItem() } diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingViewModel.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingViewModel.kt index 28e37a38eb..4c7abd99b8 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingViewModel.kt @@ -40,7 +40,7 @@ import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.getRoom -import org.matrix.android.sdk.api.session.getUser +import org.matrix.android.sdk.api.session.getUserOrDefault import org.matrix.android.sdk.api.session.room.powerlevels.PowerLevelsHelper import org.matrix.android.sdk.api.util.toMatrixItem import timber.log.Timber @@ -101,7 +101,7 @@ class LocationSharingViewModel @AssistedInject constructor( } private fun setUserItem() { - setState { copy(userItem = session.getUser(session.myUserId)?.toMatrixItem()) } + setState { copy(userItem = session.getUserOrDefault(session.myUserId).toMatrixItem()) } } private fun updatePin(isUserPin: Boolean? = true) { diff --git a/vector/src/main/java/im/vector/app/features/location/live/map/UserLiveLocationViewStateMapper.kt b/vector/src/main/java/im/vector/app/features/location/live/map/UserLiveLocationViewStateMapper.kt index 77f8c30fd3..f4f4a462ce 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/map/UserLiveLocationViewStateMapper.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/map/UserLiveLocationViewStateMapper.kt @@ -20,7 +20,7 @@ import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.features.home.room.detail.timeline.helper.LocationPinProvider import im.vector.app.features.location.toLocationData import kotlinx.coroutines.suspendCancellableCoroutine -import org.matrix.android.sdk.api.session.getUser +import org.matrix.android.sdk.api.session.getUserOrDefault import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary import org.matrix.android.sdk.api.util.toMatrixItem import javax.inject.Inject @@ -45,19 +45,17 @@ class UserLiveLocationViewStateMapper @Inject constructor( else -> { locationPinProvider.create(userId) { pinDrawable -> val session = activeSessionHolder.getActiveSession() - session.getUser(userId)?.toMatrixItem()?.let { matrixItem -> - val locationTimestampMillis = liveLocationShareAggregatedSummary.lastLocationDataContent?.getBestTimestampMillis() - val viewState = UserLiveLocationViewState( - matrixItem = matrixItem, - pinDrawable = pinDrawable, - locationData = locationData, - endOfLiveTimestampMillis = liveLocationShareAggregatedSummary.endOfLiveTimestampMillis, - locationTimestampMillis = locationTimestampMillis, - showStopSharingButton = userId == session.myUserId - ) - continuation.resume(viewState) { - // do nothing on cancellation - } + val locationTimestampMillis = liveLocationShareAggregatedSummary.lastLocationDataContent?.getBestTimestampMillis() + val viewState = UserLiveLocationViewState( + matrixItem = session.getUserOrDefault(userId).toMatrixItem(), + pinDrawable = pinDrawable, + locationData = locationData, + endOfLiveTimestampMillis = liveLocationShareAggregatedSummary.endOfLiveTimestampMillis, + locationTimestampMillis = locationTimestampMillis, + showStopSharingButton = userId == session.myUserId + ) + continuation.resume(viewState) { + // do nothing on cancellation } } } diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventResolver.kt b/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventResolver.kt index 4ee7da4b64..ba1d5c7f6f 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventResolver.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotifiableEventResolver.kt @@ -38,7 +38,7 @@ import org.matrix.android.sdk.api.session.events.model.supportsNotification import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.getRoom import org.matrix.android.sdk.api.session.getRoomSummary -import org.matrix.android.sdk.api.session.getUser +import org.matrix.android.sdk.api.session.getUserOrDefault import org.matrix.android.sdk.api.session.room.getTimelineEvent import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.RoomMemberContent @@ -112,7 +112,7 @@ class NotifiableEventResolver @Inject constructor( val notificationAction = actions.toNotificationAction() return if (notificationAction.shouldNotify) { - val user = session.getUser(event.senderId!!) ?: return null + val user = session.getUserOrDefault(event.senderId!!) val timelineEvent = TimelineEvent( root = event, diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt index 5f43ff6b90..2623045cf3 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt @@ -27,7 +27,7 @@ import im.vector.app.features.displayname.getBestName import im.vector.app.features.settings.VectorPreferences import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.content.ContentUrlResolver -import org.matrix.android.sdk.api.session.getUser +import org.matrix.android.sdk.api.session.getUserOrDefault import org.matrix.android.sdk.api.util.toMatrixItem import timber.log.Timber import javax.inject.Inject @@ -186,11 +186,11 @@ class NotificationDrawerManager @Inject constructor( } private fun renderEvents(session: Session, eventsToRender: List>) { - val user = session.getUser(session.myUserId) + val user = session.getUserOrDefault(session.myUserId) // myUserDisplayName cannot be empty else NotificationCompat.MessagingStyle() will crash - val myUserDisplayName = user?.toMatrixItem()?.getBestName() ?: session.myUserId + val myUserDisplayName = user.toMatrixItem().getBestName() val myUserAvatarUrl = session.contentUrlResolver().resolveThumbnail( - contentUrl = user?.avatarUrl, + contentUrl = user.avatarUrl, width = avatarSize, height = avatarSize, method = ContentUrlResolver.ThumbnailMethod.SCALE diff --git a/vector/src/main/java/im/vector/app/features/signout/soft/SoftLogoutViewModel.kt b/vector/src/main/java/im/vector/app/features/signout/soft/SoftLogoutViewModel.kt index f3e2f82edc..117c298878 100644 --- a/vector/src/main/java/im/vector/app/features/signout/soft/SoftLogoutViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/signout/soft/SoftLogoutViewModel.kt @@ -32,6 +32,7 @@ import im.vector.app.core.di.SingletonEntryPoint import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.extensions.hasUnsavedKeys import im.vector.app.core.platform.VectorViewModel +import im.vector.app.features.displayname.getBestName import im.vector.app.features.login.LoginMode import im.vector.app.features.login.toSsoState import kotlinx.coroutines.launch @@ -39,7 +40,8 @@ import org.matrix.android.sdk.api.auth.AuthenticationService import org.matrix.android.sdk.api.auth.LoginType import org.matrix.android.sdk.api.auth.data.LoginFlowTypes import org.matrix.android.sdk.api.session.Session -import org.matrix.android.sdk.api.session.getUser +import org.matrix.android.sdk.api.session.getUserOrDefault +import org.matrix.android.sdk.api.util.toMatrixItem import timber.log.Timber class SoftLogoutViewModel @AssistedInject constructor( @@ -68,7 +70,7 @@ class SoftLogoutViewModel @AssistedInject constructor( homeServerUrl = session.sessionParams.homeServerUrl, userId = userId, deviceId = session.sessionParams.deviceId.orEmpty(), - userDisplayName = session.getUser(userId)?.displayName ?: userId, + userDisplayName = session.getUserOrDefault(userId).toMatrixItem().getBestName(), hasUnsavedKeys = session.hasUnsavedKeys(), loginType = session.sessionParams.loginType, ) diff --git a/vector/src/main/java/im/vector/app/features/spaces/invite/SpaceInviteBottomSheetViewModel.kt b/vector/src/main/java/im/vector/app/features/spaces/invite/SpaceInviteBottomSheetViewModel.kt index ea36908dd2..27f194e8d2 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/invite/SpaceInviteBottomSheetViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/invite/SpaceInviteBottomSheetViewModel.kt @@ -34,7 +34,7 @@ import kotlinx.coroutines.launch import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.getRoomSummary -import org.matrix.android.sdk.api.session.getUser +import org.matrix.android.sdk.api.session.getUserOrDefault import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.RoomSummary import org.matrix.android.sdk.api.session.room.peeking.PeekResult @@ -49,7 +49,7 @@ class SpaceInviteBottomSheetViewModel @AssistedInject constructor( session.getRoomSummary(initialState.spaceId)?.let { roomSummary -> val knownMembers = roomSummary.otherMemberIds.filter { session.roomService().getExistingDirectRoomWithUser(it) != null - }.mapNotNull { session.getUser(it) } + }.map { session.getUserOrDefault(it) } // put one with avatar first, and take 5 val peopleYouKnow = (knownMembers.filter { it.avatarUrl != null } + knownMembers.filter { it.avatarUrl == null }) .take(5) @@ -57,7 +57,7 @@ class SpaceInviteBottomSheetViewModel @AssistedInject constructor( setState { copy( summary = Success(roomSummary), - inviterUser = roomSummary.inviterId?.let { session.getUser(it) }?.let { Success(it) } ?: Uninitialized, + inviterUser = roomSummary.inviterId?.let { session.getUserOrDefault(it) }?.let { Success(it) } ?: Uninitialized, peopleYouKnow = Success(peopleYouKnow) ) } 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 8c377cafd5..e76837f182 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 @@ -30,7 +30,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.session.Session -import org.matrix.android.sdk.api.session.getUser +import org.matrix.android.sdk.api.session.getUserOrDefault 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.user.model.User @@ -46,10 +46,10 @@ class UserCodeSharedViewModel @AssistedInject constructor( companion object : MavericksViewModelFactory by hiltMavericksViewModelFactory() init { - val user = session.getUser(initialState.userId) + val user = session.getUserOrDefault(initialState.userId) setState { copy( - matrixItem = user?.toMatrixItem(), + matrixItem = user.toMatrixItem(), shareLink = session.permalinkService().createPermalink(initialState.userId) ) }