From edca8574afc9ab015c14a18748a64de63813e568 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 17 Jul 2023 18:07:30 +0200 Subject: [PATCH] Fix SQWOTFormatter --- .../wot/cli/format/SQWOTFormatter.kt | 57 +++++++++++++------ .../wot/cli/format/SQWOTFormatterTest.kt | 4 +- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/pgpainless-wot-cli/src/main/kotlin/org/pgpainless/wot/cli/format/SQWOTFormatter.kt b/pgpainless-wot-cli/src/main/kotlin/org/pgpainless/wot/cli/format/SQWOTFormatter.kt index 27de7fb1..cf332906 100644 --- a/pgpainless-wot-cli/src/main/kotlin/org/pgpainless/wot/cli/format/SQWOTFormatter.kt +++ b/pgpainless-wot-cli/src/main/kotlin/org/pgpainless/wot/cli/format/SQWOTFormatter.kt @@ -4,6 +4,7 @@ package org.pgpainless.wot.cli.format +import org.pgpainless.wot.api.AuthenticationLevel import org.pgpainless.wot.api.Binding import org.pgpainless.wot.network.EdgeComponent import java.text.SimpleDateFormat @@ -17,20 +18,22 @@ class SQWOTFormatter: Formatter { */ override fun format(binding: Binding, amountMin: Int, amountReference: Int): String { val percentage = binding.percentage(amountReference) - val authLevel = when(binding.paths.amount) { - in 0..39 -> "not authenticated" - in 40..119 -> "partially authenticated" - in 120 .. 239 -> "fully authenticated" - else -> {if (percentage < 0) "not authenticated" else "doubly authenticated"} - } + val bAmount = binding.paths.amount + val authLevel = if (bAmount >= AuthenticationLevel.Doubly.amount) "doubly" + else if (bAmount >= AuthenticationLevel.Fully.amount) "fully" + else if (bAmount >= AuthenticationLevel.Partially.amount) "partially" + else if (bAmount > 0) "marginally" + else "not" val checkmark = if(binding.paths.amount >= amountMin) "[✓] " else "[ ] " val pathList = binding.paths.paths val singlePath = pathList.size == 1 val indent = " ".repeat(if (singlePath) 2 else 4) return buildString { + // [✓] 7F9116FEA90A5983936C7CFAA027DB2F3E1E118A Paul Schaub : fully authenticated (100%) - append(checkmark); appendLine("${binding.fingerprint} ${binding.userId}: $authLevel (${percentage}%)") + append(checkmark); appendLine("${binding.fingerprint} ${binding.userId}: $authLevel authenticated (${percentage}%)") + for ((pIndex, path) in pathList.withIndex()) { if (!singlePath) { appendLine(" Path #${pIndex + 1} of ${pathList.size}, trust amount ${path.amount}:") @@ -41,11 +44,18 @@ class SQWOTFormatter: Formatter { " \"${path.root.userIds.keys.first()}\"" else " (\"${path.root.userIds.keys.first()}\")" + // ◯ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ("Alice") append(indent); appendLine("◯ ${path.root.fingerprint}$originUserId") + for ((eIndex, edge) in path.certifications.withIndex()) { - val targetUserId = if (edge.userId == null) "" else " \"${edge.userId}\"" - append(indent); appendLine("│ ${certDegree(edge.trustAmount)}the following " + - (if (edge.userId != null) "binding" else "certificate") + + val targetUserId = if (edge.userId == null) + "" + else if (eIndex == path.certifications.lastIndex) + " \"${edge.userId}\"" + else + " (\"${edge.userId}\")" + append(indent); appendLine("│ ${certDegree(edge.trustAmount)} the following " + + (if (eIndex == path.certifications.lastIndex) "binding" else "certificate") + " on ${dateFormat.format(edge.creationTime)}" + (if (edge.expirationTime == null) "" else " (expiry: ${dateFormat.format(edge.expirationTime)})") + introducerType(edge) @@ -69,18 +79,31 @@ class SQWOTFormatter: Formatter { } private fun introducerType(edge: EdgeComponent): String { - return when (edge.trustDepth.value()) { - 0 -> "" - 1 -> " as a ${introducerDegree(edge.trustAmount)} trusted introducer (depth: ${edge.trustDepth.value()})" - else -> " as a ${introducerDegree(edge.trustAmount)} trusted meta-introducer (depth: ${edge.trustDepth.value()})" + if (edge.trustDepth.value() <= 0) { + return "" } + return buildString { + append(" as a ") + if (edge.trustAmount < AuthenticationLevel.Fully.amount) { + append("partially trusted (${edge.trustAmount} of 120) ") + } else { + append("fully trusted ") + } + + if (edge.trustDepth.value() == 1) { + append("introducer (depth: ${edge.trustDepth.value()})") + } else { + append("meta-introducer (depth: ${edge.trustDepth.value()})") + } + } } private fun certDegree(amount: Int): String { - return when (amount) { - in 1 .. 119 -> "partially certified (amount: $amount of 120) " - else -> if (amount <= 0) "did not certify (amount: $amount of 120) " else "certified " + return if (amount >= AuthenticationLevel.Fully.amount) { + "certified" + } else { + "partially certified (amount: $amount of 120)" } } } \ No newline at end of file diff --git a/pgpainless-wot-cli/src/test/kotlin/org/pgpainless/wot/cli/format/SQWOTFormatterTest.kt b/pgpainless-wot-cli/src/test/kotlin/org/pgpainless/wot/cli/format/SQWOTFormatterTest.kt index 1aec5851..20f2be16 100644 --- a/pgpainless-wot-cli/src/test/kotlin/org/pgpainless/wot/cli/format/SQWOTFormatterTest.kt +++ b/pgpainless-wot-cli/src/test/kotlin/org/pgpainless/wot/cli/format/SQWOTFormatterTest.kt @@ -35,10 +35,10 @@ class SQWOTFormatterTest { null, true, 120, - Depth.auto(10), + Depth.auto(0), RegexSet.wildcard()) )), - Depth.auto(9)), + Depth.auto(0)), 120) } )