- Finishes the transition to EventHint objects for building events.

- Removes the "a-tag" dependency on Addressable interface because the `a` can have different names in different objects.
This commit is contained in:
Vitor Pamplona
2026-02-18 09:53:03 -05:00
parent 46b17628ac
commit 2c3ca7f906
23 changed files with 114 additions and 159 deletions
@@ -28,7 +28,7 @@ import com.vitorpamplona.quartz.nip01Core.core.BaseAddressableEvent
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
import com.vitorpamplona.quartz.nip01Core.core.builder
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.hints.EventHintBundle
import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate
import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate
import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag
@@ -83,17 +83,16 @@ class InteractiveStoryReadingStateEvent(
fun update(
base: InteractiveStoryReadingStateEvent,
currentScene: InteractiveStoryBaseEvent,
currentSceneRelay: NormalizedRelayUrl?,
currentScene: EventHintBundle<InteractiveStoryBaseEvent>,
createdAt: Long = TimeUtils.now(),
): EventTemplate<InteractiveStoryReadingStateEvent> {
val rootTag = base.dTag()
val sceneTag = currentScene.aTag(currentSceneRelay)
val sceneTag = ATag(currentScene.event.address(), currentScene.relay)
val status =
if (rootTag == sceneTag.toTag()) {
ReadStatusTag.STATUS.NEW
} else if (currentScene.options().isEmpty()) {
} else if (currentScene.event.options().isEmpty()) {
ReadStatusTag.STATUS.DONE
} else {
ReadStatusTag.STATUS.READING
@@ -109,34 +108,32 @@ class InteractiveStoryReadingStateEvent(
}
fun build(
root: InteractiveStoryBaseEvent,
rootRelay: NormalizedRelayUrl?,
currentScene: InteractiveStoryBaseEvent,
currentSceneRelay: NormalizedRelayUrl?,
root: EventHintBundle<InteractiveStoryBaseEvent>,
currentScene: EventHintBundle<InteractiveStoryBaseEvent>,
createdAt: Long = TimeUtils.now(),
initializer: TagArrayBuilder<InteractiveStoryReadingStateEvent>.() -> Unit = {},
) = eventTemplate(KIND, "", createdAt) {
val rootTag = root.aTag(rootRelay)
val sceneTag = currentScene.aTag(currentSceneRelay)
val rootTag = ATag(root.event.address(), root.relay)
val sceneTag = ATag(currentScene.event.address(), currentScene.relay)
val status =
if (rootTag == sceneTag) {
ReadStatusTag.STATUS.NEW
} else if (currentScene.options().isEmpty()) {
} else if (currentScene.event.options().isEmpty()) {
ReadStatusTag.STATUS.DONE
} else {
ReadStatusTag.STATUS.READING
}
dTag(rootTag.toTag())
alt(root.title()?.let { ALT2 + it } ?: ALT1)
alt(root.event.title()?.let { ALT2 + it } ?: ALT1)
rootScene(rootTag)
currentScene(sceneTag)
status(status)
root.title()?.let { storyTitle(it) }
root.summary()?.let { storyImage(it) }
root.image()?.let { storySummary(it) }
root.event.title()?.let { storyTitle(it) }
root.event.summary()?.let { storyImage(it) }
root.event.image()?.let { storySummary(it) }
initializer()
}
@@ -32,7 +32,6 @@ import com.vitorpamplona.quartz.nip01Core.hints.AddressHintProvider
import com.vitorpamplona.quartz.nip01Core.hints.EventHintProvider
import com.vitorpamplona.quartz.nip01Core.hints.types.AddressHint
import com.vitorpamplona.quartz.nip01Core.hints.types.EventIdHint
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate
import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag
import com.vitorpamplona.quartz.nip01Core.tags.dTag.dTag
@@ -71,8 +70,6 @@ class NipTextEvent(
override fun dTag() = tags.dTag()
override fun aTag(relayHint: NormalizedRelayUrl?) = ATag(kind, pubKey, dTag(), relayHint)
override fun address() = Address(kind, pubKey, dTag())
override fun addressTag() = Address.assemble(kind, pubKey, dTag())
@@ -21,15 +21,11 @@
package com.vitorpamplona.quartz.nip01Core.core
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag
@Immutable
interface AddressableEvent : IEvent {
fun dTag(): String
fun aTag(relayHint: NormalizedRelayUrl? = null): ATag
fun address(): Address
fun addressTag(): String
@@ -21,8 +21,6 @@
package com.vitorpamplona.quartz.nip01Core.core
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag
import com.vitorpamplona.quartz.nip01Core.tags.dTag.dTag
@Immutable
@@ -38,8 +36,6 @@ open class BaseAddressableEvent(
AddressableEvent {
override fun dTag() = tags.dTag()
override fun aTag(relayHint: NormalizedRelayUrl?) = ATag(kind, pubKey, dTag(), relayHint)
override fun address() = Address(kind, pubKey, dTag())
/**
@@ -21,8 +21,6 @@
package com.vitorpamplona.quartz.nip01Core.core
import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag
@Immutable
open class BaseReplaceableEvent(
@@ -33,11 +31,10 @@ open class BaseReplaceableEvent(
tags: TagArray,
content: String,
sig: HexKey,
) : BaseAddressableEvent(id, pubKey, createdAt, kind, tags, content, sig) {
) : Event(id, pubKey, createdAt, kind, tags, content, sig),
AddressableEvent {
override fun dTag() = FIXED_D_TAG
override fun aTag(relayHint: NormalizedRelayUrl?) = ATag(kind, pubKey, FIXED_D_TAG, relayHint)
override fun address(): Address = Address(kind, pubKey, dTag())
/**
@@ -20,11 +20,20 @@
*/
package com.vitorpamplona.quartz.nip01Core.tags.aTag
import com.vitorpamplona.quartz.nip01Core.core.Address
import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
fun <T : Event> TagArrayBuilder<T>.aTag(tag: ATag) = add(tag.toATagArray())
fun <T : Event> TagArrayBuilder<T>.aTag(
address: Address,
relayHint: NormalizedRelayUrl? = null,
) = add(ATag.assemble(address, relayHint))
fun <T : Event> TagArrayBuilder<T>.aTags(tag: List<ATag>) = addAll(tag.map { it.toATagArray() })
fun <T : Event> TagArrayBuilder<T>.removeATag(tag: ATag) = this.removeIf(ATag::isSameAddress, tag.toATagArray())
fun <T : Event> TagArrayBuilder<T>.removeAddress(address: Address) = this.removeIf(ATag::isSameAddress, ATag.assemble(address, null))
@@ -89,7 +89,7 @@ class DeletionEvent(
deleteEvents.forEach {
eTag(ETag(it.id))
if (it is AddressableEvent) {
aTag(it.aTag())
aTag(it.address())
}
}
@@ -108,7 +108,7 @@ class DeletionEvent(
deleteEvents.forEach {
if (it is AddressableEvent) {
aTag(it.aTag())
aTag(it.address())
}
}
@@ -26,9 +26,9 @@ import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
import com.vitorpamplona.quartz.nip01Core.hints.AddressHintProvider
import com.vitorpamplona.quartz.nip01Core.hints.EventHintBundle
import com.vitorpamplona.quartz.nip01Core.hints.EventHintProvider
import com.vitorpamplona.quartz.nip01Core.hints.PubKeyHintProvider
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner
import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate
import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag
@@ -38,7 +38,6 @@ import com.vitorpamplona.quartz.nip01Core.tags.events.eTag
import com.vitorpamplona.quartz.nip01Core.tags.kinds.kind
import com.vitorpamplona.quartz.nip01Core.tags.people.PTag
import com.vitorpamplona.quartz.nip01Core.tags.people.pTag
import com.vitorpamplona.quartz.nip31Alts.AltTag
import com.vitorpamplona.quartz.nip31Alts.alt
import com.vitorpamplona.quartz.utils.TimeUtils
import com.vitorpamplona.quartz.utils.lastNotNullOfOrNull
@@ -93,45 +92,28 @@ class GenericRepostEvent(
const val ALT = "Generic repost"
fun build(
boostedPost: Event,
eventSourceRelay: NormalizedRelayUrl?,
authorHomeRelay: NormalizedRelayUrl?,
eventHint: EventHintBundle<Event>,
createdAt: Long = TimeUtils.now(),
initializer: TagArrayBuilder<GenericRepostEvent>.() -> Unit = {},
) = eventTemplate(KIND, boostedPost.toJson(), createdAt) {
) = eventTemplate(KIND, eventHint.event.toJson(), createdAt) {
val boostedPost = eventHint.event
alt(ALT)
kind(boostedPost.kind)
pTag(PTag(boostedPost.pubKey, authorHomeRelay))
eTag(ETag(boostedPost.id, eventSourceRelay, boostedPost.pubKey))
pTag(PTag(boostedPost.pubKey, eventHint.authorHomeRelay))
eTag(ETag(boostedPost.id, eventHint.relay, boostedPost.pubKey))
if (boostedPost is AddressableEvent) {
aTag(boostedPost.aTag(eventSourceRelay))
aTag(boostedPost.address(), eventHint.relay)
}
initializer()
}
suspend fun create(
boostedPost: Event,
eventHint: EventHintBundle<Event>,
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
): GenericRepostEvent {
val content = boostedPost.toJson()
val tags =
mutableListOf(
arrayOf("e", boostedPost.id),
arrayOf("p", boostedPost.pubKey),
)
if (boostedPost is AddressableEvent) {
tags.add(arrayOf("a", boostedPost.aTag().toTag()))
}
tags.add(arrayOf("k", "${boostedPost.kind}"))
tags.add(AltTag.assemble(ALT))
return signer.sign(createdAt, KIND, tags.toTypedArray(), content)
}
): GenericRepostEvent = signer.sign(build(eventHint, createdAt))
}
}
@@ -26,9 +26,10 @@ import com.vitorpamplona.quartz.nip01Core.core.Event
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
import com.vitorpamplona.quartz.nip01Core.hints.AddressHintProvider
import com.vitorpamplona.quartz.nip01Core.hints.EventHintBundle
import com.vitorpamplona.quartz.nip01Core.hints.EventHintProvider
import com.vitorpamplona.quartz.nip01Core.hints.PubKeyHintProvider
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.signers.NostrSigner
import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate
import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag
import com.vitorpamplona.quartz.nip01Core.tags.aTag.aTag
@@ -82,7 +83,7 @@ class RepostEvent(
fun containedPost() =
try {
fromJson(content)
} catch (e: Exception) {
} catch (_: Exception) {
null
}
@@ -91,22 +92,28 @@ class RepostEvent(
const val ALT = "Repost event"
fun build(
boostedPost: Event,
eventSourceRelay: NormalizedRelayUrl?,
authorHomeRelay: NormalizedRelayUrl?,
eventHint: EventHintBundle<Event>,
createdAt: Long = TimeUtils.now(),
initializer: TagArrayBuilder<RepostEvent>.() -> Unit = {},
) = eventTemplate(KIND, boostedPost.toJson(), createdAt) {
) = eventTemplate(KIND, eventHint.event.toJson(), createdAt) {
val boostedPost = eventHint.event
alt(ALT)
pTag(PTag(boostedPost.pubKey, authorHomeRelay))
eTag(ETag(boostedPost.id, eventSourceRelay, boostedPost.pubKey))
pTag(PTag(boostedPost.pubKey, eventHint.authorHomeRelay))
eTag(ETag(boostedPost.id, eventHint.relay, boostedPost.pubKey))
if (boostedPost is AddressableEvent) {
aTag(boostedPost.aTag(eventSourceRelay))
aTag(boostedPost.address(), eventHint.relay)
}
kind(boostedPost.kind)
initializer()
}
suspend fun create(
eventHint: EventHintBundle<Event>,
signer: NostrSigner,
createdAt: Long = TimeUtils.now(),
): RepostEvent = signer.sign(build(eventHint, createdAt))
}
}
@@ -31,9 +31,7 @@ import com.vitorpamplona.quartz.nip01Core.hints.PubKeyHintProvider
import com.vitorpamplona.quartz.nip01Core.hints.types.AddressHint
import com.vitorpamplona.quartz.nip01Core.hints.types.EventIdHint
import com.vitorpamplona.quartz.nip01Core.hints.types.PubKeyHint
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate
import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag
import com.vitorpamplona.quartz.nip01Core.tags.dTag.dTag
import com.vitorpamplona.quartz.nip01Core.tags.hashtags.hashtags
import com.vitorpamplona.quartz.nip01Core.tags.people.PTag
@@ -119,8 +117,6 @@ class LongTextNoteEvent(
override fun dTag() = tags.dTag()
override fun aTag(relayHint: NormalizedRelayUrl?) = ATag(kind, pubKey, dTag(), relayHint)
override fun address() = Address(kind, pubKey, dTag())
override fun addressTag() = Address.assemble(kind, pubKey, dTag())
@@ -91,7 +91,7 @@ class ReactionEvent(
) = eventTemplate<ReactionEvent>(KIND, reaction, createdAt) {
eTag(reactedTo.toETag())
if (reactedTo.event is AddressableEvent) {
aTag(reactedTo.event.aTag(reactedTo.relay))
aTag(reactedTo.event.address(), reactedTo.relay)
}
pTag(reactedTo.event.pubKey, reactedTo.relay)
kind(reactedTo.event.kind)
@@ -104,7 +104,7 @@ class ReactionEvent(
) = eventTemplate<ReactionEvent>(KIND, reaction.toContentEncode(), createdAt) {
eTag(reactedTo.toETag())
if (reactedTo.event is AddressableEvent) {
aTag(reactedTo.event.aTag(reactedTo.relay))
aTag(reactedTo.event.address(), reactedTo.relay)
}
pTag(reactedTo.event.pubKey, reactedTo.relay)
kind(reactedTo.event.kind)
@@ -78,7 +78,7 @@ class EmojiPackSelectionEvent(
createdAt: Long = TimeUtils.now(),
updater: TagArrayBuilder<EmojiPackSelectionEvent>.() -> Unit = {},
) = eventUpdate(currentSelection, createdAt) {
removePack(packToRemove.aTag(null))
removePack(packToRemove.address())
updater()
}
@@ -20,14 +20,15 @@
*/
package com.vitorpamplona.quartz.nip30CustomEmoji.selection
import com.vitorpamplona.quartz.nip01Core.core.Address
import com.vitorpamplona.quartz.nip01Core.core.TagArrayBuilder
import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag
import com.vitorpamplona.quartz.nip01Core.tags.aTag.aTag
import com.vitorpamplona.quartz.nip01Core.tags.aTag.aTags
import com.vitorpamplona.quartz.nip01Core.tags.aTag.removeATag
import com.vitorpamplona.quartz.nip01Core.tags.aTag.removeAddress
fun TagArrayBuilder<EmojiPackSelectionEvent>.pack(tag: ATag) = aTag(tag)
fun TagArrayBuilder<EmojiPackSelectionEvent>.packs(tags: List<ATag>) = aTags(tags)
fun TagArrayBuilder<EmojiPackSelectionEvent>.removePack(tag: ATag) = removeATag(tag)
fun TagArrayBuilder<EmojiPackSelectionEvent>.removePack(address: Address) = removeAddress(address)
@@ -34,7 +34,6 @@ import com.vitorpamplona.quartz.nip01Core.hints.PubKeyHintProvider
import com.vitorpamplona.quartz.nip01Core.hints.types.AddressHint
import com.vitorpamplona.quartz.nip01Core.hints.types.EventIdHint
import com.vitorpamplona.quartz.nip01Core.hints.types.PubKeyHint
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.signers.EventTemplate
import com.vitorpamplona.quartz.nip01Core.signers.eventTemplate
import com.vitorpamplona.quartz.nip01Core.tags.aTag.ATag
@@ -129,8 +128,6 @@ class WikiNoteEvent(
override fun dTag() = tags.dTag()
override fun aTag(relayHint: NormalizedRelayUrl?) = ATag(kind, pubKey, dTag(), relayHint)
override fun address() = Address(kind, pubKey, dTag())
override fun addressTag() = Address.assemble(kind, pubKey, dTag())
@@ -104,7 +104,7 @@ class LnZapRequestEvent(
AltTag.assemble(ALT),
)
if (zappedEvent is AddressableEvent) {
tags = tags + listOf(arrayOf("a", zappedEvent.aTag().toTag()))
tags = tags + listOf(ATag.assemble(zappedEvent.address(), null))
}
if (pollOption != null && pollOption >= 0) {
tags = tags + listOf(arrayOf(PollOptionTag.TAG_NAME, pollOption.toString()))