From 390c999f65d6e494ed5510fafe1e714c1d5d4dd7 Mon Sep 17 00:00:00 2001 From: Artem Chepurnyi Date: Sat, 28 Sep 2024 12:21:48 +0300 Subject: [PATCH] fix(Watchtower): Do not include hidden accounts in the Global alerts counter --- .../keyguard/common/model/DWatchtowerAlert.kt | 5 ++++- .../usecase/GetWatchtowerAlertsImpl.kt | 3 +++ .../usecase/GetWatchtowerUnreadCountImpl.kt | 22 +++++++++++++++---- .../keyguard/data/WatchtowerThreat.sq | 7 +++--- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/common/model/DWatchtowerAlert.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/common/model/DWatchtowerAlert.kt index 9403f618..12e0e354 100644 --- a/common/src/commonMain/kotlin/com/artemchep/keyguard/common/model/DWatchtowerAlert.kt +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/common/model/DWatchtowerAlert.kt @@ -5,10 +5,13 @@ import kotlinx.datetime.Instant data class DWatchtowerAlert( val alertId: String, val cipherId: CipherId, + val accountId: AccountId, val type: DWatchtowerAlertType, val reportedAt: Instant, val read: Boolean, val version: String, -) : HasCipherId { +) : HasCipherId, HasAccountId { override fun cipherId(): String = cipherId.id + + override fun accountId(): String = accountId.id } diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/usecase/GetWatchtowerAlertsImpl.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/usecase/GetWatchtowerAlertsImpl.kt index 21a4ccc6..6434f542 100644 --- a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/usecase/GetWatchtowerAlertsImpl.kt +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/usecase/GetWatchtowerAlertsImpl.kt @@ -3,6 +3,7 @@ package com.artemchep.keyguard.provider.bitwarden.usecase import app.cash.sqldelight.coroutines.asFlow import app.cash.sqldelight.coroutines.mapToList import com.artemchep.keyguard.common.io.effectMap +import com.artemchep.keyguard.common.model.AccountId import com.artemchep.keyguard.common.model.CipherId import com.artemchep.keyguard.common.model.DWatchtowerAlert import com.artemchep.keyguard.common.model.DWatchtowerAlertType @@ -40,10 +41,12 @@ class GetWatchtowerAlertsImpl( .mapNotNull { item -> val type = DWatchtowerAlertType.of(item.type) ?: return@mapNotNull null + val accountId = AccountId(item.accountId!!) val cipherId = CipherId(item.cipherId) DWatchtowerAlert( alertId = item.cipherId + "|" + item.type, cipherId = cipherId, + accountId = accountId, type = type, reportedAt = item.reportedAt, read = item.read, diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/usecase/GetWatchtowerUnreadCountImpl.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/usecase/GetWatchtowerUnreadCountImpl.kt index 28fe1b8b..8bb8a325 100644 --- a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/usecase/GetWatchtowerUnreadCountImpl.kt +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/usecase/GetWatchtowerUnreadCountImpl.kt @@ -1,8 +1,10 @@ package com.artemchep.keyguard.provider.bitwarden.usecase +import com.artemchep.keyguard.common.usecase.GetProfiles import com.artemchep.keyguard.common.usecase.GetWatchtowerUnreadAlerts import com.artemchep.keyguard.common.usecase.GetWatchtowerUnreadCount import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.map import org.kodein.di.DirectDI import org.kodein.di.instance @@ -12,15 +14,27 @@ import org.kodein.di.instance */ class GetWatchtowerUnreadCountImpl( private val getWatchtowerUnreadAlerts: GetWatchtowerUnreadAlerts, + private val getProfiles: GetProfiles, ) : GetWatchtowerUnreadCount { constructor(directDI: DirectDI) : this( getWatchtowerUnreadAlerts = directDI.instance(), + getProfiles = directDI.instance(), ) - private val sharedFlow = getWatchtowerUnreadAlerts() - .map { list -> - list.size - } + private val sharedFlow = combine( + getProfiles() + .map { profiles -> + profiles + .associateBy { it.accountId } + }, + getWatchtowerUnreadAlerts(), + ) { profiles, unreadAlerts -> + unreadAlerts + .count { alert -> + val profile = profiles[alert.accountId.id] + profile?.hidden == false + } + } override fun invoke(): Flow = sharedFlow diff --git a/common/src/commonMain/sqldelight/com/artemchep/keyguard/data/WatchtowerThreat.sq b/common/src/commonMain/sqldelight/com/artemchep/keyguard/data/WatchtowerThreat.sq index 8c3da428..d0a1325c 100644 --- a/common/src/commonMain/sqldelight/com/artemchep/keyguard/data/WatchtowerThreat.sq +++ b/common/src/commonMain/sqldelight/com/artemchep/keyguard/data/WatchtowerThreat.sq @@ -59,11 +59,12 @@ markAsRead { } getThreats: -SELECT * +SELECT watchtowerThreat.*, cipher.accountId AS accountId FROM watchtowerThreat +LEFT JOIN cipher ON cipher.cipherId = watchtowerThreat.cipherId WHERE - threat > 0 -ORDER BY reportedAt DESC; + watchtowerThreat.threat > 0 +ORDER BY watchtowerThreat.reportedAt DESC; getPendingCipherIds: SELECT DISTINCT