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}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | use ruma::{OwnedEventId, OwnedRoomId}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | use tokio::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | sync::mpsc, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | task::{AbortHandle, JoinSet}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 9 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 10 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | use super::timeline::{SessionContext, TimelineSession}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | use crate::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 13 | Error, ErrorKind, Event, TimelineCommand, TimelineId, 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), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | Close(Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | impl ActorRequest { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 24 | pub(super) fn into_reply(self) -> Responder { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 25 | match self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | Self::Open(reply) | Self::Execute(_, reply) | Self::Close(reply) => reply, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | pub(super) struct Exited { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | pub(super) room_id: OwnedRoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | pub(super) generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | pub(super) struct RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | pub(super) room_id: OwnedRoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | pub(super) generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | pub(super) room: Room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | pub(super) requests: mpsc::Receiver<ActorRequest>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | pub(super) exits: mpsc::UnboundedSender<Exited>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | pub(super) context: SessionContext, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | pub(super) panics: mpsc::UnboundedReceiver<&'static str>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | pub(super) opening: Vec<Responder>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | impl RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | pub(super) async fn run(mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | let exits = self.exits.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | let exited = Exited { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | room_id: self.room_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | generation: self.generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | self.serve().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | let _ = exits.send(exited); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | async fn serve(&mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | let timeline = match build_timeline( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | &self.room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | TimelineFocus::Live { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | hide_threaded_events: true, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | Ok(timeline) => timeline, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | Err(error) => return self.refuse_everything(error).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | let Ok(room_timeline) = TimelineSession::start( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | TimelineId::Room(self.room_id.clone()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | timeline, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | self.context.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | for reply in self.opening.drain(..) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | reply.done(Ok(())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | let (typing_guard, typing) = self.room.subscribe_to_typing_notifications(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | let typing_watcher = self.context.supervisor.spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | "typing watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | watch_typing(self.room_id.clone(), typing, self.context.events.clone()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | let mut timelines = Timelines { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | room: room_timeline, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | threads: Threads::new(self.requests.max_capacity()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | work: JoinSet::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | let close = self.serve_open(&mut timelines).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | // let redactions and receipts that already left finish rather than lie about them |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | while timelines.work.join_next().await.is_some() {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | drop(typing_guard); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | typing_watcher.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | timelines.threads.close().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | timelines.room.close().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | if let Some(reply) = close { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | reply.done(Ok(())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | /// returns the close request's responder, if a close is why we stopped |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | async fn serve_open(&mut self, timelines: &mut Timelines) -> Option<Responder> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | let mut room_info = self.room.subscribe_info(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | Some(info) = room_info.next() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | if matches!(info.state(), RoomState::Left | RoomState::Banned) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | tracing::debug!(room_id = %self.room_id, "room actor stopped because we left the room"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | return None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | request = self.requests.recv() => match request { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | None => return None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | Some(ActorRequest::Close(reply)) => return Some(reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | Some(ActorRequest::Open(reply)) => reply.done(timelines.room.resync().await), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | Some(ActorRequest::Execute(command, reply)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | timelines.execute(command, reply, &self.room).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | Some(built) = timelines.threads.builds.join_next(), if !timelines.threads.builds.is_empty() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | timelines.thread_built(built, &self.room_id, &self.context).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | Some(label) = self.panics.recv() => panic!("{label} panicked in room {}", self.room_id), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | Some(done) = timelines.work.join_next(), if !timelines.work.is_empty() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | if let Err(error) = done && error.is_panic() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | tracing::warn!(room_id = %self.room_id, %error, "timeline operation panicked"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | async fn refuse_everything(&mut self, error: matrix_sdk_ui::timeline::Error) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | tracing::warn!(%error, room_id = %self.room_id, "room actor could not build its timeline"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | let error = Error::with_source(ErrorKind::Timeline, "room timeline build failed", error); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | for reply in self.opening.drain(..) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | reply.done(Err(error.clone())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | self.requests.close(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 144 | while let Some(request) = self.requests.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 145 | request.into_reply().done(Err(error.clone())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 146 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 148 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 149 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 150 | async fn build_timeline( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 151 | room: &Room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 152 | focus: TimelineFocus, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 153 | ) -> Result<Timeline, matrix_sdk_ui::timeline::Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | room.timeline_builder() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | .with_focus(focus) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | .track_read_marker_and_receipts(TimelineReadReceiptTracking::AllEvents) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | .build() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 158 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 161 | async fn watch_typing( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 162 | room_id: OwnedRoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 163 | mut typing: tokio::sync::broadcast::Receiver<Vec<ruma::OwnedUserId>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 164 | events: crate::transport::EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 165 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 166 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 167 | match typing.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 168 | Ok(users) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 169 | let state = TypingState { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 170 | room_id: room_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 171 | users, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 172 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 173 | if events.send(Event::TypingChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 174 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 176 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 177 | Err(tokio::sync::broadcast::error::RecvError::Lagged(skipped)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 178 | tracing::warn!(%room_id, skipped, "typing watcher lagged"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 179 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 180 | Err(tokio::sync::broadcast::error::RecvError::Closed) => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 181 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 182 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 183 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 184 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 185 | struct Timelines { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 186 | room: TimelineSession, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 187 | threads: Threads, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 188 | work: JoinSet<()>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 189 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 190 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 191 | impl Timelines { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 192 | async fn execute(&mut self, command: TimelineCommand, reply: Responder, room: &Room) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 193 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | TimelineCommand::OpenThread { root_event_id, .. } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | self.threads.open(root_event_id, reply, room); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 196 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 197 | TimelineCommand::CloseThread { root_event_id, .. } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 198 | reply.done(self.threads.close_one(&root_event_id).await); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 199 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 200 | TimelineCommand::Resync { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 201 | timeline_id: TimelineId::Room(_), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 202 | } => reply.done(self.room.resync().await), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 203 | command => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 204 | let root = command |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 205 | .timeline_id() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 206 | .and_then(TimelineId::thread_root) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 207 | .cloned(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 208 | let Some(root) = root else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 209 | return self.room.execute(command, reply, &mut self.work).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 210 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 211 | match self.threads.route(root, command, reply) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 212 | Routed::Open(session, TimelineCommand::Resync { .. }, reply) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 213 | reply.done(session.resync().await); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 214 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | Routed::Open(session, command, reply) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 216 | session.execute(command, reply, &mut self.work).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 217 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 218 | Routed::Queued => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 219 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 220 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 221 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 222 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 223 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 224 | async fn thread_built( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 225 | &mut self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | built: Result<Built, tokio::task::JoinError>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | room_id: &OwnedRoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | context: &SessionContext, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 230 | let Some((root, queued, timeline)) = self.threads.built(built) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 231 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | let timeline = match timeline { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | Ok(timeline) => timeline, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 235 | Err(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 236 | tracing::warn!(%error, %room_id, %root, "thread timeline build failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 237 | let error = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 238 | Error::with_source(ErrorKind::Timeline, "thread timeline build failed", error); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | for (_, reply) in queued { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | reply.done(Err(error.clone())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 245 | let id = TimelineId::thread(room_id.clone(), root.clone()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 246 | let Ok(session) = TimelineSession::start(id, timeline, context.clone()).await else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 248 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 249 | let session = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 250 | .threads |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 251 | .open |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | .entry(root) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 253 | .insert_entry(session) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 254 | .into_mut(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | for (command, reply) in queued { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 257 | None => reply.done(Ok(())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 258 | Some(TimelineCommand::Resync { .. }) => reply.done(session.resync().await), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 259 | Some(command) => session.execute(command, reply, &mut self.work).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 261 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 262 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 263 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | type Built = ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | OwnedEventId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 267 | u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 268 | Result<Timeline, matrix_sdk_ui::timeline::Error>, |
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 | /// `None` is the open itself, everything else waits behind it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 272 | type Queued = VecDeque<(Option<TimelineCommand>, Responder)>; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 273 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 274 | enum Routed<'a> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 275 | Open(&'a mut TimelineSession, TimelineCommand, Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 276 | Queued, |
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 OpeningThread { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 280 | generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 281 | build: AbortHandle, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 282 | queued: Queued, |
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 | struct Threads { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 286 | open: HashMap<OwnedEventId, TimelineSession>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 287 | opening: HashMap<OwnedEventId, OpeningThread>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 288 | builds: JoinSet<Built>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 289 | next_generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 290 | queued: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 291 | capacity: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 292 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 293 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 294 | impl Threads { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 295 | fn new(capacity: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 296 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 297 | open: HashMap::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 298 | opening: HashMap::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 299 | builds: JoinSet::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 300 | next_generation: 0, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 301 | queued: 0, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 302 | capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 303 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 304 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 306 | fn enqueue(&mut self, root: &OwnedEventId, command: Option<TimelineCommand>, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 307 | if self.queued >= self.capacity { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 308 | return reply.done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | ErrorKind::QueueFull, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | "thread command queue is full", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | ))); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 313 | let Some(opening) = self.opening.get_mut(root) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | return reply.done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 315 | ErrorKind::TimelineNotOpen, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | "thread timeline is not open", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | ))); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | opening.queued.push_back((command, reply)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | self.queued += 1; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 323 | fn open(&mut self, root: OwnedEventId, reply: Responder, room: &Room) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | if self.open.contains_key(&root) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | return reply.done(Ok(())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 326 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | if !self.opening.contains_key(&root) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | let generation = self.next_generation; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | self.next_generation = self.next_generation.wrapping_add(1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 330 | let room = room.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 331 | let build_root = root.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 332 | let build = self.builds.spawn(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 333 | let focus = TimelineFocus::Thread { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 334 | root_event_id: build_root.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 335 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | (build_root, generation, build_timeline(&room, focus).await) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 338 | self.opening.insert( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 339 | root.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 340 | OpeningThread { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 341 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 342 | build, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | queued: VecDeque::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 345 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 346 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 347 | self.enqueue(&root, None, reply); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 348 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 349 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | fn route( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 351 | &mut self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 352 | root: OwnedEventId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 353 | command: TimelineCommand, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 354 | reply: Responder, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 355 | ) -> Routed<'_> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 356 | if !self.open.contains_key(&root) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 357 | self.enqueue(&root, Some(command), reply); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 358 | return Routed::Queued; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 359 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 360 | let session = self.open.get_mut(&root).expect("thread was just checked"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | Routed::Open(session, command, reply) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 362 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 364 | /// `None` when the build belongs to a thread that was closed while it ran |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | fn built( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | &mut self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | built: Result<Built, tokio::task::JoinError>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | ) -> Option<( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | OwnedEventId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | Queued, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | Result<Timeline, matrix_sdk_ui::timeline::Error>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | )> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | let (root, generation, timeline) = match built { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | Ok(built) => built, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | Err(error) if error.is_cancelled() => return None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | Err(error) => std::panic::resume_unwind(error.into_panic()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 377 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 378 | if self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 379 | .opening |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 380 | .get(&root) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 381 | .is_none_or(|opening| opening.generation != generation) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 382 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 383 | return None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 384 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 385 | let opening = self.opening.remove(&root)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 386 | self.queued -= opening.queued.len(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 387 | Some((root, opening.queued, timeline)) |
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 | async fn close_one(&mut self, root: &OwnedEventId) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 391 | if let Some(session) = self.open.remove(root) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 392 | session.close().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 393 | return Ok(()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 394 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 395 | let Some(opening) = self.opening.remove(root) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 396 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 397 | ErrorKind::TimelineNotOpen, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | "thread timeline is not open", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 400 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | opening.build.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 402 | self.queued -= opening.queued.len(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 403 | Ok(()) |
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 | async fn close(&mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 407 | self.builds.abort_all(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 408 | while self.builds.join_next().await.is_some() {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 409 | self.opening.clear(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 410 | for (_, session) in self.open.drain() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 411 | session.close().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 412 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 413 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | } |