Slight performance improvements on tag search.

This commit is contained in:
Vitor Pamplona
2023-04-19 10:33:52 -04:00
parent 4e09d9e54a
commit b55f999efb
4 changed files with 7 additions and 7 deletions
@@ -21,7 +21,7 @@ class PrivateDmEvent(
* nip-04 EncryptedDmEvent but may omit the recipient, too. This value can be queried and used * nip-04 EncryptedDmEvent but may omit the recipient, too. This value can be queried and used
* for initial messages. * for initial messages.
*/ */
fun recipientPubKey() = tags.firstOrNull { it.firstOrNull() == "p" }?.run { Hex.decode(this[1]).toHexKey() } // makes sure its a valid one fun recipientPubKey() = tags.firstOrNull { it.size > 1 && it[0] == "p" }?.run { Hex.decode(this[1]).toHexKey() } // makes sure its a valid one
/** /**
* To be fully compatible with nip-04, we read e-tags that are in violation to nip-18. * To be fully compatible with nip-04, we read e-tags that are in violation to nip-18.
@@ -29,7 +29,7 @@ class PrivateDmEvent(
* Nip-18 messages should refer to other events by inline references in the content like * Nip-18 messages should refer to other events by inline references in the content like
* `[](e/c06f795e1234a9a1aecc731d768d4f3ca73e80031734767067c82d67ce82e506). * `[](e/c06f795e1234a9a1aecc731d768d4f3ca73e80031734767067c82d67ce82e506).
*/ */
fun replyTo() = tags.firstOrNull { it.firstOrNull() == "e" }?.getOrNull(1) fun replyTo() = tags.firstOrNull { it.size > 1 && it[0] == "e" }?.get(1)
fun plainContent(privKey: ByteArray, pubKey: ByteArray): String? { fun plainContent(privKey: ByteArray, pubKey: ByteArray): String? {
return try { return try {
@@ -14,8 +14,8 @@ class ReactionEvent(
sig: HexKey sig: HexKey
) : Event(id, pubKey, createdAt, kind, tags, content, sig) { ) : Event(id, pubKey, createdAt, kind, tags, content, sig) {
fun originalPost() = tags.filter { it.firstOrNull() == "e" }.mapNotNull { it.getOrNull(1) } fun originalPost() = tags.filter { it.size > 1 && it[0] == "e" }.map { it[1] }
fun originalAuthor() = tags.filter { it.firstOrNull() == "p" }.mapNotNull { it.getOrNull(1) } fun originalAuthor() = tags.filter { it.size > 1 && it[0] == "p" }.map { it[1] }
companion object { companion object {
const val kind = 7 const val kind = 7
@@ -30,7 +30,7 @@ class ReportEvent(
} }
fun reportedPost() = tags fun reportedPost() = tags
.filter { it.firstOrNull() == "e" && it.getOrNull(1) != null } .filter { it.size > 1 && it[0] == "e" }
.map { .map {
ReportedKey( ReportedKey(
it[1], it[1],
@@ -39,7 +39,7 @@ class ReportEvent(
} }
fun reportedAuthor() = tags fun reportedAuthor() = tags
.filter { it.firstOrNull() == "p" && it.getOrNull(1) != null } .filter { it.size > 1 && it[0] == "p" }
.map { .map {
ReportedKey( ReportedKey(
it[1], it[1],
@@ -72,7 +72,7 @@ open class NewPostViewModel : ViewModel() {
this.mentions = currentMentions.plus(replyUser) this.mentions = currentMentions.plus(replyUser)
} }
} }
} ?: { } ?: run {
replyTos = null replyTos = null
mentions = null mentions = null
} }