Fix SQWOTFormatter

This commit is contained in:
Paul Schaub 2023-07-17 18:07:30 +02:00
parent dd1bbcd47f
commit edca8574af
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 42 additions and 19 deletions

View File

@ -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 <vanitasvitae@fsfe.org>: 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)"
}
}
}

View File

@ -35,10 +35,10 @@ class SQWOTFormatterTest {
null,
true,
120,
Depth.auto(10),
Depth.auto(0),
RegexSet.wildcard())
)),
Depth.auto(9)),
Depth.auto(0)),
120)
}
)