init to refresh messages
This commit is contained in:
parent
231658b980
commit
c7aa88bec6
1 changed files with 90 additions and 58 deletions
|
|
@ -99,6 +99,12 @@ export const useNostrStore = defineStore('nostr', () => {
|
||||||
}
|
}
|
||||||
}, { deep: true })
|
}, { deep: true })
|
||||||
|
|
||||||
|
// Initialize store if account exists in localStorage
|
||||||
|
if (account.value) {
|
||||||
|
console.log('Found existing account, initializing connection...')
|
||||||
|
init()
|
||||||
|
}
|
||||||
|
|
||||||
// Computed
|
// Computed
|
||||||
const isLoggedIn = computed(() => !!account.value)
|
const isLoggedIn = computed(() => !!account.value)
|
||||||
const currentMessages = computed(() =>
|
const currentMessages = computed(() =>
|
||||||
|
|
@ -252,7 +258,12 @@ export const useNostrStore = defineStore('nostr', () => {
|
||||||
since: 0 // Get all historical messages
|
since: 0 // Get all historical messages
|
||||||
}
|
}
|
||||||
|
|
||||||
relayPool.value.forEach(relay => {
|
const subscribeToRelay = (relay: any) => {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
let receivedCount = 0
|
||||||
|
let sentCount = 0
|
||||||
|
let eoseCount = 0
|
||||||
|
|
||||||
// Subscribe to received messages
|
// Subscribe to received messages
|
||||||
const receivedSub = relay.sub([receivedFilter])
|
const receivedSub = relay.sub([receivedFilter])
|
||||||
|
|
||||||
|
|
@ -263,6 +274,7 @@ export const useNostrStore = defineStore('nostr', () => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
receivedCount++
|
||||||
const decrypted = await window.NostrTools.nip04.decrypt(
|
const decrypted = await window.NostrTools.nip04.decrypt(
|
||||||
account.value!.privkey,
|
account.value!.privkey,
|
||||||
event.pubkey,
|
event.pubkey,
|
||||||
|
|
@ -298,6 +310,7 @@ export const useNostrStore = defineStore('nostr', () => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sentCount++
|
||||||
const decrypted = await window.NostrTools.nip04.decrypt(
|
const decrypted = await window.NostrTools.nip04.decrypt(
|
||||||
account.value!.privkey,
|
account.value!.privkey,
|
||||||
SUPPORT_NPUB,
|
SUPPORT_NPUB,
|
||||||
|
|
@ -317,7 +330,26 @@ export const useNostrStore = defineStore('nostr', () => {
|
||||||
console.error('Failed to decrypt sent message:', err)
|
console.error('Failed to decrypt sent message:', err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Listen for end of stored events
|
||||||
|
receivedSub.on('eose', () => {
|
||||||
|
eoseCount++
|
||||||
|
if (eoseCount >= 2) { // Both subscriptions have finished
|
||||||
|
resolve(true)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
sentSub.on('eose', () => {
|
||||||
|
eoseCount++
|
||||||
|
if (eoseCount >= 2) { // Both subscriptions have finished
|
||||||
|
resolve(true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for all relays to load their historical messages
|
||||||
|
await Promise.all(relayPool.value.map(relay => subscribeToRelay(relay)))
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue