fix(nests): align kind-4312 + kind-30312 wire format with nostrnests/EGG-07

After verifying the nostrnests reference (NestsUI-v2 @ main):
- ProfileCard.tsx writes kicks as ['action','kick'] tags with empty content
- useAdminCommands.ts reads action via tags.find(t => t==='action') and
  applies a 60-s relay since plus a processedRef Set to dedup re-deliveries
- p-tag role marker for moderators is 'admin', not 'moderator'

Amethyst was diverging on every one of those, which means our outbound
admin commands were invisible to nostrnests, theirs to us, and any
nostrnests admin (role='admin') failed our isModerator() / canSpeak()
gates entirely — kicks and force-mutes signed by them were silently
dropped.

Changes:

quartz/AdminCommandEvent.kt
  - Emit ['action', '<verb>'] tag with empty content
  - Reader prefers the tag, falls back to content for any in-flight
    Amethyst-built kick from before this commit
  - kick() and forceMute() share a common build() helper

quartz/ParticipantTag.kt
  - ROLE.MODERATOR.code = 'admin' (matches nostrnests + EGG-07)
  - Adds legacyCodes = ['moderator'] so older Amethyst-emitted
    kind-30312 events still parse as MODERATOR
  - effectiveRole() walks both code + legacyCodes

amethyst/AdminCommandsCollector
  - Filter carries since = now - 60 (EGG-07 #7)
  - Defensive per-event freshness re-check for cached events / clock skew
  - mutableSetOf<String>() processed-id dedup for the lifetime of the
    collector, mirroring useAdminCommands.ts's processedRef

EGG-07.md
  - Documents the Amethyst-only ['action','mute'] extension under a new
    'Implemented extensions' section. nostrnests doesn't emit or honour
    it today; cross-client force-mutes only work between Amethyst peers
  - 'warn' stays in the future-actions list — nostrnests has no plans
    for it either

Tests:
  - ParticipantTagTest: new asserts that 'admin' / 'Admin' / 'ADMIN'
    parse as ROLE.MODERATOR; pins the wire string to 'admin' and the
    legacy alias to 'moderator'
  - AdminCommandEventTest: kick/forceMute templates carry ['action', _]
    tag with empty content; legacy content-form still parses; tag wins
    over content when both are present
This commit is contained in:
Claude
2026-04-28 12:59:48 +00:00
parent d41a24f945
commit 138ee12a6a
6 changed files with 153 additions and 26 deletions
@@ -30,6 +30,7 @@ import com.vitorpamplona.quartz.nip25Reactions.ReactionEvent
import com.vitorpamplona.quartz.nip53LiveActivities.chat.LiveActivitiesChatMessageEvent
import com.vitorpamplona.quartz.nip53LiveActivities.meetingSpaces.MeetingSpaceEvent
import com.vitorpamplona.quartz.nip53LiveActivities.presence.MeetingRoomPresenceEvent
import com.vitorpamplona.quartz.utils.TimeUtils
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
@@ -167,13 +168,29 @@ private fun AdminCommandsCollector(
localPubkey: String,
) {
LaunchedEffect(viewModel, roomATag, localPubkey) {
// EGG-07 / nostrnests gate: ignore admin commands older than
// 60 s. The relay filter narrows the firehose; the per-cmd
// check below catches anything that slips through (cached
// events, system clock skew, etc.).
val sinceSec = TimeUtils.now() - ADMIN_COMMAND_FRESHNESS_SEC
val filter =
Filter(
kinds = listOf(AdminCommandEvent.KIND),
tags = mapOf("a" to listOf(roomATag), "p" to listOf(localPubkey)),
since = sinceSec,
)
// Replay protection per EGG-07 #7: a single kick / mute must
// act exactly once even when re-delivered from multiple
// relays. Lifetime-of-collector dedup mirrors how nostrnests'
// useAdminCommands.ts uses a `processedRef` set.
val processed = mutableSetOf<String>()
LocalCache.observeNewEvents<AdminCommandEvent>(filter).collect { cmd ->
if (cmd.targetPubkey() != localPubkey) return@collect
// Defensive freshness re-check: relay might have served a
// cached older event despite the `since` hint, or the
// user's clock might have jumped forward.
if (TimeUtils.now() - cmd.createdAt > ADMIN_COMMAND_FRESHNESS_SEC) return@collect
if (!processed.add(cmd.id)) return@collect
val signerIsAuthorised =
cmd.pubKey == event.pubKey ||
event.participants().any { it.pubKey == cmd.pubKey && (it.isHost() || it.isModerator()) }
@@ -187,6 +204,9 @@ private fun AdminCommandsCollector(
}
}
/** Spec window from EGG-07 #7 — 60-second freshness gate on kind-4312. */
private const val ADMIN_COMMAND_FRESHNESS_SEC: Long = 60L
private const val PRESENCE_EVICT_INTERVAL_MS = 60_000L
private const val PRESENCE_STALE_THRESHOLD_SEC = 6L * 60L
private const val REACTIONS_TICK_MS = 1_000L