/*
- * 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
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 ->
} 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)
}
}
}
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() }
+ }
+
}