Adds two additional helper methods to ATags and Classifieds.

This commit is contained in:
Vitor Pamplona
2024-03-15 19:29:27 -04:00
parent c74176684f
commit 2d17200f03
2 changed files with 29 additions and 0 deletions
@@ -69,6 +69,15 @@ data class ATag(val kind: Int, val pubKeyHex: String, val dTag: String, val rela
}
}
fun parseAtagUnckecked(atag: String): ATag? {
return try {
val parts = atag.split(":")
ATag(parts[0].toInt(), parts[1], parts[2], null)
} catch (t: Throwable) {
null
}
}
fun parseNAddr(naddr: String): ATag? {
try {
val key = naddr.removePrefix("nostr:")
@@ -52,6 +52,26 @@ class ClassifiedsEvent(
fun location() = tags.firstOrNull { it.size > 1 && it[0] == "location" }?.get(1)
fun isWellFormed(): Boolean {
var hasImage = false
var hasTitle = false
var hasPrice = false
tags.forEach {
if (it.size > 1) {
if (it[0] == "image") {
hasImage = true
} else if (it[0] == "title") {
hasTitle = true
} else if (it[0] == "price") {
hasPrice = true
}
}
}
return hasImage && hasPrice && hasTitle
}
fun publishedAt() =
try {
tags.firstOrNull { it.size > 1 && it[0] == "published_at" }?.get(1)?.toLongOrNull()