mirror of
https://github.com/vitorpamplona/Nostryfied.git
synced 2025-05-23 18:42:03 +00:00
Avoids downloading all kind 3s that are not authored by the user.
This commit is contained in:
parent
9242781670
commit
955566e303
@ -34,7 +34,11 @@ const fetchAndBroadcast = async () => {
|
|||||||
|
|
||||||
// get all events from relays
|
// get all events from relays
|
||||||
const filters =[{ authors: [pubkey] }, { "#p": [pubkey] }]
|
const filters =[{ authors: [pubkey] }, { "#p": [pubkey] }]
|
||||||
const data = await getEvents(filters)
|
const data = await getEvents(filters, pubkey)
|
||||||
|
|
||||||
|
const kind3 = data.filter((it) => it.kind == 3 && it.pubkey === pubkey).sort((a, b) => b.created_at - a.created_at)[0]
|
||||||
|
const myRelaySet = JSON.parse(kind3.content)
|
||||||
|
relays = Object.keys(myRelaySet).filter(url => myRelaySet[url].write).map(url => url)
|
||||||
|
|
||||||
$('#checking-relays-header-box').css('display', 'none')
|
$('#checking-relays-header-box').css('display', 'none')
|
||||||
$('#checking-relays-box').css('display', 'none')
|
$('#checking-relays-box').css('display', 'none')
|
||||||
@ -53,12 +57,10 @@ const fetchAndBroadcast = async () => {
|
|||||||
|
|
||||||
$('#checking-relays-header-box').css('display', 'flex')
|
$('#checking-relays-header-box').css('display', 'flex')
|
||||||
$('#checking-relays-box').css('display', 'flex')
|
$('#checking-relays-box').css('display', 'flex')
|
||||||
$('#checking-relays-header').text("Waiting for Relays:")
|
$('#checking-relays-header').text("Broadcasting to Relays:")
|
||||||
|
|
||||||
await broadcastEvents(data)
|
await broadcastEvents(data)
|
||||||
|
|
||||||
$('#checking-relays-header-box').css('display', 'none')
|
|
||||||
$('#checking-relays-box').css('display', 'none')
|
|
||||||
// inform user that broadcasting is done
|
// inform user that broadcasting is done
|
||||||
$('#broadcasting-status').html(txt.broadcasting + checkMark)
|
$('#broadcasting-status').html(txt.broadcasting + checkMark)
|
||||||
$('#broadcasting-progress').val(relays.length)
|
$('#broadcasting-progress').val(relays.length)
|
||||||
|
@ -60,7 +60,7 @@ function hexToBytes(hex) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// fetch events from relay, returns a promise
|
// fetch events from relay, returns a promise
|
||||||
const fetchFromRelay = async (relay, filters, events, relayStatus) =>
|
const fetchFromRelay = async (relay, filters, pubkey, events, relayStatus) =>
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
relayStatus[relay] = "Starting"
|
relayStatus[relay] = "Starting"
|
||||||
@ -101,6 +101,13 @@ function hexToBytes(hex) {
|
|||||||
}, 5_000)
|
}, 5_000)
|
||||||
|
|
||||||
const { id } = data
|
const { id } = data
|
||||||
|
|
||||||
|
// don't save/reboradcast kind 3s that are not from the author.
|
||||||
|
// their are too big.
|
||||||
|
if (data.kind == 3 && data.pubkey != pubkey) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// prevent duplicated events
|
// prevent duplicated events
|
||||||
if (events[id]) return
|
if (events[id]) return
|
||||||
else events[id] = data
|
else events[id] = data
|
||||||
@ -140,7 +147,7 @@ function hexToBytes(hex) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// query relays for events published by this pubkey
|
// query relays for events published by this pubkey
|
||||||
const getEvents = async (filters) => {
|
const getEvents = async (filters, pubkey) => {
|
||||||
// events hash
|
// events hash
|
||||||
const events = {}
|
const events = {}
|
||||||
|
|
||||||
@ -150,7 +157,7 @@ function hexToBytes(hex) {
|
|||||||
let relaysForThisRound = fetchFunctions.splice(0, 10)
|
let relaysForThisRound = fetchFunctions.splice(0, 10)
|
||||||
let relayStatus = {}
|
let relayStatus = {}
|
||||||
$('#fetching-progress').val(relays.length - fetchFunctions.length)
|
$('#fetching-progress').val(relays.length - fetchFunctions.length)
|
||||||
await Promise.allSettled( relaysForThisRound.map((relay) => fetchFromRelay(relay, filters, events, relayStatus)) )
|
await Promise.allSettled( relaysForThisRound.map((relay) => fetchFromRelay(relay, filters, pubkey, events, relayStatus)) )
|
||||||
}
|
}
|
||||||
updateRelayStatus({})
|
updateRelayStatus({})
|
||||||
|
|
||||||
@ -218,12 +225,12 @@ function hexToBytes(hex) {
|
|||||||
const broadcastEvents = async (data) => {
|
const broadcastEvents = async (data) => {
|
||||||
// batch processing of 10 relays
|
// batch processing of 10 relays
|
||||||
let broadcastFunctions = [...relays]
|
let broadcastFunctions = [...relays]
|
||||||
|
let relayStatus = {}
|
||||||
while (broadcastFunctions.length) {
|
while (broadcastFunctions.length) {
|
||||||
let relaysForThisRound = broadcastFunctions.splice(0, 10)
|
let relaysForThisRound = broadcastFunctions.splice(0, 10)
|
||||||
let relayStatus = {}
|
|
||||||
$('#broadcasting-progress').val(relays.length - broadcastFunctions.length)
|
$('#broadcasting-progress').val(relays.length - broadcastFunctions.length)
|
||||||
await Promise.allSettled( relaysForThisRound.map((relay) => sendToRelay(relay, data, relayStatus)) )
|
await Promise.allSettled( relaysForThisRound.map((relay) => sendToRelay(relay, data, relayStatus)) )
|
||||||
}
|
}
|
||||||
|
|
||||||
updateRelayStatus({})
|
updateRelayStatus(relayStatus)
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user