BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / search.rs

search.rs

search.rs 1.7 KiB Download Raw
1use chrono::{DateTime, Utc};
2use store::{ConversationHit, SearchHit};
3
4use crate::engine::{META_USER_ID, SyncEngine};
5use crate::error::Result;
6use crate::remote::Remote;
7
8impl<R: Remote> SyncEngine<R> {
9 /// Newest first, prefix match on every word, `from:name` filters the sender. Snippets mark matches with `HIGHLIGHT_START` / `HIGHLIGHT_END`.
10 pub fn search_messages(&self, query: &str, limit: usize) -> Result<Vec<SearchHit>> {
11 Ok(self.store.search_messages(query, None, limit)?)
12 }
13
14 pub fn search_messages_in(
15 &self,
16 conversation_id: &str,
17 query: &str,
18 limit: usize,
19 ) -> Result<Vec<SearchHit>> {
20 Ok(self
21 .store
22 .search_messages(query, Some(conversation_id), limit)?)
23 }
24
25 pub fn search_conversations(&self, query: &str, limit: usize) -> Result<Vec<ConversationHit>> {
26 Ok(self.store.search_conversations(query, limit)?)
27 }
28
29 /// First cached message from others after the chat's last read time, `None` for a read chat. Call before `mark_read`.
30 pub fn channel_read_until(&self, channel_id: &str) -> Option<DateTime<Utc>> {
31 self.store.channel_read_until(channel_id).ok().flatten()
32 }
33
34 pub fn first_unread_message_id(&self, conversation_id: &str) -> Option<String> {
35 let chat = self
36 .store
37 .chat(conversation_id)
38 .ok()
39 .flatten()
40 .filter(|chat| chat.unread)?;
41 let my_user_id = self
42 .store
43 .meta(META_USER_ID)
44 .ok()
45 .flatten()
46 .unwrap_or_default();
47 self.store
48 .first_message_after_from_others(conversation_id, chat.last_read_at, &my_user_id)
49 .ok()
50 .flatten()
51 }
52}