diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip51Lists/PeopleListEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip51Lists/PeopleListEvent.kt index 6cd6ad7e9..9b71dcd29 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip51Lists/PeopleListEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip51Lists/PeopleListEvent.kt @@ -109,6 +109,49 @@ class PeopleListEvent( } } + fun createListWithDescription( + dTag: String, + key: String, + tag: String, + description: String? = null, + isPrivate: Boolean, + signer: NostrSigner, + createdAt: Long = TimeUtils.now(), + onReady: (PeopleListEvent) -> Unit, + ) { + if (description == null) { + createListWithTag(name = dTag, key, tag, isPrivate, signer, createdAt, onReady) + } else { + if (isPrivate) { + encryptTags( + arrayOf(arrayOf(key, tag), arrayOf("description", description)), + signer, + ) { encryptedTags -> + create( + content = encryptedTags, + tags = arrayOf(arrayOf("d", dTag)), + signer = signer, + createdAt = createdAt, + onReady = onReady, + ) + } + } else { + create( + content = "", + tags = + arrayOf( + arrayOf("d", dTag), + arrayOf(key, tag), + arrayOf("description", description), + ), + signer = signer, + createdAt = createdAt, + onReady = onReady, + ) + } + } + } + fun createListWithUser( name: String, pubKeyHex: String, @@ -270,6 +313,61 @@ class PeopleListEvent( } } + fun modifyTag( + earlierVersion: PeopleListEvent, + key: String, + tag: String, + isPrivate: Boolean, + signer: NostrSigner, + createdAt: Long = TimeUtils.now(), + onReady: (PeopleListEvent) -> Unit, + ) { + if (isPrivate) { + earlierVersion.privateTagsOrEmpty(signer) { privateTags -> + val modifiedPrivateTags = + privateTags.replaceElementIf( + { it.size > 1 && it.contains(key) }, + arrayOf(key, tag), + ) + + encryptTags( + privateTags = modifiedPrivateTags, + signer = signer, + ) { encryptedTags -> + create( + content = encryptedTags, + tags = earlierVersion.tags, + signer = signer, + createdAt = createdAt, + onReady = onReady, + ) + } + } + } else { + val modifiedPublicTags = + earlierVersion.tags.replaceElementIf( + { it.size > 1 && it.contains(key) }, + arrayOf(key, tag), + ) + + create( + content = earlierVersion.content, + tags = modifiedPublicTags, + signer = signer, + createdAt = createdAt, + onReady = onReady, + ) + } + } + + private inline fun Array.replaceElementIf( + predicate: (T) -> Boolean, + newElem: T, + ): Array = + map { + if (predicate(it)) newElem else it + }.toTypedArray() + fun create( content: String, tags: Array>,