diff --git a/src/components/nostr/ChatComponent.vue b/src/components/nostr/ChatComponent.vue index e054709..78b7bc6 100644 --- a/src/components/nostr/ChatComponent.vue +++ b/src/components/nostr/ChatComponent.vue @@ -538,16 +538,30 @@ const subscribeToAllPeers = async () => { } console.log(`Subscribing to ${peers.value.length} peers for notifications`) + console.log('Peers to subscribe to:', peers.value.map(p => ({ pubkey: p.pubkey, username: p.username }))) + + let successCount = 0 + let errorCount = 0 for (const peer of peers.value) { try { + console.log(`Attempting to subscribe to peer: ${peer.pubkey} (${peer.username})`) // 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}`) + const subscription = await subscribeToPeerForNotifications(peer.pubkey) + if (subscription) { + console.log(`Successfully subscribed to notifications for peer: ${peer.pubkey}`) + successCount++ + } else { + console.warn(`Failed to create subscription for peer: ${peer.pubkey}`) + errorCount++ + } } catch (error) { console.warn(`Failed to subscribe to peer ${peer.pubkey}:`, error) + errorCount++ } } + + console.log(`Subscription summary: ${successCount} successful, ${errorCount} failed`) } const refreshPeers = () => { @@ -646,8 +660,22 @@ onMounted(async () => { } } + console.log('Starting connection and peer loading...') await connect() + console.log('Connection established, loading peers...') await loadPeers() + console.log('Peers loaded, checking if we should subscribe...') + + // If we're connected and have peers, subscribe to them + if (isConnected.value && peers.value.length > 0) { + console.log('Connection and peers ready, subscribing to all peers for notifications') + await subscribeToAllPeers() + } else { + console.log('Not ready to subscribe yet:', { + isConnected: isConnected.value, + peerCount: peers.value.length + }) + } }) onUnmounted(() => { @@ -656,9 +684,10 @@ onUnmounted(() => { }) // 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') +watch(isConnected, async (connected, prevConnected) => { + console.log('Connection state changed:', { connected, prevConnected, peerCount: peers.value.length }) + if (connected && peers.value.length > 0 && !prevConnected) { + console.log('Connection established and peers available, subscribing to peers for notifications') await subscribeToAllPeers() } }) diff --git a/src/composables/useNostrChat.ts b/src/composables/useNostrChat.ts index 23d677f..718fe9a 100644 --- a/src/composables/useNostrChat.ts +++ b/src/composables/useNostrChat.ts @@ -341,6 +341,9 @@ export function useNostrChat() { // Subscribe to a peer for notifications only (without loading full message history) const subscribeToPeerForNotifications = async (peerPubkey: string) => { + console.log('=== SUBSCRIBE TO PEER FOR NOTIFICATIONS START ===') + console.log('Peer pubkey:', peerPubkey) + if (!currentUser.value) { console.warn('No user logged in - cannot subscribe to peer notifications') return null @@ -362,28 +365,40 @@ export function useNostrChat() { } const myPubkey = currentUser.value.pubkey + console.log('My pubkey:', myPubkey) // Subscribe to new messages only (no historical messages) const relayConfigs = getRelays() console.log('Subscribing to notifications for peer:', peerPubkey, 'with my pubkey:', myPubkey) + console.log('Using relays:', relayConfigs.map(r => r.url)) + + const filters = [ + { + kinds: [4], + authors: [peerPubkey], + '#p': [myPubkey] + }, + { + kinds: [4], + authors: [myPubkey], + '#p': [peerPubkey] + } + ] + + console.log('Notification subscription filters:', JSON.stringify(filters, null, 2)) const sub = pool.value.subscribeMany( relayConfigs.map(r => r.url), - [ - { - kinds: [4], - authors: [peerPubkey], - '#p': [myPubkey] - }, - { - kinds: [4], - authors: [myPubkey], - '#p': [peerPubkey] - } - ], + filters, { onevent(event) { - console.log('Received notification event:', event.id, 'author:', event.pubkey, 'for peer:', peerPubkey) + console.log('Received notification event:', { + id: event.id, + author: event.pubkey, + forPeer: peerPubkey, + tags: event.tags, + contentLength: event.content?.length || 0 + }) handleIncomingMessage(event, peerPubkey) }, oneose() { @@ -393,6 +408,7 @@ export function useNostrChat() { ) console.log('Successfully created notification subscription for peer:', peerPubkey) + console.log('=== SUBSCRIBE TO PEER FOR NOTIFICATIONS END ===') return sub } @@ -446,11 +462,16 @@ export function useNostrChat() { // Handle incoming message const handleIncomingMessage = async (event: any, peerPubkey: string) => { + console.log('=== HANDLE INCOMING MESSAGE START ===') + console.log('Event ID:', event.id, 'Peer:', peerPubkey) + if (processedMessageIds.value.has(event.id)) { + console.log('Message already processed, skipping:', event.id) return } processedMessageIds.value.add(event.id) + console.log('Added to processed messages:', event.id) console.log('Handling incoming message:', { eventId: event.id,