announce and read presence

This commit is contained in:
callebtc
2026-01-12 14:12:49 +07:00
parent aff700a15e
commit 0f5299a0f5
6 changed files with 326 additions and 6 deletions
@@ -56,7 +56,8 @@ extension ChatViewModel {
}
func subscribeNostrEvent(_ event: NostrEvent) {
guard event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue,
guard (event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue ||
event.kind == NostrProtocol.EventKind.geohashPresence.rawValue),
!deduplicationService.hasProcessedNostrEvent(event.id)
else {
return
@@ -86,6 +87,11 @@ extension ChatViewModel {
// Update participants last-seen for this pubkey
participantTracker.recordParticipant(pubkeyHex: event.pubkey)
// If presence heartbeat (Kind 20001), stop here - no content to display
if event.kind == NostrProtocol.EventKind.geohashPresence.rawValue {
return
}
// Track teleported tag (only our format ["t","teleport"]) for icon state
let hasTeleportTag = event.tags.contains(where: { tag in
tag.count >= 2 && tag[0].lowercased() == "t" && tag[1].lowercased() == "teleport"
@@ -239,8 +245,9 @@ extension ChatViewModel {
}
func handleNostrEvent(_ event: NostrEvent) {
// Only handle ephemeral kind 20000 with matching tag
guard event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue else { return }
// Only handle ephemeral kind 20000 or presence kind 20001 with matching tag
guard (event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue ||
event.kind == NostrProtocol.EventKind.geohashPresence.rawValue) else { return }
// Deduplicate
if deduplicationService.hasProcessedNostrEvent(event.id) { return }
@@ -299,6 +306,11 @@ extension ChatViewModel {
// Update participants last-seen for this pubkey
participantTracker.recordParticipant(pubkeyHex: event.pubkey)
// If presence heartbeat (Kind 20001), stop here - no content to display
if event.kind == NostrProtocol.EventKind.geohashPresence.rawValue {
return
}
let senderName = displayNameForNostrPubkey(event.pubkey)
let content = event.content
@@ -464,7 +476,8 @@ extension ChatViewModel {
}
func subscribeNostrEvent(_ event: NostrEvent, gh: String) {
guard event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue else { return }
guard (event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue ||
event.kind == NostrProtocol.EventKind.geohashPresence.rawValue) else { return }
// Compute current participant count (5-minute window) BEFORE updating with this event
let existingCount = participantTracker.participantCount(for: gh)