šŸ› Merge loaded identites to have all trust values
authorDavid ā€˜Bombeā€™ Roden <bombe@pterodactylus.net>
Tue, 5 May 2020 20:39:07 +0000 (22:39 +0200)
committerDavid ā€˜Bombeā€™ Roden <bombe@pterodactylus.net>
Tue, 5 May 2020 20:39:07 +0000 (22:39 +0200)
src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoader.kt
src/test/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoaderTest.kt

index f6e1d59..bc7dc4c 100644 (file)
@@ -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<OwnIdentity>) =
@@ -82,4 +84,17 @@ class IdentityLoader @Inject constructor(private val webOfTrustConnector: WebOfT
                                loader().also { logger.fine(logMessage(stopwatch, it)) }
                        }
 
+       private fun Map<OwnIdentity, Set<Identity>>.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() }
+                                       }
+
 }
index 0e0e336..0426814 100644 (file)
 
 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<Collection<Identity>>(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<Identity> = emptySet()
 
-private fun createOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: Set<String>, properties: Map<String, String>): OwnIdentity =
+private fun createOwnIdentity(id: String = "", nickname: String = "", requestUri: String = "", insertUri: String = "", contexts: Set<String> = setOf(), properties: Map<String, String> = mapOf()): OwnIdentity =
                DefaultOwnIdentity(id, nickname, requestUri, insertUri).apply {
                        setContexts(contexts)
                        this.properties = properties
                }
 
-private fun createIdentity(id: String, nickname: String, requestUri: String, contexts: Set<String>, properties: Map<String, String>): Identity =
+private fun createIdentity(id: String = "", nickname: String = "", requestUri: String = "", contexts: Set<String> = setOf(), properties: Map<String, String> = mapOf()): Identity =
                DefaultIdentity(id, nickname, requestUri).apply {
                        setContexts(contexts)
                        this.properties = properties