From 6a31c3b1351cfab17d1efc4d213e13e60fb3646c Mon Sep 17 00:00:00 2001 From: Ryan Breen Date: Sat, 28 Jan 2023 15:33:13 -0500 Subject: [PATCH] Relays are finally working, thanks to a hacky solution. --- Shared (Extension)/Resources/background.js | 23 +++++++++++----------- Shared (Extension)/Resources/options.html | 4 ++-- Shared (Extension)/Resources/options.js | 11 ++++------- Shared (Extension)/Resources/utils.js | 16 +++++++++++---- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/Shared (Extension)/Resources/background.js b/Shared (Extension)/Resources/background.js index c39ad54..a55816a 100644 --- a/Shared (Extension)/Resources/background.js +++ b/Shared (Extension)/Resources/background.js @@ -6,6 +6,8 @@ import { nip19, } from 'nostr-tools'; +import { getProfileIndex, get, getProfile } from './utils'; + const storage = browser.storage.local; const log = msg => console.log('Background: ', msg); @@ -116,8 +118,6 @@ async function getPubKey() { async function currentProfile() { let index = await getProfileIndex(); let profiles = await get('profiles'); - let currentProfile = profiles[index]; - currentProfile.nsecKey = nip19.nsecEncode(currentProfile.privKey); return profiles[index]; } @@ -138,13 +138,14 @@ async function nip04Decrypt({ pubKey, cipherText }) { return nip04.decrypt(privKey, pubKey, cipherText); } -// Utilities - -async function get(item) { - return (await storage.get(item))[item]; -} - -async function getProfile(index) { - let profiles = await get('profiles'); - return profiles[index]; +async function getRelays() { + let profile = await currentProfile(); + let relays = profile.relays; + let relayObj = {}; + // The getRelays call expects this to be returned as an object, not array + relays.forEach(relay => { + let { url, read, write } = relay; + relayObj[url] = { read, write }; + }); + return relayObj; } diff --git a/Shared (Extension)/Resources/options.html b/Shared (Extension)/Resources/options.html index 1db8c53..e96c5ba 100644 --- a/Shared (Extension)/Resources/options.html +++ b/Shared (Extension)/Resources/options.html @@ -71,10 +71,10 @@ - + - + diff --git a/Shared (Extension)/Resources/options.js b/Shared (Extension)/Resources/options.js index 43d4fab..953b0eb 100644 --- a/Shared (Extension)/Resources/options.js +++ b/Shared (Extension)/Resources/options.js @@ -130,10 +130,8 @@ Alpine.data('options', () => ({ }, async saveRelays() { - console.log(this.relays); await saveRelays(this.profileIndex, this.relays); - await this.getRelays(this.profileIndex); - this.newRelay = ''; + await this.getRelays(); }, async addRelay(relayToAdd = null) { @@ -142,17 +140,16 @@ Alpine.data('options', () => ({ let url = new URL(newRelay); if (url.protocol !== 'wss:') { this.setUrlError('Must be a websocket url'); + return; } let urls = this.relays.map(v => v.url); if (urls.includes(url.href)) { this.setUrlError('URL already exists'); return; } - this.relays = [ - ...this.relays, - { url: url.href, read: true, write: true }, - ]; + this.relays.push({ url: url.href, read: true, write: true }); await this.saveRelays(); + this.newRelay = ''; } catch (error) { this.setUrlError('Invalid websocket URL'); } diff --git a/Shared (Extension)/Resources/utils.js b/Shared (Extension)/Resources/utils.js index 438a6bc..0609b14 100644 --- a/Shared (Extension)/Resources/utils.js +++ b/Shared (Extension)/Resources/utils.js @@ -12,7 +12,7 @@ export async function getProfiles() { return profiles.profiles; } -async function getProfile(index) { +export async function getProfile(index) { let profiles = await getProfiles(); return profiles[index]; } @@ -107,8 +107,16 @@ export async function getRelays(profileIndex) { } export async function saveRelays(profileIndex, relays) { - let profiles = await getProfile(profileIndex); - console.log('saving: ', relays); - profile.relays = [...relays]; + // Having an Alpine proxy object as a sub-object does not serialize correctly in storage, + // so we are pre-serializing here before assigning it to the profile, so the proxy + // obj doesn't bug out. + let fixedRelays = JSON.parse(JSON.stringify(relays)); + let profiles = await getProfiles(); + let profile = profiles[profileIndex]; + profile.relays = fixedRelays; await storage.set({ profiles }); } + +export async function get(item) { + return (await storage.get(item))[item]; +}