From cb538bc6a33d924eb4343845bca56857aaa95091 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 29 Nov 2024 14:31:31 -0500 Subject: [PATCH] Fixes missing reply order when the direct reply is also included as a quote. --- quartz/build.gradle | 2 +- .../quartz/events/BaseTextNoteEvent.kt | 52 ++++++++++++++----- .../quartz/events/ChannelMessageEvent.kt | 4 +- .../quartz/events/CommentEvent.kt | 4 +- .../events/LiveActivitiesChatMessageEvent.kt | 4 +- 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/quartz/build.gradle b/quartz/build.gradle index 306000b6b..555e40a9b 100644 --- a/quartz/build.gradle +++ b/quartz/build.gradle @@ -73,7 +73,7 @@ dependencies { // Parses URLs from Text: api libs.url.detector - // Parses URLs from Text: + // Normalizes URLs api libs.rfc3986.normalizer testImplementation libs.junit diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/BaseTextNoteEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/BaseTextNoteEvent.kt index ec09c454e..f4ef2ebef 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/events/BaseTextNoteEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/BaseTextNoteEvent.kt @@ -57,20 +57,14 @@ open class BaseTextNoteEvent( fun isForkFromAddressWithPubkey(authorHex: HexKey) = tags.any { it.size > 3 && it[0] == "a" && it[3] == "fork" && it[1].contains(authorHex) } - open fun replyTos(): List { - val oldStylePositional = tags.filter { it.size > 1 && it.size <= 3 && it[0] == "e" }.map { it[1] } + open fun markedReplyTos(): List { val newStyleReply = tags.lastOrNull { it.size > 3 && it[0] == "e" && it[3] == "reply" }?.get(1) val newStyleRoot = tags.lastOrNull { it.size > 3 && it[0] == "e" && it[3] == "root" }?.get(1) - - val newStyleReplyTos = listOfNotNull(newStyleReply, newStyleRoot) - - return if (newStyleReplyTos.isNotEmpty()) { - newStyleReplyTos - } else { - oldStylePositional - } + return listOfNotNull(newStyleReply, newStyleRoot) } + open fun unMarkedReplyTos(): List = tags.filter { it.size > 1 && it.size <= 3 && it[0] == "e" }.map { it[1] } + open fun replyingTo(): HexKey? { val oldStylePositional = tags.lastOrNull { it.size > 1 && it.size <= 3 && it[0] == "e" }?.get(1) val newStyleReply = tags.lastOrNull { it.size > 3 && it[0] == "e" && it[3] == "reply" }?.get(1) @@ -190,21 +184,51 @@ open class BaseTextNoteEvent( } fun tagsWithoutCitations(): List { - val repliesTo = replyTos() + val certainRepliesTo = markedReplyTos() + val uncertainRepliesTo = unMarkedReplyTos() + val tagAddresses = taggedAddresses() .filter { it.kind != CommunityDefinitionEvent.KIND && (kind != WikiNoteEvent.KIND || it.kind != WikiNoteEvent.KIND) // removes forks from itself. }.map { it.toTag() } - if (repliesTo.isEmpty() && tagAddresses.isEmpty()) return emptyList() + + if (certainRepliesTo.isEmpty() && uncertainRepliesTo.isEmpty() && tagAddresses.isEmpty()) return emptyList() val citations = findCitations() + if (id == "d349431390b141e7ea010ebabc288abbfdf8a479cf248f7e1cb2cfa4497ad278") { + certainRepliesTo.forEach { + println("AABBCC Replies $it") + } + + uncertainRepliesTo.forEach { + println("AABBCC Replies $it") + } + + tagAddresses.forEach { + println("AABBCC Addresses $it") + } + + citations.forEach { + println("AABBCC Citations $it") + } + } + return if (citations.isEmpty()) { - repliesTo + tagAddresses + if (certainRepliesTo.isNotEmpty()) { + certainRepliesTo + tagAddresses + } else { + uncertainRepliesTo + tagAddresses + } } else { - repliesTo.filter { it !in citations } + if (certainRepliesTo.isNotEmpty()) { + certainRepliesTo + tagAddresses + } else { + // mix bag between `e` for replies and `e` for citations + uncertainRepliesTo.filter { it !in citations } + } } } } diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/ChannelMessageEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/ChannelMessageEvent.kt index 104a4aace..c4facbcbb 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/events/ChannelMessageEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/ChannelMessageEvent.kt @@ -40,11 +40,13 @@ class ChannelMessageEvent( tags.firstOrNull { it.size > 3 && it[0] == "e" && it[3] == "root" }?.get(1) ?: tags.firstOrNull { it.size > 1 && it[0] == "e" }?.get(1) - override fun replyTos() = + override fun markedReplyTos() = tags .filter { it.firstOrNull() == "e" && it.getOrNull(1) != channel() } .mapNotNull { it.getOrNull(1) } + override fun unMarkedReplyTos() = emptyList() + companion object { const val KIND = 42 const val ALT = "Public chat message" diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/CommentEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/CommentEvent.kt index b85f03650..bac10e590 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/events/CommentEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/CommentEvent.kt @@ -65,7 +65,9 @@ class CommentEvent( override fun isTaggedGeoHashes(hashtags: Set) = geohashes().any { it in hashtags } - override fun replyTos(): List = tags.filter { it.size > 1 && it[0] == "e" }.map { it[1] } + tags.filter { it.size > 1 && it[0] == "E" }.map { it[1] } + override fun markedReplyTos(): List = tags.filter { it.size > 1 && it[0] == "e" }.map { it[1] } + tags.filter { it.size > 1 && it[0] == "E" }.map { it[1] } + + override fun unMarkedReplyTos() = emptyList() override fun replyingTo(): HexKey? = tags.lastOrNull { it.size > 1 && it[0] == "e" }?.get(1) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/LiveActivitiesChatMessageEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/LiveActivitiesChatMessageEvent.kt index d32ad31af..fef2cbfdd 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/events/LiveActivitiesChatMessageEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/LiveActivitiesChatMessageEvent.kt @@ -54,7 +54,9 @@ class LiveActivitiesChatMessageEvent( } } - override fun replyTos() = taggedEvents().minus(activityHex() ?: "") + override fun markedReplyTos() = super.markedReplyTos().minus(activityHex() ?: "") + + override fun unMarkedReplyTos() = super.markedReplyTos().minus(activityHex() ?: "") companion object { const val KIND = 1311