c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1 | mod feed; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 2 | #[cfg_attr(not(any(feature = "sqlite", feature = "indexeddb")), allow(dead_code))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 3 | mod index; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 4 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 5 | pub(crate) mod store; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 6 | mod text; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 7 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 8 | use std::{ |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 9 | collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 10 | sync::Arc, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 11 | time::Duration, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 12 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 13 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 14 | use futures_util::{ |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 15 | StreamExt, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 16 | future::Abortable, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 17 | stream::{AbortHandle, FuturesUnordered, SelectAll, abortable}, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 18 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 19 | use matrix_sdk::{ |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 20 | Client, Room, RoomState, SendOutsideWasm, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 21 | deserialized_responses::TimelineEvent as SdkTimelineEvent, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 22 | event_cache::{RoomEventCacheSubscriber, RoomEventCacheUpdate}, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 23 | room::{Messages, MessagesOptions}, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 24 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 25 | use matrix_sdk_base::RoomInfoNotableUpdateReasons; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 26 | use matrix_sdk_ui::eyeball_im::VectorDiff; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 27 | use ruma::{ |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 28 | OwnedRoomId, OwnedUserId, UInt, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 29 | api::client::filter::RoomEventFilter, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 30 | events::{ |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 31 | ignored_user_list::IgnoredUserListEventContent, relation::RelationType, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 32 | room::encrypted::OriginalSyncRoomEncryptedEvent, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 33 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 34 | serde::Raw, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 35 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 36 | use serde_json::Value; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 37 | use sha2::{Digest, Sha256}; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 38 | use tokio::sync::{Mutex, broadcast, mpsc, oneshot}; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 39 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 40 | use self::{ |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 41 | feed::{Effect, Observed, Redactions}, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 42 | index::{Changes, Doc, Hit, KeywordIndex}, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 43 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 44 | use crate::{ |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 45 | CommandOutput, EngineConfig, Error, ErrorKind, Event, RoomSearchCoverage, SearchBackfill, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 46 | SearchCloseReason, SearchCommand, SearchCoverage, SearchHit, SearchInMemory, SearchMatch, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 47 | SearchPage, SearchScope, SearchSession, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 48 | engine::{MediaService, SearchStore, context, executor::EngineBoxFuture}, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 49 | normalise::{MediaRegistration, MediaScope}, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 50 | transport::EventSender, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 51 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 52 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 53 | const MAX_LIMIT: u16 = 50; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 54 | /// a consumer that never closes its searches still holds at most this many |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 55 | const MAX_SEARCHES: usize = 8; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 56 | /// hits read back at once while a page is put together |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 57 | const PREVIEWS_AT_ONCE: usize = 8; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 58 | const DECRYPTS_AT_ONCE: usize = 8; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 59 | const COMMIT_AFTER_MS: u64 = 5_000; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 60 | const COMMIT_BUFFERED: usize = 2_000; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 61 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 62 | const COMMIT_RETRY_MS: u64 = 60_000; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 63 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 64 | const COMMIT_ATTEMPTS: u32 = 5; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 65 | const IDLE_SUBSCRIPTION_MS: u64 = 5 * 60_000; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 66 | const BACKFILL_RETRY_MS: u64 = 60_000; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 67 | const BACKFILL_RETRY_MAX_MS: u64 = 15 * 60_000; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 68 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 69 | const SCHEMA: u64 = 2; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 70 | /// waiting undecryptable events are saved in this many blobs, so one arriving rewrites a sixteenth |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 71 | const UTD_SHARDS: u8 = 16; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 72 | const BACKFILL_TYPES: [&str; 6] = [ |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 73 | "m.room.message", |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 74 | "m.room.encrypted", |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 75 | "m.room.redaction", |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 76 | "m.sticker", |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 77 | "m.poll.start", |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 78 | "org.matrix.msc3381.poll.start", |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 79 | ]; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 80 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 81 | #[cfg(not(target_family = "wasm"))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 82 | type BoxStream<T> = futures_util::stream::BoxStream<'static, T>; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 83 | #[cfg(target_family = "wasm")] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 84 | type BoxStream<T> = futures_util::stream::LocalBoxStream<'static, T>; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 85 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 86 | /// a local index of message text. keyword search over tantivy is the only one today, an |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 87 | /// embedding index can stand beside it later without the public api noticing |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 88 | #[cfg_attr(not(any(feature = "sqlite", feature = "indexeddb")), allow(dead_code))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 89 | pub(crate) trait MessageIndex: SendOutsideWasm { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 90 | fn get(&self, key: &str) -> Option<Doc>; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 91 | fn by_content(&self, content: &str) -> Option<Doc>; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 92 | /// replaces whatever `doc.key` held |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 93 | fn put(&mut self, doc: Doc); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 94 | fn remove(&mut self, key: &str) -> bool; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 95 | /// every provisional doc waiting on `target`, whoever sent its edit |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 96 | fn remove_provisional(&mut self, target: &str) -> bool; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 97 | fn buffered(&self) -> usize; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 98 | /// newest first, strictly older than `before`. also hands back the analysed query terms |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 99 | fn search( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 100 | &mut self, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 101 | query: &str, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 102 | rooms: Option<&[String]>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 103 | before: Option<(u64, u64)>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 104 | limit: usize, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 105 | ) -> Result<(Vec<Hit>, Vec<String>), Error>; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 106 | fn maintain(&mut self) -> Result<(), Error>; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 107 | fn changes(&mut self) -> Result<Changes, Error>; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 108 | fn committed(&mut self, changes: &Changes); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 109 | /// stops tracking what a commit would need, once nothing will be committed again |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 110 | fn detach(&mut self); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 111 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 112 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 113 | #[derive(Clone, Debug, Default)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 114 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 115 | struct RoomProgress { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 116 | oldest: Option<u64>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 117 | newest: Option<u64>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 118 | /// where the next backfill page starts, `None` before the first one |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 119 | tail: Option<String>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 120 | started: bool, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 121 | start_reached: bool, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 122 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 123 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 124 | #[derive(Clone, Debug)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 125 | #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 126 | struct Pending { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 127 | room: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 128 | session_id: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 129 | ts: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 130 | raw: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 131 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 132 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 133 | #[derive(Clone, Copy)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 134 | struct Budget { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 135 | pages_per_room: u16, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 136 | page_size: u16, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 137 | interval_ms: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 138 | max_pending: usize, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 139 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 140 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 141 | impl Budget { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 142 | fn from_config(config: &EngineConfig) -> Self { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 143 | Self { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 144 | pages_per_room: config.search_backfill_pages_per_room, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 145 | page_size: config.search_backfill_page_size.max(1), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 146 | interval_ms: config.search_backfill_interval_ms, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 147 | max_pending: config.max_search_undecrypted, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 148 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 149 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 150 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 151 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 152 | struct RawPage { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 153 | hits: Vec<Hit>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 154 | terms: Vec<String>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 155 | more: bool, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 156 | coverage: SearchCoverage, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 157 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 158 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 159 | enum Request { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 160 | Search { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 161 | query: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 162 | rooms: Option<Vec<String>>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 163 | before: Option<(u64, u64)>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 164 | limit: usize, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 165 | coverage: Vec<OwnedRoomId>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 166 | reply: oneshot::Sender<Result<RawPage, Error>>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 167 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 168 | Coverage(Vec<OwnedRoomId>, oneshot::Sender<SearchCoverage>), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 169 | /// a hit that turned out redacted when its preview was read |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 170 | Forget(String), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 171 | Stop(oneshot::Sender<()>), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 172 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 173 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 174 | #[derive(Clone)] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 175 | pub(crate) struct Handle { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 176 | requests: mpsc::Sender<Request>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 177 | client: Client, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 178 | media: Arc<MediaService>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 179 | sessions: Arc<Sessions>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 180 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 181 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 182 | enum Work { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 183 | Backfill { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 184 | room: OwnedRoomId, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 185 | head: bool, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 186 | from: Option<String>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 187 | result: Result<Messages, Error>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 188 | }, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 189 | /// the room was left while its page was in flight |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 190 | BackfillCancelled, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 191 | Decrypted { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 192 | key: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 193 | room: OwnedRoomId, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 194 | value: Option<Value>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 195 | }, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 196 | /// the original first, then whatever edits of it the cache or server still has |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 197 | Refetched { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 198 | room: OwnedRoomId, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 199 | values: Vec<Value>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 200 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 201 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 202 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 203 | enum RoomUpdate { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 204 | Events(Vec<SdkTimelineEvent>), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 205 | Lagged, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 206 | Closed, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 207 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 208 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 209 | struct Failure { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 210 | message: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 211 | retry_at_ms: u64, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 212 | delay_ms: u64, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 213 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 214 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 215 | enum Persistence { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 216 | Store(SearchStore), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 217 | Memory(SearchInMemory), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 218 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 219 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 220 | pub(crate) struct Service { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 221 | client: Client, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 222 | index: Box<dyn MessageIndex>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 223 | persistence: Persistence, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 224 | #[cfg_attr(not(any(feature = "sqlite", feature = "indexeddb")), allow(dead_code))] |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 225 | commit_failures: u32, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 226 | /// storage keys a load found nothing pointing at, dropped with the next commit |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 227 | #[cfg_attr(not(any(feature = "sqlite", feature = "indexeddb")), allow(dead_code))] |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 228 | orphans: Vec<String>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 229 | rooms: BTreeMap<String, RoomProgress>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 230 | /// each room's newest indexed message when this engine started, what a head pass looks for |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 231 | baselines: HashMap<String, u64>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 232 | pending: BTreeMap<String, Pending>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 233 | dirty_shards: BTreeSet<u8>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 234 | decrypt_queue: VecDeque<String>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 235 | decrypting: usize, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 236 | redactions: Redactions, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 237 | dirty_since: Option<u64>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 238 | commit_after: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 239 | budget: Budget, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 240 | requests: mpsc::Receiver<Request>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 241 | subscriptions: HashMap<OwnedRoomId, (AbortHandle, u64)>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 242 | updates: SelectAll<BoxStream<(OwnedRoomId, RoomUpdate)>>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 243 | work: FuturesUnordered<EngineBoxFuture<'static, Work>>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 244 | pages: HashMap<OwnedRoomId, u16>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 245 | heads: VecDeque<OwnedRoomId>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 246 | headed: HashSet<OwnedRoomId>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 247 | head_tokens: HashMap<OwnedRoomId, String>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 248 | backfilling: Option<(OwnedRoomId, AbortHandle)>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 249 | next_backfill: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 250 | failures: HashMap<OwnedRoomId, Failure>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 251 | turn: usize, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 252 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 253 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 254 | fn now() -> u64 { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 255 | ruma::MilliSecondsSinceUnixEpoch::now().get().into() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 256 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 257 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 258 | fn value_of(event: &SdkTimelineEvent) -> Option<Value> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 259 | serde_json::from_str(event.raw().json().get()).ok() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 260 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 261 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 262 | fn encode_cursor((ts, tie): (u64, u64)) -> String { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 263 | format!("{ts:x}.{tie:x}") |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 264 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 265 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 266 | fn decode_cursor(cursor: &str) -> Option<(u64, u64)> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 267 | let (ts, tie) = cursor.split_once('.')?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 268 | Some(( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 269 | u64::from_str_radix(ts, 16).ok()?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 270 | u64::from_str_radix(tie, 16).ok()?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 271 | )) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 272 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 273 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 274 | fn shard(key: &str) -> u8 { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 275 | Sha256::digest(key.as_bytes())[0] % UTD_SHARDS |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 276 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 277 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 278 | /// a 400 to a page with a saved token means the server no longer takes the token |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 279 | fn token_rejected(error: &Error) -> bool { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 280 | error |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 281 | .server |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 282 | .as_ref() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 283 | .is_some_and(|server| server.status == 400 || server.errcode == "M_INVALID_PARAM") |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 284 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 285 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 286 | impl Handle { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 287 | pub(crate) async fn execute(&self, command: SearchCommand) -> Result<CommandOutput, Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 288 | match command { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 289 | SearchCommand::Search { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 290 | query, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 291 | scope, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 292 | limit, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 293 | } => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 294 | let media_scope = self.media.context_scope(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 295 | let prefix = media_prefix(&media_scope); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 296 | let (page, media) = self.search(&query, &scope, None, limit, &prefix).await?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 297 | let search_id = self |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 298 | .sessions |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 299 | .open(&self.media, query, scope, media_scope, media) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 300 | .await; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 301 | Ok(CommandOutput::SearchSession(SearchSession { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 302 | search_id, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 303 | page, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 304 | })) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 305 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 306 | SearchCommand::More { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 307 | search_id, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 308 | cursor, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 309 | limit, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 310 | } => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 311 | let (query, scope, prefix) = self.sessions.get(&search_id).await?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 312 | let (page, media) = self |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 313 | .search(&query, &scope, Some(cursor.as_str()), limit, &prefix) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 314 | .await?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 315 | self.sessions |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 316 | .register(&self.media, &search_id, media) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 317 | .await?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 318 | Ok(CommandOutput::SearchPage(page)) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 319 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 320 | SearchCommand::Close { search_id } => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 321 | self.sessions.close(&self.media, &search_id).await?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 322 | Ok(CommandOutput::Done) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 323 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 324 | SearchCommand::Coverage(scope) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 325 | let (reply, response) = oneshot::channel(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 326 | self.requests |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 327 | .send(Request::Coverage(self.scope_rooms(&scope), reply)) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 328 | .await |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 329 | .map_err(|_| Error::interrupted())?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 330 | Ok(CommandOutput::SearchCoverage( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 331 | response.await.map_err(|_| Error::interrupted())?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 332 | )) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 333 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 334 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 335 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 336 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 337 | /// the media service is cleared beside this, so nothing is released one by one |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 338 | pub(crate) async fn stop(&self) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 339 | let (reply, response) = oneshot::channel(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 340 | if self.requests.send(Request::Stop(reply)).await.is_ok() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 341 | let _ = response.await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 342 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 343 | self.sessions.open.lock().await.clear(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 344 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 345 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 346 | fn scope_rooms(&self, scope: &SearchScope) -> Vec<OwnedRoomId> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 347 | match scope { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 348 | SearchScope::Room(room_id) => vec![room_id.clone()], |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 349 | SearchScope::Rooms(room_ids) => room_ids.clone(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 350 | SearchScope::All => self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 351 | .client |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 352 | .joined_rooms() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 353 | .into_iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 354 | .map(|room| room.room_id().to_owned()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 355 | .collect(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 356 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 357 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 358 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 359 | async fn search( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 360 | &self, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 361 | query: &str, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 362 | scope: &SearchScope, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 363 | cursor: Option<&str>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 364 | limit: u16, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 365 | media_prefix: &str, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 366 | ) -> Result<(SearchPage, Vec<MediaRegistration>), Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 367 | if limit == 0 || limit > MAX_LIMIT { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 368 | return Err(Error::new( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 369 | ErrorKind::Invalid, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 370 | "search limit must be between 1 and 50", |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 371 | )); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 372 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 373 | let before = match cursor { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 374 | Some(cursor) => Some( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 375 | decode_cursor(cursor) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 376 | .ok_or_else(|| Error::new(ErrorKind::Invalid, "search cursor is invalid"))?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 377 | ), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 378 | None => None, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 379 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 380 | let rooms = self.scope_rooms(scope); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 381 | let filter = Some(rooms.iter().map(ToString::to_string).collect()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 382 | let (reply, response) = oneshot::channel(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 383 | self.requests |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 384 | .send(Request::Search { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 385 | query: query.to_owned(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 386 | rooms: filter, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 387 | before, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 388 | limit: usize::from(limit), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 389 | coverage: rooms, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 390 | reply, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 391 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 392 | .await |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 393 | .map_err(|_| Error::interrupted())?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 394 | let raw = response.await.map_err(|_| Error::interrupted())??; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 395 | let next_cursor = raw |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 396 | .more |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 397 | .then(|| raw.hits.last().map(|hit| encode_cursor((hit.ts, hit.tie)))) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 398 | .flatten(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 399 | let ignored = self.ignored_users().await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 400 | let current_user_id = self.client.user_id().map(ToOwned::to_owned); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 401 | let terms = &raw.terms; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 402 | let previews: Vec<_> = futures_util::stream::iter( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 403 | raw.hits |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 404 | .into_iter() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 405 | .filter(|hit| !ignored.iter().any(|user| user.as_str() == hit.sender)), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 406 | ) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 407 | .map(|hit| self.preview(hit, current_user_id.clone(), terms, media_prefix)) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 408 | .buffered(PREVIEWS_AT_ONCE) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 409 | .collect() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 410 | .await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 411 | let mut hits = Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 412 | let mut media = Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 413 | for preview in previews { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 414 | match preview { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 415 | Preview::Shown(hit, registrations) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 416 | hits.push(hit); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 417 | media.extend(registrations); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 418 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 419 | Preview::Redacted(key) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 420 | let _ = self.requests.send(Request::Forget(key)).await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 421 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 422 | Preview::Unavailable => {} |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 423 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 424 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 425 | let page = SearchPage { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 426 | hits, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 427 | next_cursor, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 428 | coverage: raw.coverage, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 429 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 430 | Ok((page, media)) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 431 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 432 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 433 | async fn ignored_users(&self) -> Vec<OwnedUserId> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 434 | self.client |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 435 | .account() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 436 | .account_data::<IgnoredUserListEventContent>() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 437 | .await |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 438 | .ok() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 439 | .flatten() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 440 | .and_then(|raw| raw.deserialize().ok()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 441 | .map(|content| content.ignored_users.into_keys().collect()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 442 | .unwrap_or_default() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 443 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 444 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 445 | async fn preview( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 446 | &self, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 447 | hit: Hit, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 448 | current_user_id: Option<OwnedUserId>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 449 | terms: &[String], |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 450 | media_prefix: &str, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 451 | ) -> Preview { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 452 | let (Ok(room_id), Ok(event_id)) = ( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 453 | OwnedRoomId::try_from(hit.room.as_str()), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 454 | ruma::OwnedEventId::try_from(hit.key.as_str()), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 455 | ) else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 456 | return Preview::Unavailable; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 457 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 458 | let Some(room) = self.client.get_room(&room_id) else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 459 | return Preview::Unavailable; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 460 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 461 | let Some((mut original, highlight)) = load(&room, &hit.key).await else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 462 | return Preview::Unavailable; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 463 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 464 | if original.pointer("/unsigned/redacted_because").is_some() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 465 | return Preview::Redacted(hit.key); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 466 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 467 | // the index already settled which edit is newest, and a bundled one can be older |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 468 | if let Some(relations) = original |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 469 | .pointer_mut("/unsigned/m.relations") |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 470 | .and_then(Value::as_object_mut) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 471 | { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 472 | relations.remove("m.replace"); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 473 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 474 | let mut values = vec![original]; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 475 | if hit.content != hit.key |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 476 | && let Some((edit, _)) = load(&room, &hit.content).await |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 477 | { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 478 | values.push(edit); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 479 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 480 | let Some((event, registrations)) = context::preview( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 481 | &room, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 482 | current_user_id.as_deref(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 483 | &values, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 484 | highlight, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 485 | media_prefix, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 486 | ) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 487 | .await |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 488 | else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 489 | return Preview::Unavailable; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 490 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 491 | let text = match &event.content { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 492 | crate::TimelineContent::Message(message) => Some(message.body.as_str()), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 493 | crate::TimelineContent::Sticker { body, .. } => Some(body.as_str()), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 494 | crate::TimelineContent::Poll { question } => Some(question.as_str()), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 495 | _ => None, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 496 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 497 | let matches = text |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 498 | .map(|text| text::match_ranges(text, terms)) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 499 | .unwrap_or_default() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 500 | .into_iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 501 | .filter_map(|(start, end)| { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 502 | Some(SearchMatch { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 503 | start: u32::try_from(start).ok()?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 504 | end: u32::try_from(end).ok()?, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 505 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 506 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 507 | .collect(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 508 | Preview::Shown( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 509 | SearchHit { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 510 | room_id, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 511 | event_id, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 512 | timestamp_ms: event.timestamp_ms, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 513 | event, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 514 | matches, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 515 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 516 | registrations, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 517 | ) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 518 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 519 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 520 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 521 | enum Preview { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 522 | Shown(SearchHit, Vec<MediaRegistration>), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 523 | Redacted(String), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 524 | Unavailable, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 525 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 526 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 527 | /// open searches, oldest first, each owning the media of the hits it handed out |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 528 | struct Sessions { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 529 | open: Mutex<VecDeque<Session>>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 530 | events: EventSender, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 531 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 532 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 533 | /// media ids carry the search, so two owners of one event never share an entry |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 534 | fn media_prefix(scope: &MediaScope) -> String { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 535 | format!("{}#", scope.nonce()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 536 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 537 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 538 | struct Session { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 539 | id: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 540 | query: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 541 | scope: SearchScope, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 542 | media: MediaScope, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 543 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 544 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 545 | impl Sessions { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 546 | async fn open( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 547 | &self, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 548 | media: &MediaService, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 549 | query: String, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 550 | scope: SearchScope, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 551 | media_scope: MediaScope, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 552 | registrations: Vec<MediaRegistration>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 553 | ) -> String { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 554 | let id = format!("search-{}", media_scope.nonce()); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 555 | let mut evicted = Vec::new(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 556 | { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 557 | let mut open = self.open.lock().await; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 558 | media.register(&media_scope, registrations).await; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 559 | open.push_back(Session { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 560 | id: id.clone(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 561 | query, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 562 | scope, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 563 | media: media_scope, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 564 | }); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 565 | while open.len() > MAX_SEARCHES { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 566 | if let Some(oldest) = open.pop_front() { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 567 | media.release(&oldest.media).await; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 568 | evicted.push(oldest.id); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 569 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 570 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 571 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 572 | for search_id in evicted { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 573 | let event = Event::SearchClosed { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 574 | search_id, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 575 | reason: SearchCloseReason::Evicted, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 576 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 577 | if self.events.send(event).await.is_err() { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 578 | break; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 579 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 580 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 581 | id |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 582 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 583 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 584 | /// the query, scope and media prefix the search's pages use |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 585 | async fn get(&self, id: &str) -> Result<(String, SearchScope, String), Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 586 | let open = self.open.lock().await; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 587 | let session = open |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 588 | .iter() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 589 | .find(|session| session.id == id) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 590 | .ok_or_else(not_open)?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 591 | Ok(( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 592 | session.query.clone(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 593 | session.scope.clone(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 594 | media_prefix(&session.media), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 595 | )) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 596 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 597 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 598 | /// under the lock, so a page that lands after its close registers nothing |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 599 | async fn register( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 600 | &self, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 601 | media: &MediaService, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 602 | id: &str, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 603 | registrations: Vec<MediaRegistration>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 604 | ) -> Result<(), Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 605 | let open = self.open.lock().await; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 606 | let session = open |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 607 | .iter() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 608 | .find(|session| session.id == id) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 609 | .ok_or_else(not_open)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 610 | media.register(&session.media, registrations).await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 611 | Ok(()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 612 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 613 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 614 | async fn close(&self, media: &MediaService, id: &str) -> Result<(), Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 615 | let mut open = self.open.lock().await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 616 | let index = open |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 617 | .iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 618 | .position(|session| session.id == id) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 619 | .ok_or_else(not_open)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 620 | if let Some(session) = open.remove(index) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 621 | media.release(&session.media).await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 622 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 623 | Ok(()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 624 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 625 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 626 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 627 | fn not_open() -> Error { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 628 | Error::new( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 629 | ErrorKind::SearchNotOpen, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 630 | "search is closed or was never opened", |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 631 | ) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 632 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 633 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 634 | async fn load(room: &Room, event_id: &str) -> Option<(Value, bool)> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 635 | let event_id = ruma::EventId::parse(event_id).ok()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 636 | let event = room |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 637 | .load_or_fetch_event(&event_id, None) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 638 | .await |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 639 | .inspect_err(|error| tracing::debug!(%event_id, %error, "loading a search hit failed")) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 640 | .ok()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 641 | let highlight = event |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 642 | .push_actions() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 643 | .is_some_and(|actions| actions.iter().any(ruma::push::Action::is_highlight)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 644 | Some((value_of(&event)?, highlight)) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 645 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 646 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 647 | /// the original and every edit of it still around, so a redacted latest edit falls back to |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 648 | /// the one before it rather than to the original |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 649 | async fn load_with_edits(room: &Room, event_id: &str) -> Vec<Value> { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 650 | let Ok(event_id) = ruma::EventId::parse(event_id) else { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 651 | return Vec::new(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 652 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 653 | match room |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 654 | .load_or_fetch_event_with_relations(&event_id, Some(vec![RelationType::Replacement]), None) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 655 | .await |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 656 | { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 657 | Ok((original, edits)) => std::iter::once(original) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 658 | .chain(edits) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 659 | .filter_map(|event| value_of(&event)) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 660 | .collect(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 661 | Err(error) => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 662 | tracing::debug!(%event_id, %error, "reading an original again failed"); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 663 | Vec::new() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 664 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 665 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 666 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 667 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 668 | impl Service { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 669 | pub(crate) fn new( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 670 | client: Client, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 671 | media: Arc<MediaService>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 672 | store: Option<Result<SearchStore, Error>>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 673 | events: EventSender, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 674 | config: &EngineConfig, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 675 | ) -> (Handle, Self) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 676 | let (sender, requests) = mpsc::channel(config.command_capacity.max(1)); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 677 | let persistence = match store { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 678 | Some(Ok(store)) => Persistence::Store(store), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 679 | Some(Err(error)) => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 680 | tracing::error!(%error, "search store unavailable, the index lives in memory"); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 681 | Persistence::Memory(SearchInMemory::OpenFailed) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 682 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 683 | None => Persistence::Memory(SearchInMemory::NoStore), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 684 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 685 | let service = Self { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 686 | client: client.clone(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 687 | index: Box::new(KeywordIndex::new(matches!( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 688 | persistence, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 689 | Persistence::Store(_) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 690 | ))), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 691 | persistence, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 692 | commit_failures: 0, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 693 | orphans: Vec::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 694 | rooms: BTreeMap::new(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 695 | baselines: HashMap::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 696 | pending: BTreeMap::new(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 697 | dirty_shards: BTreeSet::new(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 698 | decrypt_queue: VecDeque::new(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 699 | decrypting: 0, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 700 | redactions: Redactions::default(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 701 | dirty_since: None, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 702 | commit_after: 0, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 703 | budget: Budget::from_config(config), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 704 | requests, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 705 | subscriptions: HashMap::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 706 | updates: SelectAll::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 707 | work: FuturesUnordered::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 708 | pages: HashMap::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 709 | heads: VecDeque::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 710 | headed: HashSet::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 711 | head_tokens: HashMap::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 712 | backfilling: None, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 713 | next_backfill: 0, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 714 | failures: HashMap::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 715 | turn: 0, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 716 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 717 | ( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 718 | Handle { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 719 | requests: sender, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 720 | client, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 721 | media, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 722 | sessions: Arc::new(Sessions { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 723 | open: Mutex::default(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 724 | events, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 725 | }), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 726 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 727 | service, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 728 | ) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 729 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 730 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 731 | fn in_memory(&self) -> Option<SearchInMemory> { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 732 | match self.persistence { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 733 | Persistence::Store(_) => None, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 734 | Persistence::Memory(reason) => Some(reason), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 735 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 736 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 737 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 738 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 739 | fn fall_back(&mut self, reason: SearchInMemory) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 740 | self.persistence = Persistence::Memory(reason); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 741 | self.index.detach(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 742 | self.orphans.clear(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 743 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 744 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 745 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 746 | async fn load(&mut self) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 747 | let Persistence::Store(store) = &self.persistence else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 748 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 749 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 750 | let mut blobs = match store.load().await { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 751 | Ok(blobs) => blobs, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 752 | Err(error) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 753 | tracing::error!(%error, "reading the search store failed, the index lives in memory"); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 754 | self.fall_back(SearchInMemory::ReadFailed); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 755 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 756 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 757 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 758 | if blobs.is_empty() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 759 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 760 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 761 | let restored = (|| { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 762 | let meta: Value = serde_json::from_slice(&store.take(&mut blobs, "meta")?).ok()?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 763 | if meta["schema"].as_u64() != Some(SCHEMA) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 764 | return None; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 765 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 766 | let rooms: BTreeMap<String, RoomProgress> = |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 767 | serde_json::from_value(meta["rooms"].clone()).ok()?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 768 | let mut pending = BTreeMap::new(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 769 | for shard in 0..UTD_SHARDS { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 770 | if let Some(bytes) = store.take(&mut blobs, &format!("utd/{shard}")) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 771 | let part: BTreeMap<String, Pending> = serde_json::from_slice(&bytes).ok()?; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 772 | pending.extend(part); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 773 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 774 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 775 | let index = KeywordIndex::load(&meta["index"], |name| store.take(&mut blobs, name)) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 776 | .inspect_err(|error| tracing::warn!(%error, "search segments are unreadable")) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 777 | .ok()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 778 | Some((index, rooms, pending)) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 779 | })(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 780 | match restored { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 781 | Some((index, rooms, pending)) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 782 | self.index = Box::new(index); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 783 | self.baselines = rooms |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 784 | .iter() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 785 | .filter_map(|(room, progress)| Some((room.clone(), progress.newest?))) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 786 | .collect(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 787 | self.rooms = rooms; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 788 | self.pending = pending; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 789 | self.orphans = blobs.leftover(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 790 | if !self.orphans.is_empty() { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 791 | self.touch(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 792 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 793 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 794 | None => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 795 | tracing::warn!("resetting an unreadable search index"); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 796 | if let Err(error) = store.reset().await { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 797 | tracing::error!(%error, "search store unavailable, the index lives in memory"); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 798 | self.fall_back(SearchInMemory::ReadFailed); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 799 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 800 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 801 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 802 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 803 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 804 | #[cfg(not(any(feature = "sqlite", feature = "indexeddb")))] |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 805 | async fn load(&mut self) {} |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 806 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 807 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 808 | async fn commit(&mut self) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 809 | if !matches!(self.persistence, Persistence::Store(_)) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 810 | if let Err(error) = self.index.maintain() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 811 | tracing::warn!(%error, "maintaining the search index failed"); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 812 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 813 | self.dirty_since = None; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 814 | self.dirty_shards.clear(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 815 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 816 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 817 | let mut changes = match self.index.changes() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 818 | Ok(changes) => changes, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 819 | Err(error) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 820 | tracing::warn!(%error, "preparing a search commit failed"); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 821 | self.commit_failed(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 822 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 823 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 824 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 825 | let mut puts = std::mem::take(&mut changes.puts); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 826 | let mut deletes = changes.deletes.clone(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 827 | let meta = serde_json::json!({ |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 828 | "schema": SCHEMA, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 829 | "index": changes.manifest, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 830 | "rooms": self.rooms, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 831 | }); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 832 | puts.push(("meta".to_owned(), meta.to_string().into_bytes())); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 833 | for shard in &self.dirty_shards { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 834 | let part: BTreeMap<&String, &Pending> = self |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 835 | .pending |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 836 | .iter() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 837 | .filter(|(key, _)| self::shard(key) == *shard) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 838 | .collect(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 839 | let name = format!("utd/{shard}"); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 840 | if part.is_empty() { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 841 | deletes.push(name); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 842 | } else { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 843 | puts.push((name, serde_json::to_vec(&part).unwrap_or_default())); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 844 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 845 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 846 | let Persistence::Store(store) = &self.persistence else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 847 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 848 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 849 | match store.commit(puts, deletes, self.orphans.clone()).await { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 850 | Ok(()) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 851 | self.index.committed(&changes); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 852 | self.dirty_shards.clear(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 853 | self.orphans.clear(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 854 | self.dirty_since = None; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 855 | self.commit_failures = 0; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 856 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 857 | Err(error) => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 858 | tracing::warn!(%error, "saving the search index failed"); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 859 | self.commit_failed(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 860 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 861 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 862 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 863 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 864 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 865 | fn commit_failed(&mut self) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 866 | self.commit_failures += 1; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 867 | if self.commit_failures >= COMMIT_ATTEMPTS { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 868 | tracing::error!("search commits keep failing, the index lives in memory from here"); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 869 | self.fall_back(SearchInMemory::CommitFailed); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 870 | self.dirty_since = None; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 871 | self.dirty_shards.clear(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 872 | } else { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 873 | self.commit_after = now() + COMMIT_RETRY_MS; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 874 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 875 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 876 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 877 | #[cfg(not(any(feature = "sqlite", feature = "indexeddb")))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 878 | async fn commit(&mut self) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 879 | if let Err(error) = self.index.maintain() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 880 | tracing::warn!(%error, "maintaining the search index failed"); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 881 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 882 | self.dirty_since = None; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 883 | self.dirty_shards.clear(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 884 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 885 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 886 | fn touch(&mut self) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 887 | self.dirty_since.get_or_insert_with(now); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 888 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 889 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 890 | fn unpend(&mut self, key: &str) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 891 | if self.pending.remove(key).is_some() { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 892 | self.dirty_shards.insert(shard(key)); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 893 | self.touch(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 894 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 895 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 896 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 897 | fn observe(&mut self, room: &OwnedRoomId, value: &Value) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 898 | let observed = feed::observe(value); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 899 | let key = match &observed { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 900 | Observed::Message { key, .. } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 901 | | Observed::Redacted { key } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 902 | | Observed::Encrypted { key, .. } => Some(key.clone()), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 903 | Observed::Edit(edit) => Some(edit.id.clone()), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 904 | Observed::Redaction { .. } | Observed::Other => None, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 905 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 906 | if let Observed::Encrypted { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 907 | key, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 908 | session_id, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 909 | ts, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 910 | } = &observed |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 911 | { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 912 | if self.index.get(key).is_none() && !self.pending.contains_key(key) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 913 | self.pending.insert( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 914 | key.clone(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 915 | Pending { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 916 | room: room.to_string(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 917 | session_id: session_id.clone(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 918 | ts: *ts, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 919 | raw: value.to_string(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 920 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 921 | ); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 922 | self.dirty_shards.insert(shard(key)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 923 | while self.pending.len() > self.budget.max_pending { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 924 | let Some(oldest) = self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 925 | .pending |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 926 | .iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 927 | .min_by_key(|(_, pending)| pending.ts) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 928 | .map(|(key, _)| key.clone()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 929 | else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 930 | break; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 931 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 932 | self.unpend(&oldest); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 933 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 934 | self.touch(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 935 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 936 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 937 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 938 | if let Some(key) = key { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 939 | self.unpend(&key); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 940 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 941 | match feed::apply( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 942 | self.index.as_mut(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 943 | &mut self.redactions, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 944 | room.as_str(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 945 | observed, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 946 | ) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 947 | Effect::Indexed(ts) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 948 | let progress = self.rooms.entry(room.to_string()).or_default(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 949 | progress.oldest = Some(progress.oldest.map_or(ts, |oldest| oldest.min(ts))); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 950 | progress.newest = Some(progress.newest.map_or(ts, |newest| newest.max(ts))); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 951 | self.touch(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 952 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 953 | Effect::Removed | Effect::Held => self.touch(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 954 | Effect::Refetch(key) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 955 | self.touch(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 956 | let room_id = room.clone(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 957 | let client = self.client.clone(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 958 | self.work.push(Box::pin(async move { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 959 | let values = match client.get_room(&room_id) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 960 | Some(room) => load_with_edits(&room, &key).await, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 961 | None => Vec::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 962 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 963 | Work::Refetched { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 964 | room: room_id, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 965 | values, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 966 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 967 | })); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 968 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 969 | Effect::Nothing => {} |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 970 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 971 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 972 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 973 | fn observe_all(&mut self, room: &OwnedRoomId, events: &[SdkTimelineEvent]) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 974 | for event in events { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 975 | if let Some(value) = value_of(event) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 976 | self.observe(room, &value); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 977 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 978 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 979 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 980 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 981 | async fn subscribe(&mut self, room_id: OwnedRoomId) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 982 | if let Some((_, seen)) = self.subscriptions.get_mut(&room_id) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 983 | *seen = now(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 984 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 985 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 986 | let Some(room) = self.client.get_room(&room_id) else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 987 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 988 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 989 | if room.state() != RoomState::Joined { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 990 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 991 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 992 | let Ok((cache, drop_handles)) = room.event_cache().await else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 993 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 994 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 995 | let Ok((initial, subscriber)) = cache.subscribe().await else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 996 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 997 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 998 | self.observe_all(&room_id, &initial); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 999 | let stream = updates(room_id.clone(), subscriber, drop_handles); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1000 | let (stream, abort) = abortable(stream); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1001 | self.updates.push(Box::pin(stream)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1002 | self.subscriptions.insert(room_id.clone(), (abort, now())); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1003 | let started = self |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1004 | .rooms |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1005 | .get(room_id.as_str()) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1006 | .is_some_and(|progress| progress.started); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1007 | if started |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1008 | && self.baselines.contains_key(room_id.as_str()) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1009 | && self.headed.insert(room_id.clone()) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1010 | { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1011 | self.heads.push_back(room_id); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1012 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1013 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1014 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1015 | /// the generic channel holds 128 updates, and a missed one could be a room we never |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1016 | /// follow, so a lag follows every joined room |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1017 | async fn generic(&mut self, update: Result<OwnedRoomId, broadcast::error::RecvError>) -> bool { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1018 | match update { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1019 | Ok(room_id) => self.subscribe(room_id).await, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1020 | Err(broadcast::error::RecvError::Lagged(_)) => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1021 | for room in self.client.joined_rooms() { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1022 | self.subscribe(room.room_id().to_owned()).await; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1023 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1024 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1025 | Err(broadcast::error::RecvError::Closed) => return false, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1026 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1027 | true |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1028 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1029 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1030 | async fn room_update(&mut self, room_id: OwnedRoomId, update: RoomUpdate) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1031 | match update { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1032 | RoomUpdate::Events(events) => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1033 | if let Some((_, seen)) = self.subscriptions.get_mut(&room_id) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1034 | *seen = now(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1035 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1036 | self.observe_all(&room_id, &events); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1037 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1038 | RoomUpdate::Lagged => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1039 | if let Some((abort, _)) = self.subscriptions.remove(&room_id) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1040 | abort.abort(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1041 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1042 | self.subscribe(room_id).await; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1043 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1044 | RoomUpdate::Closed => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1045 | self.subscriptions.remove(&room_id); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1046 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1047 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1048 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1049 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1050 | fn left(&mut self, room_id: &OwnedRoomId) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1051 | if let Some((abort, _)) = self.subscriptions.remove(room_id) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1052 | abort.abort(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1053 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1054 | self.heads.retain(|head| head != room_id); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1055 | self.head_tokens.remove(room_id); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1056 | if let Some((backfilling, abort)) = &self.backfilling |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1057 | && backfilling == room_id |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1058 | { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1059 | abort.abort(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1060 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1061 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1062 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1063 | fn drop_idle(&mut self) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1064 | let cutoff = now().saturating_sub(IDLE_SUBSCRIPTION_MS); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1065 | self.subscriptions.retain(|_, (abort, seen)| { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1066 | let keep = *seen >= cutoff; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1067 | if !keep { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1068 | abort.abort(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1069 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1070 | keep |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1071 | }); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1072 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1073 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1074 | fn coverage(&self, rooms: &[OwnedRoomId]) -> SearchCoverage { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1075 | let scoped: HashSet<&str> = rooms.iter().map(|room| room.as_str()).collect(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1076 | let undecrypted = self |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1077 | .pending |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1078 | .values() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1079 | .filter(|pending| scoped.contains(pending.room.as_str())) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1080 | .count(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1081 | let rooms = rooms |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1082 | .iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1083 | .map(|room_id| { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1084 | let progress = self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1085 | .rooms |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1086 | .get(room_id.as_str()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1087 | .cloned() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1088 | .unwrap_or_default(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1089 | let backfill = if progress.start_reached { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1090 | SearchBackfill::Complete |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1091 | } else if self.budget.pages_per_room == 0 { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1092 | SearchBackfill::Disabled |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1093 | } else if self |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1094 | .backfilling |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1095 | .as_ref() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1096 | .is_some_and(|(backfilling, _)| backfilling == room_id) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1097 | { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1098 | SearchBackfill::Running |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1099 | } else if let Some(failure) = self.failures.get(room_id) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1100 | SearchBackfill::Failed { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1101 | message: failure.message.clone(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1102 | retry_at_ms: failure.retry_at_ms, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1103 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1104 | } else if self.pages.get(room_id).copied().unwrap_or(0) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1105 | >= self.budget.pages_per_room |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1106 | { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1107 | SearchBackfill::Paused |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1108 | } else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1109 | SearchBackfill::Waiting |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1110 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1111 | RoomSearchCoverage { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1112 | room_id: room_id.clone(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1113 | oldest_indexed_ms: progress.oldest, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1114 | newest_indexed_ms: progress.newest, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1115 | start_reached: progress.start_reached, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1116 | backfill, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1117 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1118 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1119 | .collect(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1120 | let in_memory = self.in_memory(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1121 | SearchCoverage { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1122 | rooms, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1123 | persistent: in_memory.is_none(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1124 | in_memory, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1125 | undecrypted, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1126 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1127 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1128 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1129 | fn spent(&self, room_id: &OwnedRoomId) -> bool { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1130 | self.pages.get(room_id).copied().unwrap_or(0) >= self.budget.pages_per_room |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1131 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1132 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1133 | fn blocked(&self, room_id: &OwnedRoomId, at: u64) -> bool { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1134 | self.failures |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1135 | .get(room_id) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1136 | .is_some_and(|failure| failure.retry_at_ms > at) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1137 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1138 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1139 | /// head passes first, so what arrived while we were away is found before anything older |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1140 | fn schedule_backfill(&mut self) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1141 | let at = now(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1142 | if self.backfilling.is_some() || self.budget.pages_per_room == 0 || at < self.next_backfill |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1143 | { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1144 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1145 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1146 | let mut chosen = None; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1147 | for _ in 0..self.heads.len() { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1148 | let Some(room_id) = self.heads.pop_front() else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1149 | break; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1150 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1151 | if self.spent(&room_id) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1152 | self.head_tokens.remove(&room_id); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1153 | continue; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1154 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1155 | if self.blocked(&room_id, at) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1156 | self.heads.push_back(room_id); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1157 | continue; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1158 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1159 | chosen = Some((room_id, true)); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1160 | break; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1161 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1162 | if chosen.is_none() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1163 | let mut joined: Vec<_> = self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1164 | .client |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1165 | .joined_rooms() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1166 | .into_iter() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1167 | .map(|room| room.room_id().to_owned()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1168 | .collect(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1169 | joined.sort(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1170 | let count = joined.len(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1171 | for offset in 0..count { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1172 | let room_id = &joined[(self.turn + offset) % count]; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1173 | let done = self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1174 | .rooms |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1175 | .get(room_id.as_str()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1176 | .is_some_and(|progress| progress.start_reached); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1177 | if !done && !self.spent(room_id) && !self.blocked(room_id, at) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1178 | self.turn = (self.turn + offset + 1) % count; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1179 | chosen = Some((room_id.clone(), false)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1180 | break; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1181 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1182 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1183 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1184 | let Some((room_id, head)) = chosen else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1185 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1186 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1187 | let Some(room) = self.client.get_room(&room_id) else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1188 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1189 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1190 | let from = if head { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1191 | self.head_tokens.get(&room_id).cloned() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1192 | } else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1193 | self.rooms |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1194 | .get(room_id.as_str()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1195 | .and_then(|progress| progress.tail.clone()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1196 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1197 | *self.pages.entry(room_id.clone()).or_default() += 1; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1198 | let mut options = MessagesOptions::backward(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1199 | options.from = from.clone(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1200 | options.limit = UInt::from(self.budget.page_size); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1201 | let mut filter = RoomEventFilter::default(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1202 | filter.types = Some( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1203 | BACKFILL_TYPES |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1204 | .iter() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1205 | .map(|kind| (*kind).to_owned()) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1206 | .collect(), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1207 | ); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1208 | options.filter = filter; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1209 | let (abort, registration) = AbortHandle::new_pair(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1210 | self.backfilling = Some((room_id.clone(), abort)); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1211 | let page = Abortable::new( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1212 | async move { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1213 | room.messages(options) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1214 | .await |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1215 | .map_err(|error| Error::from_sdk("backfilling search history failed", error)) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1216 | }, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1217 | registration, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1218 | ); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1219 | self.work.push(Box::pin(async move { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1220 | match page.await { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1221 | Ok(result) => Work::Backfill { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1222 | room: room_id, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1223 | head, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1224 | from, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1225 | result, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1226 | }, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1227 | Err(_) => Work::BackfillCancelled, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1228 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1229 | })); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1230 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1231 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1232 | fn fail_backfill(&mut self, room_id: OwnedRoomId, message: String, delay_ms: u64) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1233 | self.failures.insert( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1234 | room_id, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1235 | Failure { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1236 | message, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1237 | retry_at_ms: now() + delay_ms, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1238 | delay_ms, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1239 | }, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1240 | ); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1241 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1242 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1243 | fn backfilled( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1244 | &mut self, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1245 | room_id: OwnedRoomId, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1246 | head: bool, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1247 | from: Option<String>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1248 | result: Result<Messages, Error>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1249 | ) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1250 | self.backfilling = None; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1251 | self.next_backfill = now() + self.budget.interval_ms; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1252 | let messages = match result { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1253 | Ok(messages) => messages, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1254 | Err(error) if from.is_some() && token_rejected(&error) => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1255 | if head { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1256 | self.head_tokens.remove(&room_id); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1257 | } else { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1258 | let progress = self.rooms.entry(room_id.to_string()).or_default(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1259 | progress.tail = None; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1260 | progress.started = false; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1261 | self.touch(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1262 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1263 | let message = format!("{}, starting again from the newest", error.message); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1264 | self.fail_backfill(room_id, message, self.budget.interval_ms); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1265 | return; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1266 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1267 | Err(error) => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1268 | let delay_ms = self |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1269 | .failures |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1270 | .get(&room_id) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1271 | .map_or(BACKFILL_RETRY_MS, |failure| { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1272 | (failure.delay_ms * 2).min(BACKFILL_RETRY_MAX_MS) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1273 | }); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1274 | self.fail_backfill(room_id, error.message.clone(), delay_ms); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1275 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1276 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1277 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1278 | self.failures.remove(&room_id); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1279 | let baseline = self.baselines.get(room_id.as_str()).copied(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1280 | let mut overlapped = false; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1281 | for event in &messages.chunk { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1282 | let Some(value) = value_of(event) else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1283 | continue; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1284 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1285 | if head && head_overlap(self.index.as_ref(), baseline, &value) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1286 | overlapped = true; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1287 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1288 | self.observe(&room_id, &value); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1289 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1290 | let ended = messages.end.is_none() || (messages.chunk.is_empty() && messages.end == from); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1291 | if head { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1292 | match messages.end { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1293 | Some(end) if !overlapped && !ended => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1294 | self.head_tokens.insert(room_id.clone(), end); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1295 | self.heads.push_front(room_id); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1296 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1297 | _ => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1298 | self.head_tokens.remove(&room_id); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1299 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1300 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1301 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1302 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1303 | let progress = self.rooms.entry(room_id.to_string()).or_default(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1304 | progress.started = true; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1305 | progress.tail = messages.end; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1306 | if ended { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1307 | progress.start_reached = true; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1308 | progress.tail = None; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1309 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1310 | self.touch(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1311 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1312 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1313 | fn retry_pending(&mut self, room_id: Option<&OwnedRoomId>, session_id: Option<&str>) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1314 | let queued: HashSet<String> = self.decrypt_queue.iter().cloned().collect(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1315 | let due: Vec<_> = self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1316 | .pending |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1317 | .iter() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1318 | .filter(|(key, pending)| { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1319 | !queued.contains(*key) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1320 | && room_id.is_none_or(|room_id| pending.room == room_id.as_str()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1321 | && session_id.is_none_or(|session_id| pending.session_id == session_id) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1322 | }) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1323 | .map(|(key, _)| key.clone()) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1324 | .collect(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1325 | self.decrypt_queue.extend(due); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1326 | self.pump_decrypts(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1327 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1328 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1329 | fn pump_decrypts(&mut self) { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1330 | while self.decrypting < DECRYPTS_AT_ONCE { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1331 | let Some(key) = self.decrypt_queue.pop_front() else { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1332 | return; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1333 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1334 | let Some(pending) = self.pending.get(&key).cloned() else { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1335 | continue; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1336 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1337 | let Ok(room_id) = OwnedRoomId::try_from(pending.room.as_str()) else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1338 | continue; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1339 | }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1340 | let Some(room) = self.client.get_room(&room_id) else { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1341 | continue; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1342 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1343 | self.decrypting += 1; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1344 | self.work.push(Box::pin(async move { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1345 | let value = async { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1346 | let raw: Raw<OriginalSyncRoomEncryptedEvent> = |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1347 | Raw::from_json_string(pending.raw).ok()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1348 | let event = room.decrypt_event(&raw, None).await.ok()?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1349 | value_of(&event).filter(|value| value["type"] != "m.room.encrypted") |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1350 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1351 | .await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1352 | Work::Decrypted { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1353 | key, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1354 | room: room_id, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1355 | value, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1356 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1357 | })); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1358 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1359 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1360 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1361 | fn complete(&mut self, work: Work) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1362 | match work { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1363 | Work::Backfill { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1364 | room, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1365 | head, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1366 | from, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1367 | result, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1368 | } => self.backfilled(room, head, from, result), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1369 | Work::BackfillCancelled => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1370 | self.backfilling = None; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1371 | self.next_backfill = now() + self.budget.interval_ms; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1372 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1373 | Work::Decrypted { key, room, value } => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1374 | self.decrypting -= 1; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1375 | if let Some(value) = value { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1376 | self.unpend(&key); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1377 | self.observe(&room, &value); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1378 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1379 | self.pump_decrypts(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1380 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1381 | Work::Refetched { room, values } => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1382 | for value in values { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1383 | self.observe(&room, &value); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1384 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1385 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1386 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1387 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1388 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1389 | fn search( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1390 | &mut self, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1391 | query: &str, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1392 | rooms: Option<&[String]>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1393 | before: Option<(u64, u64)>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1394 | limit: usize, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1395 | coverage: &[OwnedRoomId], |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1396 | ) -> Result<RawPage, Error> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1397 | let (mut hits, terms) = self.index.search(query, rooms, before, limit + 1)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1398 | let more = hits.len() > limit; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1399 | hits.truncate(limit); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1400 | Ok(RawPage { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1401 | hits, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1402 | terms, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1403 | more, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1404 | coverage: self.coverage(coverage), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1405 | }) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1406 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1407 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1408 | pub(crate) async fn run(mut self) { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1409 | let client = self.client.clone(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1410 | let mut generic = client.event_cache().subscribe_to_room_generic_updates(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1411 | let mut room_info = client.room_info_notable_update_receiver(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1412 | self.load().await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1413 | let mut keys = client |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1414 | .encryption() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1415 | .room_keys_received_stream() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1416 | .await |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1417 | .map(Box::pin); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1418 | self.retry_pending(None, None); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1419 | let mut tick = Box::pin(matrix_sdk::sleep::sleep(Duration::from_secs(1))); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1420 | loop { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1421 | self.schedule_backfill(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1422 | tokio::select! { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1423 | biased; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1424 | request = self.requests.recv() => match request { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1425 | Some(Request::Stop(reply)) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1426 | if self.dirty_since.is_some() || self.index.buffered() > 0 { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1427 | self.commit().await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1428 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1429 | for (abort, _) in self.subscriptions.values() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1430 | abort.abort(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1431 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1432 | let _ = reply.send(()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1433 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1434 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1435 | Some(Request::Search { query, rooms, before, limit, coverage, reply }) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1436 | let _ = reply.send(self.search(&query, rooms.as_deref(), before, limit, &coverage)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1437 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1438 | Some(Request::Coverage(rooms, reply)) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1439 | let _ = reply.send(self.coverage(&rooms)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1440 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1441 | Some(Request::Forget(key)) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1442 | let observed = Observed::Redacted { key }; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1443 | if feed::apply(self.index.as_mut(), &mut self.redactions, "", observed) == Effect::Removed { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1444 | self.touch(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1445 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1446 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1447 | None => return, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1448 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1449 | Some(work) = self.work.next(), if !self.work.is_empty() => self.complete(work), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1450 | Some((room_id, update)) = self.updates.next(), if !self.updates.is_empty() => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1451 | self.room_update(room_id, update).await; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1452 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1453 | update = generic.recv() => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1454 | if !self.generic(update.map(|update| update.room_id)).await { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1455 | return; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1456 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1457 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1458 | Some(update) = async { keys.as_mut()?.next().await }, if keys.is_some() => match update { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1459 | Ok(infos) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1460 | for info in infos { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1461 | self.retry_pending(Some(&info.room_id), Some(&info.session_id)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1462 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1463 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1464 | Err(_) => self.retry_pending(None, None), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1465 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1466 | update = room_info.recv() => match update { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1467 | Ok(update) if update.reasons.contains(RoomInfoNotableUpdateReasons::MEMBERSHIP) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1468 | let left = self.client.get_room(&update.room_id).is_none_or(|room| room.state() != RoomState::Joined); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1469 | if left { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1470 | self.left(&update.room_id); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1471 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1472 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1473 | Ok(_) | Err(broadcast::error::RecvError::Lagged(_)) => {} |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1474 | Err(broadcast::error::RecvError::Closed) => return, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1475 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1476 | () = &mut tick => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1477 | tick = Box::pin(matrix_sdk::sleep::sleep(Duration::from_secs(1))); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1478 | self.drop_idle(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1479 | let due = self.dirty_since.is_some_and(|since| now() >= since + COMMIT_AFTER_MS) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1480 | || self.index.buffered() >= COMMIT_BUFFERED; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1481 | if due && now() >= self.commit_after { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1482 | self.commit().await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1483 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1484 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1485 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1486 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1487 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1488 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1489 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1490 | /// a head pass stops at what this engine found already indexed, and live events indexed |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1491 | /// since it started don't count, or the first page would always stop it |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1492 | fn head_overlap(index: &dyn MessageIndex, baseline: Option<u64>, value: &Value) -> bool { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1493 | let (Some(baseline), Some(key), Some(ts)) = ( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1494 | baseline, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1495 | value.get("event_id").and_then(Value::as_str), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1496 | value.get("origin_server_ts").and_then(Value::as_u64), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1497 | ) else { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1498 | return false; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1499 | }; |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1500 | ts <= baseline && index.get(key).is_some_and(|doc| !doc.provisional) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1501 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1502 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1503 | fn updates( |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1504 | room_id: OwnedRoomId, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1505 | subscriber: RoomEventCacheSubscriber, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1506 | drop_handles: Arc<matrix_sdk::event_cache::EventCacheDropHandles>, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1507 | ) -> BoxStream<(OwnedRoomId, RoomUpdate)> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1508 | Box::pin(futures_util::stream::unfold( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1509 | Some((subscriber, drop_handles)), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1510 | move |state| { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1511 | let room_id = room_id.clone(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1512 | async move { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1513 | let (mut subscriber, drop_handles) = state?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1514 | loop { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1515 | match subscriber.recv().await { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1516 | Ok(RoomEventCacheUpdate::UpdateTimelineEvents(update)) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1517 | let events = update.diffs.into_iter().flat_map(added).collect(); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1518 | return Some(( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1519 | (room_id, RoomUpdate::Events(events)), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1520 | Some((subscriber, drop_handles)), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1521 | )); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1522 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1523 | Ok(_) => {} |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1524 | Err(broadcast::error::RecvError::Lagged(_)) => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1525 | return Some(( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1526 | (room_id, RoomUpdate::Lagged), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1527 | Some((subscriber, drop_handles)), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1528 | )); |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1529 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1530 | Err(broadcast::error::RecvError::Closed) => { |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1531 | return Some(((room_id, RoomUpdate::Closed), None)); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1532 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1533 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1534 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1535 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1536 | }, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1537 | )) |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1538 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1539 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1540 | fn added(diff: VectorDiff<SdkTimelineEvent>) -> Vec<SdkTimelineEvent> { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1541 | match diff { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1542 | VectorDiff::Append { values } | VectorDiff::Reset { values } => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1543 | values.into_iter().collect() |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1544 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1545 | VectorDiff::PushFront { value } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1546 | | VectorDiff::PushBack { value } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1547 | | VectorDiff::Insert { value, .. } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1548 | | VectorDiff::Set { value, .. } => vec![value], |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1549 | VectorDiff::Clear |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1550 | | VectorDiff::PopFront |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1551 | | VectorDiff::PopBack |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1552 | | VectorDiff::Remove { .. } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1553 | | VectorDiff::Truncate { .. } => Vec::new(), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1554 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1555 | } |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1556 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1557 | #[cfg(all(test, feature = "sqlite"))] |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1558 | #[path = "service_tests.rs"] |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1559 | mod tests; |