X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FIdentityManagerImpl.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FIdentityManagerImpl.kt;h=829affc5b35ccbd86942094b24af28b8ff3c7655;hp=67e70e08814570b0d1b900db45908ed40dc6c55b;hb=5c5bee980f9cab5792e34d1c9840f73b8b191830;hpb=faf66247a34f64946990a985d2ea3003465969cb diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt index 67e70e0..829affc 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt @@ -17,12 +17,18 @@ package net.pterodactylus.sone.freenet.wot -import com.google.common.eventbus.* -import com.google.inject.* -import net.pterodactylus.util.service.* -import java.util.concurrent.TimeUnit.* -import java.util.logging.* -import java.util.logging.Logger.* +import com.google.common.eventbus.EventBus +import com.google.common.eventbus.Subscribe +import com.google.inject.Inject +import com.google.inject.Singleton +import net.pterodactylus.sone.core.event.StrictFilteringActivatedEvent +import net.pterodactylus.sone.core.event.StrictFilteringDeactivatedEvent +import net.pterodactylus.util.service.AbstractService +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean +import java.util.logging.Level +import java.util.logging.Logger +import java.util.logging.Logger.getLogger /** * The identity manager takes care of loading and storing identities, their @@ -42,6 +48,7 @@ class IdentityManagerImpl @Inject constructor( ) : AbstractService("Sone Identity Manager", false), IdentityManager { private val currentOwnIdentities = mutableSetOf() + private val strictFiltering = AtomicBoolean(false) override val isConnected: Boolean get() = notThrowing { webOfTrustConnector.ping() } @@ -56,7 +63,7 @@ class IdentityManagerImpl @Inject constructor( while (!shouldStop()) { try { - val currentIdentities = identityLoader.loadIdentities() + val currentIdentities = identityLoader.loadAllIdentities().applyStrictFiltering() val identityChangeEventSender = IdentityChangeEventSender(eventBus, oldIdentities) identityChangeEventSender.detectChanges(currentIdentities) @@ -78,6 +85,38 @@ class IdentityManagerImpl @Inject constructor( } } + private fun Map>.applyStrictFiltering() = + if (strictFiltering.get()) { + val identitiesWithTrust = values.flatten() + .groupBy { it.id } + .mapValues { (_, identities) -> + identities.reduce { accIdentity, identity -> + identity.trust.forEach { (ownIdentity: OwnIdentity?, trust: Trust?) -> + accIdentity.setTrust(ownIdentity, trust) + } + accIdentity + } + } + + mapValues { (_, trustedIdentities) -> + trustedIdentities.filter { trustedIdentity -> + identitiesWithTrust[trustedIdentity.id]!!.trust.all { it.value.hasZeroOrPositiveTrust() } + } + } + } else { + this + } + + @Subscribe + fun strictFilteringActivated(event: StrictFilteringActivatedEvent) { + strictFiltering.set(true) + } + + @Subscribe + fun strictFilteringDeactivated(event: StrictFilteringDeactivatedEvent) { + strictFiltering.set(false) + } + } private val logger: Logger = getLogger(IdentityManagerImpl::class.java.name) @@ -89,3 +128,10 @@ private fun notThrowing(action: () -> Unit): Boolean = } catch (e: Exception) { false } + +private fun Trust.hasZeroOrPositiveTrust() = + if (explicit == null) { + implicit == null || implicit >= 0 + } else { + explicit >= 0 + }