Skip to content

Commit

Permalink
Merge branch 'search' into unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedilger committed Nov 29, 2024
2 parents 716ba35 + f5412b9 commit b2e7d0c
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 31 deletions.
43 changes: 27 additions & 16 deletions gossip-bin/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ enum Page {
RelaysCoverage,
RelaysMine,
RelaysKnownNetwork(Option<RelayUrl>),
Search,
SearchLocal,
SearchRelays,
Settings,
HelpHelp,
HelpStats,
Expand Down Expand Up @@ -226,7 +227,8 @@ impl Page {
Page::RelaysCoverage => (SubMenu::Relays.as_str(), "Coverage Report".into()),
Page::RelaysMine => (SubMenu::Relays.as_str(), "My Relays".into()),
Page::RelaysKnownNetwork(_) => (SubMenu::Relays.as_str(), "Known Network".into()),
Page::Search => ("Search", "Search".into()),
Page::SearchLocal => ("Search Local", "Search Local".into()),
Page::SearchRelays => ("Search Relays", "Search Relays".into()),
Page::Settings => ("Settings", "Settings".into()),
Page::HelpHelp => (SubMenu::Help.as_str(), "Troubleshooting".into()),
Page::HelpStats => (SubMenu::Help.as_str(), "Stats".into()),
Expand Down Expand Up @@ -273,6 +275,7 @@ impl Page {
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum SubMenu {
Feeds,
Search,
Relays,
Account,
Help,
Expand All @@ -282,6 +285,7 @@ impl SubMenu {
fn as_str(&self) -> &'static str {
match self {
SubMenu::Feeds => "Feeds",
SubMenu::Search => "Search",
SubMenu::Relays => "Relays",
SubMenu::Account => "Account",
SubMenu::Help => "Help",
Expand All @@ -291,6 +295,7 @@ impl SubMenu {
fn as_id_str(&self) -> &'static str {
match self {
SubMenu::Feeds => "feeds_submenu_id",
SubMenu::Search => "search_submenu_id",
SubMenu::Account => "account_submenu_id",
SubMenu::Relays => "relays_submenu_id",
SubMenu::Help => "help_submenu_id",
Expand Down Expand Up @@ -516,7 +521,7 @@ struct GossipUi {
import_priv: String,
import_pub: String,
search: String,
entering_search_page: bool,
entering_a_search_page: bool,
editing_petname: bool,
petname: String,
deleting_list: Option<PersonList>,
Expand Down Expand Up @@ -585,6 +590,7 @@ impl GossipUi {

let mut submenu_ids: HashMap<SubMenu, egui::Id> = HashMap::new();
submenu_ids.insert(SubMenu::Feeds, egui::Id::new(SubMenu::Feeds.as_id_str()));
submenu_ids.insert(SubMenu::Search, egui::Id::new(SubMenu::Search.as_id_str()));
submenu_ids.insert(
SubMenu::Account,
egui::Id::new(SubMenu::Account.as_id_str()),
Expand Down Expand Up @@ -766,7 +772,7 @@ impl GossipUi {
import_priv: "".to_owned(),
import_pub: "".to_owned(),
search: "".to_owned(),
entering_search_page: false,
entering_a_search_page: false,
editing_petname: false,
petname: "".to_owned(),
deleting_list: None,
Expand Down Expand Up @@ -911,9 +917,13 @@ impl GossipUi {
self.relays.enter_page(some_relay.as_ref());
self.open_menu(ctx, SubMenu::Relays);
}
Page::Search => {
self.entering_search_page = true;
self.close_all_menus_except_feeds(ctx);
Page::SearchLocal => {
self.entering_a_search_page = true;
self.open_menu(ctx, SubMenu::Search);
}
Page::SearchRelays => {
self.entering_a_search_page = true;
self.open_menu(ctx, SubMenu::Search);
}
Page::Settings => {
self.close_all_menus_except_feeds(ctx);
Expand Down Expand Up @@ -979,7 +989,7 @@ impl GossipUi {
self.add_global_feed(ui, ctx);
self.add_personal_notes(ui, ctx);
self.add_private_chats(ui, ctx);
self.add_search(ui, ctx);
self.add_search_submenu(ui, ctx);

ui.add_space(10.0);

Expand Down Expand Up @@ -1146,13 +1156,13 @@ impl GossipUi {
}
}

fn add_search(&mut self, ui: &mut Ui, ctx: &Context) {
if self
.add_selected_label(ui, self.page == Page::Search, "Search")
.clicked()
{
self.set_page(ctx, Page::Search);
}
fn add_search_submenu(&mut self, ui: &mut Ui, ctx: &Context) {
let (mut cstate, header_response) = self.get_openable_menu(ui, ctx, SubMenu::Search);
cstate.show_body_indented(&header_response, ui, |ui| {
self.add_menu_item_page(ui, Page::SearchLocal, None, true);
self.add_menu_item_page(ui, Page::SearchRelays, None, true);
});
self.after_openable_menu(ui, &cstate);
}

fn add_people_lists(&mut self, ui: &mut Ui, ctx: &Context) {
Expand Down Expand Up @@ -2335,7 +2345,8 @@ impl eframe::App for GossipUi {
| Page::RelaysCoverage
| Page::RelaysMine
| Page::RelaysKnownNetwork(_) => relays::update(self, ctx, frame, ui),
Page::Search => search::update(self, ctx, frame, ui),
Page::SearchLocal => search::update(self, ctx, frame, ui, true),
Page::SearchRelays => search::update(self, ctx, frame, ui, false),
Page::Settings => settings::update(self, ctx, frame, ui),
Page::HelpHelp | Page::HelpStats | Page::HelpAbout => {
help::update(self, ctx, frame, ui)
Expand Down
55 changes: 46 additions & 9 deletions gossip-bin/src/ui/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,40 @@ use eframe::{egui, Frame};
use egui::widgets::Button;
use egui::{Context, Label, RichText, Sense, Ui};
use gossip_lib::comms::ToOverlordMessage;
use gossip_lib::{FeedKind, PersonTable, Table, GLOBALS};

pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut Frame, ui: &mut Ui) {
use gossip_lib::{FeedKind, PersonTable, Relay, Table, GLOBALS};

pub(super) fn update(
app: &mut GossipUi,
ctx: &Context,
_frame: &mut Frame,
ui: &mut Ui,
local: bool,
) {
ui.add_space(10.0);
ui.heading("Search notes and users");
if local {
ui.heading("Search notes and users in local database");
} else {
ui.heading("Search notes and users on search relays");
}

// Warn if there are no search relays configured
if !local {
let search_relays: Vec<Relay> = GLOBALS
.db()
.filter_relays(|relay| relay.has_usage_bits(Relay::SEARCH))
.unwrap_or_default();

if search_relays.is_empty() {
ui.horizontal_wrapped(|ui| {
ui.label("You must first configure SEARCH relays on the ");
if ui.link("relays").clicked() {
app.set_page(ctx, Page::RelaysKnownNetwork(None));
}
ui.label(" page.");
});
return;
}
}

ui.add_space(12.0);

Expand All @@ -20,9 +49,11 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut Frame, ui:
.desired_width(600.0),
);

if app.entering_search_page {
if app.entering_a_search_page {
// Focus on the search input
response.request_focus();
app.entering_search_page = false;

app.entering_a_search_page = false;
}

if ui.add(Button::new("Search")).clicked() {
Expand All @@ -34,9 +65,15 @@ pub(super) fn update(app: &mut GossipUi, ctx: &Context, _frame: &mut Frame, ui:
});

if trigger_search {
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::Search(app.search.clone()));
if local {
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::SearchLocally(app.search.clone()));
} else {
let _ = GLOBALS
.to_overlord
.send(ToOverlordMessage::SearchRelays(app.search.clone()));
}
}

ui.add_space(12.0);
Expand Down
10 changes: 8 additions & 2 deletions gossip-lib/src/comms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@ pub enum ToOverlordMessage {
/// Calls [repost](crate::Overlord::repost)
Repost(Id),

/// Calls [search](crate::Overlord::search)
Search(String),
/// Calls [search](crate::Overlord::search_locally)
SearchLocally(String),

/// Calls [search](crate::Overlord::search_relays)
SearchRelays(String),

/// Calls [set_active_person](crate::Overlord::set_active_person)
SetActivePerson(PublicKey),
Expand Down Expand Up @@ -310,6 +313,7 @@ pub enum RelayConnectionReason {
PostMuteList,
PostNostrConnect,
ReadThread,
Search,
SubscribePerson,
SubscribeGlobal,
}
Expand Down Expand Up @@ -345,6 +349,7 @@ impl RelayConnectionReason {
PostMetadata => "Posting our metadata",
PostNostrConnect => "Posting nostrconnect",
ReadThread => "Reading ancestors to build a thread",
Search => "Search",
SubscribePerson => "Subscribe to the events of a person",
SubscribeGlobal => "Subscribe to the global feed on a relay",
}
Expand Down Expand Up @@ -373,6 +378,7 @@ impl RelayConnectionReason {
PostMetadata => false,
PostNostrConnect => false,
ReadThread => true,
Search => false,
SubscribePerson => false,
SubscribeGlobal => false,
}
Expand Down
15 changes: 15 additions & 0 deletions gossip-lib/src/filter_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub enum FilterSet {
},
RepliesToId(Id),
RepliesToAddr(NAddr),
Search(String),
}

impl FilterSet {
Expand All @@ -76,6 +77,7 @@ impl FilterSet {
FilterSet::PersonFeedChunk { .. } => true,
FilterSet::RepliesToId(_) => false,
FilterSet::RepliesToAddr(_) => false,
FilterSet::Search(_) => true,
}
}

Expand Down Expand Up @@ -118,6 +120,7 @@ impl FilterSet {
FilterSet::PersonFeedChunk { .. } => "person_feed_chunk",
FilterSet::RepliesToId(_) => "id_replies",
FilterSet::RepliesToAddr(_) => "addr_replies",
FilterSet::Search(_) => "relay_search",
}
}

Expand Down Expand Up @@ -460,6 +463,18 @@ impl FilterSet {
};
filters.push(filter);
}
FilterSet::Search(what) => {
// Explicitly ignore spam filtering during searches (for now)
// We may revisit this decision if spam becomes the main results.

let event_kinds = crate::feed::feed_displayable_event_kinds(false);
let filter = Filter {
kinds: event_kinds,
search: Some(what.to_string()),
..Default::default()
};
filters.push(filter);
}
}

filters
Expand Down
25 changes: 22 additions & 3 deletions gossip-lib/src/overlord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,11 @@ impl Overlord {
ToOverlordMessage::Repost(id) => {
self.repost(id)?;
}
ToOverlordMessage::Search(text) => {
Self::search(text)?;
ToOverlordMessage::SearchLocally(text) => {
Self::search_locally(text)?;
}
ToOverlordMessage::SearchRelays(text) => {
Self::search_relays(text)?;
}
ToOverlordMessage::SetActivePerson(pubkey) => {
Self::set_active_person(pubkey).await?;
Expand Down Expand Up @@ -2299,7 +2302,7 @@ impl Overlord {

/// Search people and notes in the local database.
/// Search results eventually arrive in `GLOBALS.people_search_results` and `GLOBALS.note_search_results`
pub fn search(mut text: String) -> Result<(), Error> {
pub fn search_locally(mut text: String) -> Result<(), Error> {
if text.len() < 2 {
GLOBALS
.status_queue
Expand Down Expand Up @@ -2424,6 +2427,22 @@ impl Overlord {
Ok(())
}

/// Search all search relays for events matching the text
pub fn search_relays(text: String) -> Result<(), Error> {
let filter_set = FilterSet::Search(text);
let job = RelayJob {
reason: RelayConnectionReason::Search,
payload: ToMinionPayload {
job_id: rand::random::<u64>(),
detail: ToMinionPayloadDetail::Subscribe(filter_set),
},
};
let search_relays: Vec<RelayUrl> = Relay::choose_relay_urls(Relay::SEARCH, |_| true)?;
manager::run_jobs_on_all_relays(search_relays, vec![job]);

Ok(())
}

/// Set a particular person as active in the `People` structure. This affects the results of
/// some functions of that structure
pub async fn set_active_person(pubkey: PublicKey) -> Result<(), Error> {
Expand Down
4 changes: 3 additions & 1 deletion gossip-lib/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ pub fn process_new_event(
);

// If we were searching for this event, add it to the search results
let is_a_search_result: bool = GLOBALS.events_being_searched_for.read().contains(&event.id);
let is_a_search_result: bool = subscription.is_some_and(|s| s.contains("relay_search"))
|| GLOBALS.events_being_searched_for.read().contains(&event.id);

if is_a_search_result {
GLOBALS
.events_being_searched_for
Expand Down

0 comments on commit b2e7d0c

Please sign in to comment.