♻️ Use new filter class instead of filtering inline
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / freenet / wot / IdentityManagerImpl.kt
index b7a48dd..eac1fa9 100644 (file)
@@ -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
 
 package net.pterodactylus.sone.freenet.wot
 
-import com.google.common.eventbus.*
-import com.google.inject.*
-import net.pterodactylus.util.service.*
-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
@@ -41,6 +48,8 @@ class IdentityManagerImpl @Inject constructor(
 ) : AbstractService("Sone Identity Manager", false), IdentityManager {
 
        private val currentOwnIdentities = mutableSetOf<OwnIdentity>()
+       private val strictFiltering = AtomicBoolean(false)
+       private val noNegativeIdentityFilter = NoNegativeIdentityFilter()
 
        override val isConnected: Boolean
                get() = notThrowing { webOfTrustConnector.ping() }
@@ -55,7 +64,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)
@@ -68,13 +77,32 @@ class IdentityManagerImpl @Inject constructor(
                                }
                        } 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((60 * 1000).toLong())
+                       sleep(SECONDS.toMillis(60))
                }
        }
 
+       private fun Map<OwnIdentity, Set<Identity>>.applyStrictFiltering() =
+                       if (strictFiltering.get()) {
+                               noNegativeIdentityFilter.filter(this)
+                       } 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)