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