Create LnZapEventInterface

This commit is contained in:
Chemaclass
2023-03-05 23:00:19 +01:00
parent b8937594bc
commit f18ea8cf76
3 changed files with 46 additions and 12 deletions

View File

@@ -3,30 +3,38 @@ package com.vitorpamplona.amethyst.service.model
import com.vitorpamplona.amethyst.model.HexKey import com.vitorpamplona.amethyst.model.HexKey
import com.vitorpamplona.amethyst.service.lnurl.LnInvoiceUtil import com.vitorpamplona.amethyst.service.lnurl.LnInvoiceUtil
import com.vitorpamplona.amethyst.service.relays.Client import com.vitorpamplona.amethyst.service.relays.Client
import java.math.BigDecimal
class LnZapEvent ( class LnZapEvent(
id: HexKey, id: HexKey,
pubKey: HexKey, pubKey: HexKey,
createdAt: Long, createdAt: Long,
tags: List<List<String>>, tags: List<List<String>>,
content: String, content: String,
sig: HexKey sig: HexKey
): Event(id, pubKey, createdAt, kind, tags, content, sig) { ): LnZapEventInterface, Event(id, pubKey, createdAt, kind, tags, content, sig) {
fun zappedPost() = tags.filter { it.firstOrNull() == "e" }.mapNotNull { it.getOrNull(1) } override fun zappedPost() = tags
fun zappedAuthor() = tags.filter { it.firstOrNull() == "p" }.mapNotNull { it.getOrNull(1) } .filter { it.firstOrNull() == "e" }
.mapNotNull { it.getOrNull(1) }
fun taggedAddresses() = tags.filter { it.firstOrNull() == "a" }.mapNotNull { it.getOrNull(1) }.mapNotNull { ATag.parse(it) } override fun zappedAuthor() = tags
.filter { it.firstOrNull() == "p" }
.mapNotNull { it.getOrNull(1) }
fun lnInvoice() = tags.filter { it.firstOrNull() == "bolt11" }.mapNotNull { it.getOrNull(1) }.firstOrNull() override fun taggedAddresses(): List<ATag> = tags
fun preimage() = tags.filter { it.firstOrNull() == "preimage" }.mapNotNull { it.getOrNull(1) }.firstOrNull() .filter { it.firstOrNull() == "a" }
.mapNotNull { it.getOrNull(1) }
.mapNotNull { ATag.parse(it) }
fun description() = tags.filter { it.firstOrNull() == "description" }.mapNotNull { it.getOrNull(1) }.firstOrNull() override fun amount(): BigDecimal? {
return lnInvoice()?.let { LnInvoiceUtil.getAmountInSats(it) }
}
// Keeps this as a field because it's a heavier function used everywhere. // Keeps this as a field because it's a heavier function used everywhere.
val amount = lnInvoice()?.let { LnInvoiceUtil.getAmountInSats(it) } val amount = lnInvoice()?.let { LnInvoiceUtil.getAmountInSats(it) }
fun containedPost() = try { override fun containedPost(): Event? = try {
description()?.let { description()?.let {
fromJson(it, Client.lenient) fromJson(it, Client.lenient)
} }
@@ -34,7 +42,17 @@ class LnZapEvent (
null null
} }
private fun lnInvoice(): String? = tags
.filter { it.firstOrNull() == "bolt11" }
.mapNotNull { it.getOrNull(1) }
.firstOrNull()
private fun description(): String? = tags
.filter { it.firstOrNull() == "description" }
.mapNotNull { it.getOrNull(1) }
.firstOrNull()
companion object { companion object {
const val kind = 9735 const val kind = 9735
} }
} }

View File

@@ -0,0 +1,16 @@
package com.vitorpamplona.amethyst.service.model
import java.math.BigDecimal
interface LnZapEventInterface: EventInterface {
fun zappedPost(): List<String>
fun zappedAuthor(): List<String>
fun taggedAddresses(): List<ATag>
fun amount(): BigDecimal?
fun containedPost(): Event?
}

View File

@@ -1,7 +1,7 @@
package com.vitorpamplona.amethyst.service.model.zaps package com.vitorpamplona.amethyst.service.model.zaps
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.service.model.LnZapEvent import com.vitorpamplona.amethyst.service.model.LnZapEventInterface
object UserZaps { object UserZaps {
fun groupByUser(zaps: Map<Note, Note?>?): List<Pair<Note, Note>> { fun groupByUser(zaps: Map<Note, Note?>?): List<Pair<Note, Note>> {
@@ -10,7 +10,7 @@ object UserZaps {
return (zaps return (zaps
.filter { it.value != null } .filter { it.value != null }
.toList() .toList()
.sortedBy { (it.second?.event as? LnZapEvent)?.amount } .sortedBy { (it.second?.event as? LnZapEventInterface)?.amount() }
.reversed()) as List<Pair<Note, Note>> .reversed()) as List<Pair<Note, Note>>
} }
} }