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