This commit is contained in:
Vitor Pamplona
2025-10-14 14:15:38 -04:00
33 changed files with 1286 additions and 337 deletions
@@ -134,7 +134,7 @@ class BookmarkListEvent(
val privateTags = earlierVersion.privateTags(signer) ?: throw SignerExceptions.UnauthorizedDecryptionException()
resign(
privateTags = privateTags.remove(bookmarkIdTag.toTagIdOnly()),
tags = earlierVersion.tags.remove(bookmarkIdTag.toTagIdOnly()),
tags = earlierVersion.tags,
signer = signer,
createdAt = createdAt,
)
@@ -155,6 +155,31 @@ class PeopleListEvent(
)
}
suspend fun remove(
earlierVersion: PeopleListEvent,
person: UserTag,
isPrivate: Boolean,
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
): PeopleListEvent {
if (isPrivate) {
val privateTags = earlierVersion.privateTags(signer) ?: throw SignerExceptions.UnauthorizedDecryptionException()
return resign(
publicTags = earlierVersion.tags,
privateTags = privateTags.remove(person.toTagArray()),
signer = signer,
createdAt = createdAt,
)
} else {
return resign(
content = earlierVersion.content,
tags = earlierVersion.tags.remove(person.toTagArray()),
signer = signer,
createdAt = createdAt,
)
}
}
suspend fun resign(
publicTags: TagArray,
privateTags: TagArray,
@@ -223,51 +248,61 @@ class PeopleListEvent(
title: String,
description: String? = null,
isPrivate: Boolean,
firstMemberHex: String? = null,
firstPublicMembers: List<String> = emptyList(),
firstPrivateMembers: List<String> = emptyList(),
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
onReady: (PeopleListEvent) -> Unit,
) {
if (description == null) {
val newList =
create(
name = title,
person = UserTag(pubKey = firstMemberHex ?: signer.pubKey),
isPrivate = isPrivate,
signer = signer,
dTag = dTag,
createdAt = createdAt,
)
onReady(newList)
} else {
if (isPrivate) {
val event =
build(
name = title,
privatePeople = listOf(UserTag(pubKey = firstMemberHex ?: signer.pubKey)),
signer = signer,
dTag = dTag,
createdAt = createdAt,
) {
addUnique(arrayOf("description", description))
}
val list = signer.sign(event)
onReady(list)
} else {
val event =
build(
name = title,
publicPeople = listOf(UserTag(pubKey = firstMemberHex ?: signer.pubKey)),
signer = signer,
dTag = dTag,
createdAt = createdAt,
) {
addUnique(arrayOf("description", description))
}
val list = signer.sign(event)
onReady(list)
val newListTemplate =
build(
name = title,
publicPeople =
if (!isPrivate && firstPublicMembers.isNotEmpty()) {
firstPublicMembers.map { UserTag(pubKey = it) }
} else {
emptyList()
},
privatePeople =
if (isPrivate && firstPrivateMembers.isNotEmpty()) {
firstPrivateMembers.map { UserTag(pubKey = it) }
} else {
emptyList()
},
signer = signer,
dTag = dTag,
createdAt = createdAt,
) {
if (description != null) addUnique(DescriptionTag.assemble(description))
}
}
val newList = signer.sign(newListTemplate)
onReady(newList)
}
suspend fun copy(
dTag: String,
title: String,
description: String? = null,
firstPublicMembers: List<String> = emptyList(),
firstPrivateMembers: List<String> = emptyList(),
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
onReady: (PeopleListEvent) -> Unit,
) {
val cloneTemplate =
build(
name = title,
publicPeople = firstPublicMembers.map { UserTag(pubKey = it) },
privatePeople = firstPrivateMembers.map { UserTag(pubKey = it) },
signer = signer,
dTag = dTag,
createdAt = createdAt,
) {
if (description != null) addUnique(DescriptionTag.assemble(description))
}
val listClone = signer.sign(cloneTemplate)
onReady(listClone)
}
suspend fun createListWithUser(
@@ -311,6 +346,7 @@ class PeopleListEvent(
suspend fun removeUser(
earlierVersion: PeopleListEvent,
pubKeyHex: String,
isUserPrivate: Boolean,
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
onReady: (PeopleListEvent) -> Unit,
@@ -319,6 +355,7 @@ class PeopleListEvent(
remove(
earlierVersion = earlierVersion,
person = UserTag(pubKey = pubKeyHex),
isPrivate = isUserPrivate,
signer = signer,
createdAt = createdAt,
)
@@ -351,5 +388,51 @@ class PeopleListEvent(
)
onReady(modified)
}
suspend fun modifyDescription(
earlierVersion: PeopleListEvent,
newDescription: String?,
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
onReady: (PeopleListEvent) -> Unit = {},
) {
val privateTags = earlierVersion.privateTags(signer) ?: throw SignerExceptions.UnauthorizedDecryptionException()
val currentDescriptionTag = earlierVersion.tags.firstOrNull { it[0] == DescriptionTag.TAG_NAME }
val currentDescription = currentDescriptionTag?.get(1)
if (currentDescription.equals(newDescription)) {
// Do nothing
return
} else {
if (newDescription == null || newDescription.isEmpty()) {
val modified =
resign(
publicTags = earlierVersion.tags.remove { it[0] == DescriptionTag.TAG_NAME },
privateTags = privateTags.remove { it[0] == DescriptionTag.TAG_NAME },
signer = signer,
createdAt = createdAt,
)
onReady(modified)
} else {
val newDescriptionTag = DescriptionTag.assemble(newDescription)
val modified =
if (currentDescriptionTag == null) {
resign(
publicTags = earlierVersion.tags.plusElement(newDescriptionTag),
privateTags = privateTags,
signer = signer,
createdAt = createdAt,
)
} else {
resign(
publicTags = earlierVersion.tags.replaceAll(currentDescriptionTag, newDescriptionTag),
privateTags = privateTags.replaceAll(currentDescriptionTag, newDescriptionTag),
signer = signer,
createdAt = createdAt,
)
}
onReady(modified)
}
}
}
}
}
@@ -0,0 +1,123 @@
/**
* 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.bookmarkList
import com.vitorpamplona.quartz.nip01Core.signers.NostrSignerInternal
import com.vitorpamplona.quartz.nip51Lists.bookmarkList.tags.EventBookmark
import com.vitorpamplona.quartz.utils.nsecToKeyPair
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class BookmarkListEventTest {
private val signer = NostrSignerInternal("nsec10g0wheggqn9dawlc0yuv6adnat6n09anr7eyykevw2dm8xa5fffs0wsdsr".nsecToKeyPair())
@Test
fun publicTagsPreservedWhenRemovingFromPrivateBookmarks() =
runBlocking {
// Create a test event bookmark
val testEventId = "a".repeat(64)
val testBookmark = EventBookmark(testEventId)
// Create a bookmark list with event in public bookmarks
val initialEvent =
BookmarkListEvent.create(
publicBookmarks = listOf(testBookmark),
privateBookmarks = emptyList(),
signer = signer,
createdAt = 1740669816,
)
// Count the public tags (should be 1 for the bookmark)
val initialPublicTagCount = initialEvent.tags.count { tag -> tag.size >= 2 && tag[0] == "e" && tag[1] == testEventId }
assertTrue(
initialPublicTagCount == 1,
"Should have exactly 1 public bookmark tag initially",
)
// Try to remove the bookmark from private bookmarks (even though it's only in public)
// This simulates the scenario where a bookmark exists in both lists
val updatedEvent =
BookmarkListEvent.remove(
earlierVersion = initialEvent,
bookmarkIdTag = testBookmark,
isPrivate = true,
signer = signer,
createdAt = 1740669817,
)
// Count the public tags again
val finalPublicTagCount = updatedEvent.tags.count { tag -> tag.size >= 2 && tag[0] == "e" && tag[1] == testEventId }
// CRITICAL: The public tag count should be unchanged
// Without the fix, this would be 0 because the bug also removed public tags
assertTrue(
finalPublicTagCount == initialPublicTagCount,
"Public tags should be preserved when removing from private bookmarks. Initial: $initialPublicTagCount, Final: $finalPublicTagCount",
)
// Also verify using publicBookmarks() method
assertTrue(
updatedEvent.publicBookmarks().any { it is EventBookmark && it.eventId == testEventId },
"Bookmark should still be accessible via publicBookmarks() method",
)
}
@Test
fun publicTagsRemovedWhenRemovingFromPublicBookmarks() =
runBlocking {
// Create a test event bookmark
val testEventId = "b".repeat(64)
val testBookmark = EventBookmark(testEventId)
// Create a bookmark list with event only in public bookmarks
val initialEvent =
BookmarkListEvent.create(
publicBookmarks = listOf(testBookmark),
privateBookmarks = emptyList(),
signer = signer,
createdAt = 1740669816,
)
// Remove from public bookmarks
val updatedEvent =
BookmarkListEvent.remove(
earlierVersion = initialEvent,
bookmarkIdTag = testBookmark,
isPrivate = false,
signer = signer,
createdAt = 1740669817,
)
// Verify the bookmark was removed from public bookmarks
val finalPublicTagCount = updatedEvent.tags.count { tag -> tag.size >= 2 && tag[0] == "e" && tag[1] == testEventId }
assertTrue(
finalPublicTagCount == 0,
"Public tags should be removed when isPrivate=false",
)
assertFalse(
updatedEvent.publicBookmarks().any { it is EventBookmark && it.eventId == testEventId },
"Bookmark should be removed from public bookmarks",
)
}
}