X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FIdentityManagerImpl.kt;h=ae5dd1e9bc20a6ffb1d9c7e1496bc0149c7b7b0b;hp=cb7d0bdb055f79bf268728953f8b3861db0b17f8;hb=884606571eff6e4f80af9485695848bfb5e7fa95;hpb=b70917b1b2384646c4c7b6e202bbfc4de1769124 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 cb7d0bd..ae5dd1e 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt @@ -1,5 +1,5 @@ /* - * Sone - IdentityManagerImpl.java - Copyright © 2010–2019 David Roden + * Sone - IdentityManagerImpl.kt - Copyright © 2010–2020 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -56,16 +56,34 @@ class IdentityManagerImpl @Inject constructor( while (!shouldStop()) { try { - val currentIdentities = identityLoader.loadIdentities() + val currentIdentities = identityLoader.loadAllIdentities() + + val identitiesWithTrust = currentIdentities.values.flatten() + .groupBy { it.id } + .mapValues { (_, identities) -> + identities.reduce { accIdentity, identity -> + identity.trust.forEach { (ownIdentity: OwnIdentity?, trust: Trust?) -> + accIdentity.setTrust(ownIdentity, trust) + } + accIdentity + } + } + + val onlyTrustedByAll = currentIdentities.mapValues { (_, trustedIdentities) -> + trustedIdentities.filter { trustedIdentity -> + identitiesWithTrust[trustedIdentity.id]!!.trust.all { it.value.hasZeroOrPositiveTrust() } + } + } + logger.log(Level.FINE, "Reduced (${currentIdentities.size},(${currentIdentities.values.joinToString { it.size.toString() }})) identities to (${onlyTrustedByAll.size},(${onlyTrustedByAll.values.joinToString { it.size.toString() }})).") val identityChangeEventSender = IdentityChangeEventSender(eventBus, oldIdentities) - identityChangeEventSender.detectChanges(currentIdentities) + identityChangeEventSender.detectChanges(onlyTrustedByAll) - oldIdentities = currentIdentities + oldIdentities = onlyTrustedByAll synchronized(currentOwnIdentities) { currentOwnIdentities.clear() - currentOwnIdentities.addAll(currentIdentities.keys) + currentOwnIdentities.addAll(onlyTrustedByAll.keys) } } catch (wote1: WebOfTrustException) { logger.log(Level.WARNING, "WoT has disappeared!", wote1) @@ -89,3 +107,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 + }