1
0
forked from Kieran/snort

chore: pick random relays for new users

This commit is contained in:
Kieran 2023-02-02 18:22:57 +00:00
parent 1770e09159
commit ea7209c12f
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 20 additions and 2 deletions

View File

@ -66,7 +66,23 @@ export default function LoginPage() {
async function makeRandomKey() { async function makeRandomKey() {
let newKey = secp.utils.bytesToHex(secp.utils.randomPrivateKey()); let newKey = secp.utils.bytesToHex(secp.utils.randomPrivateKey());
dispatch(setPrivateKey(newKey)) dispatch(setPrivateKey(newKey));
try {
let rsp = await fetch("https://api.nostr.watch/v1/online");
if (rsp.ok) {
let online: string[] = await rsp.json();
let pickRandom = online.sort((a, b) => Math.random() >= 0.5 ? 1 : -1).slice(0, 4); // pick 4 random relays
let relayObjects = pickRandom.map(a => [a, { read: true, write: true }]);
dispatch(setRelays({
relays: Object.fromEntries(relayObjects),
createdAt: 1
}));
}
} catch (e) {
console.warn(e);
}
navigate("/new"); navigate("/new");
} }

View File

@ -340,10 +340,12 @@ const LoginSlice = createSlice({
state.dmInteraction += 1; state.dmInteraction += 1;
}, },
logout: (state) => { logout: (state) => {
let relays = { ...state.relays };
Object.assign(state, InitState); Object.assign(state, InitState);
state.loggedOut = true; state.loggedOut = true;
state.relays = Object.fromEntries(DefaultRelays.entries());
window.localStorage.clear(); window.localStorage.clear();
state.relays = relays;
window.localStorage.setItem(RelayListKey, JSON.stringify(relays));
}, },
markNotificationsRead: (state) => { markNotificationsRead: (state) => {
state.readNotifications = new Date().getTime(); state.readNotifications = new Date().getTime();