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:
parent
5fa3fcf60f
commit
d48cbbeec0
2 changed files with 105 additions and 0 deletions
|
|
@ -329,10 +329,70 @@ export function useNostrChat() {
|
|||
onevent(event) {
|
||||
console.log('Received live event:', event.id, 'author:', event.pubkey)
|
||||
handleIncomingMessage(event, peerPubkey)
|
||||
},
|
||||
oneose() {
|
||||
console.log('Subscription closed for peer:', peerPubkey)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
return sub
|
||||
}
|
||||
|
||||
// Subscribe to a peer for notifications only (without loading full message history)
|
||||
const subscribeToPeerForNotifications = async (peerPubkey: string) => {
|
||||
if (!currentUser.value) {
|
||||
console.warn('No user logged in - cannot subscribe to peer notifications')
|
||||
return null
|
||||
}
|
||||
|
||||
// Check if we have a pool and are connected
|
||||
if (!pool.value) {
|
||||
console.warn('No pool available - initializing...')
|
||||
initializePool()
|
||||
}
|
||||
|
||||
if (!isConnected.value) {
|
||||
console.warn('Not connected to relays - attempting to connect...')
|
||||
await connect()
|
||||
}
|
||||
|
||||
if (!pool.value) {
|
||||
throw new Error('Failed to initialize Nostr pool')
|
||||
}
|
||||
|
||||
const myPubkey = currentUser.value.pubkey
|
||||
|
||||
// Subscribe to new messages only (no historical messages)
|
||||
const relayConfigs = getRelays()
|
||||
console.log('Subscribing to notifications for peer:', peerPubkey, 'with my pubkey:', myPubkey)
|
||||
|
||||
const sub = pool.value.subscribeMany(
|
||||
relayConfigs.map(r => r.url),
|
||||
[
|
||||
{
|
||||
kinds: [4],
|
||||
authors: [peerPubkey],
|
||||
'#p': [myPubkey]
|
||||
},
|
||||
{
|
||||
kinds: [4],
|
||||
authors: [myPubkey],
|
||||
'#p': [peerPubkey]
|
||||
}
|
||||
],
|
||||
{
|
||||
onevent(event) {
|
||||
console.log('Received notification event:', event.id, 'author:', event.pubkey, 'for peer:', peerPubkey)
|
||||
handleIncomingMessage(event, peerPubkey)
|
||||
},
|
||||
oneose() {
|
||||
console.log('Notification subscription closed for peer:', peerPubkey)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
console.log('Successfully created notification subscription for peer:', peerPubkey)
|
||||
return sub
|
||||
}
|
||||
|
||||
|
|
@ -631,6 +691,7 @@ export function useNostrChat() {
|
|||
connect,
|
||||
disconnect,
|
||||
subscribeToPeer,
|
||||
subscribeToPeerForNotifications,
|
||||
sendMessage,
|
||||
getMessages,
|
||||
clearMessages,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue