From 997e6474dfae9f901ae1fb582d65a10ff2af5a49 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Sun, 1 Jan 2023 22:27:46 +1300 Subject: [PATCH] relays: Add more default relays; added ability to add your own; status bar --- src/db/mod.rs | 1 + src/db/schema7.sql | 7 +++++++ src/overlord/mod.rs | 6 ++++++ src/ui/mod.rs | 12 ++++++++++++ src/ui/relays.rs | 35 +++++++++++++++++++++++++++++++---- 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/db/schema7.sql diff --git a/src/db/mod.rs b/src/db/mod.rs index 9d97bdd2..39f40223 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -109,6 +109,7 @@ fn upgrade(db: &Connection, mut version: u16) -> Result<(), Error> { apply_sql!(db, version, 4, "schema4.sql"); apply_sql!(db, version, 5, "schema5.sql"); apply_sql!(db, version, 6, "schema6.sql"); + apply_sql!(db, version, 7, "schema7.sql"); info!("Database is at version {}", version); Ok(()) } diff --git a/src/db/schema7.sql b/src/db/schema7.sql new file mode 100644 index 00000000..6a535437 --- /dev/null +++ b/src/db/schema7.sql @@ -0,0 +1,7 @@ +INSERT OR IGNORE INTO relay (url) values +('wss://nostr.fmt.wiz.biz'), +('wss://nostr.v0l.io'), +('wss://nostr.zebedee.cloud'), +('wss://nostr-2.zebedee.cloud'), +('wss://nostr.orangepill.dev'), +('wss://relay.nostr.bg'); diff --git a/src/overlord/mod.rs b/src/overlord/mod.rs index 86dd7a4d..a8485318 100644 --- a/src/overlord/mod.rs +++ b/src/overlord/mod.rs @@ -514,6 +514,12 @@ impl Overlord { } }); } + "add_relay" => { + let relay_str: String = serde_json::from_str(&bus_message.json_payload)?; + if let Ok(dbrelay) = DbRelay::new(relay_str) { + DbRelay::insert(dbrelay).await?; + } + } _ => {} }, _ => {} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index d6e904cf..fa947576 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -69,6 +69,7 @@ enum Page { struct GossipUi { next_frame: Instant, page: Page, + status: String, about: About, icon: TextureHandle, placeholder_avatar: TextureHandle, @@ -88,6 +89,7 @@ struct GossipUi { person_view_name: Option, avatars: HashMap, failed_avatars: HashSet, + new_relay_url: String, } impl Drop for GossipUi { @@ -141,6 +143,9 @@ impl GossipUi { GossipUi { next_frame: Instant::now(), page: Page::Feed, + status: + "Welcome to Gossip. Status messages will appear here, clobbering previous ones." + .to_owned(), about: crate::about::about(), icon: icon_texture_handle, placeholder_avatar: placeholder_avatar_texture_handle, @@ -160,6 +165,7 @@ impl GossipUi { person_view_name: None, avatars: HashMap::new(), failed_avatars: HashSet::new(), + new_relay_url: "".to_owned(), } } } @@ -215,6 +221,12 @@ impl eframe::App for GossipUi { Page::HelpHelp => help::update(self, ctx, frame, ui), Page::HelpAbout => help::update(self, ctx, frame, ui), }); + + egui::TopBottomPanel::bottom("status").show(ctx, |ui| { + ui.horizontal(|ui| { + ui.label(&self.status); + }); + }); } } diff --git a/src/ui/relays.rs b/src/ui/relays.rs index 0d15fd89..3b11c785 100644 --- a/src/ui/relays.rs +++ b/src/ui/relays.rs @@ -3,21 +3,48 @@ use crate::comms::BusMessage; use crate::db::DbRelay; use crate::globals::GLOBALS; use eframe::egui; -use egui::{Align, Context, Layout, RichText, ScrollArea, TextStyle, Ui}; +use egui::{Align, Context, Layout, RichText, ScrollArea, TextEdit, TextStyle, Ui}; use nostr_types::Url; -pub(super) fn update(_app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Frame, ui: &mut Ui) { +pub(super) fn update(app: &mut GossipUi, _ctx: &Context, _frame: &mut eframe::Frame, ui: &mut Ui) { ui.add_space(8.0); - ui.heading("Relays known"); + ui.heading("Relays"); ui.add_space(18.0); ui.label( RichText::new( - "Relays on this list have been automatically discovered in various kinds of events.", + "Relays on this list were selected by the developer, but more relays will show up as they are automatically discovered in various kinds of events.", ) .text_style(TextStyle::Body), ); + ui.horizontal(|ui| { + ui.label("Enter a new relay URL:"); + ui.add(TextEdit::singleline(&mut app.new_relay_url)); + if ui.button("Add").clicked() { + let test_url = Url::new(&app.new_relay_url); + if test_url.is_valid_relay_url() { + let tx = GLOBALS.to_overlord.clone(); + let _ = tx.send(BusMessage { + target: "overlord".to_string(), + kind: "add_relay".to_string(), + json_payload: serde_json::to_string(&app.new_relay_url).unwrap(), + }); + app.new_relay_url = "".to_owned(); + app.status = format!( + "I asked the overlord to add relay {}. Check for it below.", + &app.new_relay_url + ); + } else { + app.status = "That's not a valid relay URL.".to_owned(); + } + } + }); + + ui.add_space(10.0); + ui.separator(); + ui.add_space(10.0); + // TBD time how long this takes. We don't want expensive code in the UI let mut relays = GLOBALS.relays.blocking_read().clone(); let mut relays: Vec = relays.drain().map(|(_, relay)| relay).collect();