Fixes r-tag migration

This commit is contained in:
Vitor Pamplona 2025-02-18 12:00:22 -05:00
parent 2de4bb1adc
commit 4fcf962c71
3 changed files with 27 additions and 27 deletions

View File

@ -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<String>) = tags.isTaggedReferences(hashtags)
fun Event.firstIsTaggedReferences(hashtags: Set<String>) = tags.firstIsTaggedReferences(hashtags)
fun Event.isTaggedReferences(references: Set<String>) = tags.isTaggedReferences(references)

View File

@ -27,14 +27,34 @@ class ReferenceTag {
const val TAG_NAME = "r"
const val TAG_SIZE = 2
@JvmStatic
fun isTagged(
tag: Array<String>,
reference: String,
): Boolean = tag.size >= 2 && tag[0] == TAG_NAME && tag[1] == reference
@JvmStatic
fun isIn(
tag: Array<String>,
references: Set<String>,
): Boolean = tag.size >= 2 && tag[0] == TAG_NAME && tag[1] in references
@JvmStatic
fun hasReference(tag: Array<String>): Boolean {
if (tag.size < TAG_SIZE || tag[0] != TAG_NAME) return false
return tag[1].isNotEmpty()
}
@JvmStatic
fun parse(tag: Array<String>): 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<String>): List<Array<String>> = urls.mapTo(HashSet()) { HttpUrlFormatter.normalize(it) }.map { arrayOf(TAG_NAME, it) }
}
}

View File

@ -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<String>) = this.isAnyLowercaseTagged(ReferenceTag.TAG_NAME, hashtags)
fun TagArray.firstIsTaggedReferences(hashtags: Set<String>) = this.firstAnyLowercaseTaggedValue(ReferenceTag.TAG_NAME, hashtags)
fun TagArray.isTaggedReferences(references: Set<String>) = this.any(ReferenceTag::isIn, references)