✨ Apply strict filtering when getting identities
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Apr 2020 22:20:42 +0000 (00:20 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Apr 2020 22:20:42 +0000 (00:20 +0200)
src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt

index ae5dd1e..829affc 100644 (file)
 
 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<OwnIdentity>()
+       private val strictFiltering = AtomicBoolean(false)
 
        override val isConnected: Boolean
                get() = notThrowing { webOfTrustConnector.ping() }
@@ -56,9 +63,31 @@ class IdentityManagerImpl @Inject constructor(
 
                while (!shouldStop()) {
                        try {
-                               val currentIdentities = identityLoader.loadAllIdentities()
+                               val currentIdentities = identityLoader.loadAllIdentities().applyStrictFiltering()
 
-                               val identitiesWithTrust = currentIdentities.values.flatten()
+                               val identityChangeEventSender = IdentityChangeEventSender(eventBus, oldIdentities)
+                               identityChangeEventSender.detectChanges(currentIdentities)
+
+                               oldIdentities = currentIdentities
+
+                               synchronized(currentOwnIdentities) {
+                                       currentOwnIdentities.clear()
+                                       currentOwnIdentities.addAll(currentIdentities.keys)
+                               }
+                       } catch (wote1: WebOfTrustException) {
+                               logger.log(Level.WARNING, "WoT has disappeared!", wote1)
+                       } catch (e: Exception) {
+                               logger.log(Level.SEVERE, "Uncaught exception in IdentityManager thread!", e)
+                       }
+
+                       /* wait a minute before checking again. */
+                       sleep(SECONDS.toMillis(60))
+               }
+       }
+
+       private fun Map<OwnIdentity, Set<Identity>>.applyStrictFiltering() =
+                       if (strictFiltering.get()) {
+                               val identitiesWithTrust = values.flatten()
                                                .groupBy { it.id }
                                                .mapValues { (_, identities) ->
                                                        identities.reduce { accIdentity, identity ->
@@ -69,31 +98,23 @@ class IdentityManagerImpl @Inject constructor(
                                                        }
                                                }
 
-                               val onlyTrustedByAll = currentIdentities.mapValues { (_, trustedIdentities) ->
+                               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(onlyTrustedByAll)
-
-                               oldIdentities = onlyTrustedByAll
-
-                               synchronized(currentOwnIdentities) {
-                                       currentOwnIdentities.clear()
-                                       currentOwnIdentities.addAll(onlyTrustedByAll.keys)
-                               }
-                       } catch (wote1: WebOfTrustException) {
-                               logger.log(Level.WARNING, "WoT has disappeared!", wote1)
-                       } catch (e: Exception) {
-                               logger.log(Level.SEVERE, "Uncaught exception in IdentityManager thread!", e)
+                       } else {
+                               this
                        }
 
-                       /* wait a minute before checking again. */
-                       sleep(SECONDS.toMillis(60))
-               }
+       @Subscribe
+       fun strictFilteringActivated(event: StrictFilteringActivatedEvent) {
+               strictFiltering.set(true)
+       }
+
+       @Subscribe
+       fun strictFilteringDeactivated(event: StrictFilteringDeactivatedEvent) {
+               strictFiltering.set(false)
        }
 
 }