From 3ad754e732cdc9b17cf2baa5ca4de1525a40bb7d Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Tue, 19 Jul 2022 14:51:44 +0200 Subject: [PATCH 1/4] Adding start of live timestamp threshold when deactivating previous beacons --- .../LiveLocationShareAggregatedSummaryEntity.kt | 2 ++ ...iveLocationShareAggregatedSummaryEntityQuery.kt | 2 ++ .../LiveLocationAggregationProcessor.kt | 14 +++++++++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/livelocation/LiveLocationShareAggregatedSummaryEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/livelocation/LiveLocationShareAggregatedSummaryEntity.kt index 08ea06bb1e..ca793ffd8e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/livelocation/LiveLocationShareAggregatedSummaryEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/livelocation/LiveLocationShareAggregatedSummaryEntity.kt @@ -44,6 +44,8 @@ internal open class LiveLocationShareAggregatedSummaryEntity( */ var isActive: Boolean? = null, + var startOfLiveTimestampMillis: Long? = null, + var endOfLiveTimestampMillis: Long? = null, /** diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/LiveLocationShareAggregatedSummaryEntityQuery.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/LiveLocationShareAggregatedSummaryEntityQuery.kt index fbf7e963a7..a1179ccdce 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/LiveLocationShareAggregatedSummaryEntityQuery.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/query/LiveLocationShareAggregatedSummaryEntityQuery.kt @@ -92,12 +92,14 @@ internal fun LiveLocationShareAggregatedSummaryEntity.Companion.findActiveLiveIn roomId: String, userId: String, ignoredEventId: String, + startOfLiveTimestampThreshold: Long, ): List { return LiveLocationShareAggregatedSummaryEntity .whereRoomId(realm, roomId = roomId) .equalTo(LiveLocationShareAggregatedSummaryEntityFields.USER_ID, userId) .equalTo(LiveLocationShareAggregatedSummaryEntityFields.IS_ACTIVE, true) .notEqualTo(LiveLocationShareAggregatedSummaryEntityFields.EVENT_ID, ignoredEventId) + .lessThan(LiveLocationShareAggregatedSummaryEntityFields.START_OF_LIVE_TIMESTAMP_MILLIS, startOfLiveTimestampThreshold) .findAll() .toList() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/aggregation/livelocation/LiveLocationAggregationProcessor.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/aggregation/livelocation/LiveLocationAggregationProcessor.kt index 3f5b1e1360..510c20497b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/aggregation/livelocation/LiveLocationAggregationProcessor.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/aggregation/livelocation/LiveLocationAggregationProcessor.kt @@ -84,11 +84,12 @@ internal class LiveLocationAggregationProcessor @Inject constructor( val endOfLiveTimestampMillis = content.getBestTimestampMillis()?.let { it + (content.timeout ?: 0) } Timber.d("updating summary of id=$targetEventId with isActive=$isActive and endTimestamp=$endOfLiveTimestampMillis") + aggregatedSummary.startOfLiveTimestampMillis = content.getBestTimestampMillis() aggregatedSummary.endOfLiveTimestampMillis = endOfLiveTimestampMillis aggregatedSummary.isActive = isActive aggregatedSummary.userId = event.senderId - deactivateAllPreviousBeacons(realm, roomId, event.senderId, targetEventId) + deactivateAllPreviousBeacons(realm, roomId, event.senderId, targetEventId, content.getBestTimestampMillis() ?: 0) if (isActive) { scheduleDeactivationAfterTimeout(targetEventId, roomId, endOfLiveTimestampMillis) @@ -182,13 +183,20 @@ internal class LiveLocationAggregationProcessor @Inject constructor( aggregatedSummary.relatedEventIds = RealmList(*updatedEventIds.toTypedArray()) } - private fun deactivateAllPreviousBeacons(realm: Realm, roomId: String, userId: String, currentEventId: String) { + private fun deactivateAllPreviousBeacons( + realm: Realm, + roomId: String, + userId: String, + currentEventId: String, + currentEventTimestamp: Long + ) { LiveLocationShareAggregatedSummaryEntity .findActiveLiveInRoomForUser( realm = realm, roomId = roomId, userId = userId, - ignoredEventId = currentEventId + ignoredEventId = currentEventId, + startOfLiveTimestampThreshold = currentEventTimestamp ) .forEach { it.isActive = false } } From a48d33dc3c17cd156ca021ff0e531b85ad92db13 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Tue, 19 Jul 2022 15:43:10 +0200 Subject: [PATCH 2/4] Adding changelog entry --- changelog.d/6596.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/6596.bugfix diff --git a/changelog.d/6596.bugfix b/changelog.d/6596.bugfix new file mode 100644 index 0000000000..8cf97778c8 --- /dev/null +++ b/changelog.d/6596.bugfix @@ -0,0 +1 @@ +[Location Share] - Live is considered as ended while still active From ca8e4f51e95c4244b627f291a6779ac09758f6fe Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Tue, 19 Jul 2022 16:20:16 +0200 Subject: [PATCH 3/4] Updating unit tests of aggregation process --- .../livelocation/LiveLocationAggregationProcessorTest.kt | 3 +++ .../java/org/matrix/android/sdk/test/fakes/FakeRealm.kt | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/room/aggregation/livelocation/LiveLocationAggregationProcessorTest.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/room/aggregation/livelocation/LiveLocationAggregationProcessorTest.kt index a5e91714b7..25d441ef5c 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/room/aggregation/livelocation/LiveLocationAggregationProcessorTest.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/session/room/aggregation/livelocation/LiveLocationAggregationProcessorTest.kt @@ -36,6 +36,7 @@ import org.matrix.android.sdk.test.fakes.FakeWorkManagerProvider import org.matrix.android.sdk.test.fakes.givenEqualTo import org.matrix.android.sdk.test.fakes.givenFindAll import org.matrix.android.sdk.test.fakes.givenFindFirst +import org.matrix.android.sdk.test.fakes.givenLessThan import org.matrix.android.sdk.test.fakes.givenNotEqualTo private const val A_SESSION_ID = "session_id" @@ -183,6 +184,7 @@ internal class LiveLocationAggregationProcessorTest { aggregatedEntity.roomId shouldBeEqualTo A_ROOM_ID aggregatedEntity.userId shouldBeEqualTo A_SENDER_ID aggregatedEntity.isActive shouldBeEqualTo true + aggregatedEntity.startOfLiveTimestampMillis shouldBeEqualTo A_TIMESTAMP aggregatedEntity.endOfLiveTimestampMillis shouldBeEqualTo A_TIMESTAMP + A_TIMEOUT_MILLIS aggregatedEntity.lastLocationContent shouldBeEqualTo null previousEntities.forEach { entity -> @@ -404,6 +406,7 @@ internal class LiveLocationAggregationProcessorTest { .givenNotEqualTo(LiveLocationShareAggregatedSummaryEntityFields.EVENT_ID, AN_EVENT_ID) .givenEqualTo(LiveLocationShareAggregatedSummaryEntityFields.USER_ID, A_SENDER_ID) .givenEqualTo(LiveLocationShareAggregatedSummaryEntityFields.IS_ACTIVE, true) + .givenLessThan(LiveLocationShareAggregatedSummaryEntityFields.START_OF_LIVE_TIMESTAMP_MILLIS, A_TIMESTAMP) .givenFindAll(summaryList) return summaryList } diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeRealm.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeRealm.kt index cb40889fb7..afdcf111f8 100644 --- a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeRealm.kt +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeRealm.kt @@ -101,6 +101,14 @@ inline fun RealmQuery.givenIsNotNull( return this } +inline fun RealmQuery.givenLessThan( + fieldName: String, + value: Long +): RealmQuery { + every { lessThan(fieldName, value) } returns this + return this +} + /** * Should be called on a mocked RealmObject and not on a real RealmObject so that the underlying final method is mocked. */ From a5fffdfc1b1f042e7b122e243c09f053907925e8 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 20 Jul 2022 10:00:19 +0200 Subject: [PATCH 4/4] DB migration --- .../database/RealmSessionStoreMigration.kt | 4 ++- .../database/migration/MigrateSessionTo034.kt | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo034.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt index 9784412761..64e69bb3e9 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt @@ -50,6 +50,7 @@ import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo030 import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo031 import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo032 import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo033 +import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo034 import org.matrix.android.sdk.internal.util.Normalizer import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration import javax.inject.Inject @@ -58,7 +59,7 @@ internal class RealmSessionStoreMigration @Inject constructor( private val normalizer: Normalizer ) : MatrixRealmMigration( dbName = "Session", - schemaVersion = 33L, + schemaVersion = 34L, ) { /** * Forces all RealmSessionStoreMigration instances to be equal. @@ -101,5 +102,6 @@ internal class RealmSessionStoreMigration @Inject constructor( if (oldVersion < 31) MigrateSessionTo031(realm).perform() if (oldVersion < 32) MigrateSessionTo032(realm).perform() if (oldVersion < 33) MigrateSessionTo033(realm).perform() + if (oldVersion < 34) MigrateSessionTo034(realm).perform() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo034.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo034.kt new file mode 100644 index 0000000000..b23e84706f --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo034.kt @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 The Matrix.org Foundation C.I.C. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.database.migration + +import io.realm.DynamicRealm +import org.matrix.android.sdk.internal.database.model.livelocation.LiveLocationShareAggregatedSummaryEntityFields +import org.matrix.android.sdk.internal.util.database.RealmMigrator + +/** + * Migrating to: + * Live location sharing aggregated summary: adding new field startOfLiveTimestampMillis. + */ +internal class MigrateSessionTo034(realm: DynamicRealm) : RealmMigrator(realm, 34) { + + override fun doMigrate(realm: DynamicRealm) { + realm.schema.get("LiveLocationShareAggregatedSummaryEntity") + ?.addField(LiveLocationShareAggregatedSummaryEntityFields.START_OF_LIVE_TIMESTAMP_MILLIS, Long::class.java) + ?.setNullable(LiveLocationShareAggregatedSummaryEntityFields.START_OF_LIVE_TIMESTAMP_MILLIS, true) + } +}