Updates NIP-14 subjects to the new style of extension functions

This commit is contained in:
Vitor Pamplona 2025-01-15 09:57:06 -05:00
parent 4a4f03c12b
commit 4b52cdb101
6 changed files with 99 additions and 16 deletions

View File

@ -23,8 +23,19 @@ package com.vitorpamplona.quartz.nip01Core.core
class TagArrayBuilder {
val tagList = mutableListOf<Array<String>>()
fun add(tag: Array<String>) {
fun add(tag: Array<String>): TagArrayBuilder {
tagList.add(tag)
return this
}
fun addAll(tag: List<Array<String>>): TagArrayBuilder {
tagList.addAll(tag)
return this
}
fun addAll(tag: Array<Array<String>>): TagArrayBuilder {
tagList.addAll(tag)
return this
}
fun build() = tagList.toTypedArray()

View File

@ -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()

View File

@ -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<String>): SubjectTag {
require(tags[0] == TAG_NAME)
return SubjectTag(tags[1])
}
@JvmStatic
fun assemble(subject: String) = arrayOf(TAG_NAME, subject)
}
}

View File

@ -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))

View File

@ -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)

View File

@ -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<Array<String>>()
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)
}
}
}