diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/TagArrayExt.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/TagArrayExt.kt index 46ebe49dee..b94b093067 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/TagArrayExt.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/TagArrayExt.kt @@ -21,6 +21,7 @@ package com.vitorpamplona.quartz.nip51Lists.muteList import com.vitorpamplona.quartz.nip01Core.core.TagArray +import com.vitorpamplona.quartz.nip51Lists.muteList.tags.EventTag import com.vitorpamplona.quartz.nip51Lists.muteList.tags.MuteTag import com.vitorpamplona.quartz.nip51Lists.muteList.tags.UserTag import com.vitorpamplona.quartz.nip51Lists.muteList.tags.WordTag @@ -36,3 +37,9 @@ fun TagArray.mutedUserIdSet() = mapNotNullTo(mutableSetOf(), UserTag::parseKey) fun TagArray.mutedWords() = mapNotNull(WordTag::parse) fun TagArray.mutedWordSet() = mapNotNullTo(mutableSetOf(), WordTag::parse) + +fun TagArray.mutedThreads() = mapNotNull(EventTag::parse) + +fun TagArray.mutedThreadIds() = mapNotNull(EventTag::parseId) + +fun TagArray.mutedThreadIdSet() = mapNotNullTo(mutableSetOf(), EventTag::parseId) diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/EventTag.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/EventTag.kt new file mode 100644 index 0000000000..765e0f0a5d --- /dev/null +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/EventTag.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip51Lists.muteList.tags + +import androidx.compose.runtime.Immutable +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import com.vitorpamplona.quartz.nip01Core.core.Tag +import com.vitorpamplona.quartz.nip01Core.core.has +import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl +import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer +import com.vitorpamplona.quartz.utils.arrayOfNotNull +import com.vitorpamplona.quartz.utils.ensure + +@Immutable +class EventTag( + val eventId: HexKey, + val relayHint: NormalizedRelayUrl? = null, + val pubKeyHint: HexKey? = null, +) : MuteTag { + override fun toTagArray() = assemble(eventId, relayHint, pubKeyHint) + + override fun toTagIdOnly() = assemble(eventId, null, null) + + companion object { + const val TAG_NAME = "e" + + fun isTagged(tag: Array): Boolean = tag.has(1) && tag[0] == TAG_NAME && tag[1].length == 64 + + fun parse(tag: Tag): EventTag? { + ensure(tag.has(1)) { return null } + ensure(tag[0] == TAG_NAME) { return null } + ensure(tag[1].length == 64) { return null } + val hint = tag.getOrNull(2)?.takeIf { it.isNotEmpty() }?.let { RelayUrlNormalizer.normalizeOrNull(it) } + val pubKey = tag.getOrNull(3)?.takeIf { it.length == 64 } + return EventTag(tag[1], hint, pubKey) + } + + fun parseId(tag: Array): HexKey? { + ensure(tag.has(1)) { return null } + ensure(tag[0] == TAG_NAME) { return null } + ensure(tag[1].length == 64) { return null } + return tag[1] + } + + fun assemble( + eventId: HexKey, + relayHint: NormalizedRelayUrl?, + pubKeyHint: HexKey?, + ) = arrayOfNotNull(TAG_NAME, eventId, relayHint?.url, pubKeyHint) + } +} diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/MuteTag.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/MuteTag.kt index 90b6779792..7d95b71067 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/MuteTag.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/MuteTag.kt @@ -28,8 +28,8 @@ sealed interface MuteTag { fun toTagIdOnly(): Tag companion object { - fun isTagged(tag: Array) = WordTag.isTagged(tag) || UserTag.isTagged(tag) + fun isTagged(tag: Array) = WordTag.isTagged(tag) || UserTag.isTagged(tag) || EventTag.isTagged(tag) - fun parse(tag: Array): MuteTag? = WordTag.parse(tag) ?: UserTag.parse(tag) + fun parse(tag: Array): MuteTag? = WordTag.parse(tag) ?: UserTag.parse(tag) ?: EventTag.parse(tag) } } diff --git a/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/MuteListEventTest.kt b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/MuteListEventTest.kt new file mode 100644 index 0000000000..8dfd37e726 --- /dev/null +++ b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/MuteListEventTest.kt @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip51Lists.muteList + +import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal +import com.vitorpamplona.quartz.nip51Lists.muteList.tags.EventTag +import com.vitorpamplona.quartz.nip51Lists.muteList.tags.UserTag +import com.vitorpamplona.quartz.nip51Lists.muteList.tags.WordTag +import com.vitorpamplona.quartz.utils.nsecToKeyPair +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertTrue + +class MuteListEventTest { + private val signer = NostrSignerInternal("nsec10g0wheggqn9dawlc0yuv6adnat6n09anr7eyykevw2dm8xa5fffs0wsdsr".nsecToKeyPair()) + + // 64-char hex IDs + private val rootA = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1" + private val rootB = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb2b" + private val pubA = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc3c" + + @Test + fun create_withEventTag_privateContent() = + runTest { + val event = + MuteListEvent.create( + mute = EventTag(rootA), + isPrivate = true, + signer = signer, + createdAt = 1_700_000_000, + ) + + // No public "e" tag should be present + val publicETags = event.tags.filter { it.size >= 2 && it[0] == "e" && it[1] == rootA } + assertTrue(publicETags.isEmpty(), "Private mute must not appear as a public e-tag") + + // Content must be non-empty (it is encrypted) + assertTrue(event.content.isNotEmpty(), "Content must be non-empty when mute is private") + + // Decrypting must yield exactly one EventTag with eventId == rootA + val privateMutes = assertNotNull(event.privateMutes(signer), "privateMutes must not return null") + assertEquals(1, privateMutes.size, "Expected exactly one private mute") + val tag = assertNotNull(privateMutes.firstOrNull() as? EventTag, "Mute must be an EventTag") + assertEquals(rootA, tag.eventId) + } + + @Test + fun add_eventTagToExistingMute_combinesPrivateSet() = + runTest { + val firstEvent = + MuteListEvent.create( + mute = EventTag(rootA), + isPrivate = true, + signer = signer, + createdAt = 1_700_000_001, + ) + + val secondEvent = + MuteListEvent.add( + earlierVersion = firstEvent, + mute = EventTag(rootB), + isPrivate = true, + signer = signer, + createdAt = 1_700_000_002, + ) + + val privateMutes = assertNotNull(secondEvent.privateMutes(signer), "privateMutes must not return null") + val ids = privateMutes.filterIsInstance().map { it.eventId }.toSet() + assertTrue(ids.contains(rootA), "rootA must be present after add") + assertTrue(ids.contains(rootB), "rootB must be present after add") + } + + @Test + fun add_eventTagPreservesPriorUserAndWordTags() = + runTest { + // Build a kind-10000 with one UserTag and one WordTag (both private) + val base = + MuteListEvent.create( + publicMutes = emptyList(), + privateMutes = listOf(UserTag(pubA), WordTag("spam")), + signer = signer, + createdAt = 1_700_000_003, + ) + + val updated = + MuteListEvent.add( + earlierVersion = base, + mute = EventTag(rootA), + isPrivate = true, + signer = signer, + createdAt = 1_700_000_004, + ) + + val privateMutes = assertNotNull(updated.privateMutes(signer), "privateMutes must not return null") + assertEquals(3, privateMutes.size, "Expected three private mutes (UserTag + WordTag + EventTag)") + + val userTags = privateMutes.filterIsInstance() + val wordTags = privateMutes.filterIsInstance() + val eventTags = privateMutes.filterIsInstance() + + assertEquals(1, userTags.size, "Must have exactly one UserTag") + assertEquals(pubA, userTags.first().pubKey) + + assertEquals(1, wordTags.size, "Must have exactly one WordTag") + assertEquals("spam", wordTags.first().word) + + assertEquals(1, eventTags.size, "Must have exactly one EventTag") + assertEquals(rootA, eventTags.first().eventId) + } + + @Test + fun remove_eventTagLeavesOthers() = + runTest { + // Build event with both rootA and rootB muted privately + val base = + MuteListEvent.create( + publicMutes = emptyList(), + privateMutes = listOf(EventTag(rootA), EventTag(rootB)), + signer = signer, + createdAt = 1_700_000_005, + ) + + val updated = + MuteListEvent.remove( + earlierVersion = base, + mute = EventTag(rootA), + signer = signer, + createdAt = 1_700_000_006, + ) + + val privateMutes = assertNotNull(updated.privateMutes(signer), "privateMutes must not return null") + val ids = privateMutes.filterIsInstance().map { it.eventId }.toSet() + assertTrue(!ids.contains(rootA), "rootA must have been removed") + assertTrue(ids.contains(rootB), "rootB must still be present") + } + + @Test + fun removeAll_mixedTagsRemovesUserAndEvent_keepsWord() = + runTest { + val base = + MuteListEvent.create( + publicMutes = emptyList(), + privateMutes = listOf(UserTag(pubA), WordTag("spam"), EventTag(rootA)), + signer = signer, + createdAt = 1_700_000_007, + ) + + val updated = + MuteListEvent.removeAll( + earlierVersion = base, + mutes = listOf(UserTag(pubA), EventTag(rootA)), + signer = signer, + createdAt = 1_700_000_008, + ) + + val privateMutes = assertNotNull(updated.privateMutes(signer), "privateMutes must not return null") + + val userTags = privateMutes.filterIsInstance() + val wordTags = privateMutes.filterIsInstance() + val eventTags = privateMutes.filterIsInstance() + + assertTrue(userTags.none { it.pubKey == pubA }, "UserTag(pubA) must have been removed") + assertTrue(eventTags.none { it.eventId == rootA }, "EventTag(rootA) must have been removed") + assertEquals(1, wordTags.size, "WordTag must still be present") + assertEquals("spam", wordTags.first().word) + } + + @Test + fun legacyMuteListWithoutEventTags_decodesToEmptyThreadSet() = + runTest { + // Build a kind-10000 with only p + word tags (public), no e tags + val legacyEvent = + MuteListEvent.create( + publicMutes = listOf(UserTag(pubA), WordTag("spam")), + privateMutes = emptyList(), + signer = signer, + createdAt = 1_700_000_009, + ) + + // Calling mutedThreadIdSet() on a tag array with no e-tags must not crash and return empty + val publicThreadIds = legacyEvent.tags.mutedThreadIdSet() + assertTrue(publicThreadIds.isEmpty(), "Legacy event with only p+word tags must have empty thread id set") + + // privateMutes returns empty list (content is blank/empty for no private mutes) + val privateMutes = legacyEvent.privateMutes(signer) + val privateEventTags = (privateMutes ?: emptyList()).filterIsInstance() + assertTrue(privateEventTags.isEmpty(), "No private EventTags in a legacy event") + } + + @Test + fun roundTrip_eventTagsViaEncryption_preservesIds() = + runTest { + val event = + MuteListEvent.create( + publicMutes = emptyList(), + privateMutes = listOf(EventTag(rootA), EventTag(rootB)), + signer = signer, + createdAt = 1_700_000_010, + ) + + val decrypted = assertNotNull(event.privateMutes(signer), "privateMutes must not return null") + val ids = decrypted.filterIsInstance().map { it.eventId }.toSet() + + assertEquals(setOf(rootA, rootB), ids, "Round-trip must preserve all muted thread IDs") + } +} diff --git a/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/TagArrayExtMutedThreadsTest.kt b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/TagArrayExtMutedThreadsTest.kt new file mode 100644 index 0000000000..b73ea24aef --- /dev/null +++ b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/TagArrayExtMutedThreadsTest.kt @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip51Lists.muteList + +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class TagArrayExtMutedThreadsTest { + private val id1 = "3ae34f70016c33d36f9e6ad395591ea36fee7ac488d1dad383ae64ae3d988f50" + private val id2 = "00000000016c33d36f9e6ad395591ea36fee7ac488d1dad383ae64ae3d988f50" + + @Test fun mutedThreads_returnsEventTagsOnly() { + val tags = + arrayOf( + arrayOf("e", id1), + arrayOf("p", id2), + arrayOf("word", "spam"), + arrayOf("e", id2, "wss://relay.damus.io"), + ) + val parsed = tags.mutedThreads() + assertEquals(2, parsed.size) + assertEquals(id1, parsed[0].eventId) + assertEquals(id2, parsed[1].eventId) + } + + @Test fun mutedThreadIdSet_extractsIdsAcrossMixedTags() { + val tags = + arrayOf( + arrayOf("e", id1), + arrayOf("p", id2), + arrayOf("e", id2), + ) + val ids = tags.mutedThreadIdSet() + assertEquals(setOf(id1, id2), ids) + } + + @Test fun mutedThreadIdSet_emptyOnNoEventTags() { + val tags = arrayOf(arrayOf("p", id1), arrayOf("word", "spam")) + assertTrue(tags.mutedThreadIdSet().isEmpty()) + } +} diff --git a/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/EventTagTest.kt b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/EventTagTest.kt new file mode 100644 index 0000000000..00d2051981 --- /dev/null +++ b/quartz/src/commonTest/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/EventTagTest.kt @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.quartz.nip51Lists.muteList.tags + +import com.vitorpamplona.quartz.nip01Core.relay.normalizer.RelayUrlNormalizer +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class EventTagTest { + private val rootHex = "3ae34f70016c33d36f9e6ad395591ea36fee7ac488d1dad383ae64ae3d988f50" + private val authorHex = "d0debf9fb12def81f43d7c69429bb784812ac1e4d2d53a202db6aac7ea4b466c" + private val relayHint = "wss://relay.damus.io" + + @Test fun isTagged_eventTagWithIdOnly() { + assertTrue(EventTag.isTagged(arrayOf("e", rootHex))) + } + + @Test fun isTagged_eventTagWithRelayHint() { + assertTrue(EventTag.isTagged(arrayOf("e", rootHex, relayHint))) + } + + @Test fun isTagged_rejectsShortId() { + assertFalse(EventTag.isTagged(arrayOf("e", "tooShort"))) + } + + @Test fun isTagged_rejectsWrongPrefix() { + assertFalse(EventTag.isTagged(arrayOf("p", rootHex))) + } + + @Test fun parse_idOnly() { + val tag = assertNotNull(EventTag.parse(arrayOf("e", rootHex))) + assertEquals(rootHex, tag.eventId) + assertNull(tag.relayHint) + assertNull(tag.pubKeyHint) + } + + @Test fun parse_withRelayHint() { + val tag = assertNotNull(EventTag.parse(arrayOf("e", rootHex, relayHint))) + assertEquals(rootHex, tag.eventId) + assertEquals("wss://relay.damus.io/", tag.relayHint?.url) + } + + @Test fun parse_withRelayAndPubkeyHint() { + val tag = assertNotNull(EventTag.parse(arrayOf("e", rootHex, relayHint, authorHex))) + assertEquals(authorHex, tag.pubKeyHint) + } + + @Test fun parse_rejectsShortId() { + assertNull(EventTag.parse(arrayOf("e", "tooShort"))) + } + + @Test fun parseId_extractsIdOnly() { + assertEquals(rootHex, EventTag.parseId(arrayOf("e", rootHex, relayHint))) + } + + @Test fun toTagArray_roundTripsWithHints() { + val original = EventTag(rootHex, RelayUrlNormalizer.normalizeOrNull(relayHint), authorHex) + val parsed = assertNotNull(EventTag.parse(original.toTagArray())) + assertEquals(rootHex, parsed.eventId) + assertEquals(authorHex, parsed.pubKeyHint) + } + + @Test fun toTagIdOnly_stripsHints() { + val tag = EventTag(rootHex, RelayUrlNormalizer.normalizeOrNull(relayHint), authorHex) + val stripped = tag.toTagIdOnly() + assertEquals(2, stripped.size) + assertEquals("e", stripped[0]) + assertEquals(rootHex, stripped[1]) + } + + @Test fun muteTagCompanion_parsesEventTag() { + val parsed = assertNotNull(MuteTag.parse(arrayOf("e", rootHex))) + assertTrue(parsed is EventTag) + } + + @Test fun muteTagCompanion_isTaggedRecognizesEventTag() { + assertTrue(MuteTag.isTagged(arrayOf("e", rootHex))) + } +}