From 6c9bd8dcb139302e29d9eb9c8e509589f31fcb58 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 5 May 2020 22:39:07 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Merge=20loaded=20identites=20to?= =?utf8?q?=20have=20all=20trust=20values?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/freenet/wot/IdentityLoader.kt | 15 ++++++++ .../sone/freenet/wot/IdentityLoaderTest.kt | 45 +++++++++++++++++++--- 2 files changed, 55 insertions(+), 5 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 f6e1d59..bc7dc4c 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoader.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoader.kt @@ -35,11 +35,13 @@ class IdentityLoader @Inject constructor(private val webOfTrustConnector: WebOfT time({ stopwatch, identities -> "Loaded ${identities.size} own identities in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) { webOfTrustConnector.loadAllOwnIdentities() }.let(this::loadTrustedIdentitiesForOwnIdentities) + .mergeRemoteIdentities() fun loadAllIdentities() = time({ stopwatch, identities -> "Loaded ${identities.size} own identities in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) { webOfTrustConnector.loadAllOwnIdentities() }.let(this::loadAllIdentitiesForOwnIdentities) + .mergeRemoteIdentities() @Throws(PluginException::class) private fun loadTrustedIdentitiesForOwnIdentities(ownIdentities: Collection) = @@ -82,4 +84,17 @@ class IdentityLoader @Inject constructor(private val webOfTrustConnector: WebOfT loader().also { logger.fine(logMessage(stopwatch, it)) } } + private fun Map>.mergeRemoteIdentities() = + values.flatten() + .groupBy { it.id } + .mapValues { + it.value.reduce { accIdentity, identity -> + identity.trust.forEach { (ownIdentity, trust) -> accIdentity.setTrust(ownIdentity, trust) } + accIdentity + } + } + .let { reducedIdentities -> + mapValues { it.value.map { identity -> reducedIdentities[identity.id]!! }.toSet() } + } + } 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 0e0e336..0426814 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt @@ -17,9 +17,12 @@ package net.pterodactylus.sone.freenet.wot -import org.hamcrest.MatcherAssert.* -import org.hamcrest.Matchers.* -import org.junit.* +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.containsInAnyOrder +import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.hasSize +import org.hamcrest.Matchers.notNullValue +import org.junit.Test /** * Unit test for [IdentityLoader]. @@ -67,6 +70,38 @@ class IdentityLoaderTest { assertThat(identities[ownIdentity], equalTo>(trustedIdentities)) } + @Test + fun `loading all identities merges remote identities’ trust values`() { + val ownIdentity1 = createOwnIdentity("o1") + val ownIdentity2 = createOwnIdentity("o2") + val webOfTrustConnector = object : TestWebOfTrustConnector() { + override fun loadAllOwnIdentities() = setOf(ownIdentity1, ownIdentity2) + override fun loadAllIdentities(ownIdentity: OwnIdentity, context: String?) = when (ownIdentity) { + ownIdentity1 -> setOf(createIdentity().setTrust(ownIdentity1, Trust(100, 50, 2))) + else -> setOf(createIdentity().setTrust(ownIdentity2, Trust(80, 40, 2))) + } + } + val identityLoader = IdentityLoader(webOfTrustConnector) + val allIdentities = identityLoader.loadAllIdentities() + assertThat(allIdentities[ownIdentity1]!!.first().trust[ownIdentity2], notNullValue()) + } + + @Test + fun `loading trusted identities merges remote identities’ trust values`() { + val ownIdentity1 = createOwnIdentity("o1") + val ownIdentity2 = createOwnIdentity("o2") + val webOfTrustConnector = object : TestWebOfTrustConnector() { + override fun loadAllOwnIdentities() = setOf(ownIdentity1, ownIdentity2) + override fun loadTrustedIdentities(ownIdentity: OwnIdentity, context: String?) = when (ownIdentity) { + ownIdentity1 -> setOf(createIdentity().setTrust(ownIdentity1, Trust(100, 50, 2))) + else -> setOf(createIdentity().setTrust(ownIdentity2, Trust(80, 40, 2))) + } + } + val identityLoader = IdentityLoader(webOfTrustConnector) + val allIdentities = identityLoader.loadTrustedIdentities() + assertThat(allIdentities[ownIdentity1]!!.first().trust[ownIdentity2], notNullValue()) + } + } private fun createOwnIdentities() = listOf( @@ -90,13 +125,13 @@ private fun createTrustedIdentitiesForThirdOwnIdentity() = setOf( private fun createTrustedIdentitiesForFourthOwnIdentity(): Set = emptySet() -private fun createOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: Set, properties: Map): OwnIdentity = +private fun createOwnIdentity(id: String = "", nickname: String = "", requestUri: String = "", insertUri: String = "", contexts: Set = setOf(), properties: Map = mapOf()): OwnIdentity = DefaultOwnIdentity(id, nickname, requestUri, insertUri).apply { setContexts(contexts) this.properties = properties } -private fun createIdentity(id: String, nickname: String, requestUri: String, contexts: Set, properties: Map): Identity = +private fun createIdentity(id: String = "", nickname: String = "", requestUri: String = "", contexts: Set = setOf(), properties: Map = mapOf()): Identity = DefaultIdentity(id, nickname, requestUri).apply { setContexts(contexts) this.properties = properties -- 2.7.4