Code review:
- consolidate thread-root resolution - consolidate mute-state writes
This commit is contained in:
@@ -39,6 +39,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||
import com.vitorpamplona.quartz.nip01Core.tags.hashtags.anyHashTag
|
||||
import com.vitorpamplona.quartz.nip01Core.tags.publishedAt.PublishedAtProvider
|
||||
import com.vitorpamplona.quartz.nip10Notes.BaseThreadedEvent
|
||||
import com.vitorpamplona.quartz.nip10Notes.threadRootIdOrSelf
|
||||
import com.vitorpamplona.quartz.nip18Reposts.GenericRepostEvent
|
||||
import com.vitorpamplona.quartz.nip18Reposts.RepostEvent
|
||||
import com.vitorpamplona.quartz.nip19Bech32.entities.NAddress
|
||||
@@ -860,10 +861,10 @@ open class Note(
|
||||
return true
|
||||
}
|
||||
|
||||
// if this note belongs to a muted thread (by NIP-10 root id)
|
||||
if (accountChoices.mutedThreads.isNotEmpty()) {
|
||||
val rootId = (thisEvent as? BaseThreadedEvent)?.root()?.eventId ?: idHex
|
||||
if (accountChoices.mutedThreads.contains(rootId)) return true
|
||||
if (accountChoices.mutedThreads.isNotEmpty() &&
|
||||
accountChoices.mutedThreads.contains(thisEvent.threadRootIdOrSelf())
|
||||
) {
|
||||
return true
|
||||
}
|
||||
|
||||
// if the post is sensitive and the user doesn't want to see sensitive content
|
||||
|
||||
+4
-49
@@ -25,22 +25,11 @@ import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/**
|
||||
* Unit tests for Note.isHiddenFor() focusing on the mutedThreads check.
|
||||
*
|
||||
* Fixture approach:
|
||||
* - TextNoteEvent is constructed directly via its primary constructor (no signing needed —
|
||||
* the sig field is not validated in isHiddenFor).
|
||||
* - A Note is created with the event's id, then note.event is set.
|
||||
* - LiveHiddenUsers is a plain data class — no Android plumbing required.
|
||||
*/
|
||||
class NoteIsHiddenForTest {
|
||||
// 64-char hex constants
|
||||
private val rootId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
private val replyId = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
||||
private val authorPubKey = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
|
||||
|
||||
// Minimal LiveHiddenUsers with all filters off (used as a baseline)
|
||||
private val noHidden =
|
||||
LiveHiddenUsers(
|
||||
showSensitiveContent = null,
|
||||
@@ -50,12 +39,6 @@ class NoteIsHiddenForTest {
|
||||
mutedThreads = emptySet(),
|
||||
)
|
||||
|
||||
/**
|
||||
* Build a TextNoteEvent.
|
||||
*
|
||||
* @param id The event id (64 hex chars).
|
||||
* @param eTags Raw `e` tag arrays included in the event's tags.
|
||||
*/
|
||||
private fun textNoteEvent(
|
||||
id: String,
|
||||
pubKey: String = authorPubKey,
|
||||
@@ -69,21 +52,12 @@ class NoteIsHiddenForTest {
|
||||
sig = "sig",
|
||||
)
|
||||
|
||||
/** A marked `e` tag array with marker="root" at index 3. */
|
||||
private fun rootETag(eventId: String) = arrayOf("e", eventId, "", "root")
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
fun reply_inMutedThread_isHidden() {
|
||||
// A reply whose NIP-10 root points to `rootId`, which is muted.
|
||||
val event =
|
||||
textNoteEvent(
|
||||
id = replyId,
|
||||
eTags = arrayOf(rootETag(rootId)),
|
||||
)
|
||||
val event = textNoteEvent(id = replyId, eTags = arrayOf(rootETag(rootId)))
|
||||
val note = Note(replyId).also { it.event = event }
|
||||
|
||||
val choices = noHidden.copy(mutedThreads = setOf(rootId))
|
||||
|
||||
assertTrue(note.isHiddenFor(choices), "Reply inside a muted thread must be hidden")
|
||||
@@ -91,14 +65,8 @@ class NoteIsHiddenForTest {
|
||||
|
||||
@Test
|
||||
fun topLevelNote_ownIdMuted_isHidden() {
|
||||
// A top-level note (no e-tags) whose own id is in mutedThreads.
|
||||
val event =
|
||||
textNoteEvent(
|
||||
id = rootId,
|
||||
eTags = emptyArray(),
|
||||
)
|
||||
val event = textNoteEvent(id = rootId, eTags = emptyArray())
|
||||
val note = Note(rootId).also { it.event = event }
|
||||
|
||||
val choices = noHidden.copy(mutedThreads = setOf(rootId))
|
||||
|
||||
assertTrue(note.isHiddenFor(choices), "Top-level note whose id is muted must be hidden")
|
||||
@@ -106,16 +74,9 @@ class NoteIsHiddenForTest {
|
||||
|
||||
@Test
|
||||
fun note_inUnmutedThread_isNotHidden() {
|
||||
// A reply note whose root is NOT in mutedThreads, author not hidden.
|
||||
val otherRoot = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
|
||||
val event =
|
||||
textNoteEvent(
|
||||
id = replyId,
|
||||
eTags = arrayOf(rootETag(otherRoot)),
|
||||
)
|
||||
val event = textNoteEvent(id = replyId, eTags = arrayOf(rootETag(otherRoot)))
|
||||
val note = Note(replyId).also { it.event = event }
|
||||
|
||||
// rootId is muted, but this note's root is otherRoot
|
||||
val choices = noHidden.copy(mutedThreads = setOf(rootId))
|
||||
|
||||
assertFalse(note.isHiddenFor(choices), "Reply in an un-muted thread must not be hidden")
|
||||
@@ -123,15 +84,9 @@ class NoteIsHiddenForTest {
|
||||
|
||||
@Test
|
||||
fun authorHidden_isHidden_regression() {
|
||||
// Regression guard: author-hidden notes must still return true.
|
||||
val event = textNoteEvent(id = replyId)
|
||||
val note = Note(replyId).also { it.event = event }
|
||||
|
||||
// Put the author's pubKey hashCode into hiddenUsersHashCodes
|
||||
val choices =
|
||||
noHidden.copy(
|
||||
hiddenUsersHashCodes = setOf(authorPubKey.hashCode()),
|
||||
)
|
||||
val choices = noHidden.copy(hiddenUsersHashCodes = setOf(authorPubKey.hashCode()))
|
||||
|
||||
assertTrue(note.isHiddenFor(choices), "Note whose author is hidden must still be hidden")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user