From 4b52cdb10161c68258ec641698e223ca422e9f01 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 15 Jan 2025 09:57:06 -0500 Subject: [PATCH] Updates NIP-14 subjects to the new style of extension functions --- .../quartz/nip01Core/core/TagArrayBuilder.kt | 13 +++++- .../quartz/nip14Subject/EventExt.kt | 5 +-- .../quartz/nip14Subject/SubjectTag.kt | 45 +++++++++++++++++++ ...TagSerializer.kt => TagArrayBuilderExt.kt} | 9 ++-- .../quartz/nip14Subject/TagArrayExt.kt | 32 +++++++++++++ .../quartz/nip17Dm/ChatMessageEvent.kt | 11 ++--- 6 files changed, 99 insertions(+), 16 deletions(-) create mode 100644 quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/SubjectTag.kt rename quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/{SubjectTagSerializer.kt => TagArrayBuilderExt.kt} (88%) create mode 100644 quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/TagArrayExt.kt diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/core/TagArrayBuilder.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/core/TagArrayBuilder.kt index 9827b415b..1c0188f81 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/core/TagArrayBuilder.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/core/TagArrayBuilder.kt @@ -23,8 +23,19 @@ package com.vitorpamplona.quartz.nip01Core.core class TagArrayBuilder { val tagList = mutableListOf>() - fun add(tag: Array) { + fun add(tag: Array): TagArrayBuilder { tagList.add(tag) + return this + } + + fun addAll(tag: List>): TagArrayBuilder { + tagList.addAll(tag) + return this + } + + fun addAll(tag: Array>): TagArrayBuilder { + tagList.addAll(tag) + return this } fun build() = tagList.toTypedArray() diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/EventExt.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/EventExt.kt index c30e37095..88f70fd58 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/EventExt.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/EventExt.kt @@ -21,8 +21,5 @@ package com.vitorpamplona.quartz.nip14Subject import com.vitorpamplona.quartz.nip01Core.core.Event -import com.vitorpamplona.quartz.nip01Core.core.firstTagValue -val SUBJECT_TAG = "subject" - -fun Event.subject() = tags.firstTagValue(SUBJECT_TAG) +fun Event.subject() = tags.subject() diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/SubjectTag.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/SubjectTag.kt new file mode 100644 index 000000000..fc6a3f50f --- /dev/null +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/SubjectTag.kt @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2024 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.nip14Subject + +import com.vitorpamplona.quartz.utils.bytesUsedInMemory +import com.vitorpamplona.quartz.utils.pointerSizeInBytes + +class SubjectTag( + val subject: String, +) { + fun countMemory(): Long = 1 * pointerSizeInBytes + subject.bytesUsedInMemory() + + fun toTagArray() = assemble(subject) + + companion object { + val TAG_NAME = "subject" + + @JvmStatic + fun parse(tags: Array): SubjectTag { + require(tags[0] == TAG_NAME) + return SubjectTag(tags[1]) + } + + @JvmStatic + fun assemble(subject: String) = arrayOf(TAG_NAME, subject) + } +} diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/SubjectTagSerializer.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/TagArrayBuilderExt.kt similarity index 88% rename from quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/SubjectTagSerializer.kt rename to quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/TagArrayBuilderExt.kt index afaede02f..bae13bc22 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/SubjectTagSerializer.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/TagArrayBuilderExt.kt @@ -20,9 +20,6 @@ */ package com.vitorpamplona.quartz.nip14Subject -class SubjectTagSerializer { - companion object { - @JvmStatic - fun toTagArray(subject: String = "") = arrayOf(SUBJECT_TAG, subject) - } -} +import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder + +fun TagArrayBuilder.subject(subject: String) = add(SubjectTag.assemble(subject)) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/TagArrayExt.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/TagArrayExt.kt new file mode 100644 index 000000000..4e7f538c8 --- /dev/null +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip14Subject/TagArrayExt.kt @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2024 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.nip14Subject + +import com.vitorpamplona.quartz.nip01Core.core.TagArray +import com.vitorpamplona.quartz.nip01Core.core.firstTagValue +import com.vitorpamplona.quartz.nip01Core.core.hasTagWithContent +import com.vitorpamplona.quartz.nip01Core.core.mapValues + +fun TagArray.subject() = this.firstTagValue(SubjectTag.TAG_NAME) + +fun TagArray.subjects() = this.mapValues(SubjectTag.TAG_NAME) + +fun TagArray.hasSubject() = this.hasTagWithContent(SubjectTag.TAG_NAME) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip17Dm/ChatMessageEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip17Dm/ChatMessageEvent.kt index af5d86b1b..dceb8aede 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip17Dm/ChatMessageEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip17Dm/ChatMessageEvent.kt @@ -22,9 +22,10 @@ package com.vitorpamplona.quartz.nip17Dm import androidx.compose.runtime.Immutable import com.vitorpamplona.quartz.nip01Core.HexKey +import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner import com.vitorpamplona.quartz.nip01Core.tags.geohash.geohashMipMap -import com.vitorpamplona.quartz.nip14Subject.SubjectTagSerializer +import com.vitorpamplona.quartz.nip14Subject.subject import com.vitorpamplona.quartz.nip30CustomEmoji.EmojiUrl import com.vitorpamplona.quartz.nip36SensitiveContent.ContentWarningSerializer import com.vitorpamplona.quartz.nip57Zaps.splits.ZapSplitSetup @@ -95,7 +96,7 @@ class ChatMessageEvent( isDraft: Boolean, onReady: (ChatMessageEvent) -> Unit, ) { - val tags = mutableListOf>() + val tags = TagArrayBuilder() to?.forEach { tags.add(arrayOf("p", it)) } replyTos?.forEach { tags.add(arrayOf("e", it, "", "reply")) } mentions?.forEach { tags.add(arrayOf("p", it, "", "mention")) } @@ -106,7 +107,7 @@ class ChatMessageEvent( tags.add(ContentWarningSerializer.toTagArray()) } geohash?.let { tags.addAll(geohashMipMap(it)) } - subject?.let { tags.add(SubjectTagSerializer.toTagArray(it)) } + subject?.let { tags.subject(subject) } imetas?.forEach { tags.add(Nip92MediaAttachments.createTag(it)) } @@ -114,9 +115,9 @@ class ChatMessageEvent( // tags.add(AltTagSerializer.toTagArray(ALT)) if (isDraft) { - signer.assembleRumor(createdAt, KIND, tags.toTypedArray(), msg, onReady) + signer.assembleRumor(createdAt, KIND, tags.build(), msg, onReady) } else { - signer.sign(createdAt, KIND, tags.toTypedArray(), msg, onReady) + signer.sign(createdAt, KIND, tags.build(), msg, onReady) } } }