fix: processing gift wraps and notifications

This commit is contained in:
enes 2024-09-06 18:01:49 +02:00
parent 7b64bc523a
commit c09837ea20

View File

@ -341,12 +341,6 @@ export const createWrap = (unsignedEvent: UnsignedEvent, receiver: string) => {
// Check if the leading zero bits meet the required difficulty // Check if the leading zero bits meet the required difficulty
if (leadingZeroes >= difficulty) { if (leadingZeroes >= difficulty) {
// Finalize the event (sign it) and return the result // Finalize the event (sign it) and return the result
console.log(
'event OUT: ',
unsignedEvent,
' for this user ',
hexToNpub(receiver)
)
return finalizeEvent(event, randomKey) return finalizeEvent(event, randomKey)
} }
@ -532,18 +526,13 @@ export const updateUsersAppData = async (meta: Meta) => {
if (existingMeta.modifiedAt < meta.modifiedAt) { if (existingMeta.modifiedAt < meta.modifiedAt) {
sigits[id] = meta sigits[id] = meta
isUpdated = true isUpdated = true
} else {
console.log('event NEWER EXISTS', existingMeta, meta)
} }
} else { } else {
sigits[id] = meta sigits[id] = meta
isUpdated = true isUpdated = true
} }
if (!isUpdated) { if (!isUpdated) return null
console.log('event NOT UPDATED', sigits[id], meta)
return null
}
const blossomUrls = [...appData.blossomUrls] const blossomUrls = [...appData.blossomUrls]
@ -870,9 +859,16 @@ export const subscribeForSigits = async (pubkey: string) => {
'#p': [pubkey] '#p': [pubkey]
} }
relayController.subscribeForEvents(filter, relaySet.read, (event) => { // Process the received event synchronously
processReceivedEvent(event) // Process the received event const events = await relayController.fetchEvents(filter, relaySet.read)
}) for (const e of events) {
await processReceivedEvent(e)
}
// Async processing of the events has a race condition
// relayController.subscribeForEvents(filter, relaySet.read, (event) => {
// processReceivedEvent(event)
// })
} }
const processReceivedEvent = async (event: Event, difficulty: number = 5) => { const processReceivedEvent = async (event: Event, difficulty: number = 5) => {
@ -880,13 +876,13 @@ const processReceivedEvent = async (event: Event, difficulty: number = 5) => {
.processedGiftWraps .processedGiftWraps
if (processedEvents.includes(event.id)) return if (processedEvents.includes(event.id)) return
// Consider the gift wrap as processed immediately
store.dispatch(updateProcessedGiftWraps([...processedEvents, event.id]))
// validate PoW // validate PoW
// Count the number of leading zero bits in the hash // Count the number of leading zero bits in the hash
const leadingZeroes = countLeadingZeroes(event.id) const leadingZeroes = countLeadingZeroes(event.id)
if (leadingZeroes < difficulty) { if (leadingZeroes < difficulty) return
console.log('processReceivedEvent leading zeroes error')
return
}
// decrypt the content of gift wrap event // decrypt the content of gift wrap event
const nostrController = NostrController.getInstance() const nostrController = NostrController.getInstance()
@ -904,21 +900,8 @@ const processReceivedEvent = async (event: Event, difficulty: number = 5) => {
} }
) )
if (!internalUnsignedEvent || internalUnsignedEvent.kind !== 938) { if (!internalUnsignedEvent || internalUnsignedEvent.kind !== 938) return
console.log(
'processReceivedEvent missing internal unsigned event, or internal event not sigit kind'
)
return
}
console.log(
'event IN: ',
new Date(internalUnsignedEvent.created_at * 1000),
internalUnsignedEvent.kind,
internalUnsignedEvent.pubkey,
hexToNpub(internalUnsignedEvent.pubkey),
internalUnsignedEvent.content
)
const meta = await parseJson<Meta>(internalUnsignedEvent.content).catch( const meta = await parseJson<Meta>(internalUnsignedEvent.content).catch(
(err) => { (err) => {
console.log( console.log(
@ -932,7 +915,6 @@ const processReceivedEvent = async (event: Event, difficulty: number = 5) => {
if (!meta) return if (!meta) return
await updateUsersAppData(meta) await updateUsersAppData(meta)
store.dispatch(updateProcessedGiftWraps([...processedEvents, event.id]))
} }
/** /**