Nostryfied/js/nostr-broadcast.js

84 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-02-19 20:20:17 +05:30
// button click handler
const fetchAndBroadcast = async () => {
// reset UI
$('#fetching-status').html('')
$('#fetching-progress').css('visibility', 'hidden')
$('#fetching-progress').val(0)
$('#file-download').html('')
$('#events-found').text('')
$('#broadcasting-status').html('')
$('#broadcasting-progress').css('visibility', 'hidden')
$('#broadcasting-progress').val(0)
// messages to show to user
const checkMark = '✓'
const txt = {
broadcasting: 'Broadcasting to relays... ',
fetching: 'Fetching from relays... ',
download: `Downloading Backup file... ${checkMark}`,
}
$('#checking-relays-header').text("Waiting for Relays: ")
2023-02-19 20:20:17 +05:30
// parse pubkey ('npub' or hexa)
const pubkey = parsePubkey($('#pubkey').val())
if (!pubkey) return
// disable button (will be re-enable at the end of the process)
$('#fetch-and-broadcast').prop('disabled', true)
// inform user that app is fetching from relays
$('#fetching-status').text(txt.fetching)
// show and update fetching progress bar
$('#fetching-progress').css('visibility', 'visible')
$('#fetching-progress').prop('max', relays.length)
$('#checking-relays-header-box').css('display', 'flex')
$('#checking-relays-box').css('display', 'flex')
$('#checking-relays-header').text("Waiting for Relays:")
2023-02-19 20:20:17 +05:30
// get all events from relays
const filters =[{ authors: [pubkey] }, { "#p": [pubkey] }]
2023-08-03 16:48:22 -04:00
const data = (await getEvents(filters, pubkey)).sort((a, b) => b.created_at - a.created_at)
const latestKind3 = data.filter((it) => it.kind == 3 && it.pubkey === pubkey)[0]
const myRelaySet = JSON.parse(latestKind3.content)
relays = Object.keys(myRelaySet).filter(url => myRelaySet[url].write).map(url => url)
$('#checking-relays-header-box').css('display', 'none')
$('#checking-relays-box').css('display', 'none')
2023-02-19 20:20:17 +05:30
// inform user fetching is done
$('#fetching-status').html(txt.fetching + checkMark)
$('#fetching-progress').val(relays.length)
2023-02-19 20:20:17 +05:30
// inform user that backup file (js format) is being downloaded
$('#file-download').html(txt.download)
2023-02-24 02:09:51 +05:30
downloadFile(data, 'nostr-backup.js')
2023-02-19 20:20:17 +05:30
// inform user that app is broadcasting events to relays
$('#broadcasting-status').html(txt.broadcasting)
// show and update broadcasting progress bar
$('#broadcasting-progress').css('visibility', 'visible')
$('#broadcasting-progress').prop('max', relays.length)
$('#checking-relays-header-box').css('display', 'flex')
$('#checking-relays-box').css('display', 'flex')
$('#checking-relays-header').text("Broadcasting to Relays:")
2023-02-19 20:20:17 +05:30
await broadcastEvents(data)
2023-02-19 20:20:17 +05:30
// inform user that broadcasting is done
$('#broadcasting-status').html(txt.broadcasting + checkMark)
$('#broadcasting-progress').val(relays.length)
2023-02-19 20:20:17 +05:30
// re-enable broadcast button
$('#fetch-and-broadcast').prop('disabled', false)
}
const getFromExtension = async () => {
const pubkey = await window.nostr.getPublicKey()
if (pubkey) $('#pubkey').val(pubkey).change()
}
const pubkeyOnChange = () => {
$('#fetch-and-broadcast').css('display', '')
$('#get-from-extension').css('display', 'none')
}
if (window.nostr) {
$('#fetch-and-broadcast').css('display', 'none')
$('#get-from-extension').css('display', '')
}