fix stale relays bug

Also, attempts to send updated relays to all online relays using Blastr. If that fails, it falls back to broadcasting to 20 random online relays.
This commit is contained in:
Sam Samskies 2023-02-20 14:22:07 -06:00
parent 2fc4719b8a
commit 32c2e663a9
No known key found for this signature in database
GPG Key ID: E3F697EE1B6EB156
2 changed files with 32 additions and 19 deletions

View File

@ -116,6 +116,15 @@ export default function useEventPublisher() {
}
}
},
/**
* Write to every online relay using Blastr.
* https://github.com/MutinyWallet/blastr
*/
broadcastWithBlastr: (ev: NEvent | undefined) => {
if (ev) {
System.WriteOnceToRelay("wss://nostr.mutinywallet.com", ev);
}
},
muted: async (keys: HexKey[], priv: HexKey[]) => {
if (pubKey) {
const ev = NEvent.ForPubKey(pubKey);
@ -251,18 +260,6 @@ export default function useEventPublisher() {
return await signEvent(ev);
}
},
saveRelays: async () => {
if (pubKey) {
const ev = NEvent.ForPubKey(pubKey);
ev.Kind = EventKind.ContactList;
ev.Content = JSON.stringify(relays);
for (const pk of follows) {
ev.Tags.push(new Tag(["p", pk], ev.Tags.length));
}
return await signEvent(ev);
}
},
saveRelaysSettings: async () => {
if (pubKey) {
const ev = NEvent.ForPubKey(pubKey);

View File

@ -18,17 +18,33 @@ const RelaySettingsPage = () => {
const [newRelay, setNewRelay] = useState<string>();
async function saveRelays() {
const ev = await publisher.saveRelays();
publisher.broadcast(ev);
publisher.broadcastForBootstrap(ev);
const updatedRelaysEvent = await publisher.saveRelaysSettings();
let wasBlastrBrodcastSuccessful: boolean;
try {
const onlineRelays = await fetch("https://api.nostr.watch/v1/online").then(r => r.json());
const settingsEv = await publisher.saveRelaysSettings();
const rs = Object.keys(relays).concat(randomSample(onlineRelays, 20));
publisher.broadcastAll(settingsEv, rs);
publisher.broadcast(updatedRelaysEvent);
publisher.broadcastForBootstrap(updatedRelaysEvent);
} catch (error) {
console.error(error);
}
try {
publisher.broadcastWithBlastr(updatedRelaysEvent);
wasBlastrBrodcastSuccessful = true;
} catch (error) {
console.error(error);
wasBlastrBrodcastSuccessful = false;
}
if (!wasBlastrBrodcastSuccessful) {
try {
const onlineRelays = await fetch("https://api.nostr.watch/v1/online").then(r => r.json());
const rs = Object.keys(relays).concat(randomSample(onlineRelays, 20));
publisher.broadcastAll(updatedRelaysEvent, rs);
} catch (error) {
console.error(error);
}
}
}
function addRelay() {