🚧 Load ALL identities instead of only trusted ones
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Apr 2020 14:46:49 +0000 (16:46 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Apr 2020 14:47:06 +0000 (16:47 +0200)
src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoader.kt
src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt
src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt
src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.kt
src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt

index c039aaa..f6e1d59 100644 (file)
@@ -36,6 +36,11 @@ class IdentityLoader @Inject constructor(private val webOfTrustConnector: WebOfT
                                webOfTrustConnector.loadAllOwnIdentities()
                        }.let(this::loadTrustedIdentitiesForOwnIdentities)
 
+       fun loadAllIdentities() =
+                       time({ stopwatch, identities -> "Loaded ${identities.size} own identities in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) {
+                               webOfTrustConnector.loadAllOwnIdentities()
+                       }.let(this::loadAllIdentitiesForOwnIdentities)
+
        @Throws(PluginException::class)
        private fun loadTrustedIdentitiesForOwnIdentities(ownIdentities: Collection<OwnIdentity>) =
                        ownIdentities
@@ -53,6 +58,22 @@ class IdentityLoader @Inject constructor(private val webOfTrustConnector: WebOfT
                                                }
                                        }
 
+       private fun loadAllIdentitiesForOwnIdentities(ownIdentities: Collection<OwnIdentity>) =
+                       ownIdentities
+                                       .also { logger.fine { "Getting trusted identities for ${it.size} own identities..." } }
+                                       .associateWith { ownIdentity ->
+                                               logger.fine { "Getting trusted identities for $ownIdentity..." }
+                                               if (ownIdentity.doesNotHaveCorrectContext()) {
+                                                       logger.fine { "Skipping $ownIdentity because of incorrect context." }
+                                                       emptySet()
+                                               } else {
+                                                       logger.fine { "Loading trusted identities for $ownIdentity from WoT..." }
+                                                       time({ stopwatch, identities -> "Loaded ${identities.size} identities for ${ownIdentity.nickname} in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) {
+                                                               webOfTrustConnector.loadAllIdentities(ownIdentity, context?.context)
+                                                       }
+                                               }
+                                       }
+
        private fun OwnIdentity.doesNotHaveCorrectContext() =
                        context?.let { it.context !in contexts } ?: false
 
index 36d31fb..c8c758d 100644 (file)
@@ -56,7 +56,7 @@ class IdentityManagerImpl @Inject constructor(
 
                while (!shouldStop()) {
                        try {
-                               val currentIdentities = identityLoader.loadTrustedIdentities()
+                               val currentIdentities = identityLoader.loadAllIdentities()
 
                                val identitiesWithTrust = currentIdentities.values.flatten()
                                                .groupBy { it.id }
index 2fad1d1..d829734 100644 (file)
 
 package net.pterodactylus.sone.freenet.wot
 
-import com.google.inject.*
-import freenet.support.*
-import kotlinx.coroutines.*
-import net.pterodactylus.sone.freenet.*
-import net.pterodactylus.sone.freenet.plugin.*
-import java.lang.String.*
-import java.util.logging.*
+import com.google.inject.Inject
+import freenet.support.SimpleFieldSet
+import kotlinx.coroutines.runBlocking
+import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder
+import net.pterodactylus.sone.freenet.plugin.PluginConnector
+import net.pterodactylus.sone.freenet.plugin.PluginException
+import net.pterodactylus.sone.freenet.plugin.PluginReply
+import java.lang.String.format
+import java.util.logging.Level
 import java.util.logging.Logger
-import java.util.logging.Logger.*
+import java.util.logging.Logger.getLogger
 
 /**
  * Connector for the Web of Trust plugin.
@@ -46,6 +48,14 @@ class PluginWebOfTrustConnector @Inject constructor(private val pluginConnector:
                                        .fields
                                        .parseIdentities { parseTrustedIdentity(it, ownIdentity) }
 
+       override fun loadAllIdentities(ownIdentity: OwnIdentity, context: String?): Set<Identity> =
+                       performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.id).put("Selection", "+").put("Context", context ?: "").put("WantTrustValues", "true").get())
+                                       .fields
+                                       .parseIdentities { parseTrustedIdentity(it, ownIdentity) } +
+                                       performRequest(SimpleFieldSetBuilder().put("Message", "GetIdentitiesByScore").put("Truster", ownIdentity.id).put("Selection", "-").put("Context", context ?: "").put("WantTrustValues", "true").get())
+                                                       .fields
+                                                       .parseIdentities { parseTrustedIdentity(it, ownIdentity) }
+
        @Throws(PluginException::class)
        override fun addContext(ownIdentity: OwnIdentity, context: String) {
                performRequest(SimpleFieldSetBuilder().put("Message", "AddContext").put("Identity", ownIdentity.id).put("Context", context).get())
index e31dce6..a407e2a 100644 (file)
@@ -29,6 +29,16 @@ interface WebOfTrustConnector {
        fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String? = null): Set<Identity>
 
        /**
+        * Loads all identities known to the given own identity that have the (optional) given context.
+        *
+        * @param ownIdentity The own identity
+        * @param context The context to filter, or `null`
+        * @return All trusted identities
+        * @throws PluginException if an error occured talking to the Web of Trust plugin
+        */
+       fun loadAllIdentities(ownIdentity: OwnIdentity, context: String? = null): Set<Identity>
+
+       /**
         * Adds the given context to the given identity.
         *
         * @param ownIdentity The identity to add the context to
index 8827d77..0e0e336 100644 (file)
@@ -106,6 +106,7 @@ private open class TestWebOfTrustConnector : WebOfTrustConnector {
 
        override fun loadAllOwnIdentities() = emptySet<OwnIdentity>()
        override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?) = emptySet<Identity>()
+       override fun loadAllIdentities(ownIdentity: OwnIdentity, context: String?) = emptySet<Identity>()
        override fun addContext(ownIdentity: OwnIdentity, context: String) = Unit
        override fun removeContext(ownIdentity: OwnIdentity, context: String) = Unit
        override fun setProperty(ownIdentity: OwnIdentity, name: String, value: String) = Unit