mirror of
https://github.com/vitorpamplona/Nostryfied.git
synced 2025-06-06 09:22:03 +00:00
Allowing choice of to relays.
Authenticates Sending
This commit is contained in:
parent
e97cffbf89
commit
b16cfc09f2
12
index.html
12
index.html
@ -101,6 +101,18 @@
|
||||
</script>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="space-between-small">
|
||||
<p>
|
||||
To relays
|
||||
<input
|
||||
type="text"
|
||||
class="cool-field"
|
||||
id="relaySetBroadcast"
|
||||
name="relaySetBroadcast"
|
||||
placeholder="wss://nos.lol, wss://nostr.mom or leave blank to use your write relay list" />
|
||||
</p>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
|
@ -18,7 +18,7 @@ const fetchAndBroadcast = async () => {
|
||||
}
|
||||
|
||||
// parse pubkey ('npub' or hexa)
|
||||
const relaySet = parseRelaySet($('#relaySet').val())
|
||||
const relaySet = parseRelaySet($('#relaySet').val(), allAvailableRelays)
|
||||
const pubkey = parsePubkey($('#pubkey').val())
|
||||
if (!pubkey) return
|
||||
// disable button (will be re-enable at the end of the process)
|
||||
@ -28,7 +28,7 @@ const fetchAndBroadcast = async () => {
|
||||
$('#fetching-status').text(txt.fetching)
|
||||
// show and update fetching progress bar
|
||||
$('#fetching-progress').css('visibility', 'visible')
|
||||
$('#fetching-progress').prop('max', relays.length)
|
||||
$('#fetching-progress').prop('max', relaySet.length)
|
||||
|
||||
$('#checking-relays-header-box').css('display', 'flex')
|
||||
$('#checking-relays-box').css('display', 'flex')
|
||||
@ -52,7 +52,7 @@ const fetchAndBroadcast = async () => {
|
||||
|
||||
// inform user fetching is done
|
||||
$('#fetching-status').html(txt.fetching + checkMark)
|
||||
$('#fetching-progress').val(relays.length)
|
||||
$('#fetching-progress').val(relaySet.length)
|
||||
|
||||
const latestKind3 = data.filter((it) => it.kind == 3 && it.pubkey === pubkey)[0]
|
||||
|
||||
@ -64,14 +64,15 @@ const fetchAndBroadcast = async () => {
|
||||
downloadFile(data, 'nostr-backup.json')
|
||||
|
||||
if (latestKind3) {
|
||||
const myRelaySet = JSON.parse(latestKind3.content)
|
||||
relays = Object.keys(myRelaySet).filter(url => myRelaySet[url].write).map(url => url)
|
||||
const parsedRelaySet = JSON.parse(latestKind3.content)
|
||||
const kind3RelaySet = Object.keys(parsedRelaySet).filter(url => parsedRelaySet[url].write).map(url => url)
|
||||
const relaySetBroadcast = parseRelaySet($('#relaySetBroadcast').val(), kind3RelaySet)
|
||||
|
||||
// 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)
|
||||
$('#broadcasting-progress').prop('max', relaySetBroadcast.length)
|
||||
|
||||
$('#checking-relays-header').html("")
|
||||
$('#checking-relays').html("<tr id=\"checking-relays-header\"></tr>")
|
||||
@ -80,14 +81,14 @@ const fetchAndBroadcast = async () => {
|
||||
$('#checking-relays-box').css('display', 'flex')
|
||||
$('#checking-relays-header').html("<th>Relay</th><th>Status</th><th></th><th></th><th>Events</th><th>Message</th>")
|
||||
|
||||
await broadcastEvents(data)
|
||||
}
|
||||
await broadcastEvents(data, relaySetBroadcast)
|
||||
|
||||
// inform user that broadcasting is done
|
||||
$('#broadcasting-status').html(txt.broadcasting + checkMark)
|
||||
$('#broadcasting-progress').val(relays.length)
|
||||
// re-enable broadcast button
|
||||
$('#fetch-and-broadcast').prop('disabled', false)
|
||||
// inform user that broadcasting is done
|
||||
$('#broadcasting-status').html(txt.broadcasting + checkMark)
|
||||
$('#broadcasting-progress').val(relaySetBroadcast.length)
|
||||
// re-enable broadcast button
|
||||
$('#fetch-and-broadcast').prop('disabled', false)
|
||||
}
|
||||
}
|
||||
|
||||
const getFromExtension = async () => {
|
||||
@ -116,7 +117,6 @@ const justBroadcast = async (fileName) => {
|
||||
}
|
||||
|
||||
const broadcast = async (data) => {
|
||||
console.log(data)
|
||||
// reset UI
|
||||
$('#fetching-status').html('')
|
||||
$('#fetching-progress').css('visibility', 'hidden')
|
||||
@ -133,20 +133,22 @@ const broadcast = async (data) => {
|
||||
fetching: 'Loading from file... ',
|
||||
download: `Downloading Backup file... ${checkMark}`,
|
||||
}
|
||||
|
||||
const latestKind3 = data.filter((it) => it.kind == 3)[0]
|
||||
const parsedRelaySet = JSON.parse(latestKind3.content)
|
||||
const kind3RelaySet = Object.keys(parsedRelaySet).filter(url => parsedRelaySet[url].write).map(url => url)
|
||||
const relaySetBroadcast = parseRelaySet($('#relaySetBroadcast').val(), kind3RelaySet)
|
||||
|
||||
// disable button (will be re-enable at the end of the process)
|
||||
$('#fetch-and-broadcast').prop('disabled', true)
|
||||
$('#just-broadcast').prop('disabled', true)
|
||||
// show and update fetching progress bar
|
||||
$('#fetching-progress').css('visibility', 'visible')
|
||||
$('#fetching-progress').prop('max', relays.length)
|
||||
$('#fetching-progress').prop('max', relaySetBroadcast.length)
|
||||
|
||||
// inform user fetching is done
|
||||
$('#fetching-status').html(txt.fetching + checkMark)
|
||||
$('#fetching-progress').val(relays.length)
|
||||
|
||||
const latestKind3 = data.filter((it) => it.kind == 3)[0]
|
||||
const myRelaySet = JSON.parse(latestKind3.content)
|
||||
relays = Object.keys(myRelaySet).filter(url => myRelaySet[url].write).map(url => url)
|
||||
$('#fetching-progress').val(relaySetBroadcast.length)
|
||||
|
||||
$('#checking-relays-header-box').css('display', 'none')
|
||||
$('#checking-relays-box').css('display', 'none')
|
||||
@ -155,17 +157,17 @@ const broadcast = async (data) => {
|
||||
$('#broadcasting-status').html(txt.broadcasting)
|
||||
// show and update broadcasting progress bar
|
||||
$('#broadcasting-progress').css('visibility', 'visible')
|
||||
$('#broadcasting-progress').prop('max', relays.length)
|
||||
$('#broadcasting-progress').prop('max', relaySetBroadcast.length)
|
||||
|
||||
$('#checking-relays-header-box').css('display', 'flex')
|
||||
$('#checking-relays-box').css('display', 'flex')
|
||||
$('#checking-relays-header').html("<th>Relay</th><th>Status</th><th></th><th></th><th>Events</th><th>Message</th>")
|
||||
|
||||
await broadcastEvents(data)
|
||||
await broadcastEvents(data, relaySetBroadcast)
|
||||
|
||||
// inform user that broadcasting is done
|
||||
$('#broadcasting-status').html(txt.broadcasting + checkMark)
|
||||
$('#broadcasting-progress').val(relays.length)
|
||||
$('#broadcasting-progress').val(relaySetBroadcast.length)
|
||||
// re-enable broadcast button
|
||||
$('#fetch-and-broadcast').prop('disabled', false)
|
||||
}
|
@ -37,13 +37,13 @@ const hexa2npub = (hex) => {
|
||||
const parsePubkey = (pubkey) =>
|
||||
pubkey.match('npub1') ? npub2hexa(pubkey) : pubkey
|
||||
|
||||
const parseRelaySet = (commaSeparatedRelayString) => {
|
||||
const parseRelaySet = (commaSeparatedRelayString, defaultSet) => {
|
||||
let list = commaSeparatedRelayString.split(",")
|
||||
|
||||
if (list.length == 0) return undefined
|
||||
if (list.length == 1 && list[0].trim() === "") return undefined
|
||||
|
||||
return list
|
||||
if (list && list.length > 0 && list[0] !== "")
|
||||
return list.map((it) => it.trim())
|
||||
else
|
||||
return defaultSet
|
||||
}
|
||||
|
||||
// download js file
|
||||
@ -110,7 +110,7 @@ const displayRelayStatus = (relayStatusAndCount) => {
|
||||
const relayName = it.replace("wss://", "").replace("ws://", "")
|
||||
const line = "<td>" + relayName + "</td><td>" + relayStatusAndCount[it].status + "</td>" + untilStr + "<td>" + relayStatusAndCount[it].count + "</td>" + "<td>" + msg + "</td>"
|
||||
|
||||
const elemId = relayName.replaceAll(".", "-")
|
||||
const elemId = relayName.replaceAll(".", "")
|
||||
|
||||
if ($('#' + elemId).length > 0) {
|
||||
$('#' + elemId).html(line)
|
||||
@ -322,15 +322,8 @@ const getEvents = async (filters, addedFilters, pubkey, relaySet) => {
|
||||
// events hash
|
||||
const events = {}
|
||||
|
||||
let myRelaySet = null
|
||||
|
||||
if (relaySet && relaySet.length > 0)
|
||||
myRelaySet = relaySet
|
||||
else
|
||||
myRelaySet = relays
|
||||
|
||||
// batch processing of 10 relays
|
||||
await processInPool(myRelaySet, (relay, poolStatus) => fetchFromRelay(relay, filters, addedFilters, pubkey, events, poolStatus), 10, (progress) => $('#fetching-progress').val(progress))
|
||||
await processInPool(relaySet, (relay, poolStatus) => fetchFromRelay(relay, filters, addedFilters, pubkey, events, poolStatus), 10, (progress) => $('#fetching-progress').val(progress))
|
||||
|
||||
displayRelayStatus({})
|
||||
|
||||
@ -339,10 +332,10 @@ const getEvents = async (filters, addedFilters, pubkey, relaySet) => {
|
||||
}
|
||||
|
||||
// broadcast events to list of relays
|
||||
const broadcastEvents = async (data) => {
|
||||
const poolStatus = await processInPool(relays, (relay, poolStatus) => sendToRelay(relay, data, poolStatus), 10, (progress) => $('#broadcasting-progress').val(progress))
|
||||
const broadcastEvents = async (data, relaySet) => {
|
||||
const poolStatus = await processInPool(relaySet, (relay, poolStatus) => sendToRelay(relay, data, poolStatus), 10, (progress) => $('#broadcasting-progress').val(progress))
|
||||
|
||||
displayRelayStatus(relayStatus)
|
||||
displayRelayStatus(poolStatus)
|
||||
}
|
||||
|
||||
const processInPool = async (items, processItem, poolSize, onProgress) => {
|
||||
@ -427,6 +420,25 @@ const sendToRelay = async (relay, data, relayStatus) =>
|
||||
} else {
|
||||
console.log(relay, event.data)
|
||||
}
|
||||
|
||||
if (msgType === 'AUTH') {
|
||||
signNostrAuthEvent(relay, subscriptionId).then(
|
||||
(event) => {
|
||||
if (event) {
|
||||
ws.send(JSON.stringify(['AUTH', event]))
|
||||
} else {
|
||||
updateRelayStatus(relay, "AUTH Req", 0, undefined, undefined, relayStatus)
|
||||
ws.close()
|
||||
reject(relay)
|
||||
}
|
||||
},
|
||||
(reason) => {
|
||||
updateRelayStatus(relay, "AUTH Req", 0, undefined, undefined, relayStatus)
|
||||
ws.close()
|
||||
reject(relay)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
ws.onerror = (err) => {
|
||||
updateRelayStatus(relay, "Error", 0, undefined, undefined, undefined, relayStatus)
|
||||
|
@ -54,8 +54,8 @@ const fixedRelays = [
|
||||
'wss://relay.nostr.band'
|
||||
]
|
||||
|
||||
var relays = []
|
||||
var allAvailableRelays = []
|
||||
|
||||
fetch("https://api.nostr.watch/v1/online")
|
||||
.then(response => response.json())
|
||||
.then(json => relays = [... new Set(fixedRelays.concat(json))] );
|
||||
.then(json => allAvailableRelays = [... new Set(fixedRelays.concat(json))] );
|
||||
|
20
style.css
20
style.css
@ -240,6 +240,26 @@ h2 {
|
||||
}
|
||||
|
||||
|
||||
#relaySetBroadcast {
|
||||
width: 450px !important;
|
||||
}
|
||||
|
||||
@media screen and (max-width: 1080px) {
|
||||
#relaySetBroadcast {
|
||||
width: 450px !important;
|
||||
}
|
||||
}
|
||||
@media screen and (max-width: 800px) {
|
||||
#relaySetBroadcast {
|
||||
width: 350px !important;
|
||||
}
|
||||
}
|
||||
@media screen and (max-width: 600px) {
|
||||
#relaySetBroadcast {
|
||||
width: 250px !important;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user