From 6ebf42a54cc1ef44bdd762ad06e9b1a84bd7ad25 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 18 Apr 2020 16:46:49 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=9A=A7=20Load=20ALL=20identities=20instead?= =?utf8?q?=20of=20only=20trusted=20ones?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/freenet/wot/IdentityLoader.kt | 21 +++++++++++++++++ .../sone/freenet/wot/IdentityManagerImpl.kt | 2 +- .../sone/freenet/wot/PluginWebOfTrustConnector.kt | 26 +++++++++++++++------- .../sone/freenet/wot/WebOfTrustConnector.kt | 10 +++++++++ .../sone/freenet/wot/IdentityLoaderTest.kt | 1 + 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoader.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoader.kt index c039aaa..f6e1d59 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoader.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoader.kt @@ -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) = ownIdentities @@ -53,6 +58,22 @@ class IdentityLoader @Inject constructor(private val webOfTrustConnector: WebOfT } } + private fun loadAllIdentitiesForOwnIdentities(ownIdentities: Collection) = + 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 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 36d31fb..c8c758d 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt @@ -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 } diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt index 2fad1d1..d829734 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt @@ -17,15 +17,17 @@ 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 = + 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()) diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.kt index e31dce6..a407e2a 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.kt @@ -29,6 +29,16 @@ interface WebOfTrustConnector { fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String? = null): Set /** + * 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 + + /** * Adds the given context to the given identity. * * @param ownIdentity The identity to add the context to diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt index 8827d77..0e0e336 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt @@ -106,6 +106,7 @@ private open class TestWebOfTrustConnector : WebOfTrustConnector { override fun loadAllOwnIdentities() = emptySet() override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?) = emptySet() + override fun loadAllIdentities(ownIdentity: OwnIdentity, context: String?) = emptySet() 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 -- 2.7.4