feat: Add peer notification subscriptions in Nostr chat

- Implement a new method to subscribe to peers for notifications without loading full message history.
- Enhance the ChatComponent to automatically subscribe to peers when the connection is established.
- Update the useNostrChat composable to include the new subscription method and handle notification events.
- Improve logging for subscription status and errors to aid in debugging.
This commit is contained in:
padreug 2025-08-08 21:59:25 +02:00
parent 5fa3fcf60f
commit d48cbbeec0
2 changed files with 105 additions and 0 deletions

View file

@ -450,6 +450,7 @@ const {
connect,
disconnect,
subscribeToPeer,
subscribeToPeerForNotifications,
sendMessage: sendNostrMessage,
onMessageAdded,
markMessagesAsRead,
@ -502,6 +503,9 @@ const loadPeers = async () => {
}))
console.log(`Loaded ${peers.value.length} peers`)
// Note: Subscriptions will be handled by the isConnected watcher
} catch (parseError) {
console.error('JSON Parse Error for peers:', parseError)
console.error('Response was:', responseText)
@ -514,6 +518,38 @@ const loadPeers = async () => {
}
}
// Subscribe to all peers for notifications (without loading full message history)
const subscribeToAllPeers = async () => {
if (!peers.value.length) {
console.log('No peers to subscribe to')
return
}
// Wait for connection to be established
if (!isConnected.value) {
console.log('Waiting for connection to be established before subscribing to peers')
// Wait a bit for connection to establish
await new Promise(resolve => setTimeout(resolve, 1000))
if (!isConnected.value) {
console.warn('Still not connected, skipping peer subscriptions')
return
}
}
console.log(`Subscribing to ${peers.value.length} peers for notifications`)
for (const peer of peers.value) {
try {
// Subscribe to peer for notifications only (don't load full history)
await subscribeToPeerForNotifications(peer.pubkey)
console.log(`Successfully subscribed to notifications for peer: ${peer.pubkey}`)
} catch (error) {
console.warn(`Failed to subscribe to peer ${peer.pubkey}:`, error)
}
}
}
const refreshPeers = () => {
loadPeers()
}
@ -619,6 +655,14 @@ onUnmounted(() => {
disconnect()
})
// Watch for connection state changes and subscribe to peers when connected
watch(isConnected, async (connected) => {
if (connected && peers.value.length > 0) {
console.log('Connection established, subscribing to peers for notifications')
await subscribeToAllPeers()
}
})
// Watch for new messages and scroll to bottom
watch(currentMessages, (newMessages, oldMessages) => {
console.log('Messages changed:', {