Fix inactivity status when last seen timestamp is null

This commit is contained in:
Maxime NATUREL 2022-12-27 16:30:05 +01:00 committed by Maxime NATUREL
parent 87928b4e12
commit 01429b352a
5 changed files with 49 additions and 21 deletions

View file

@ -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)
}
}

View file

@ -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

View file

@ -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())
}
}
}

View file

@ -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

View file

@ -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()
}
}