Deactivate all previous active beacons when receiving one from user

This commit is contained in:
Maxime NATUREL 2022-05-18 09:22:19 +02:00
parent 40d8d5c605
commit 79212321a2
3 changed files with 33 additions and 5 deletions

View File

@ -24,7 +24,7 @@ import dagger.assisted.AssistedInject
import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary
import org.matrix.android.sdk.internal.database.mapper.LiveLocationShareAggregatedSummaryMapper
import org.matrix.android.sdk.internal.database.model.livelocation.LiveLocationShareAggregatedSummaryEntity
import org.matrix.android.sdk.internal.database.query.findRunningLiveLocationShareInRoom
import org.matrix.android.sdk.internal.database.query.findRunningLiveInRoom
import org.matrix.android.sdk.internal.di.SessionDatabase
// TODO add unit tests
@ -41,7 +41,7 @@ internal class DefaultLocationSharingService @AssistedInject constructor(
override fun getRunningLiveLocationShareSummaries(): LiveData<List<LiveLocationShareAggregatedSummary>> {
return monarchy.findAllMappedWithChanges(
{ LiveLocationShareAggregatedSummaryEntity.findRunningLiveLocationShareInRoom(it, roomId = roomId) },
{ LiveLocationShareAggregatedSummaryEntity.findRunningLiveInRoom(it, roomId = roomId) },
{ liveLocationShareAggregatedSummaryMapper.map(it) }
)
}

View File

@ -20,7 +20,6 @@ import io.realm.Realm
import io.realm.RealmQuery
import io.realm.kotlin.where
import org.matrix.android.sdk.internal.database.model.EventAnnotationsSummaryEntity
import org.matrix.android.sdk.internal.database.model.TimelineEventEntityFields
import org.matrix.android.sdk.internal.database.model.livelocation.LiveLocationShareAggregatedSummaryEntity
import org.matrix.android.sdk.internal.database.model.livelocation.LiveLocationShareAggregatedSummaryEntityFields
@ -37,7 +36,7 @@ internal fun LiveLocationShareAggregatedSummaryEntity.Companion.where(
internal fun LiveLocationShareAggregatedSummaryEntity.Companion.whereRoomId(realm: Realm,
roomId: String): RealmQuery<LiveLocationShareAggregatedSummaryEntity> {
return realm.where<LiveLocationShareAggregatedSummaryEntity>()
.equalTo(TimelineEventEntityFields.ROOM_ID, roomId)
.equalTo(LiveLocationShareAggregatedSummaryEntityFields.ROOM_ID, roomId)
}
internal fun LiveLocationShareAggregatedSummaryEntity.Companion.create(
@ -71,7 +70,22 @@ internal fun LiveLocationShareAggregatedSummaryEntity.Companion.get(
return LiveLocationShareAggregatedSummaryEntity.where(realm, roomId, eventId).findFirst()
}
internal fun LiveLocationShareAggregatedSummaryEntity.Companion.findRunningLiveLocationShareInRoom(
internal fun LiveLocationShareAggregatedSummaryEntity.Companion.findActiveLiveInRoomForUser(
realm: Realm,
roomId: String,
userId: String,
): List<LiveLocationShareAggregatedSummaryEntity> {
return LiveLocationShareAggregatedSummaryEntity
.whereRoomId(realm, roomId = roomId)
.equalTo(LiveLocationShareAggregatedSummaryEntityFields.USER_ID, userId)
.equalTo(LiveLocationShareAggregatedSummaryEntityFields.IS_ACTIVE, true)
.findAll()
}
/**
* A live is considered as running when active and with at least a last known location.
*/
internal fun LiveLocationShareAggregatedSummaryEntity.Companion.findRunningLiveInRoom(
realm: Realm,
roomId: String,
): RealmQuery<LiveLocationShareAggregatedSummaryEntity> {

View File

@ -26,6 +26,7 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconInfoCo
import org.matrix.android.sdk.api.session.room.model.message.MessageBeaconLocationDataContent
import org.matrix.android.sdk.internal.database.mapper.ContentMapper
import org.matrix.android.sdk.internal.database.model.livelocation.LiveLocationShareAggregatedSummaryEntity
import org.matrix.android.sdk.internal.database.query.findActiveLiveInRoomForUser
import org.matrix.android.sdk.internal.database.query.getOrCreate
import org.matrix.android.sdk.internal.di.SessionId
import org.matrix.android.sdk.internal.di.WorkManagerProvider
@ -72,6 +73,8 @@ internal class LiveLocationAggregationProcessor @Inject constructor(
aggregatedSummary.isActive = isLive
aggregatedSummary.userId = event.senderId
deactivateAllPreviousBeacons(realm, roomId, event.senderId, targetEventId)
if (isLive) {
scheduleDeactivationAfterTimeout(targetEventId, roomId, endOfLiveTimestampMillis)
} else {
@ -138,5 +141,16 @@ internal class LiveLocationAggregationProcessor @Inject constructor(
}
}
private fun deactivateAllPreviousBeacons(realm: Realm, roomId: String, userId: String, currentEventId: String) {
LiveLocationShareAggregatedSummaryEntity
.findActiveLiveInRoomForUser(
realm = realm,
roomId = roomId,
userId = userId
)
.filterNot { it.eventId == currentEventId }
.forEach { it.isActive = false }
}
private fun Long.isMoreRecentThan(timestamp: Long) = this > timestamp
}