| 1 | use std::{ |
| 2 | collections::HashMap, |
| 3 | fmt, |
| 4 | pin::Pin, |
| 5 | sync::{Arc, Mutex, MutexGuard, PoisonError}, |
| 6 | task::{Context, Poll}, |
| 7 | time::Duration, |
| 8 | }; |
| 9 | |
| 10 | use futures_util::{Stream, StreamExt}; |
| 11 | use imbl::Vector; |
| 12 | use matrix_sdk::{ |
| 13 | SendOutsideWasm, |
| 14 | executor::{AbortHandle, spawn}, |
| 15 | }; |
| 16 | use ruma::{OwnedDeviceId, OwnedRoomId, OwnedTransactionId, OwnedUserId}; |
| 17 | #[cfg(feature = "serde")] |
| 18 | use serde::{Deserialize, Serialize}; |
| 19 | use tokio::sync::broadcast; |
| 20 | |
| 21 | use crate::{ |
| 22 | ClientHandle, Command, ConnectionState, EncryptionState, EngineFailure, ErrorKind, Event, |
| 23 | EventContextCloseReason, HistoryRepairReport, HistoryState, ImagePacks, RecentHistoryState, |
| 24 | RoomSummary, ThreadListCloseReason, ThreadListEntry, TimelineCommand, TimelineDiff, TimelineId, |
| 25 | TimelineItem, TypingState, UploadOutcome, VerificationState, VerificationTarget, |
| 26 | engine::executor::EngineBoxFuture, transport::PublicationRegistration, |
| 27 | }; |
| 28 | |
| 29 | #[cfg(not(target_family = "wasm"))] |
| 30 | type EngineBoxStream<T> = futures_util::stream::BoxStream<'static, T>; |
| 31 | #[cfg(target_family = "wasm")] |
| 32 | type EngineBoxStream<T> = futures_util::stream::LocalBoxStream<'static, T>; |
| 33 | |
| 34 | const DEFAULT_CAPACITY: usize = 256; |
| 35 | const RESYNC_BACKOFF: Duration = Duration::from_millis(100); |
| 36 | const RESYNC_BACKOFF_CAP: Duration = Duration::from_secs(10); |
| 37 | |
| 38 | /// everything the engine has said so far, folded together |
| 39 | #[derive(Clone, Debug, PartialEq)] |
| 40 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 41 | #[non_exhaustive] |
| 42 | pub struct State { |
| 43 | pub user_id: Option<OwnedUserId>, |
| 44 | pub session: SessionStatus, |
| 45 | pub connection: ConnectionState, |
| 46 | pub encryption: Option<EncryptionState>, |
| 47 | pub verification: Option<VerificationState>, |
| 48 | pub verification_target: Option<VerificationTarget>, |
| 49 | pub mentions: Option<crate::MentionsPage>, |
| 50 | pub rooms: Vector<RoomSummary>, |
| 51 | pub image_packs: ImagePacks, |
| 52 | pub timelines: imbl::HashMap<TimelineId, TimelineState>, |
| 53 | pub thread_lists: imbl::HashMap<OwnedRoomId, ThreadListState>, |
| 54 | /// a room without an entry is idle. closing a room drops its entries here and in `repairs` |
| 55 | pub recent_history: imbl::HashMap<OwnedRoomId, RecentHistoryState>, |
| 56 | pub repairs: imbl::HashMap<OwnedRoomId, HistoryRepairReport>, |
| 57 | pub typing: imbl::HashMap<OwnedRoomId, Vec<OwnedUserId>>, |
| 58 | pub uploads: imbl::HashMap<OwnedTransactionId, UploadState>, |
| 59 | /// what stopped the engine, if something went wrong |
| 60 | pub engine_failure: Option<EngineFailure>, |
| 61 | /// the last save of the session record that failed, until one succeeds |
| 62 | pub session_save_failure: Option<String>, |
| 63 | } |
| 64 | |
| 65 | #[derive(Clone, Debug, PartialEq)] |
| 66 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 67 | #[non_exhaustive] |
| 68 | pub struct TimelineState { |
| 69 | pub items: Vector<TimelineItem>, |
| 70 | pub history: HistoryState, |
| 71 | /// a batch didn't fit, so `items` is the last good copy until the next reset |
| 72 | pub diverged: bool, |
| 73 | } |
| 74 | |
| 75 | #[derive(Clone, Debug, PartialEq)] |
| 76 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 77 | #[non_exhaustive] |
| 78 | pub struct ThreadListState { |
| 79 | pub entries: Vector<ThreadListEntry>, |
| 80 | pub history: HistoryState, |
| 81 | } |
| 82 | |
| 83 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 84 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 85 | #[non_exhaustive] |
| 86 | pub struct UploadState { |
| 87 | pub room_id: OwnedRoomId, |
| 88 | pub uploaded_bytes: u64, |
| 89 | pub total_bytes: u64, |
| 90 | pub complete: bool, |
| 91 | pub failure: Option<UploadFailure>, |
| 92 | } |
| 93 | |
| 94 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 95 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 96 | #[non_exhaustive] |
| 97 | pub struct UploadFailure { |
| 98 | pub kind: ErrorKind, |
| 99 | pub will_retry: bool, |
| 100 | } |
| 101 | |
| 102 | /// the `Termination` it'll end in, as far as the events tell |
| 103 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 104 | #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 105 | pub enum SessionStatus { |
| 106 | Active, |
| 107 | SoftLoggedOut { |
| 108 | device_id: OwnedDeviceId, |
| 109 | }, |
| 110 | AccessRevoked, |
| 111 | LoggedOut, |
| 112 | /// stopped with the session still good |
| 113 | Stopped, |
| 114 | } |
| 115 | |
| 116 | #[derive(Clone, Debug)] |
| 117 | #[non_exhaustive] |
| 118 | pub struct Update { |
| 119 | /// one more than the update before it, and a `State` carries the one it's current as of |
| 120 | pub sequence: u64, |
| 121 | pub change: StateChange, |
| 122 | } |
| 123 | |
| 124 | #[derive(Clone, Debug)] |
| 125 | pub enum StateChange { |
| 126 | /// start over from this. a subscription opens with one and gets another after lagging |
| 127 | State(Arc<State>), |
| 128 | Authenticated(OwnedUserId), |
| 129 | Session(SessionStatus), |
| 130 | /// the record on disk may now hold a spent refresh token |
| 131 | SessionSaveFailed(String), |
| 132 | /// saved again after `SessionSaveFailed` |
| 133 | SessionSaved, |
| 134 | Connection(ConnectionState), |
| 135 | Encryption(EncryptionState), |
| 136 | Verification(VerificationState), |
| 137 | VerificationTarget(VerificationTarget), |
| 138 | Mentions(crate::MentionsUpdate), |
| 139 | /// room modes arrive with the `Rooms` that follows |
| 140 | NotificationSettings, |
| 141 | Rooms(Vector<RoomSummary>), |
| 142 | ImagePacks(ImagePacks), |
| 143 | Timeline { |
| 144 | timeline_id: TimelineId, |
| 145 | change: TimelineChange, |
| 146 | }, |
| 147 | History { |
| 148 | timeline_id: TimelineId, |
| 149 | state: HistoryState, |
| 150 | }, |
| 151 | ThreadList { |
| 152 | room_id: OwnedRoomId, |
| 153 | change: ThreadListChange, |
| 154 | }, |
| 155 | RecentHistory { |
| 156 | room_id: OwnedRoomId, |
| 157 | state: RecentHistoryState, |
| 158 | }, |
| 159 | HistoryRepair(HistoryRepairReport), |
| 160 | Typing(TypingState), |
| 161 | EventContextClosed { |
| 162 | context_id: String, |
| 163 | reason: EventContextCloseReason, |
| 164 | }, |
| 165 | Upload { |
| 166 | transaction_id: OwnedTransactionId, |
| 167 | upload: UploadState, |
| 168 | }, |
| 169 | UploadFinished { |
| 170 | room_id: OwnedRoomId, |
| 171 | transaction_id: OwnedTransactionId, |
| 172 | outcome: UploadOutcome, |
| 173 | }, |
| 174 | Failed(EngineFailure), |
| 175 | } |
| 176 | |
| 177 | #[derive(Clone, Debug)] |
| 178 | pub enum TimelineChange { |
| 179 | Reset { |
| 180 | items: Vector<TimelineItem>, |
| 181 | }, |
| 182 | /// `items` is what applying `diffs` left |
| 183 | Changed { |
| 184 | diffs: Arc<[TimelineDiff]>, |
| 185 | items: Vector<TimelineItem>, |
| 186 | }, |
| 187 | /// a batch didn't fit. a resync is on its way and nothing changes until its reset |
| 188 | Diverged, |
| 189 | Closed, |
| 190 | } |
| 191 | |
| 192 | #[derive(Clone, Debug)] |
| 193 | pub enum ThreadListChange { |
| 194 | Reset { |
| 195 | entries: Vector<ThreadListEntry>, |
| 196 | }, |
| 197 | /// `entries` is what applying `upserts` left |
| 198 | Changed { |
| 199 | upserts: Arc<[ThreadListEntry]>, |
| 200 | entries: Vector<ThreadListEntry>, |
| 201 | }, |
| 202 | History(HistoryState), |
| 203 | Closed(ThreadListCloseReason), |
| 204 | } |
| 205 | |
| 206 | /// drains the event stream into a `State` that any number of `MirrorHandle`s watch |
| 207 | pub struct Mirror { |
| 208 | handle: ClientHandle, |
| 209 | events: EngineBoxStream<Event>, |
| 210 | shared: Arc<Shared>, |
| 211 | _publication: PublicationRegistration, |
| 212 | resyncs: HashMap<TimelineId, AbortHandle>, |
| 213 | rooms_fetch: Option<AbortHandle>, |
| 214 | packs_fetch: Option<AbortHandle>, |
| 215 | } |
| 216 | |
| 217 | impl fmt::Debug for Mirror { |
| 218 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 219 | formatter.debug_struct("Mirror").finish_non_exhaustive() |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | impl Mirror { |
| 224 | /// takes any stream of the engine's events, so a host can still look at them on the way past |
| 225 | pub fn new( |
| 226 | handle: ClientHandle, |
| 227 | events: impl Stream<Item = Event> + SendOutsideWasm + 'static, |
| 228 | ) -> (Self, MirrorHandle) { |
| 229 | Self::with_capacity(handle, events, DEFAULT_CAPACITY) |
| 230 | } |
| 231 | |
| 232 | /// `capacity` updates can queue for a subscriber before it lags and starts over. a zero |
| 233 | /// capacity counts as one |
| 234 | pub fn with_capacity( |
| 235 | handle: ClientHandle, |
| 236 | events: impl Stream<Item = Event> + SendOutsideWasm + 'static, |
| 237 | capacity: usize, |
| 238 | ) -> (Self, MirrorHandle) { |
| 239 | let state = State { |
| 240 | user_id: None, |
| 241 | session: SessionStatus::Active, |
| 242 | connection: *handle.connection_state().borrow(), |
| 243 | encryption: handle.encryption_state(), |
| 244 | verification: None, |
| 245 | verification_target: None, |
| 246 | mentions: None, |
| 247 | rooms: Vector::new(), |
| 248 | image_packs: ImagePacks::default(), |
| 249 | timelines: imbl::HashMap::new(), |
| 250 | thread_lists: imbl::HashMap::new(), |
| 251 | recent_history: imbl::HashMap::new(), |
| 252 | repairs: imbl::HashMap::new(), |
| 253 | typing: imbl::HashMap::new(), |
| 254 | uploads: imbl::HashMap::new(), |
| 255 | engine_failure: None, |
| 256 | session_save_failure: None, |
| 257 | }; |
| 258 | let shared = Arc::new(Shared { |
| 259 | published: Mutex::new(Published { |
| 260 | state: Arc::new(state), |
| 261 | sequence: 0, |
| 262 | updates: Some(broadcast::channel(capacity.max(1)).0), |
| 263 | rooms_fetch: Fetch::Idle, |
| 264 | packs_fetch: Fetch::Idle, |
| 265 | }), |
| 266 | }); |
| 267 | let publication = handle.register_publication_mirror(); |
| 268 | #[cfg(not(target_family = "wasm"))] |
| 269 | let events = events.boxed(); |
| 270 | #[cfg(target_family = "wasm")] |
| 271 | let events = events.boxed_local(); |
| 272 | let mirror = Self { |
| 273 | handle, |
| 274 | events, |
| 275 | shared: shared.clone(), |
| 276 | _publication: publication, |
| 277 | resyncs: HashMap::new(), |
| 278 | rooms_fetch: None, |
| 279 | packs_fetch: None, |
| 280 | }; |
| 281 | (mirror, MirrorHandle { shared }) |
| 282 | } |
| 283 | |
| 284 | /// resolves once the event stream ends, which is just after `Stopped` |
| 285 | pub fn run(mut self) -> EngineBoxFuture<'static, ()> { |
| 286 | Box::pin(async move { |
| 287 | self.refetch_rooms(); |
| 288 | self.refetch_image_packs(); |
| 289 | while let Some(event) = self.events.next().await { |
| 290 | self.apply(event); |
| 291 | } |
| 292 | }) |
| 293 | } |
| 294 | |
| 295 | fn apply(&mut self, event: Event) { |
| 296 | match event { |
| 297 | Event::ConnectionChanged(connection) => self.publish(|state| { |
| 298 | state.connection = connection; |
| 299 | Some(StateChange::Connection(connection)) |
| 300 | }), |
| 301 | Event::Authenticated { user_id } => self.publish(|state| { |
| 302 | state.user_id = Some(user_id.clone()); |
| 303 | Some(StateChange::Authenticated(user_id)) |
| 304 | }), |
| 305 | Event::SessionSaveFailed { message } => self.publish(|state| { |
| 306 | state.session_save_failure = Some(message.clone()); |
| 307 | Some(StateChange::SessionSaveFailed(message)) |
| 308 | }), |
| 309 | Event::SessionTokensChanged => self.publish(|state| { |
| 310 | state.session_save_failure.take()?; |
| 311 | Some(StateChange::SessionSaved) |
| 312 | }), |
| 313 | Event::SessionAccessRejected { soft_logout } => self.publish(|state| { |
| 314 | state.mentions = None; |
| 315 | state.session = match soft_logout { |
| 316 | Some(device_id) => SessionStatus::SoftLoggedOut { device_id }, |
| 317 | None => SessionStatus::AccessRevoked, |
| 318 | }; |
| 319 | Some(StateChange::Session(state.session.clone())) |
| 320 | }), |
| 321 | Event::LoggedOut => self.publish(|state| { |
| 322 | state.mentions = None; |
| 323 | state.session = SessionStatus::LoggedOut; |
| 324 | Some(StateChange::Session(SessionStatus::LoggedOut)) |
| 325 | }), |
| 326 | Event::Stopped => { |
| 327 | self.cancel_resyncs(); |
| 328 | self.publish(|state| { |
| 329 | if state.session != SessionStatus::Active { |
| 330 | return None; |
| 331 | } |
| 332 | state.session = SessionStatus::Stopped; |
| 333 | Some(StateChange::Session(SessionStatus::Stopped)) |
| 334 | }); |
| 335 | } |
| 336 | Event::Failed(failure) => self.publish(|state| { |
| 337 | state.engine_failure = Some(failure.clone()); |
| 338 | Some(StateChange::Failed(failure)) |
| 339 | }), |
| 340 | Event::NotificationSettingsChanged => { |
| 341 | self.publish(|_| Some(StateChange::NotificationSettings)); |
| 342 | } |
| 343 | Event::RoomsChanged => self.refetch_rooms(), |
| 344 | Event::MentionsChanged(update) => self.publish(|state| { |
| 345 | if state.session != SessionStatus::Active |
| 346 | || state |
| 347 | .user_id |
| 348 | .as_ref() |
| 349 | .is_some_and(|account| account != &update.snapshot.boundary.account) |
| 350 | { |
| 351 | return None; |
| 352 | } |
| 353 | if state |
| 354 | .mentions |
| 355 | .as_ref() |
| 356 | .map_or(false, |current| update.snapshot.revision < current.revision) |
| 357 | { |
| 358 | return None; |
| 359 | } |
| 360 | state.mentions = Some(update.snapshot.clone()); |
| 361 | Some(StateChange::Mentions(update)) |
| 362 | }), |
| 363 | Event::ImagePacksChanged => self.refetch_image_packs(), |
| 364 | Event::EncryptionChanged(encryption) => self.publish(|state| { |
| 365 | state.encryption = Some(encryption.clone()); |
| 366 | Some(StateChange::Encryption(encryption)) |
| 367 | }), |
| 368 | Event::VerificationChanged(verification) => self.publish(|state| { |
| 369 | state.verification = Some(verification.clone()); |
| 370 | Some(StateChange::Verification(verification)) |
| 371 | }), |
| 372 | Event::VerificationTargetChanged(target) => self.publish(|state| { |
| 373 | state.verification_target = Some(target.clone()); |
| 374 | Some(StateChange::VerificationTarget(target)) |
| 375 | }), |
| 376 | Event::TimelineReset { timeline_id, items } => { |
| 377 | self.cancel_resync(&timeline_id); |
| 378 | self.publish(|state| { |
| 379 | let history = state |
| 380 | .timelines |
| 381 | .get(&timeline_id) |
| 382 | .map_or(HistoryState::Idle, |timeline| timeline.history.clone()); |
| 383 | let timeline = TimelineState { |
| 384 | items: items.clone(), |
| 385 | history, |
| 386 | diverged: false, |
| 387 | }; |
| 388 | state.timelines.insert(timeline_id.clone(), timeline); |
| 389 | Some(StateChange::Timeline { |
| 390 | timeline_id, |
| 391 | change: TimelineChange::Reset { items }, |
| 392 | }) |
| 393 | }); |
| 394 | } |
| 395 | Event::TimelineChanged { timeline_id, diffs } => self.apply_diffs(timeline_id, diffs), |
| 396 | Event::HistoryChanged { |
| 397 | timeline_id, |
| 398 | state: history, |
| 399 | } => self.publish(|state| { |
| 400 | state.timelines.get_mut(&timeline_id)?.history = history.clone(); |
| 401 | Some(StateChange::History { |
| 402 | timeline_id, |
| 403 | state: history, |
| 404 | }) |
| 405 | }), |
| 406 | Event::RecentHistoryChanged { |
| 407 | room_id, |
| 408 | state: recent, |
| 409 | } => self.publish(|state| { |
| 410 | if recent == RecentHistoryState::Idle { |
| 411 | state.recent_history.remove(&room_id); |
| 412 | } else { |
| 413 | state.recent_history.insert(room_id.clone(), recent.clone()); |
| 414 | } |
| 415 | Some(StateChange::RecentHistory { |
| 416 | room_id, |
| 417 | state: recent, |
| 418 | }) |
| 419 | }), |
| 420 | Event::HistoryRepairChanged(report) => self.publish(|state| { |
| 421 | state.repairs.insert(report.room_id.clone(), report.clone()); |
| 422 | Some(StateChange::HistoryRepair(report)) |
| 423 | }), |
| 424 | Event::PublicationBarrier { acknowledgement } => { |
| 425 | if let Some(acknowledgement) = acknowledgement { |
| 426 | acknowledgement.complete(true); |
| 427 | } |
| 428 | } |
| 429 | Event::TimelineClosed { timeline_id } => { |
| 430 | self.cancel_resync(&timeline_id); |
| 431 | self.publish(|state| { |
| 432 | state.timelines.remove(&timeline_id)?; |
| 433 | if let TimelineId::Room(room_id) = &timeline_id { |
| 434 | state.repairs.remove(room_id); |
| 435 | } |
| 436 | Some(StateChange::Timeline { |
| 437 | timeline_id, |
| 438 | change: TimelineChange::Closed, |
| 439 | }) |
| 440 | }); |
| 441 | } |
| 442 | Event::EventContextClosed { context_id, reason } => { |
| 443 | self.publish(|_| Some(StateChange::EventContextClosed { context_id, reason })); |
| 444 | } |
| 445 | Event::ThreadListReset { room_id, entries } => self.publish(|state| { |
| 446 | let history = state |
| 447 | .thread_lists |
| 448 | .get(&room_id) |
| 449 | .map_or(HistoryState::Idle, |list| list.history.clone()); |
| 450 | state.thread_lists.insert( |
| 451 | room_id.clone(), |
| 452 | ThreadListState { |
| 453 | entries: entries.clone(), |
| 454 | history, |
| 455 | }, |
| 456 | ); |
| 457 | Some(StateChange::ThreadList { |
| 458 | room_id, |
| 459 | change: ThreadListChange::Reset { entries }, |
| 460 | }) |
| 461 | }), |
| 462 | Event::ThreadListChanged { room_id, upserts } => self.publish(|state| { |
| 463 | let list = state.thread_lists.get_mut(&room_id)?; |
| 464 | for entry in &upserts { |
| 465 | ThreadListEntry::upsert(&mut list.entries, entry.clone()); |
| 466 | } |
| 467 | Some(StateChange::ThreadList { |
| 468 | room_id, |
| 469 | change: ThreadListChange::Changed { |
| 470 | upserts: upserts.into(), |
| 471 | entries: list.entries.clone(), |
| 472 | }, |
| 473 | }) |
| 474 | }), |
| 475 | Event::ThreadListHistoryChanged { |
| 476 | room_id, |
| 477 | state: history, |
| 478 | } => self.publish(|state| { |
| 479 | state.thread_lists.get_mut(&room_id)?.history = history.clone(); |
| 480 | Some(StateChange::ThreadList { |
| 481 | room_id, |
| 482 | change: ThreadListChange::History(history), |
| 483 | }) |
| 484 | }), |
| 485 | Event::ThreadListClosed { room_id, reason } => self.publish(|state| { |
| 486 | state.thread_lists.remove(&room_id)?; |
| 487 | Some(StateChange::ThreadList { |
| 488 | room_id, |
| 489 | change: ThreadListChange::Closed(reason), |
| 490 | }) |
| 491 | }), |
| 492 | Event::TypingChanged(typing) => self.publish(|state| { |
| 493 | if typing.users.is_empty() { |
| 494 | state.typing.remove(&typing.room_id); |
| 495 | } else { |
| 496 | state |
| 497 | .typing |
| 498 | .insert(typing.room_id.clone(), typing.users.clone()); |
| 499 | } |
| 500 | Some(StateChange::Typing(typing)) |
| 501 | }), |
| 502 | Event::MediaUploadProgress { |
| 503 | room_id, |
| 504 | transaction_id, |
| 505 | uploaded_bytes, |
| 506 | total_bytes, |
| 507 | complete, |
| 508 | } => self.publish(|state| { |
| 509 | let upload = UploadState { |
| 510 | room_id, |
| 511 | uploaded_bytes, |
| 512 | total_bytes, |
| 513 | complete, |
| 514 | failure: None, |
| 515 | }; |
| 516 | state.uploads.insert(transaction_id.clone(), upload.clone()); |
| 517 | Some(StateChange::Upload { |
| 518 | transaction_id, |
| 519 | upload, |
| 520 | }) |
| 521 | }), |
| 522 | Event::MediaUploadFailed { |
| 523 | room_id, |
| 524 | transaction_id, |
| 525 | kind, |
| 526 | will_retry, |
| 527 | } => self.publish(|state| { |
| 528 | let upload = state |
| 529 | .uploads |
| 530 | .entry(transaction_id.clone()) |
| 531 | .or_insert_with(|| UploadState { |
| 532 | room_id, |
| 533 | uploaded_bytes: 0, |
| 534 | total_bytes: 0, |
| 535 | complete: false, |
| 536 | failure: None, |
| 537 | }); |
| 538 | upload.failure = Some(UploadFailure { kind, will_retry }); |
| 539 | Some(StateChange::Upload { |
| 540 | transaction_id, |
| 541 | upload: upload.clone(), |
| 542 | }) |
| 543 | }), |
| 544 | Event::MediaUploadFinished { |
| 545 | room_id, |
| 546 | transaction_id, |
| 547 | outcome, |
| 548 | } => self.publish(|state| { |
| 549 | state.uploads.remove(&transaction_id); |
| 550 | Some(StateChange::UploadFinished { |
| 551 | room_id, |
| 552 | transaction_id, |
| 553 | outcome, |
| 554 | }) |
| 555 | }), |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | fn apply_diffs(&mut self, timeline_id: TimelineId, diffs: Vec<TimelineDiff>) { |
| 560 | let mut diverged = false; |
| 561 | self.publish(|state| { |
| 562 | let Some(timeline) = state.timelines.get_mut(&timeline_id) else { |
| 563 | diverged = true; |
| 564 | state.timelines.insert( |
| 565 | timeline_id.clone(), |
| 566 | TimelineState { |
| 567 | items: Vector::new(), |
| 568 | history: HistoryState::Idle, |
| 569 | diverged: true, |
| 570 | }, |
| 571 | ); |
| 572 | return Some(StateChange::Timeline { |
| 573 | timeline_id: timeline_id.clone(), |
| 574 | change: TimelineChange::Diverged, |
| 575 | }); |
| 576 | }; |
| 577 | if timeline.diverged { |
| 578 | return None; |
| 579 | } |
| 580 | let diffs: Arc<[TimelineDiff]> = diffs.into(); |
| 581 | let change = match TimelineDiff::try_apply_all(diffs.to_vec(), &mut timeline.items) { |
| 582 | Ok(()) => TimelineChange::Changed { |
| 583 | diffs, |
| 584 | items: timeline.items.clone(), |
| 585 | }, |
| 586 | Err(error) => { |
| 587 | tracing::debug!(?timeline_id, length = error.length, "timeline diverged"); |
| 588 | timeline.diverged = true; |
| 589 | diverged = true; |
| 590 | TimelineChange::Diverged |
| 591 | } |
| 592 | }; |
| 593 | Some(StateChange::Timeline { |
| 594 | timeline_id: timeline_id.clone(), |
| 595 | change, |
| 596 | }) |
| 597 | }); |
| 598 | if diverged { |
| 599 | let task = spawn(resync(self.handle.clone(), timeline_id.clone())); |
| 600 | self.resyncs.insert(timeline_id, task.abort_handle()); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | fn refetch_rooms(&mut self) { |
| 605 | let fetch = fetch_rooms(self.handle.clone(), self.shared.clone()); |
| 606 | if let Some(task) = self |
| 607 | .shared |
| 608 | .start(|published| &mut published.rooms_fetch, fetch) |
| 609 | { |
| 610 | self.rooms_fetch = Some(task); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | fn refetch_image_packs(&mut self) { |
| 615 | let fetch = fetch_image_packs(self.handle.clone(), self.shared.clone()); |
| 616 | if let Some(task) = self |
| 617 | .shared |
| 618 | .start(|published| &mut published.packs_fetch, fetch) |
| 619 | { |
| 620 | self.packs_fetch = Some(task); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | fn cancel_resync(&mut self, timeline_id: &TimelineId) { |
| 625 | if let Some(task) = self.resyncs.remove(timeline_id) { |
| 626 | task.abort(); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | fn cancel_resyncs(&mut self) { |
| 631 | for (_, task) in self.resyncs.drain() { |
| 632 | task.abort(); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | fn publish(&self, change: impl FnOnce(&mut State) -> Option<StateChange>) { |
| 637 | self.shared.publish(change); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | impl Drop for Mirror { |
| 642 | fn drop(&mut self) { |
| 643 | self.cancel_resyncs(); |
| 644 | for task in [self.rooms_fetch.take(), self.packs_fetch.take()] |
| 645 | .into_iter() |
| 646 | .flatten() |
| 647 | { |
| 648 | task.abort(); |
| 649 | } |
| 650 | self.shared.lock().updates = None; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /// asks until the engine answers, since a busy room or a full queue is only ever for now. |
| 655 | /// a reset or a close for the timeline aborts it, and the engine sends a reset before it answers |
| 656 | async fn resync(handle: ClientHandle, timeline_id: TimelineId) { |
| 657 | let mut backoff = RESYNC_BACKOFF; |
| 658 | loop { |
| 659 | let command = Command::Timeline(TimelineCommand::Resync { |
| 660 | timeline_id: timeline_id.clone(), |
| 661 | }); |
| 662 | match handle.execute(command).await { |
| 663 | Ok(_) => return, |
| 664 | Err(error) if error.kind == ErrorKind::Interrupted => return, |
| 665 | Err(error) => { |
| 666 | tracing::debug!(%error, ?timeline_id, "resync refused, trying again"); |
| 667 | matrix_sdk::sleep::sleep(backoff).await; |
| 668 | backoff = (backoff * 2).min(RESYNC_BACKOFF_CAP); |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | async fn fetch_rooms(handle: ClientHandle, shared: Arc<Shared>) { |
| 675 | loop { |
| 676 | let rooms: Vector<RoomSummary> = handle.rooms().await.into(); |
| 677 | shared.publish(|state| { |
| 678 | state.rooms = rooms.clone(); |
| 679 | Some(StateChange::Rooms(rooms)) |
| 680 | }); |
| 681 | if !shared.fetched(|published| &mut published.rooms_fetch) { |
| 682 | return; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /// a failed read keeps the packs from before, the next change asks again |
| 688 | async fn fetch_image_packs(handle: ClientHandle, shared: Arc<Shared>) { |
| 689 | loop { |
| 690 | match handle.image_packs().await { |
| 691 | Ok(packs) => shared.publish(|state| { |
| 692 | if state.image_packs == packs { |
| 693 | return None; |
| 694 | } |
| 695 | state.image_packs = packs.clone(); |
| 696 | Some(StateChange::ImagePacks(packs)) |
| 697 | }), |
| 698 | Err(error) => tracing::warn!(%error, "reading image packs failed"), |
| 699 | } |
| 700 | if !shared.fetched(|published| &mut published.packs_fetch) { |
| 701 | return; |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | /// cheap to clone, one per frontend |
| 707 | #[derive(Clone, Debug)] |
| 708 | pub struct MirrorHandle { |
| 709 | shared: Arc<Shared>, |
| 710 | } |
| 711 | |
| 712 | impl MirrorHandle { |
| 713 | #[must_use] |
| 714 | pub fn snapshot(&self) -> Arc<State> { |
| 715 | self.shared.lock().state.clone() |
| 716 | } |
| 717 | |
| 718 | /// opens with the current state, then every update after it. ends once the mirror stops |
| 719 | #[must_use] |
| 720 | pub fn subscribe(&self) -> Subscription { |
| 721 | let (first, receiving) = self.shared.clone().subscribe(); |
| 722 | let rest = futures_util::stream::unfold(receiving, |(shared, receiver)| async move { |
| 723 | let mut receiver = receiver?; |
| 724 | match receiver.recv().await { |
| 725 | Ok(update) => Some((update, (shared, Some(receiver)))), |
| 726 | Err(broadcast::error::RecvError::Lagged(missed)) => { |
| 727 | tracing::debug!(missed, "mirror subscriber lagged, starting it over"); |
| 728 | Some(shared.subscribe()) |
| 729 | } |
| 730 | Err(broadcast::error::RecvError::Closed) => None, |
| 731 | } |
| 732 | }); |
| 733 | let updates = futures_util::stream::once(std::future::ready(first)).chain(rest); |
| 734 | #[cfg(not(target_family = "wasm"))] |
| 735 | let updates = updates.boxed(); |
| 736 | #[cfg(target_family = "wasm")] |
| 737 | let updates = updates.boxed_local(); |
| 738 | Subscription(updates) |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | pub struct Subscription(EngineBoxStream<Update>); |
| 743 | |
| 744 | impl fmt::Debug for Subscription { |
| 745 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 746 | formatter |
| 747 | .debug_struct("Subscription") |
| 748 | .finish_non_exhaustive() |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | impl Stream for Subscription { |
| 753 | type Item = Update; |
| 754 | |
| 755 | fn poll_next(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<Update>> { |
| 756 | self.0.poll_next_unpin(context) |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | #[derive(Debug)] |
| 761 | struct Shared { |
| 762 | published: Mutex<Published>, |
| 763 | } |
| 764 | |
| 765 | #[derive(Debug)] |
| 766 | struct Published { |
| 767 | state: Arc<State>, |
| 768 | sequence: u64, |
| 769 | /// gone once the mirror stops, which ends every subscription |
| 770 | updates: Option<broadcast::Sender<Update>>, |
| 771 | rooms_fetch: Fetch, |
| 772 | packs_fetch: Fetch, |
| 773 | } |
| 774 | |
| 775 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 776 | enum Fetch { |
| 777 | Idle, |
| 778 | Running, |
| 779 | Again, |
| 780 | } |
| 781 | |
| 782 | impl Shared { |
| 783 | fn lock(&self) -> MutexGuard<'_, Published> { |
| 784 | self.published |
| 785 | .lock() |
| 786 | .unwrap_or_else(PoisonError::into_inner) |
| 787 | } |
| 788 | |
| 789 | /// one fetch at a time, with at most one more owed |
| 790 | fn start( |
| 791 | &self, |
| 792 | fetch: impl FnOnce(&mut Published) -> &mut Fetch, |
| 793 | task: impl Future<Output = ()> + SendOutsideWasm + 'static, |
| 794 | ) -> Option<AbortHandle> { |
| 795 | let mut published = self.lock(); |
| 796 | let fetch = fetch(&mut published); |
| 797 | match *fetch { |
| 798 | Fetch::Idle => { |
| 799 | *fetch = Fetch::Running; |
| 800 | drop(published); |
| 801 | Some(spawn(task).abort_handle()) |
| 802 | } |
| 803 | Fetch::Running => { |
| 804 | *fetch = Fetch::Again; |
| 805 | None |
| 806 | } |
| 807 | Fetch::Again => None, |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /// whether another fetch was asked for while this one ran |
| 812 | fn fetched(&self, fetch: impl FnOnce(&mut Published) -> &mut Fetch) -> bool { |
| 813 | let mut published = self.lock(); |
| 814 | let fetch = fetch(&mut published); |
| 815 | if *fetch == Fetch::Again { |
| 816 | *fetch = Fetch::Running; |
| 817 | return true; |
| 818 | } |
| 819 | *fetch = Fetch::Idle; |
| 820 | false |
| 821 | } |
| 822 | |
| 823 | /// state, sequence and send move together under the one lock, which is what lets |
| 824 | /// `subscribe` take a snapshot and a receiver with nothing between them |
| 825 | fn publish(&self, change: impl FnOnce(&mut State) -> Option<StateChange>) { |
| 826 | let mut published = self.lock(); |
| 827 | let Some(change) = change(Arc::make_mut(&mut published.state)) else { |
| 828 | return; |
| 829 | }; |
| 830 | published.sequence += 1; |
| 831 | let update = Update { |
| 832 | sequence: published.sequence, |
| 833 | change, |
| 834 | }; |
| 835 | if let Some(updates) = &published.updates { |
| 836 | let _ = updates.send(update); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | fn subscribe(self: Arc<Self>) -> (Update, (Arc<Self>, Option<broadcast::Receiver<Update>>)) { |
| 841 | let published = self.lock(); |
| 842 | let update = Update { |
| 843 | sequence: published.sequence, |
| 844 | change: StateChange::State(published.state.clone()), |
| 845 | }; |
| 846 | let receiver = published.updates.as_ref().map(broadcast::Sender::subscribe); |
| 847 | drop(published); |
| 848 | (update, (self, receiver)) |
| 849 | } |
| 850 | } |