From 01429b352af7616f868b9dc9588d3847360ac3c4 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Tue, 27 Dec 2022 16:30:05 +0100 Subject: [PATCH] Fix inactivity status when last seen timestamp is null --- .../settings/devices/DevicesViewModel.kt | 2 +- .../v2/GetDeviceFullInfoListUseCase.kt | 2 +- .../list/CheckIfSessionIsInactiveUseCase.kt | 14 +++--- .../v2/overview/GetDeviceFullInfoUseCase.kt | 2 +- .../CheckIfSessionIsInactiveUseCaseTest.kt | 50 ++++++++++++++----- 5 files changed, 49 insertions(+), 21 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/DevicesViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/devices/DevicesViewModel.kt index e779948b41..cbc02ba0f0 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/DevicesViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/DevicesViewModel.kt @@ -142,7 +142,7 @@ class DevicesViewModel @AssistedInject constructor( .map { deviceInfo -> val cryptoDeviceInfo = cryptoList.firstOrNull { it.deviceId == deviceInfo.deviceId } val trustLevelForShield = getEncryptionTrustLevelForDeviceUseCase.execute(currentSessionCrossSigningInfo, cryptoDeviceInfo) - val isInactive = checkIfSessionIsInactiveUseCase.execute(deviceInfo.lastSeenTs ?: 0) + val isInactive = checkIfSessionIsInactiveUseCase.execute(deviceInfo.lastSeenTs) DeviceFullInfo(deviceInfo, cryptoDeviceInfo, trustLevelForShield, isInactive) } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/GetDeviceFullInfoListUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/GetDeviceFullInfoListUseCase.kt index 6adb33d5ab..1ae7d8836f 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/GetDeviceFullInfoListUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/GetDeviceFullInfoListUseCase.kt @@ -75,7 +75,7 @@ class GetDeviceFullInfoListUseCase @Inject constructor( .map { deviceInfo -> val cryptoDeviceInfo = cryptoList.firstOrNull { it.deviceId == deviceInfo.deviceId } val roomEncryptionTrustLevel = getEncryptionTrustLevelForDeviceUseCase.execute(currentSessionCrossSigningInfo, cryptoDeviceInfo) - val isInactive = checkIfSessionIsInactiveUseCase.execute(deviceInfo.lastSeenTs ?: 0) + val isInactive = checkIfSessionIsInactiveUseCase.execute(deviceInfo.lastSeenTs) val isCurrentDevice = currentSessionCrossSigningInfo.deviceId == cryptoDeviceInfo?.deviceId val deviceExtendedInfo = parseDeviceUserAgentUseCase.execute(deviceInfo.getBestLastSeenUserAgent()) val matrixClientInfo = deviceInfo.deviceId diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/list/CheckIfSessionIsInactiveUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/list/CheckIfSessionIsInactiveUseCase.kt index 8991ad1e3d..f3670793bd 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/list/CheckIfSessionIsInactiveUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/list/CheckIfSessionIsInactiveUseCase.kt @@ -24,11 +24,13 @@ class CheckIfSessionIsInactiveUseCase @Inject constructor( private val clock: Clock, ) { - fun execute(lastSeenTs: Long): Boolean { - // In case of the server doesn't send the last seen date. - if (lastSeenTs == 0L) return true - - val diffMilliseconds = clock.epochMillis() - lastSeenTs - return diffMilliseconds >= TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong()) + fun execute(lastSeenTsMillis: Long?): Boolean { + return if (lastSeenTsMillis == null || lastSeenTsMillis <= 0) { + // in these situations we cannot say anything about the inactivity of the session + false + } else { + val diffMilliseconds = clock.epochMillis() - lastSeenTsMillis + diffMilliseconds >= TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong()) + } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCase.kt index 140b55a30c..3b67eee780 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/overview/GetDeviceFullInfoUseCase.kt @@ -51,7 +51,7 @@ class GetDeviceFullInfoUseCase @Inject constructor( val cryptoInfo = cryptoDeviceInfo.getOrNull() val fullInfo = if (info != null && cryptoInfo != null) { val roomEncryptionTrustLevel = getEncryptionTrustLevelForDeviceUseCase.execute(currentSessionCrossSigningInfo, cryptoInfo) - val isInactive = checkIfSessionIsInactiveUseCase.execute(info.lastSeenTs ?: 0) + val isInactive = checkIfSessionIsInactiveUseCase.execute(info.lastSeenTs) val isCurrentDevice = currentSessionCrossSigningInfo.deviceId == cryptoInfo.deviceId val deviceUserAgent = parseDeviceUserAgentUseCase.execute(info.getBestLastSeenUserAgent()) val matrixClientInfo = info.deviceId diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/list/CheckIfSessionIsInactiveUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/list/CheckIfSessionIsInactiveUseCaseTest.kt index b7d56a88bf..20291e5ee2 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/list/CheckIfSessionIsInactiveUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/list/CheckIfSessionIsInactiveUseCaseTest.kt @@ -17,43 +17,69 @@ package im.vector.app.features.settings.devices.v2.list import im.vector.app.test.fakes.FakeClock -import org.amshove.kluent.shouldBeEqualTo +import org.amshove.kluent.shouldBeFalse +import org.amshove.kluent.shouldBeTrue import org.junit.Test import java.util.concurrent.TimeUnit -private const val A_TIMESTAMP = 1654689143L +private const val A_TIMESTAMP_MILLIS = 1654689143000L class CheckIfSessionIsInactiveUseCaseTest { - private val clock = FakeClock().apply { givenEpoch(A_TIMESTAMP) } + private val clock = FakeClock().apply { givenEpoch(A_TIMESTAMP_MILLIS) } private val checkIfSessionIsInactiveUseCase = CheckIfSessionIsInactiveUseCase(clock) @Test fun `given an old last seen date then session is inactive`() { - val lastSeenDate = A_TIMESTAMP - TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong()) - 1 + val lastSeenDate = A_TIMESTAMP_MILLIS - TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong()) - 1 - checkIfSessionIsInactiveUseCase.execute(lastSeenDate) shouldBeEqualTo true + val result = checkIfSessionIsInactiveUseCase.execute(lastSeenDate) + + result.shouldBeTrue() } @Test fun `given a last seen date equal to the threshold then session is inactive`() { - val lastSeenDate = A_TIMESTAMP - TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong()) + val lastSeenDate = A_TIMESTAMP_MILLIS - TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong()) - checkIfSessionIsInactiveUseCase.execute(lastSeenDate) shouldBeEqualTo true + val result = checkIfSessionIsInactiveUseCase.execute(lastSeenDate) + + result.shouldBeTrue() } @Test fun `given a recent last seen date then session is active`() { - val lastSeenDate = A_TIMESTAMP - TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong()) + 1 + val lastSeenDate = A_TIMESTAMP_MILLIS - TimeUnit.DAYS.toMillis(SESSION_IS_MARKED_AS_INACTIVE_AFTER_DAYS.toLong()) + 1 - checkIfSessionIsInactiveUseCase.execute(lastSeenDate) shouldBeEqualTo false + val result = checkIfSessionIsInactiveUseCase.execute(lastSeenDate) + + result.shouldBeFalse() } @Test - fun `given a last seen date as zero then session is inactive`() { - // In case of the server doesn't send the last seen date. + fun `given a last seen date as zero then session is not inactive`() { val lastSeenDate = 0L - checkIfSessionIsInactiveUseCase.execute(lastSeenDate) shouldBeEqualTo true + val result = checkIfSessionIsInactiveUseCase.execute(lastSeenDate) + + result.shouldBeFalse() + } + + @Test + fun `given a last seen date as null then session is not inactive`() { + val lastSeenDate = null + + val result = checkIfSessionIsInactiveUseCase.execute(lastSeenDate) + + result.shouldBeFalse() + } + + @Test + fun `given a last seen date as negative then session is not inactive`() { + val lastSeenDate = -3L + + val result = checkIfSessionIsInactiveUseCase.execute(lastSeenDate) + + result.shouldBeFalse() } }