Adds StatusEvent support

This commit is contained in:
Vitor Pamplona
2023-08-24 08:56:51 -04:00
parent 8aad4d8030
commit 36ea33a919
3 changed files with 16 additions and 42 deletions

View File

@@ -120,6 +120,12 @@ open class Event(
return tags.any { it.size > 1 && it[0] == "a" && it[1].startsWith(kindStr) } return tags.any { it.size > 1 && it[0] == "a" && it[1].startsWith(kindStr) }
} }
override fun expiration() = try {
tags.firstOrNull { it.size > 1 && it[0] == "expiration" }?.get(1)?.toLongOrNull()
} catch (_: Exception) {
null
}
override fun getTagOfAddressableKind(kind: Int): ATag? { override fun getTagOfAddressableKind(kind: Int): ATag? {
val kindStr = kind.toString() val kindStr = kind.toString()
val aTag = tags val aTag = tags

View File

@@ -49,6 +49,8 @@ interface EventInterface {
fun isTaggedAddressableKind(kind: Int): Boolean fun isTaggedAddressableKind(kind: Int): Boolean
fun getTagOfAddressableKind(kind: Int): ATag? fun getTagOfAddressableKind(kind: Int): ATag?
fun expiration(): Long?
fun hashtags(): List<String> fun hashtags(): List<String>
fun geohashes(): List<String> fun geohashes(): List<String>

View File

@@ -8,7 +8,7 @@ import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.encoders.HexKey import com.vitorpamplona.quartz.encoders.HexKey
@Immutable @Immutable
class ClassifiedsEvent( class StatusEvent(
id: HexKey, id: HexKey,
pubKey: HexKey, pubKey: HexKey,
createdAt: Long, createdAt: Long,
@@ -16,59 +16,25 @@ class ClassifiedsEvent(
content: String, content: String,
sig: HexKey sig: HexKey
) : BaseAddressableEvent(id, pubKey, createdAt, kind, tags, content, sig) { ) : BaseAddressableEvent(id, pubKey, createdAt, kind, tags, content, sig) {
fun title() = tags.firstOrNull { it.size > 1 && it[0] == "title" }?.get(1)
fun image() = tags.firstOrNull { it.size > 1 && it[0] == "image" }?.get(1)
fun summary() = tags.firstOrNull { it.size > 1 && it[0] == "summary" }?.get(1)
fun price() = tags.firstOrNull { it.size > 1 && it[0] == "price" }?.let {
Price(it[1], it.getOrNull(2), it.getOrNull(3))
}
fun location() = tags.firstOrNull { it.size > 1 && it[0] == "location" }?.get(1)
fun publishedAt() = try {
tags.firstOrNull { it.size > 1 && it[0] == "published_at" }?.get(1)?.toLongOrNull()
} catch (_: Exception) {
null
}
companion object { companion object {
const val kind = 30402 const val kind = 30315
fun create( fun create(
dTag: String, type: String,
title: String?, expiration: Long?,
image: String?,
summary: String?,
price: Price?,
location: String?,
publishedAt: Long?,
privateKey: ByteArray, privateKey: ByteArray,
createdAt: Long = TimeUtils.now() createdAt: Long = TimeUtils.now()
): ClassifiedsEvent { ): StatusEvent {
val tags = mutableListOf<List<String>>() val tags = mutableListOf<List<String>>()
tags.add(listOf("d", dTag)) tags.add(listOf("d", type))
title?.let { tags.add(listOf("title", it)) } expiration?.let { tags.add(listOf("publishedAt", it.toString())) }
image?.let { tags.add(listOf("image", it)) }
summary?.let { tags.add(listOf("summary", it)) }
price?.let {
if (it.frequency != null && it.currency != null) {
tags.add(listOf("price", it.amount, it.currency, it.frequency))
} else if (it.currency != null) {
tags.add(listOf("price", it.amount, it.currency))
} else {
tags.add(listOf("price", it.amount))
}
}
location?.let { tags.add(listOf("location", it)) }
publishedAt?.let { tags.add(listOf("publishedAt", it.toString())) }
title?.let { tags.add(listOf("title", it)) }
val pubKey = CryptoUtils.pubkeyCreate(privateKey).toHexKey() val pubKey = CryptoUtils.pubkeyCreate(privateKey).toHexKey()
val id = generateId(pubKey, createdAt, kind, tags, "") val id = generateId(pubKey, createdAt, kind, tags, "")
val sig = CryptoUtils.sign(id, privateKey) val sig = CryptoUtils.sign(id, privateKey)
return ClassifiedsEvent(id.toHexKey(), pubKey, createdAt, tags, "", sig.toHexKey()) return StatusEvent(id.toHexKey(), pubKey, createdAt, tags, "", sig.toHexKey())
} }
} }
} }
data class Price(val amount: String, val currency: String?, val frequency: String?)