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 9403f61..12e0e35 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 21a4ccc..6434f54 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 28fe1b8..8bb8a32 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 8c3da42..d0a1325 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