X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FIdentityLoader.kt;h=736b4522457344a4da001718d70e9808d587ff67;hb=58bb46435ed76f41df7b2ffd9a74fe5a4c1762d3;hp=fe5be5c5f91caab017260101be3af562b14cccc2;hpb=d0de7459fa121c7e3048109df4b9c06f3eaef21a;p=Sone.git 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 fe5be5c..736b452 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoader.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityLoader.kt @@ -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,25 +30,27 @@ 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) - - @Throws(PluginException::class) - private fun loadTrustedIdentitiesForOwnIdentities(ownIdentities: Collection) = - ownIdentities.associateWith { ownIdentity -> - logger.fine { "Getting trusted identities for ${ownIdentities.size} own identities..."} - if (ownIdentity.doesNotHaveCorrectContext()) { - logger.fine { "Skipping $ownIdentity because of incorrect context."} - emptySet() - } else { - time({ stopwatch, identities -> "Loaded ${identities.size} identities for ${ownIdentity.nickname} in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) { - webOfTrustConnector.loadTrustedIdentities(ownIdentity, context?.context) + }.let(this::loadAllIdentitiesForOwnIdentities) + .mergeRemoteIdentities() + + 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 @@ -58,4 +60,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() } + } + }