31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | use std::collections::{HashMap, VecDeque}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 3 | use matrix_sdk::{Room, RoomState}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 4 | use matrix_sdk_ui::timeline::{RoomExt, Timeline, TimelineFocus, TimelineReadReceiptTracking}; |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 5 | use ruma::OwnedRoomId; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 6 | use tokio::sync::mpsc; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | |
2819830aef32 | atagen | 2026-10-26 08:42 UTC | 8 | use super::executor::{AbortHandle, JoinError, TaskSet, is_panic, label, resume_unwind}; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 9 | use super::repair::{Completion, Outcome, RepairController}; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 10 | use super::timeline::{SessionContext, TaskGuard, TimelineSession}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | use crate::{ |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 12 | Error, ErrorKind, Event, HistoryRepairCommand, RecentHistoryState, TimelineCommand, TimelineId, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 13 | TypingState, transport::Responder, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 15 | |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 16 | /// a build stuck on the event cache would otherwise hold the room's open forever. a big room on |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 17 | /// a slow device can take tens of seconds to load from IndexedDB, so this is generous |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 18 | const BUILD_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60); |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 19 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | pub(super) enum ActorRequest { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | /// opening an open room answers with a fresh reset |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | Open(Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | Execute(TimelineCommand, Responder), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 24 | Repair(HistoryRepairCommand, Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 25 | Close(Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | impl ActorRequest { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | pub(super) fn into_reply(self) -> Responder { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | match self { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 31 | Self::Open(reply) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 32 | | Self::Execute(_, reply) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 33 | | Self::Repair(_, reply) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 34 | | Self::Close(reply) => reply, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 39 | pub(super) enum ActorNote { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 40 | /// stopping on its own, after a leave or a kick. anything sent its way now comes back in `Exited` |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 41 | Leaving { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 42 | room_id: OwnedRoomId, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 43 | generation: u64, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 44 | }, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 45 | Exited { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 46 | room_id: OwnedRoomId, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 47 | generation: u64, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 48 | /// requests that arrived too late to be served, for the engine to answer or replay |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 49 | leftovers: Vec<ActorRequest>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 50 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | pub(super) struct RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | pub(super) room_id: OwnedRoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | pub(super) generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | pub(super) room: Room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | pub(super) requests: mpsc::Receiver<ActorRequest>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 58 | pub(super) notes: mpsc::UnboundedSender<ActorNote>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | pub(super) context: SessionContext, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | pub(super) panics: mpsc::UnboundedReceiver<&'static str>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | pub(super) opening: Vec<Responder>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | impl RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | pub(super) async fn run(mut self) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 66 | self.serve().await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 67 | self.requests.close(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 68 | let mut leftovers = Vec::new(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 69 | while let Ok(request) = self.requests.try_recv() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 70 | leftovers.push(request); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 71 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 72 | let _ = self.notes.send(ActorNote::Exited { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | room_id: self.room_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | generation: self.generation, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 75 | leftovers, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 76 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | async fn serve(&mut self) { |
2819830aef32 | atagen | 2026-10-26 08:42 UTC | 80 | tracing::debug!(room_id = %self.room_id, "building the room timeline"); |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 81 | let build = build_timeline( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | &self.room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | TimelineFocus::Live { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | hide_threaded_events: true, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | }, |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 86 | ); |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 87 | let timeout = matrix_sdk::sleep::sleep(BUILD_TIMEOUT); |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 88 | let timeline = tokio::select! { |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 89 | built = build => match built { |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 90 | Ok(timeline) => timeline, |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 91 | Err(error) => { |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 92 | tracing::warn!(%error, room_id = %self.room_id, "room actor could not build its timeline"); |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 93 | let error = Error::with_source(ErrorKind::Timeline, "room timeline build failed", error); |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 94 | return self.refuse_everything(error).await; |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 95 | } |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 96 | }, |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 97 | () = timeout => { |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 98 | tracing::warn!(room_id = %self.room_id, "room timeline build timed out"); |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 99 | let error = Error::new(ErrorKind::Timeline, "room timeline build timed out"); |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 100 | return self.refuse_everything(error).await; |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 101 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | }; |
2819830aef32 | atagen | 2026-10-26 08:42 UTC | 103 | tracing::debug!(room_id = %self.room_id, "starting the room timeline session"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | let Ok(room_timeline) = TimelineSession::start( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | TimelineId::Room(self.room_id.clone()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | timeline, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | self.context.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | let (typing_guard, typing) = self.room.subscribe_to_typing_notifications(); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 114 | let mut typing_watcher = TaskGuard(self.context.supervisor.spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | "typing watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | watch_typing(self.room_id.clone(), typing, self.context.events.clone()), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 117 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | let mut timelines = Timelines { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | room: room_timeline, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | threads: Threads::new(self.requests.max_capacity()), |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 121 | work: TaskSet::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | }; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 123 | let mut repair = RepairController::new(self.room.clone(), self.context.clone()); |
2819830aef32 | atagen | 2026-10-26 08:42 UTC | 124 | tracing::debug!(room_id = %self.room_id, "starting recent history"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 125 | repair.start_recent().await; |
2819830aef32 | atagen | 2026-10-26 08:42 UTC | 126 | tracing::debug!(room_id = %self.room_id, "room opened"); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 127 | for reply in self.opening.drain(..) { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 128 | reply.done(Ok(())); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 129 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 130 | let close = self.serve_open(&mut timelines, &mut repair).await; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 131 | let recent_settled = match repair.shutdown().await { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 132 | Some(completion) => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 133 | let recent = matches!(completion.outcome, Outcome::Recent(_)); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 134 | self.finish_repair(completion, &mut timelines).await; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 135 | recent |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 136 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 137 | None => false, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 138 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 139 | if !recent_settled { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 140 | let _ = self |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 141 | .context |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 142 | .events |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 143 | .send(Event::RecentHistoryChanged { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 144 | room_id: self.room_id.clone(), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 145 | state: RecentHistoryState::Idle, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 146 | }) |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 147 | .await; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 148 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 149 | if close.is_none() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 150 | self.requests.close(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 151 | let _ = self.notes.send(ActorNote::Leaving { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 152 | room_id: self.room_id.clone(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 153 | generation: self.generation, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 154 | }); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 155 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | // let redactions and receipts that already left finish rather than lie about them |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 158 | while timelines.work.join_next().await.is_some() {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | drop(typing_guard); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 160 | typing_watcher.0.abort(); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 161 | let _ = (&mut typing_watcher.0).await; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 162 | let _ = self |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 163 | .context |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 164 | .events |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 165 | .send(Event::TypingChanged(TypingState { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 166 | room_id: self.room_id.clone(), |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 167 | users: Vec::new(), |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 168 | })) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 169 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 170 | timelines.threads.close().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 171 | timelines.room.close().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 172 | if let Some(reply) = close { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 173 | reply.done(Ok(())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 174 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 176 | |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 177 | async fn finish_repair(&self, mut completion: Completion, timelines: &mut Timelines) { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 178 | let mut cancellation = Ok(()); |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 179 | // pages a failed or cancelled operation already committed still have to be on |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 180 | // screen before anyone hears it stopped |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 181 | if completion.committed |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 182 | && !completion.closing |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 183 | && let Err(error) = timelines.room.publish_history().await |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 184 | { |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 185 | if completion.result.is_ok() { |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 186 | completion.unpublished(error.clone()); |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 187 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 188 | cancellation = Err(error); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 189 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 190 | let event = match &completion.outcome { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 191 | Outcome::Recent(state) => Event::RecentHistoryChanged { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 192 | room_id: self.room_id.clone(), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 193 | state: state.clone(), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 194 | }, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 195 | Outcome::Fill(report) => Event::HistoryRepairChanged(report.clone()), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 196 | }; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 197 | let mut resolved = completion.result.clone(); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 198 | let events = &self.context.events; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 199 | if events.send(event).await.is_err() || events.publish_barrier().await.is_err() { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 200 | resolved = Err(Error::interrupted()); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 201 | cancellation = Err(Error::interrupted()); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 202 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 203 | let limit = self.context.initial_history_limit; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 204 | if matches!( |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 205 | completion.outcome, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 206 | Outcome::Recent(RecentHistoryState::Failed(_)) |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 207 | ) && limit > 0 |
002c79e4c6ec | atagen | 2026-10-25 10:42 UTC | 208 | && !timelines.room.has_events().await |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 209 | { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 210 | timelines.room.paginate(limit, None); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 211 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 212 | completion.resolve(resolved, cancellation); |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 213 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 214 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | /// returns the close request's responder, if a close is why we stopped |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 216 | async fn serve_open( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 217 | &mut self, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 218 | timelines: &mut Timelines, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 219 | repair: &mut RepairController, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 220 | ) -> Option<Responder> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 221 | let mut room_info = self.room.subscribe_info(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 222 | // a leave that landed while the timeline was building won't come round again |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 223 | if matches!(self.room.state(), RoomState::Left | RoomState::Banned) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 224 | return None; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 225 | } |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 226 | timelines.work.spawn(probe_sync_hole(self.room.clone())); |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 227 | let mut probe = Box::pin(matrix_sdk::sleep::sleep(PROBE_INTERVAL)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | tokio::select! { |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 230 | () = &mut probe => { |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 231 | timelines.work.spawn(probe_sync_hole(self.room.clone())); |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 232 | probe = Box::pin(matrix_sdk::sleep::sleep(PROBE_INTERVAL)); |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 233 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | Some(info) = room_info.next() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 235 | if matches!(info.state(), RoomState::Left | RoomState::Banned) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 236 | tracing::debug!(room_id = %self.room_id, "room actor stopped because we left the room"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 237 | return None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 238 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | request = self.requests.recv() => match request { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | None => return None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | Some(ActorRequest::Close(reply)) => return Some(reply), |
a951cb7cb7ca | atagen | 2026-10-24 23:17 UTC | 243 | Some(ActorRequest::Open(reply)) => reply.done(timelines.room.resync().await), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | Some(ActorRequest::Execute(command, reply)) => { |
2819830aef32 | atagen | 2026-10-26 08:42 UTC | 245 | let label = label(&command); |
2819830aef32 | atagen | 2026-10-26 08:42 UTC | 246 | tracing::debug!(room_id = %self.room_id, command = %label, "room command"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | timelines.execute(command, reply, &self.room).await; |
2819830aef32 | atagen | 2026-10-26 08:42 UTC | 248 | tracing::debug!(room_id = %self.room_id, command = %label, "room command handed off"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 249 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 250 | Some(ActorRequest::Repair(command, reply)) => { |
2819830aef32 | atagen | 2026-10-26 08:42 UTC | 251 | let label = label(&command); |
2819830aef32 | atagen | 2026-10-26 08:42 UTC | 252 | tracing::debug!(room_id = %self.room_id, command = %label, "room repair command"); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 253 | repair.execute(command, reply).await; |
2819830aef32 | atagen | 2026-10-26 08:42 UTC | 254 | tracing::debug!(room_id = %self.room_id, command = %label, "room repair command handed off"); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 255 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | }, |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 257 | completion = repair.next_completion(), if repair.is_running() => { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 258 | if let Some(completion) = completion { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 259 | self.finish_repair(completion, timelines).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 260 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 261 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 262 | Some(built) = timelines.threads.builds.join_next(), if !timelines.threads.builds.is_empty() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 263 | timelines.thread_built(built, &self.room_id, &self.context).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | Some(label) = self.panics.recv() => panic!("{label} panicked in room {}", self.room_id), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | Some(done) = timelines.work.join_next(), if !timelines.work.is_empty() => { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 267 | if let Err(error) = done && is_panic(&error) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 268 | tracing::warn!(room_id = %self.room_id, %error, "timeline operation panicked"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 269 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 270 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 271 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 272 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 273 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 274 | |
123824465d8b | atagen | 2026-10-26 09:18 UTC | 275 | async fn refuse_everything(&mut self, error: Error) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 276 | for reply in self.opening.drain(..) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 277 | reply.done(Err(error.clone())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 278 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 279 | self.requests.close(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 280 | while let Some(request) = self.requests.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 281 | request.into_reply().done(Err(error.clone())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 282 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 283 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 284 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 285 | |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 286 | /// a server that under-reports `limited` hides skipped events behind a sync token the sdk drops, |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 287 | /// so an open room looks behind the newest one now and then. without one recorded it costs nothing |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 288 | const PROBE_INTERVAL: std::time::Duration = std::time::Duration::from_secs(30); |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 289 | |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 290 | async fn probe_sync_hole(room: Room) { |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 291 | let result = async { |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 292 | let (cache, _handles) = room.event_cache().await?; |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 293 | cache.probe_sync_hole().await |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 294 | } |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 295 | .await; |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 296 | match result { |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 297 | Ok(Some(found)) if found > 0 => { |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 298 | tracing::info!(room_id = %room.room_id(), found, "history gap probe: new events"); |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 299 | } |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 300 | Ok(_) => {} |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 301 | Err(error) => { |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 302 | tracing::debug!(room_id = %room.room_id(), %error, "history gap probe failed") |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 303 | } |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 304 | } |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 305 | } |
aced11e80b47 | atagen | 2026-10-27 00:35 UTC | 306 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 307 | async fn build_timeline( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 308 | room: &Room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | focus: TimelineFocus, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | ) -> Result<Timeline, matrix_sdk_ui::timeline::Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | room.timeline_builder() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | .with_focus(focus) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 313 | .track_read_marker_and_receipts(TimelineReadReceiptTracking::AllEvents) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | .build() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 315 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | async fn watch_typing( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | room_id: OwnedRoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | mut typing: tokio::sync::broadcast::Receiver<Vec<ruma::OwnedUserId>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | events: crate::transport::EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 323 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | match typing.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | Ok(users) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 326 | let state = TypingState { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | room_id: room_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | users, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 330 | if events.send(Event::TypingChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 331 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 332 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 333 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 334 | Err(tokio::sync::broadcast::error::RecvError::Lagged(skipped)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 335 | tracing::warn!(%room_id, skipped, "typing watcher lagged"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | Err(tokio::sync::broadcast::error::RecvError::Closed) => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 338 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 339 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 340 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 341 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 342 | struct Timelines { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | room: TimelineSession, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | threads: Threads, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 345 | work: TaskSet<()>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 346 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 347 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 348 | impl Timelines { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 349 | async fn execute(&mut self, command: TimelineCommand, reply: Responder, room: &Room) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | match command { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 351 | TimelineCommand::OpenThread { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 352 | room_id, |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 353 | root_event_id, |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 354 | } => self |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 355 | .threads |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 356 | .open(TimelineId::thread(room_id, root_event_id), reply, room), |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 357 | TimelineCommand::CloseThread { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 358 | room_id, |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 359 | root_event_id, |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 360 | } => { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 361 | let id = TimelineId::thread(room_id, root_event_id); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 362 | reply.done(self.threads.close_one(&id).await); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 364 | TimelineCommand::OpenPinned(room_id) => { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 365 | self.threads.open(TimelineId::Pinned(room_id), reply, room); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 366 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 367 | TimelineCommand::ClosePinned(room_id) => { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 368 | reply.done(self.threads.close_one(&TimelineId::Pinned(room_id)).await); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | TimelineCommand::Resync { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | timeline_id: TimelineId::Room(_), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | } => reply.done(self.room.resync().await), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | command => { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 374 | let Some(id) = command |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | .timeline_id() |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 376 | .filter(|id| !matches!(id, TimelineId::Room(_))) |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 377 | .cloned() |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 378 | else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 379 | return self.room.execute(command, reply, &mut self.work).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 380 | }; |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 381 | if matches!(id, TimelineId::Pinned(_)) |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 382 | && matches!( |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 383 | command, |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 384 | TimelineCommand::Send { .. } | TimelineCommand::Paginate { .. } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 385 | ) |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 386 | { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 387 | return reply.done(Err(Error::new( |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 388 | ErrorKind::Invalid, |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 389 | "the pinned timeline can't send or paginate", |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 390 | ))); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 391 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 392 | if let TimelineCommand::MarkRead { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 393 | timeline_id, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 394 | event_id, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 395 | } = &command |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 396 | && !self.threads.open.contains_key(&id) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 397 | { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 398 | let client = room.client(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 399 | let (timeline_id, event_id) = (timeline_id.clone(), event_id.clone()); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 400 | self.work.spawn(async move { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 401 | reply.done( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 402 | super::commands::mark_read(&client, &timeline_id, event_id).await, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 403 | ); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 404 | }); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 405 | return; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 406 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 407 | match self.threads.route(id, command, reply) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 408 | Routed::Open(session, TimelineCommand::Resync { .. }, reply) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 409 | reply.done(session.resync().await); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 410 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 411 | Routed::Open(session, command, reply) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 412 | session.execute(command, reply, &mut self.work).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 413 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | Routed::Queued => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 415 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 416 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 417 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 418 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 419 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 420 | async fn thread_built( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 421 | &mut self, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 422 | built: Result<Built, JoinError>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 423 | room_id: &OwnedRoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 424 | context: &SessionContext, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 425 | ) { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 426 | let Some((id, queued, timeline)) = self.threads.built(built) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 427 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 428 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 429 | let timeline = match timeline { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 430 | Ok(timeline) => timeline, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 431 | Err(error) => { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 432 | tracing::warn!(%error, %room_id, ?id, "timeline build failed"); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 433 | let error = Error::with_source(ErrorKind::Timeline, "timeline build failed", error); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 434 | for (_, reply) in queued { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 435 | reply.done(Err(error.clone())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 436 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 437 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 438 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 439 | }; |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 440 | let Ok(session) = TimelineSession::start(id.clone(), timeline, context.clone()).await |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 441 | else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 442 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 443 | }; |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 444 | let session = self.threads.open.entry(id).insert_entry(session).into_mut(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 445 | for (command, reply) in queued { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 446 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 447 | None => reply.done(Ok(())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 448 | Some(TimelineCommand::Resync { .. }) => reply.done(session.resync().await), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 449 | Some(command) => session.execute(command, reply, &mut self.work).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 450 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 451 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 452 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 453 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 454 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 455 | type Built = ( |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 456 | TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 457 | u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 458 | Result<Timeline, matrix_sdk_ui::timeline::Error>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 459 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 460 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 461 | /// `None` is the open itself, everything else waits behind it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 462 | type Queued = VecDeque<(Option<TimelineCommand>, Responder)>; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 463 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 464 | enum Routed<'a> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 465 | Open(&'a mut TimelineSession, TimelineCommand, Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 466 | Queued, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 467 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 468 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 469 | struct OpeningThread { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 470 | generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 471 | build: AbortHandle, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 472 | queued: Queued, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 473 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 474 | |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 475 | /// threads and the pinned timeline, which build off the actor and queue what arrives meanwhile |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 476 | struct Threads { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 477 | open: HashMap<TimelineId, TimelineSession>, |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 478 | opening: HashMap<TimelineId, OpeningThread>, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 479 | builds: TaskSet<Built>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 480 | next_generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 481 | queued: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 482 | capacity: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 483 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 484 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 485 | impl Threads { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 486 | fn new(capacity: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 487 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 488 | open: HashMap::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 489 | opening: HashMap::new(), |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 490 | builds: TaskSet::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 491 | next_generation: 0, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 492 | queued: 0, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 493 | capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 494 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 495 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 496 | |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 497 | fn enqueue(&mut self, id: &TimelineId, command: Option<TimelineCommand>, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 498 | if self.queued >= self.capacity { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 499 | return reply.done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 500 | ErrorKind::QueueFull, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 501 | "thread command queue is full", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 502 | ))); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 503 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 504 | let Some(opening) = self.opening.get_mut(id) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 505 | return reply.done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 506 | ErrorKind::TimelineNotOpen, |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 507 | "timeline is not open", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 508 | ))); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 509 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 510 | opening.queued.push_back((command, reply)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 511 | self.queued += 1; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 512 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 513 | |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 514 | fn open(&mut self, id: TimelineId, reply: Responder, room: &Room) { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 515 | if self.open.contains_key(&id) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 516 | return reply.done(Ok(())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 517 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 518 | if !self.opening.contains_key(&id) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 519 | let generation = self.next_generation; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 520 | self.next_generation = self.next_generation.wrapping_add(1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 521 | let room = room.clone(); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 522 | let build_id = id.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 523 | let build = self.builds.spawn(async move { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 524 | let focus = match &build_id { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 525 | TimelineId::Thread { root_event_id, .. } => TimelineFocus::Thread { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 526 | root_event_id: root_event_id.clone(), |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 527 | }, |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 528 | _ => TimelineFocus::PinnedEvents, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 529 | }; |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 530 | (build_id, generation, build_timeline(&room, focus).await) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 531 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 532 | self.opening.insert( |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 533 | id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 534 | OpeningThread { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 535 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 536 | build, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 537 | queued: VecDeque::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 538 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 539 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 540 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 541 | self.enqueue(&id, None, reply); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 542 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 543 | |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 544 | fn route(&mut self, id: TimelineId, command: TimelineCommand, reply: Responder) -> Routed<'_> { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 545 | if !self.open.contains_key(&id) { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 546 | self.enqueue(&id, Some(command), reply); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 547 | return Routed::Queued; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 548 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 549 | let session = self.open.get_mut(&id).expect("timeline was just checked"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 550 | Routed::Open(session, command, reply) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 551 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 552 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 553 | /// `None` when the build belongs to a thread that was closed while it ran |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 554 | fn built( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 555 | &mut self, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 556 | built: Result<Built, JoinError>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 557 | ) -> Option<( |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 558 | TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 559 | Queued, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 560 | Result<Timeline, matrix_sdk_ui::timeline::Error>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 561 | )> { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 562 | let (id, generation, timeline) = match built { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 563 | Ok(built) => built, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 564 | Err(error) if error.is_cancelled() => return None, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 565 | Err(error) => resume_unwind(error), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 566 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 567 | if self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 568 | .opening |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 569 | .get(&id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 570 | .is_none_or(|opening| opening.generation != generation) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 571 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 572 | return None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 573 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 574 | let opening = self.opening.remove(&id)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 575 | self.queued -= opening.queued.len(); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 576 | Some((id, opening.queued, timeline)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 577 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 578 | |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 579 | async fn close_one(&mut self, id: &TimelineId) -> Result<(), Error> { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 580 | if let Some(session) = self.open.remove(id) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 581 | session.close().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 582 | return Ok(()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 583 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 584 | let Some(opening) = self.opening.remove(id) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 585 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 586 | ErrorKind::TimelineNotOpen, |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 587 | "timeline is not open", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 588 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 589 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 590 | opening.build.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 591 | self.queued -= opening.queued.len(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 592 | Ok(()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 593 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 594 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 595 | async fn close(&mut self) { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 596 | self.builds.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 597 | self.opening.clear(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 598 | for (_, session) in self.open.drain() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 599 | session.close().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 600 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 601 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 602 | } |