♻️ Use new filter class instead of filtering inline
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 6 Dec 2022 21:42:31 +0000 (22:42 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 6 Dec 2022 21:42:31 +0000 (22:42 +0100)
src/main/kotlin/net/pterodactylus/sone/freenet/wot/IdentityManagerImpl.kt

index 829affc..eac1fa9 100644 (file)
@@ -49,6 +49,7 @@ class IdentityManagerImpl @Inject constructor(
 
        private val currentOwnIdentities = mutableSetOf<OwnIdentity>()
        private val strictFiltering = AtomicBoolean(false)
+       private val noNegativeIdentityFilter = NoNegativeIdentityFilter()
 
        override val isConnected: Boolean
                get() = notThrowing { webOfTrustConnector.ping() }
@@ -87,22 +88,7 @@ class IdentityManagerImpl @Inject constructor(
 
        private fun Map<OwnIdentity, Set<Identity>>.applyStrictFiltering() =
                        if (strictFiltering.get()) {
-                               val identitiesWithTrust = values.flatten()
-                                               .groupBy { it.id }
-                                               .mapValues { (_, identities) ->
-                                                       identities.reduce { accIdentity, identity ->
-                                                               identity.trust.forEach { (ownIdentity: OwnIdentity?, trust: Trust?) ->
-                                                                       accIdentity.setTrust(ownIdentity, trust)
-                                                               }
-                                                               accIdentity
-                                                       }
-                                               }
-
-                               mapValues { (_, trustedIdentities) ->
-                                       trustedIdentities.filter { trustedIdentity ->
-                                               identitiesWithTrust[trustedIdentity.id]!!.trust.all { it.value.hasZeroOrPositiveTrust() }
-                                       }
-                               }
+                               noNegativeIdentityFilter.filter(this)
                        } else {
                                this
                        }
@@ -128,10 +114,3 @@ private fun notThrowing(action: () -> Unit): Boolean =
                } catch (e: Exception) {
                        false
                }
-
-private fun Trust.hasZeroOrPositiveTrust() =
-               if (explicit == null) {
-                       implicit == null || implicit >= 0
-               } else {
-                       explicit >= 0
-               }