search messages via in-memory index

This commit is contained in:
2026-10-06 08:28:22 +07:00
parent 453a462fc0
commit 1f9cfd92cb
5 changed files with 205 additions and 209 deletions
+54 -132
View File
@@ -1,12 +1,12 @@
use std::cmp::Reverse;
use std::collections::HashSet;
use std::ops::Range;
use std::rc::Rc;
use std::time::{SystemTime, UNIX_EPOCH};
use anyhow::Error;
use auto_update::AutoUpdater;
use chat::{ChatEvent, ChatRegistry, Room, RoomKind};
use common::{DebouncedDelay, TimestampExt};
use common::TimestampExt;
use gpui::prelude::FluentBuilder;
use gpui::{
AnyElement, App, AppContext, ClipboardItem, Context, Div, Entity, EventEmitter, FocusHandle,
@@ -14,13 +14,12 @@ use gpui::{
Stateful, Styled, StyledImage, Subscription, Task, UniformListScrollHandle, WeakEntity, Window,
div, img, px, relative, retain_all, uniform_list,
};
use instant::Duration;
use nip29::{Group, GroupKey, GroupsEvent, GroupsRegistry};
use nostr_sdk::prelude::*;
use person::PersonRegistry;
use settings::AppSettings;
use smallvec::{SmallVec, smallvec};
use state::{FIND_DELAY, FIND_LIMIT, NostrRegistry, StateEvent};
use state::{FIND_LIMIT, NostrRegistry, StateEvent};
use theme::{ActiveTheme, TABBAR_HEIGHT};
use ui::avatar::Avatar;
use ui::button::{Button, ButtonCustomVariant, ButtonVariants};
@@ -41,11 +40,6 @@ use ui::{
use crate::Command;
use crate::dialogs::{import, profile, screening};
mod utils;
pub(crate) use utils::pick_banner;
use utils::{MessageMatch, search_messages};
/// A collapsible section of the sidebar's main list.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ListSection {
@@ -90,7 +84,7 @@ enum SearchResult {
Message(MessageHit),
}
/// A message found by searching the content of the local database.
/// A message found by searching the indexed chat history.
#[derive(Clone)]
pub(crate) struct MessageHit {
room: Entity<Room>,
@@ -143,6 +137,19 @@ fn find_groups(terms: &[String], cx: &App) -> Vec<Entity<Group>> {
.collect()
}
/// Brand backgrounds shown behind the signed-out screen; one is picked at random.
const SIGNED_OUT_BANNERS: [&str; 2] = ["brand/bg1.jpg", "brand/bg2.jpg"];
/// Pick one of the signed-out backgrounds at random.
fn pick_banner() -> SharedString {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|elapsed| elapsed.subsec_nanos())
.unwrap_or_default();
let index = nanos as usize % SIGNED_OUT_BANNERS.len();
SIGNED_OUT_BANNERS[index].into()
}
/// A collapsible section of the group view.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum GroupSection {
@@ -211,16 +218,8 @@ pub struct Sidebar {
search_open: bool,
/// Search input state.
find_input: Entity<InputState>,
/// Debounced delay for the search input.
find_debouncer: DebouncedDelay<Self>,
/// Whether a search is in progress.
finding: bool,
/// The query the current search results belong to.
searched: Option<String>,
/// Search results: rooms and groups by name, then messages by content.
results: Vec<SearchResult>,
/// The async message content search.
search_task: Option<Task<()>>,
/// Scroll state of the search results list.
search_scroll: UniformListScrollHandle,
@@ -268,7 +267,6 @@ impl Sidebar {
subscriptions.push(
cx.subscribe_in(&nostr, window, |this, _, event, window, cx| {
// Re-pick the background each time the signed-out screen is shown.
if let StateEvent::NoSigner = event {
this.banner = pick_banner();
if this.search_open {
@@ -279,29 +277,21 @@ impl Sidebar {
}),
);
subscriptions.push(
// Subscribe for find input events
cx.subscribe_in(
&find_input,
window,
|this, state, event, window, cx| match event {
InputEvent::PressEnter { .. } => this.search(window, cx),
InputEvent::Change => {
if state.read(cx).value().is_empty() {
this.clear_results(window, cx);
} else {
this.find_debouncer.fire_new(
Duration::from_millis(FIND_DELAY),
window,
cx,
|this, window, cx| this.debounced_search(window, cx),
);
}
subscriptions.push(cx.subscribe_in(
&find_input,
window,
|this, state, event, _window, cx| match event {
InputEvent::PressEnter { .. } => this.search(cx),
InputEvent::Change => {
if state.read(cx).value().is_empty() {
this.clear_results(cx);
} else {
this.search(cx);
}
_ => {}
},
),
);
}
_ => {}
},
));
Self {
focus_handle: cx.focus_handle(),
@@ -325,11 +315,7 @@ impl Sidebar {
new_requests: false,
search_open: false,
find_input,
find_debouncer: DebouncedDelay::new(),
finding: false,
searched: None,
results: Vec::new(),
search_task: None,
search_scroll: UniformListScrollHandle::new(),
tasks: smallvec![],
_subscriptions: subscriptions,
@@ -532,24 +518,14 @@ impl Sidebar {
self.find_input.update(cx, |input, cx| {
input.set_value("", window, cx);
});
self.clear_results(window, cx);
self.clear_results(cx);
}
cx.notify();
}
/// Trigger a search once the input debounce delay has passed.
fn debounced_search(&mut self, window: &mut Window, cx: &mut Context<Self>) -> Task<()> {
cx.spawn_in(window, async move |this, cx| {
this.update_in(cx, |this, window, cx| {
this.search(window, cx);
})
.ok();
})
}
/// Search rooms and groups by name, then messages by content.
fn search(&mut self, window: &mut Window, cx: &mut Context<Self>) {
/// Search rooms and groups by name, then indexed messages by content.
fn search(&mut self, cx: &mut Context<Self>) {
let query = self.find_input.read(cx).value().to_string();
let terms: Vec<String> = query
.to_lowercase()
@@ -561,49 +537,13 @@ impl Sidebar {
return;
}
// Name matches are local so show them right away while messages are searched
let rooms = ChatRegistry::global(cx).read(cx).find(&query, cx);
let chat = ChatRegistry::global(cx);
let rooms = chat.read(cx).find(&query, cx);
let groups = find_groups(&terms, cx);
self.searched = Some(query.clone());
self.results = rooms
.into_iter()
.map(SearchResult::Room)
.chain(groups.into_iter().map(SearchResult::Group))
.collect();
cx.notify();
// Block the input until the search completes
self.set_finding(true, window, cx);
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let find_messages = cx.background_spawn(search_messages(client, terms, FIND_LIMIT));
self.search_task = Some(cx.spawn_in(window, async move |this, cx| {
let matches = find_messages.await;
this.update_in(cx, move |this, window, cx| {
match matches {
Ok(matches) => this.push_messages(query, matches, cx),
Err(error) => {
window.push_notification(Notification::error(error.to_string()), cx);
}
}
this.set_finding(false, window, cx);
})
.ok();
}));
}
/// Append message content matches, keeping results for the latest query only.
fn push_messages(&mut self, query: String, matches: Vec<MessageMatch>, cx: &mut Context<Self>) {
if self.searched.as_deref() != Some(query.as_str()) {
return;
}
let chat = ChatRegistry::global(cx);
let mut hits = matches
let messages = chat
.read(cx)
.search_messages(&terms, FIND_LIMIT)
.into_iter()
.filter_map(|matched| {
let room = chat.read(cx).room(&matched.room, cx)?.upgrade()?;
@@ -611,37 +551,23 @@ impl Sidebar {
Some(MessageHit {
room,
author: matched.author,
content: matched.content,
content: matched.content.into(),
created_at: matched.created_at,
})
})
.collect::<Vec<_>>();
// Newest matching message first
hits.sort_by_key(|hit| Reverse(hit.created_at));
self.results
.extend(hits.into_iter().map(SearchResult::Message));
self.results = rooms
.into_iter()
.map(SearchResult::Room)
.chain(groups.into_iter().map(SearchResult::Group))
.chain(messages.into_iter().map(SearchResult::Message))
.collect();
cx.notify();
}
/// Set the search status and block the input while searching.
fn set_finding(&mut self, finding: bool, window: &mut Window, cx: &mut Context<Self>) {
self.finding = finding;
self.find_input.update(cx, |input, cx| {
input.set_loading(finding, window, cx);
});
cx.notify();
}
/// Discard the search results and cancel any ongoing search.
fn clear_results(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if self.finding {
self.set_finding(false, window, cx);
}
self.searched = None;
self.search_task = None;
/// Discard the search results.
fn clear_results(&mut self, cx: &mut Context<Self>) {
self.results.clear();
cx.notify();
}
@@ -858,11 +784,7 @@ impl Sidebar {
rows.push(SidebarRow::Heading("Messages"));
if messages.is_empty() {
rows.push(SidebarRow::Empty(if self.finding {
"Searching..."
} else {
"No messages found"
}));
rows.push(SidebarRow::Empty("No messages found"));
} else {
rows.extend(messages.into_iter().map(|hit| SidebarRow::Message { hit }));
}
@@ -1136,17 +1058,19 @@ impl Sidebar {
match row {
SidebarRow::Section(section) => {
let section = *section;
let label = section.label();
let open = match section {
ListSection::Pins => self.pins_open,
ListSection::Groups => self.is_groups_open(cx),
ListSection::Chats => self.is_chats_open(cx),
};
let icon = if open {
IconName::CaretDown
} else {
IconName::CaretRight
};
let label = section.label();
Nav::new(label)
.label(label)
@@ -1178,10 +1102,11 @@ impl Sidebar {
.text_color(cx.theme().text_placeholder)
.into_any_element()
}
SidebarRow::Empty(text) => h_flex()
SidebarRow::Empty(text) => v_flex()
.h_10()
.w_full()
.px_1p5()
.items_center()
.child(
h_flex()
.h_9()
@@ -1210,7 +1135,6 @@ impl Sidebar {
let nav = Nav::new(index)
.label(name)
.text_sm()
.font_medium()
.when(!hide_avatar, |this| {
this.prefix(
Avatar::from_source(picture)
@@ -1266,7 +1190,6 @@ impl Sidebar {
Nav::new(index)
.label(person.name())
.text_sm()
.font_medium()
.when(!hide_avatar, |this| {
this.prefix(
Avatar::from_source(person.avatar())
@@ -1307,7 +1230,6 @@ impl Sidebar {
let nav = Nav::new(index)
.label(name)
.text_sm()
.font_medium()
.when(!hide_avatar, |this| {
this.prefix(
Avatar::from_source(picture)