X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FNoNegativeIdentityFilter.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FNoNegativeIdentityFilter.kt;h=44468f294256a83044f48915ffc33caf51f9020a;hp=0000000000000000000000000000000000000000;hb=5247fb4b48329d1525e7b2537a81c29ddfb102fe;hpb=a5c00f1b023ee87a8d3c2f2ece6b955c87e62afb diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/NoNegativeIdentityFilter.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/NoNegativeIdentityFilter.kt new file mode 100644 index 0000000..44468f2 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/NoNegativeIdentityFilter.kt @@ -0,0 +1,37 @@ +package net.pterodactylus.sone.freenet.wot + +/** + * Filter for identities that retains only those remote identities that do + * not have a single negative trust value (explicit or implicit) from any own + * identity. + */ +class NoNegativeIdentityFilter { + + fun filter(identities: Map>) = + identities.run { + 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() } + } + } + } + +} + +private fun Trust.hasZeroOrPositiveTrust() = + if (explicit == null) { + implicit == null || implicit >= 0 + } else { + explicit >= 0 + }