🔥 Remove unused method from identity loader
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / freenet / wot / IdentityLoader.kt
index 474ab57..736b452 100644 (file)
@@ -30,14 +30,13 @@ class IdentityLoader @Inject constructor(private val webOfTrustConnector: WebOfT
 
        private val logger: Logger = Logger.getLogger(IdentityLoader::class.java.name)
 
-       @Throws(WebOfTrustException::class)
-       fun loadIdentities() =
+       fun loadAllIdentities() =
                        time({ stopwatch, identities -> "Loaded ${identities.size} own identities in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) {
                                webOfTrustConnector.loadAllOwnIdentities()
-                       }.let(this::loadTrustedIdentitiesForOwnIdentities)
+                       }.let(this::loadAllIdentitiesForOwnIdentities)
+                                       .mergeRemoteIdentities()
 
-       @Throws(PluginException::class)
-       private fun loadTrustedIdentitiesForOwnIdentities(ownIdentities: Collection<OwnIdentity>) =
+       private fun loadAllIdentitiesForOwnIdentities(ownIdentities: Collection<OwnIdentity>) =
                        ownIdentities
                                        .also { logger.fine { "Getting trusted identities for ${it.size} own identities..." } }
                                        .associateWith { ownIdentity ->
@@ -48,7 +47,7 @@ class IdentityLoader @Inject constructor(private val webOfTrustConnector: WebOfT
                                                } 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.loadTrustedIdentities(ownIdentity, context?.context)
+                                                               webOfTrustConnector.loadAllIdentities(ownIdentity, context?.context)
                                                        }
                                                }
                                        }
@@ -61,4 +60,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() }
+                                       }
+
 }