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=3fa87badd030e15d48aef68f14e75b613b1643da;hpb=dab61bd939edba1be97054a141c6e26bbea6ef5c;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 3fa87ba..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,30 +30,47 @@ class IdentityLoader @Inject constructor(private val webOfTrustConnector: WebOfT private val logger: Logger = Logger.getLogger(IdentityLoader::class.java.name) - @Throws(WebOfTrustException::class) - fun loadIdentities(): Map> = + 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 -> - if (ownIdentity.doesNotHaveCorrectContext()) { - 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 != null && !hasContext(context.context) + context?.let { it.context !in contexts } ?: false - private fun time(logMessage: (Stopwatch, Collection) -> String, loader: () -> Collection): Collection = + private fun time(logMessage: (Stopwatch, R) -> String, loader: () -> R) = Stopwatch.createStarted().let { stopwatch -> 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() } + } + }