diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/EventExt.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/EventExt.kt index a14d9296e..679879c91 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/EventExt.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/EventExt.kt @@ -20,19 +20,12 @@ */ package com.vitorpamplona.quartz.nip01Core.tags.references -import com.vitorpamplona.quartz.nip01Core.HexKey import com.vitorpamplona.quartz.nip01Core.core.Event -fun Event.forEachReferenceTag(onEach: (eventId: HexKey) -> Unit) = tags.forEachReference(onEach) - -fun Event.anyReferenceTag(onEach: (str: String) -> Boolean) = tags.anyReference(onEach) - fun Event.hasReferenceTag() = tags.hasReferences() fun Event.references() = tags.references() -fun Event.isTaggedReference(hashtag: String) = tags.isTaggedReference(hashtag) +fun Event.isTaggedReference(reference: String) = tags.isTaggedReference(reference) -fun Event.isTaggedReferences(hashtags: Set) = tags.isTaggedReferences(hashtags) - -fun Event.firstIsTaggedReferences(hashtags: Set) = tags.firstIsTaggedReferences(hashtags) +fun Event.isTaggedReferences(references: Set) = tags.isTaggedReferences(references) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/ReferenceTag.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/ReferenceTag.kt index a60167807..8c07af9ff 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/ReferenceTag.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/ReferenceTag.kt @@ -27,14 +27,34 @@ class ReferenceTag { const val TAG_NAME = "r" const val TAG_SIZE = 2 + @JvmStatic + fun isTagged( + tag: Array, + reference: String, + ): Boolean = tag.size >= 2 && tag[0] == TAG_NAME && tag[1] == reference + + @JvmStatic + fun isIn( + tag: Array, + references: Set, + ): Boolean = tag.size >= 2 && tag[0] == TAG_NAME && tag[1] in references + + @JvmStatic + fun hasReference(tag: Array): Boolean { + if (tag.size < TAG_SIZE || tag[0] != TAG_NAME) return false + return tag[1].isNotEmpty() + } + @JvmStatic fun parse(tag: Array): String? { if (tag.size < TAG_SIZE || tag[0] != TAG_NAME) return null return tag[1] } + @JvmStatic fun assemble(url: String) = arrayOf(TAG_NAME, HttpUrlFormatter.normalize(url)) + @JvmStatic fun assemble(urls: List): List> = urls.mapTo(HashSet()) { HttpUrlFormatter.normalize(it) }.map { arrayOf(TAG_NAME, it) } } } diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/TagArrayExt.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/TagArrayExt.kt index c7c74d35a..4a3fb5427 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/TagArrayExt.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/tags/references/TagArrayExt.kt @@ -20,26 +20,13 @@ */ package com.vitorpamplona.quartz.nip01Core.tags.references -import com.vitorpamplona.quartz.nip01Core.HexKey import com.vitorpamplona.quartz.nip01Core.core.TagArray -import com.vitorpamplona.quartz.nip01Core.core.anyTagged -import com.vitorpamplona.quartz.nip01Core.core.firstAnyLowercaseTaggedValue -import com.vitorpamplona.quartz.nip01Core.core.forEachTagged -import com.vitorpamplona.quartz.nip01Core.core.hasTagWithContent -import com.vitorpamplona.quartz.nip01Core.core.isAnyLowercaseTagged -import com.vitorpamplona.quartz.nip01Core.core.isTagged -import com.vitorpamplona.quartz.nip01Core.core.mapValues +import com.vitorpamplona.quartz.nip01Core.core.any -fun TagArray.forEachReference(onEach: (eventId: HexKey) -> Unit) = this.forEachTagged(ReferenceTag.TAG_NAME, onEach) +fun TagArray.hasReferences() = this.any(ReferenceTag::hasReference) -fun TagArray.anyReference(onEach: (str: String) -> Boolean) = this.anyTagged(ReferenceTag.TAG_NAME, onEach) +fun TagArray.references() = this.mapNotNull(ReferenceTag::parse) -fun TagArray.hasReferences() = this.hasTagWithContent(ReferenceTag.TAG_NAME) +fun TagArray.isTaggedReference(reference: String) = this.any(ReferenceTag::isTagged, reference) -fun TagArray.references() = this.mapValues(ReferenceTag.TAG_NAME) - -fun TagArray.isTaggedReference(hashtag: String) = this.isTagged(ReferenceTag.TAG_NAME, hashtag, true) - -fun TagArray.isTaggedReferences(hashtags: Set) = this.isAnyLowercaseTagged(ReferenceTag.TAG_NAME, hashtags) - -fun TagArray.firstIsTaggedReferences(hashtags: Set) = this.firstAnyLowercaseTaggedValue(ReferenceTag.TAG_NAME, hashtags) +fun TagArray.isTaggedReferences(references: Set) = this.any(ReferenceTag::isIn, references)