bc7dc4c1330e701a14c3932a24b38d140fc534d6
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / freenet / wot / IdentityLoader.kt
1 /*
2  * Sone - IdentityLoader.kt - Copyright © 2013–2020 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.freenet.wot
19
20 import com.google.common.base.*
21 import com.google.inject.*
22 import net.pterodactylus.sone.freenet.plugin.*
23 import java.util.concurrent.TimeUnit.*
24 import java.util.logging.*
25
26 /**
27  * Loads [OwnIdentity]s and the [Identity]s they trust.
28  */
29 class IdentityLoader @Inject constructor(private val webOfTrustConnector: WebOfTrustConnector, private val context: Context? = null) {
30
31         private val logger: Logger = Logger.getLogger(IdentityLoader::class.java.name)
32
33         @Throws(WebOfTrustException::class)
34         fun loadTrustedIdentities() =
35                         time({ stopwatch, identities -> "Loaded ${identities.size} own identities in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) {
36                                 webOfTrustConnector.loadAllOwnIdentities()
37                         }.let(this::loadTrustedIdentitiesForOwnIdentities)
38                                         .mergeRemoteIdentities()
39
40         fun loadAllIdentities() =
41                         time({ stopwatch, identities -> "Loaded ${identities.size} own identities in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) {
42                                 webOfTrustConnector.loadAllOwnIdentities()
43                         }.let(this::loadAllIdentitiesForOwnIdentities)
44                                         .mergeRemoteIdentities()
45
46         @Throws(PluginException::class)
47         private fun loadTrustedIdentitiesForOwnIdentities(ownIdentities: Collection<OwnIdentity>) =
48                         ownIdentities
49                                         .also { logger.fine { "Getting trusted identities for ${it.size} own identities..." } }
50                                         .associateWith { ownIdentity ->
51                                                 logger.fine { "Getting trusted identities for $ownIdentity..." }
52                                                 if (ownIdentity.doesNotHaveCorrectContext()) {
53                                                         logger.fine { "Skipping $ownIdentity because of incorrect context." }
54                                                         emptySet()
55                                                 } else {
56                                                         logger.fine { "Loading trusted identities for $ownIdentity from WoT..." }
57                                                         time({ stopwatch, identities -> "Loaded ${identities.size} identities for ${ownIdentity.nickname} in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) {
58                                                                 webOfTrustConnector.loadTrustedIdentities(ownIdentity, context?.context)
59                                                         }
60                                                 }
61                                         }
62
63         private fun loadAllIdentitiesForOwnIdentities(ownIdentities: Collection<OwnIdentity>) =
64                         ownIdentities
65                                         .also { logger.fine { "Getting trusted identities for ${it.size} own identities..." } }
66                                         .associateWith { ownIdentity ->
67                                                 logger.fine { "Getting trusted identities for $ownIdentity..." }
68                                                 if (ownIdentity.doesNotHaveCorrectContext()) {
69                                                         logger.fine { "Skipping $ownIdentity because of incorrect context." }
70                                                         emptySet()
71                                                 } else {
72                                                         logger.fine { "Loading trusted identities for $ownIdentity from WoT..." }
73                                                         time({ stopwatch, identities -> "Loaded ${identities.size} identities for ${ownIdentity.nickname} in ${stopwatch.elapsed(MILLISECONDS) / 1000.0}s." }) {
74                                                                 webOfTrustConnector.loadAllIdentities(ownIdentity, context?.context)
75                                                         }
76                                                 }
77                                         }
78
79         private fun OwnIdentity.doesNotHaveCorrectContext() =
80                         context?.let { it.context !in contexts } ?: false
81
82         private fun <R> time(logMessage: (Stopwatch, R) -> String, loader: () -> R) =
83                         Stopwatch.createStarted().let { stopwatch ->
84                                 loader().also { logger.fine(logMessage(stopwatch, it)) }
85                         }
86
87         private fun Map<OwnIdentity, Set<Identity>>.mergeRemoteIdentities() =
88                         values.flatten()
89                                         .groupBy { it.id }
90                                         .mapValues {
91                                                 it.value.reduce { accIdentity, identity ->
92                                                         identity.trust.forEach { (ownIdentity, trust) -> accIdentity.setTrust(ownIdentity, trust) }
93                                                         accIdentity
94                                                 }
95                                         }
96                                         .let { reducedIdentities ->
97                                                 mapValues { it.value.map { identity -> reducedIdentities[identity.id]!! }.toSet() }
98                                         }
99
100 }