🚧 Use strictly-filtered identities
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / freenet / wot / IdentityManagerImpl.kt
index 5f087c7..ae5dd1e 100644 (file)
@@ -56,23 +56,34 @@ class IdentityManagerImpl @Inject constructor(
 
                while (!shouldStop()) {
                        try {
-                               val currentIdentities = identityLoader.loadIdentities()
-
-                               val onlyTrustedByAll = currentIdentities.mapValues { (ownIdentity, trustedIdentities) ->
+                               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 ->
-                                               currentIdentities.all { trustedIdentity in it.value }
+                                               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)
@@ -96,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
+               }