| 1 | use ruma::{OwnedEventId, OwnedRoomId}; |
| 2 | #[cfg(feature = "serde")] |
| 3 | use serde::{Deserialize, Serialize}; |
| 4 | |
| 5 | use crate::TimelineEvent; |
| 6 | |
| 7 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 8 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 9 | #[non_exhaustive] |
| 10 | pub enum SearchScope { |
| 11 | Room(OwnedRoomId), |
| 12 | /// a space's rooms, resolved by the host |
| 13 | Rooms(Vec<OwnedRoomId>), |
| 14 | /// every joined room |
| 15 | All, |
| 16 | } |
| 17 | |
| 18 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 19 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 20 | #[non_exhaustive] |
| 21 | pub enum SearchCommand { |
| 22 | /// opens a search that owns the media of every hit it hands out. at most 8 stay open, |
| 23 | /// opening another closes the oldest. words must all match, `"quoted phrases"` match |
| 24 | /// in order and `-word` excludes, newest first |
| 25 | Search { |
| 26 | query: String, |
| 27 | scope: SearchScope, |
| 28 | limit: u16, |
| 29 | }, |
| 30 | /// `cursor` is the previous page's `next_cursor` |
| 31 | More { |
| 32 | search_id: String, |
| 33 | cursor: String, |
| 34 | limit: u16, |
| 35 | }, |
| 36 | /// frees the search's media. its hits' media ids stop resolving |
| 37 | Close { search_id: String }, |
| 38 | Coverage(SearchScope), |
| 39 | } |
| 40 | |
| 41 | /// a search's media stay resolvable until it closes, and past `EngineConfig::max_context_media` |
| 42 | /// its least recently used are dropped |
| 43 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 44 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 45 | pub struct SearchSession { |
| 46 | pub search_id: String, |
| 47 | pub page: SearchPage, |
| 48 | } |
| 49 | |
| 50 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 51 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 52 | pub struct SearchPage { |
| 53 | pub hits: Vec<SearchHit>, |
| 54 | /// `None` once there's nothing older that matches |
| 55 | pub next_cursor: Option<String>, |
| 56 | pub coverage: SearchCoverage, |
| 57 | } |
| 58 | |
| 59 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 60 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 61 | pub struct SearchHit { |
| 62 | pub room_id: OwnedRoomId, |
| 63 | pub event_id: OwnedEventId, |
| 64 | pub timestamp_ms: u64, |
| 65 | /// as event context shows it, with the latest edit applied |
| 66 | pub event: TimelineEvent, |
| 67 | /// utf-8 byte ranges of matched words in the content's text, the message body, a |
| 68 | /// sticker's body or a poll's question |
| 69 | pub matches: Vec<SearchMatch>, |
| 70 | } |
| 71 | |
| 72 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 73 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 74 | pub struct SearchMatch { |
| 75 | pub start: u32, |
| 76 | pub end: u32, |
| 77 | } |
| 78 | |
| 79 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 80 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 81 | pub struct SearchCoverage { |
| 82 | pub rooms: Vec<RoomSearchCoverage>, |
| 83 | /// false when the index lives in memory and starts over with the next engine |
| 84 | pub persistent: bool, |
| 85 | /// messages that couldn't be decrypted yet, so can't be found yet |
| 86 | pub undecrypted: usize, |
| 87 | } |
| 88 | |
| 89 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 90 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 91 | pub struct RoomSearchCoverage { |
| 92 | pub room_id: OwnedRoomId, |
| 93 | pub oldest_indexed_ms: Option<u64>, |
| 94 | pub newest_indexed_ms: Option<u64>, |
| 95 | /// backfill walked back to the oldest event the server would hand over |
| 96 | pub start_reached: bool, |
| 97 | pub backfill: SearchBackfill, |
| 98 | } |
| 99 | |
| 100 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 101 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 102 | pub enum SearchBackfill { |
| 103 | Waiting, |
| 104 | Running, |
| 105 | /// this engine's page budget for the room is spent, the next one carries on |
| 106 | Paused, |
| 107 | Complete, |
| 108 | Disabled, |
| 109 | Failed { |
| 110 | message: String, |
| 111 | retry_at_ms: u64, |
| 112 | }, |
| 113 | } |