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