fix(Watchtower): Do not include hidden accounts in the Global alerts counter

This commit is contained in:
Artem Chepurnyi 2024-09-28 12:21:48 +03:00
parent 1c973be83f
commit 390c999f65
4 changed files with 29 additions and 8 deletions

View File

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

View File

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

View File

@ -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,14 +14,26 @@ 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<Int> = sharedFlow

View File

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