♻️ Use new filter class instead of filtering inline
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / freenet / wot / IdentityManagerImpl.kt
1 /*
2  * Sone - IdentityManagerImpl.kt - Copyright © 2010–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.eventbus.EventBus
21 import com.google.common.eventbus.Subscribe
22 import com.google.inject.Inject
23 import com.google.inject.Singleton
24 import net.pterodactylus.sone.core.event.StrictFilteringActivatedEvent
25 import net.pterodactylus.sone.core.event.StrictFilteringDeactivatedEvent
26 import net.pterodactylus.util.service.AbstractService
27 import java.util.concurrent.TimeUnit.SECONDS
28 import java.util.concurrent.atomic.AtomicBoolean
29 import java.util.logging.Level
30 import java.util.logging.Logger
31 import java.util.logging.Logger.getLogger
32
33 /**
34  * The identity manager takes care of loading and storing identities, their
35  * contexts, and properties. It does so in a way that does not expose errors via
36  * exceptions but it only logs them and tries to return sensible defaults.
37  *
38  *
39  * It is also responsible for polling identities from the Web of Trust plugin
40  * and sending events to the [EventBus] when [Identity]s and
41  * [OwnIdentity]s are discovered or disappearing.
42  */
43 @Singleton
44 class IdentityManagerImpl @Inject constructor(
45                 private val eventBus: EventBus,
46                 private val webOfTrustConnector: WebOfTrustConnector,
47                 private val identityLoader: IdentityLoader
48 ) : AbstractService("Sone Identity Manager", false), IdentityManager {
49
50         private val currentOwnIdentities = mutableSetOf<OwnIdentity>()
51         private val strictFiltering = AtomicBoolean(false)
52         private val noNegativeIdentityFilter = NoNegativeIdentityFilter()
53
54         override val isConnected: Boolean
55                 get() = notThrowing { webOfTrustConnector.ping() }
56
57         override val allOwnIdentities: Set<OwnIdentity>
58                 get() = synchronized(currentOwnIdentities) {
59                         currentOwnIdentities.toSet()
60                 }
61
62         override fun serviceRun() {
63                 var oldIdentities = mapOf<OwnIdentity, Collection<Identity>>()
64
65                 while (!shouldStop()) {
66                         try {
67                                 val currentIdentities = identityLoader.loadAllIdentities().applyStrictFiltering()
68
69                                 val identityChangeEventSender = IdentityChangeEventSender(eventBus, oldIdentities)
70                                 identityChangeEventSender.detectChanges(currentIdentities)
71
72                                 oldIdentities = currentIdentities
73
74                                 synchronized(currentOwnIdentities) {
75                                         currentOwnIdentities.clear()
76                                         currentOwnIdentities.addAll(currentIdentities.keys)
77                                 }
78                         } catch (wote1: WebOfTrustException) {
79                                 logger.log(Level.WARNING, "WoT has disappeared!", wote1)
80                         } catch (e: Exception) {
81                                 logger.log(Level.SEVERE, "Uncaught exception in IdentityManager thread!", e)
82                         }
83
84                         /* wait a minute before checking again. */
85                         sleep(SECONDS.toMillis(60))
86                 }
87         }
88
89         private fun Map<OwnIdentity, Set<Identity>>.applyStrictFiltering() =
90                         if (strictFiltering.get()) {
91                                 noNegativeIdentityFilter.filter(this)
92                         } else {
93                                 this
94                         }
95
96         @Subscribe
97         fun strictFilteringActivated(event: StrictFilteringActivatedEvent) {
98                 strictFiltering.set(true)
99         }
100
101         @Subscribe
102         fun strictFilteringDeactivated(event: StrictFilteringDeactivatedEvent) {
103                 strictFiltering.set(false)
104         }
105
106 }
107
108 private val logger: Logger = getLogger(IdentityManagerImpl::class.java.name)
109
110 private fun notThrowing(action: () -> Unit): Boolean =
111                 try {
112                         action()
113                         true
114                 } catch (e: Exception) {
115                         false
116                 }