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