🔥 Remove unused method from identity loader
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / freenet / wot / IdentityLoader.kt
index 3a71772..736b452 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - IdentityLoader.java - Copyright Â© 2013–2019 David Roden
+ * Sone - IdentityLoader.kt - Copyright Â© 2013–2020 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -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,12 +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." }) {
-                                                               try {
-                                                                       webOfTrustConnector.loadTrustedIdentities(ownIdentity, context?.context)
-                                                               } catch (e: Exception) {
-                                                                       logger.warning { "Caught $e on loading trusted identities for $ownIdentity."}
-                                                                       throw e
-                                                               }
+                                                               webOfTrustConnector.loadAllIdentities(ownIdentity, context?.context)
                                                        }
                                                }
                                        }
@@ -66,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() }
+                                       }
+
 }