atagen

macltok

public
Star0
Watch0
502ed9fc1d3eaa3f7654cebdb42a0f35a5bccfe1
3
ssh://brewery@pls.lobotomise.me:33/atagen/macltok.git

Blame: src/engine/room.rs

31ce2453c82fatagen2026-09-24 13:07 UTC1
use std::collections::{HashMap, VecDeque};
31ce2453c82fatagen2026-09-24 13:07 UTC2
31ce2453c82fatagen2026-09-24 13:07 UTC3
use matrix_sdk::{Room, RoomState};
31ce2453c82fatagen2026-09-24 13:07 UTC4
use matrix_sdk_ui::timeline::{RoomExt, Timeline, TimelineFocus, TimelineReadReceiptTracking};
fa5b4e09943aatagen2026-10-24 09:33 UTC5
use ruma::OwnedRoomId;
1fe480c16a3catagen2026-09-26 08:44 UTC6
use tokio::sync::mpsc;
31ce2453c82fatagen2026-09-24 13:07 UTC7
2819830aef32atagen2026-10-26 08:42 UTC8
use super::executor::{AbortHandle, JoinError, TaskSet, is_panic, label, resume_unwind};
d9b96952ec60atagen2026-10-25 09:07 UTC9
use super::repair::{Completion, Outcome, RepairController};
5a52012c9b1datagen2026-09-24 23:45 UTC10
use super::timeline::{SessionContext, TaskGuard, TimelineSession};
31ce2453c82fatagen2026-09-24 13:07 UTC11
use crate::{
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC12
    Error, ErrorKind, Event, HistoryRepairCommand, RecentHistoryState, TimelineCommand, TimelineId,
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC13
    TypingState, transport::Responder,
31ce2453c82fatagen2026-09-24 13:07 UTC14
};
31ce2453c82fatagen2026-09-24 13:07 UTC15
123824465d8batagen2026-10-26 09:18 UTC16
/// a build stuck on the event cache would otherwise hold the room's open forever. a big room on
123824465d8batagen2026-10-26 09:18 UTC17
/// a slow device can take tens of seconds to load from IndexedDB, so this is generous
123824465d8batagen2026-10-26 09:18 UTC18
const BUILD_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);
123824465d8batagen2026-10-26 09:18 UTC19
31ce2453c82fatagen2026-09-24 13:07 UTC20
pub(super) enum ActorRequest {
31ce2453c82fatagen2026-09-24 13:07 UTC21
    /// opening an open room answers with a fresh reset
31ce2453c82fatagen2026-09-24 13:07 UTC22
    Open(Responder),
31ce2453c82fatagen2026-09-24 13:07 UTC23
    Execute(TimelineCommand, Responder),
80381e06a573atagen2026-10-25 03:02 UTC24
    Repair(HistoryRepairCommand, Responder),
31ce2453c82fatagen2026-09-24 13:07 UTC25
    Close(Responder),
31ce2453c82fatagen2026-09-24 13:07 UTC26
}
31ce2453c82fatagen2026-09-24 13:07 UTC27
31ce2453c82fatagen2026-09-24 13:07 UTC28
impl ActorRequest {
31ce2453c82fatagen2026-09-24 13:07 UTC29
    pub(super) fn into_reply(self) -> Responder {
31ce2453c82fatagen2026-09-24 13:07 UTC30
        match self {
80381e06a573atagen2026-10-25 03:02 UTC31
            Self::Open(reply)
80381e06a573atagen2026-10-25 03:02 UTC32
            | Self::Execute(_, reply)
80381e06a573atagen2026-10-25 03:02 UTC33
            | Self::Repair(_, reply)
80381e06a573atagen2026-10-25 03:02 UTC34
            | Self::Close(reply) => reply,
31ce2453c82fatagen2026-09-24 13:07 UTC35
        }
31ce2453c82fatagen2026-09-24 13:07 UTC36
    }
31ce2453c82fatagen2026-09-24 13:07 UTC37
}
31ce2453c82fatagen2026-09-24 13:07 UTC38
5a52012c9b1datagen2026-09-24 23:45 UTC39
pub(super) enum ActorNote {
5a52012c9b1datagen2026-09-24 23:45 UTC40
    /// stopping on its own, after a leave or a kick. anything sent its way now comes back in `Exited`
5a52012c9b1datagen2026-09-24 23:45 UTC41
    Leaving {
5a52012c9b1datagen2026-09-24 23:45 UTC42
        room_id: OwnedRoomId,
5a52012c9b1datagen2026-09-24 23:45 UTC43
        generation: u64,
5a52012c9b1datagen2026-09-24 23:45 UTC44
    },
5a52012c9b1datagen2026-09-24 23:45 UTC45
    Exited {
5a52012c9b1datagen2026-09-24 23:45 UTC46
        room_id: OwnedRoomId,
5a52012c9b1datagen2026-09-24 23:45 UTC47
        generation: u64,
5a52012c9b1datagen2026-09-24 23:45 UTC48
        /// requests that arrived too late to be served, for the engine to answer or replay
5a52012c9b1datagen2026-09-24 23:45 UTC49
        leftovers: Vec<ActorRequest>,
5a52012c9b1datagen2026-09-24 23:45 UTC50
    },
31ce2453c82fatagen2026-09-24 13:07 UTC51
}
31ce2453c82fatagen2026-09-24 13:07 UTC52
31ce2453c82fatagen2026-09-24 13:07 UTC53
pub(super) struct RoomActor {
31ce2453c82fatagen2026-09-24 13:07 UTC54
    pub(super) room_id: OwnedRoomId,
31ce2453c82fatagen2026-09-24 13:07 UTC55
    pub(super) generation: u64,
31ce2453c82fatagen2026-09-24 13:07 UTC56
    pub(super) room: Room,
31ce2453c82fatagen2026-09-24 13:07 UTC57
    pub(super) requests: mpsc::Receiver<ActorRequest>,
5a52012c9b1datagen2026-09-24 23:45 UTC58
    pub(super) notes: mpsc::UnboundedSender<ActorNote>,
31ce2453c82fatagen2026-09-24 13:07 UTC59
    pub(super) context: SessionContext,
31ce2453c82fatagen2026-09-24 13:07 UTC60
    pub(super) panics: mpsc::UnboundedReceiver<&'static str>,
31ce2453c82fatagen2026-09-24 13:07 UTC61
    pub(super) opening: Vec<Responder>,
31ce2453c82fatagen2026-09-24 13:07 UTC62
}
31ce2453c82fatagen2026-09-24 13:07 UTC63
31ce2453c82fatagen2026-09-24 13:07 UTC64
impl RoomActor {
31ce2453c82fatagen2026-09-24 13:07 UTC65
    pub(super) async fn run(mut self) {
5a52012c9b1datagen2026-09-24 23:45 UTC66
        self.serve().await;
5a52012c9b1datagen2026-09-24 23:45 UTC67
        self.requests.close();
5a52012c9b1datagen2026-09-24 23:45 UTC68
        let mut leftovers = Vec::new();
5a52012c9b1datagen2026-09-24 23:45 UTC69
        while let Ok(request) = self.requests.try_recv() {
5a52012c9b1datagen2026-09-24 23:45 UTC70
            leftovers.push(request);
5a52012c9b1datagen2026-09-24 23:45 UTC71
        }
5a52012c9b1datagen2026-09-24 23:45 UTC72
        let _ = self.notes.send(ActorNote::Exited {
31ce2453c82fatagen2026-09-24 13:07 UTC73
            room_id: self.room_id.clone(),
31ce2453c82fatagen2026-09-24 13:07 UTC74
            generation: self.generation,
5a52012c9b1datagen2026-09-24 23:45 UTC75
            leftovers,
5a52012c9b1datagen2026-09-24 23:45 UTC76
        });
31ce2453c82fatagen2026-09-24 13:07 UTC77
    }
31ce2453c82fatagen2026-09-24 13:07 UTC78
31ce2453c82fatagen2026-09-24 13:07 UTC79
    async fn serve(&mut self) {
2819830aef32atagen2026-10-26 08:42 UTC80
        tracing::debug!(room_id = %self.room_id, "building the room timeline");
123824465d8batagen2026-10-26 09:18 UTC81
        let build = build_timeline(
31ce2453c82fatagen2026-09-24 13:07 UTC82
            &self.room,
31ce2453c82fatagen2026-09-24 13:07 UTC83
            TimelineFocus::Live {
31ce2453c82fatagen2026-09-24 13:07 UTC84
                hide_threaded_events: true,
31ce2453c82fatagen2026-09-24 13:07 UTC85
            },
123824465d8batagen2026-10-26 09:18 UTC86
        );
123824465d8batagen2026-10-26 09:18 UTC87
        let timeout = matrix_sdk::sleep::sleep(BUILD_TIMEOUT);
123824465d8batagen2026-10-26 09:18 UTC88
        let timeline = tokio::select! {
123824465d8batagen2026-10-26 09:18 UTC89
            built = build => match built {
123824465d8batagen2026-10-26 09:18 UTC90
                Ok(timeline) => timeline,
123824465d8batagen2026-10-26 09:18 UTC91
                Err(error) => {
123824465d8batagen2026-10-26 09:18 UTC92
                    tracing::warn!(%error, room_id = %self.room_id, "room actor could not build its timeline");
123824465d8batagen2026-10-26 09:18 UTC93
                    let error = Error::with_source(ErrorKind::Timeline, "room timeline build failed", error);
123824465d8batagen2026-10-26 09:18 UTC94
                    return self.refuse_everything(error).await;
123824465d8batagen2026-10-26 09:18 UTC95
                }
123824465d8batagen2026-10-26 09:18 UTC96
            },
123824465d8batagen2026-10-26 09:18 UTC97
            () = timeout => {
123824465d8batagen2026-10-26 09:18 UTC98
                tracing::warn!(room_id = %self.room_id, "room timeline build timed out");
123824465d8batagen2026-10-26 09:18 UTC99
                let error = Error::new(ErrorKind::Timeline, "room timeline build timed out");
123824465d8batagen2026-10-26 09:18 UTC100
                return self.refuse_everything(error).await;
123824465d8batagen2026-10-26 09:18 UTC101
            }
31ce2453c82fatagen2026-09-24 13:07 UTC102
        };
2819830aef32atagen2026-10-26 08:42 UTC103
        tracing::debug!(room_id = %self.room_id, "starting the room timeline session");
31ce2453c82fatagen2026-09-24 13:07 UTC104
        let Ok(room_timeline) = TimelineSession::start(
31ce2453c82fatagen2026-09-24 13:07 UTC105
            TimelineId::Room(self.room_id.clone()),
31ce2453c82fatagen2026-09-24 13:07 UTC106
            timeline,
31ce2453c82fatagen2026-09-24 13:07 UTC107
            self.context.clone(),
31ce2453c82fatagen2026-09-24 13:07 UTC108
        )
31ce2453c82fatagen2026-09-24 13:07 UTC109
        .await
31ce2453c82fatagen2026-09-24 13:07 UTC110
        else {
31ce2453c82fatagen2026-09-24 13:07 UTC111
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC112
        };
31ce2453c82fatagen2026-09-24 13:07 UTC113
        let (typing_guard, typing) = self.room.subscribe_to_typing_notifications();
0bfa3f46f0beatagen2026-09-26 09:12 UTC114
        let mut typing_watcher = TaskGuard(self.context.supervisor.spawn(
31ce2453c82fatagen2026-09-24 13:07 UTC115
            "typing watcher",
31ce2453c82fatagen2026-09-24 13:07 UTC116
            watch_typing(self.room_id.clone(), typing, self.context.events.clone()),
5a52012c9b1datagen2026-09-24 23:45 UTC117
        ));
31ce2453c82fatagen2026-09-24 13:07 UTC118
        let mut timelines = Timelines {
31ce2453c82fatagen2026-09-24 13:07 UTC119
            room: room_timeline,
31ce2453c82fatagen2026-09-24 13:07 UTC120
            threads: Threads::new(self.requests.max_capacity()),
1fe480c16a3catagen2026-09-26 08:44 UTC121
            work: TaskSet::new(),
31ce2453c82fatagen2026-09-24 13:07 UTC122
        };
80381e06a573atagen2026-10-25 03:02 UTC123
        let mut repair = RepairController::new(self.room.clone(), self.context.clone());
2819830aef32atagen2026-10-26 08:42 UTC124
        tracing::debug!(room_id = %self.room_id, "starting recent history");
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC125
        repair.start_recent().await;
2819830aef32atagen2026-10-26 08:42 UTC126
        tracing::debug!(room_id = %self.room_id, "room opened");
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC127
        for reply in self.opening.drain(..) {
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC128
            reply.done(Ok(()));
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC129
        }
80381e06a573atagen2026-10-25 03:02 UTC130
        let close = self.serve_open(&mut timelines, &mut repair).await;
d9b96952ec60atagen2026-10-25 09:07 UTC131
        let recent_settled = match repair.shutdown().await {
d9b96952ec60atagen2026-10-25 09:07 UTC132
            Some(completion) => {
d9b96952ec60atagen2026-10-25 09:07 UTC133
                let recent = matches!(completion.outcome, Outcome::Recent(_));
d9b96952ec60atagen2026-10-25 09:07 UTC134
                self.finish_repair(completion, &mut timelines).await;
d9b96952ec60atagen2026-10-25 09:07 UTC135
                recent
d9b96952ec60atagen2026-10-25 09:07 UTC136
            }
d9b96952ec60atagen2026-10-25 09:07 UTC137
            None => false,
d9b96952ec60atagen2026-10-25 09:07 UTC138
        };
d9b96952ec60atagen2026-10-25 09:07 UTC139
        if !recent_settled {
d9b96952ec60atagen2026-10-25 09:07 UTC140
            let _ = self
d9b96952ec60atagen2026-10-25 09:07 UTC141
                .context
d9b96952ec60atagen2026-10-25 09:07 UTC142
                .events
d9b96952ec60atagen2026-10-25 09:07 UTC143
                .send(Event::RecentHistoryChanged {
d9b96952ec60atagen2026-10-25 09:07 UTC144
                    room_id: self.room_id.clone(),
d9b96952ec60atagen2026-10-25 09:07 UTC145
                    state: RecentHistoryState::Idle,
d9b96952ec60atagen2026-10-25 09:07 UTC146
                })
d9b96952ec60atagen2026-10-25 09:07 UTC147
                .await;
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC148
        }
5a52012c9b1datagen2026-09-24 23:45 UTC149
        if close.is_none() {
5a52012c9b1datagen2026-09-24 23:45 UTC150
            self.requests.close();
5a52012c9b1datagen2026-09-24 23:45 UTC151
            let _ = self.notes.send(ActorNote::Leaving {
5a52012c9b1datagen2026-09-24 23:45 UTC152
                room_id: self.room_id.clone(),
5a52012c9b1datagen2026-09-24 23:45 UTC153
                generation: self.generation,
5a52012c9b1datagen2026-09-24 23:45 UTC154
            });
5a52012c9b1datagen2026-09-24 23:45 UTC155
        }
31ce2453c82fatagen2026-09-24 13:07 UTC156
31ce2453c82fatagen2026-09-24 13:07 UTC157
        // let redactions and receipts that already left finish rather than lie about them
31ce2453c82fatagen2026-09-24 13:07 UTC158
        while timelines.work.join_next().await.is_some() {}
31ce2453c82fatagen2026-09-24 13:07 UTC159
        drop(typing_guard);
0bfa3f46f0beatagen2026-09-26 09:12 UTC160
        typing_watcher.0.abort();
0bfa3f46f0beatagen2026-09-26 09:12 UTC161
        let _ = (&mut typing_watcher.0).await;
0bfa3f46f0beatagen2026-09-26 09:12 UTC162
        let _ = self
0bfa3f46f0beatagen2026-09-26 09:12 UTC163
            .context
0bfa3f46f0beatagen2026-09-26 09:12 UTC164
            .events
0bfa3f46f0beatagen2026-09-26 09:12 UTC165
            .send(Event::TypingChanged(TypingState {
0bfa3f46f0beatagen2026-09-26 09:12 UTC166
                room_id: self.room_id.clone(),
0bfa3f46f0beatagen2026-09-26 09:12 UTC167
                users: Vec::new(),
0bfa3f46f0beatagen2026-09-26 09:12 UTC168
            }))
0bfa3f46f0beatagen2026-09-26 09:12 UTC169
            .await;
31ce2453c82fatagen2026-09-24 13:07 UTC170
        timelines.threads.close().await;
31ce2453c82fatagen2026-09-24 13:07 UTC171
        timelines.room.close().await;
31ce2453c82fatagen2026-09-24 13:07 UTC172
        if let Some(reply) = close {
31ce2453c82fatagen2026-09-24 13:07 UTC173
            reply.done(Ok(()));
31ce2453c82fatagen2026-09-24 13:07 UTC174
        }
31ce2453c82fatagen2026-09-24 13:07 UTC175
    }
31ce2453c82fatagen2026-09-24 13:07 UTC176
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC177
    async fn finish_repair(&self, mut completion: Completion, timelines: &mut Timelines) {
d9b96952ec60atagen2026-10-25 09:07 UTC178
        let mut cancellation = Ok(());
002c79e4c6ecatagen2026-10-25 10:42 UTC179
        // pages a failed or cancelled operation already committed still have to be on
002c79e4c6ecatagen2026-10-25 10:42 UTC180
        // screen before anyone hears it stopped
d9b96952ec60atagen2026-10-25 09:07 UTC181
        if completion.committed
d9b96952ec60atagen2026-10-25 09:07 UTC182
            && !completion.closing
d9b96952ec60atagen2026-10-25 09:07 UTC183
            && let Err(error) = timelines.room.publish_history().await
d9b96952ec60atagen2026-10-25 09:07 UTC184
        {
002c79e4c6ecatagen2026-10-25 10:42 UTC185
            if completion.result.is_ok() {
002c79e4c6ecatagen2026-10-25 10:42 UTC186
                completion.unpublished(error.clone());
002c79e4c6ecatagen2026-10-25 10:42 UTC187
            }
d9b96952ec60atagen2026-10-25 09:07 UTC188
            cancellation = Err(error);
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC189
        }
d9b96952ec60atagen2026-10-25 09:07 UTC190
        let event = match &completion.outcome {
d9b96952ec60atagen2026-10-25 09:07 UTC191
            Outcome::Recent(state) => Event::RecentHistoryChanged {
d9b96952ec60atagen2026-10-25 09:07 UTC192
                room_id: self.room_id.clone(),
d9b96952ec60atagen2026-10-25 09:07 UTC193
                state: state.clone(),
d9b96952ec60atagen2026-10-25 09:07 UTC194
            },
d9b96952ec60atagen2026-10-25 09:07 UTC195
            Outcome::Fill(report) => Event::HistoryRepairChanged(report.clone()),
d9b96952ec60atagen2026-10-25 09:07 UTC196
        };
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC197
        let mut resolved = completion.result.clone();
d9b96952ec60atagen2026-10-25 09:07 UTC198
        let events = &self.context.events;
d9b96952ec60atagen2026-10-25 09:07 UTC199
        if events.send(event).await.is_err() || events.publish_barrier().await.is_err() {
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC200
            resolved = Err(Error::interrupted());
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC201
            cancellation = Err(Error::interrupted());
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC202
        }
d9b96952ec60atagen2026-10-25 09:07 UTC203
        let limit = self.context.initial_history_limit;
d9b96952ec60atagen2026-10-25 09:07 UTC204
        if matches!(
d9b96952ec60atagen2026-10-25 09:07 UTC205
            completion.outcome,
d9b96952ec60atagen2026-10-25 09:07 UTC206
            Outcome::Recent(RecentHistoryState::Failed(_))
d9b96952ec60atagen2026-10-25 09:07 UTC207
        ) && limit > 0
002c79e4c6ecatagen2026-10-25 10:42 UTC208
            && !timelines.room.has_events().await
d9b96952ec60atagen2026-10-25 09:07 UTC209
        {
d9b96952ec60atagen2026-10-25 09:07 UTC210
            timelines.room.paginate(limit, None);
d9b96952ec60atagen2026-10-25 09:07 UTC211
        }
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC212
        completion.resolve(resolved, cancellation);
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC213
    }
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC214
31ce2453c82fatagen2026-09-24 13:07 UTC215
    /// returns the close request's responder, if a close is why we stopped
80381e06a573atagen2026-10-25 03:02 UTC216
    async fn serve_open(
80381e06a573atagen2026-10-25 03:02 UTC217
        &mut self,
80381e06a573atagen2026-10-25 03:02 UTC218
        timelines: &mut Timelines,
80381e06a573atagen2026-10-25 03:02 UTC219
        repair: &mut RepairController,
80381e06a573atagen2026-10-25 03:02 UTC220
    ) -> Option<Responder> {
31ce2453c82fatagen2026-09-24 13:07 UTC221
        let mut room_info = self.room.subscribe_info();
5a52012c9b1datagen2026-09-24 23:45 UTC222
        // a leave that landed while the timeline was building won't come round again
5a52012c9b1datagen2026-09-24 23:45 UTC223
        if matches!(self.room.state(), RoomState::Left | RoomState::Banned) {
5a52012c9b1datagen2026-09-24 23:45 UTC224
            return None;
5a52012c9b1datagen2026-09-24 23:45 UTC225
        }
31ce2453c82fatagen2026-09-24 13:07 UTC226
        loop {
31ce2453c82fatagen2026-09-24 13:07 UTC227
            tokio::select! {
31ce2453c82fatagen2026-09-24 13:07 UTC228
                Some(info) = room_info.next() => {
31ce2453c82fatagen2026-09-24 13:07 UTC229
                    if matches!(info.state(), RoomState::Left | RoomState::Banned) {
31ce2453c82fatagen2026-09-24 13:07 UTC230
                        tracing::debug!(room_id = %self.room_id, "room actor stopped because we left the room");
31ce2453c82fatagen2026-09-24 13:07 UTC231
                        return None;
31ce2453c82fatagen2026-09-24 13:07 UTC232
                    }
31ce2453c82fatagen2026-09-24 13:07 UTC233
                }
31ce2453c82fatagen2026-09-24 13:07 UTC234
                request = self.requests.recv() => match request {
31ce2453c82fatagen2026-09-24 13:07 UTC235
                    None => return None,
31ce2453c82fatagen2026-09-24 13:07 UTC236
                    Some(ActorRequest::Close(reply)) => return Some(reply),
a951cb7cb7caatagen2026-10-24 23:17 UTC237
                    Some(ActorRequest::Open(reply)) => reply.done(timelines.room.resync().await),
31ce2453c82fatagen2026-09-24 13:07 UTC238
                    Some(ActorRequest::Execute(command, reply)) => {
2819830aef32atagen2026-10-26 08:42 UTC239
                        let label = label(&command);
2819830aef32atagen2026-10-26 08:42 UTC240
                        tracing::debug!(room_id = %self.room_id, command = %label, "room command");
31ce2453c82fatagen2026-09-24 13:07 UTC241
                        timelines.execute(command, reply, &self.room).await;
2819830aef32atagen2026-10-26 08:42 UTC242
                        tracing::debug!(room_id = %self.room_id, command = %label, "room command handed off");
31ce2453c82fatagen2026-09-24 13:07 UTC243
                    }
80381e06a573atagen2026-10-25 03:02 UTC244
                    Some(ActorRequest::Repair(command, reply)) => {
2819830aef32atagen2026-10-26 08:42 UTC245
                        let label = label(&command);
2819830aef32atagen2026-10-26 08:42 UTC246
                        tracing::debug!(room_id = %self.room_id, command = %label, "room repair command");
80381e06a573atagen2026-10-25 03:02 UTC247
                        repair.execute(command, reply).await;
2819830aef32atagen2026-10-26 08:42 UTC248
                        tracing::debug!(room_id = %self.room_id, command = %label, "room repair command handed off");
80381e06a573atagen2026-10-25 03:02 UTC249
                    }
31ce2453c82fatagen2026-09-24 13:07 UTC250
                },
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC251
                completion = repair.next_completion(), if repair.is_running() => {
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC252
                    if let Some(completion) = completion {
c9f0fd0ee1ccatagen2026-10-25 06:16 UTC253
                        self.finish_repair(completion, timelines).await;
80381e06a573atagen2026-10-25 03:02 UTC254
                    }
80381e06a573atagen2026-10-25 03:02 UTC255
                }
31ce2453c82fatagen2026-09-24 13:07 UTC256
                Some(built) = timelines.threads.builds.join_next(), if !timelines.threads.builds.is_empty() => {
31ce2453c82fatagen2026-09-24 13:07 UTC257
                    timelines.thread_built(built, &self.room_id, &self.context).await;
31ce2453c82fatagen2026-09-24 13:07 UTC258
                }
31ce2453c82fatagen2026-09-24 13:07 UTC259
                Some(label) = self.panics.recv() => panic!("{label} panicked in room {}", self.room_id),
31ce2453c82fatagen2026-09-24 13:07 UTC260
                Some(done) = timelines.work.join_next(), if !timelines.work.is_empty() => {
1fe480c16a3catagen2026-09-26 08:44 UTC261
                    if let Err(error) = done && is_panic(&error) {
31ce2453c82fatagen2026-09-24 13:07 UTC262
                        tracing::warn!(room_id = %self.room_id, %error, "timeline operation panicked");
31ce2453c82fatagen2026-09-24 13:07 UTC263
                    }
31ce2453c82fatagen2026-09-24 13:07 UTC264
                }
31ce2453c82fatagen2026-09-24 13:07 UTC265
            }
31ce2453c82fatagen2026-09-24 13:07 UTC266
        }
31ce2453c82fatagen2026-09-24 13:07 UTC267
    }
31ce2453c82fatagen2026-09-24 13:07 UTC268
123824465d8batagen2026-10-26 09:18 UTC269
    async fn refuse_everything(&mut self, error: Error) {
31ce2453c82fatagen2026-09-24 13:07 UTC270
        for reply in self.opening.drain(..) {
31ce2453c82fatagen2026-09-24 13:07 UTC271
            reply.done(Err(error.clone()));
31ce2453c82fatagen2026-09-24 13:07 UTC272
        }
31ce2453c82fatagen2026-09-24 13:07 UTC273
        self.requests.close();
31ce2453c82fatagen2026-09-24 13:07 UTC274
        while let Some(request) = self.requests.recv().await {
31ce2453c82fatagen2026-09-24 13:07 UTC275
            request.into_reply().done(Err(error.clone()));
31ce2453c82fatagen2026-09-24 13:07 UTC276
        }
31ce2453c82fatagen2026-09-24 13:07 UTC277
    }
31ce2453c82fatagen2026-09-24 13:07 UTC278
}
31ce2453c82fatagen2026-09-24 13:07 UTC279
31ce2453c82fatagen2026-09-24 13:07 UTC280
async fn build_timeline(
31ce2453c82fatagen2026-09-24 13:07 UTC281
    room: &Room,
31ce2453c82fatagen2026-09-24 13:07 UTC282
    focus: TimelineFocus,
31ce2453c82fatagen2026-09-24 13:07 UTC283
) -> Result<Timeline, matrix_sdk_ui::timeline::Error> {
31ce2453c82fatagen2026-09-24 13:07 UTC284
    room.timeline_builder()
31ce2453c82fatagen2026-09-24 13:07 UTC285
        .with_focus(focus)
31ce2453c82fatagen2026-09-24 13:07 UTC286
        .track_read_marker_and_receipts(TimelineReadReceiptTracking::AllEvents)
31ce2453c82fatagen2026-09-24 13:07 UTC287
        .build()
31ce2453c82fatagen2026-09-24 13:07 UTC288
        .await
31ce2453c82fatagen2026-09-24 13:07 UTC289
}
31ce2453c82fatagen2026-09-24 13:07 UTC290
31ce2453c82fatagen2026-09-24 13:07 UTC291
async fn watch_typing(
31ce2453c82fatagen2026-09-24 13:07 UTC292
    room_id: OwnedRoomId,
31ce2453c82fatagen2026-09-24 13:07 UTC293
    mut typing: tokio::sync::broadcast::Receiver<Vec<ruma::OwnedUserId>>,
31ce2453c82fatagen2026-09-24 13:07 UTC294
    events: crate::transport::EventSender,
31ce2453c82fatagen2026-09-24 13:07 UTC295
) {
31ce2453c82fatagen2026-09-24 13:07 UTC296
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC297
        match typing.recv().await {
31ce2453c82fatagen2026-09-24 13:07 UTC298
            Ok(users) => {
31ce2453c82fatagen2026-09-24 13:07 UTC299
                let state = TypingState {
31ce2453c82fatagen2026-09-24 13:07 UTC300
                    room_id: room_id.clone(),
31ce2453c82fatagen2026-09-24 13:07 UTC301
                    users,
31ce2453c82fatagen2026-09-24 13:07 UTC302
                };
31ce2453c82fatagen2026-09-24 13:07 UTC303
                if events.send(Event::TypingChanged(state)).await.is_err() {
31ce2453c82fatagen2026-09-24 13:07 UTC304
                    return;
31ce2453c82fatagen2026-09-24 13:07 UTC305
                }
31ce2453c82fatagen2026-09-24 13:07 UTC306
            }
31ce2453c82fatagen2026-09-24 13:07 UTC307
            Err(tokio::sync::broadcast::error::RecvError::Lagged(skipped)) => {
31ce2453c82fatagen2026-09-24 13:07 UTC308
                tracing::warn!(%room_id, skipped, "typing watcher lagged");
31ce2453c82fatagen2026-09-24 13:07 UTC309
            }
31ce2453c82fatagen2026-09-24 13:07 UTC310
            Err(tokio::sync::broadcast::error::RecvError::Closed) => return,
31ce2453c82fatagen2026-09-24 13:07 UTC311
        }
31ce2453c82fatagen2026-09-24 13:07 UTC312
    }
31ce2453c82fatagen2026-09-24 13:07 UTC313
}
31ce2453c82fatagen2026-09-24 13:07 UTC314
31ce2453c82fatagen2026-09-24 13:07 UTC315
struct Timelines {
31ce2453c82fatagen2026-09-24 13:07 UTC316
    room: TimelineSession,
31ce2453c82fatagen2026-09-24 13:07 UTC317
    threads: Threads,
1fe480c16a3catagen2026-09-26 08:44 UTC318
    work: TaskSet<()>,
31ce2453c82fatagen2026-09-24 13:07 UTC319
}
31ce2453c82fatagen2026-09-24 13:07 UTC320
31ce2453c82fatagen2026-09-24 13:07 UTC321
impl Timelines {
31ce2453c82fatagen2026-09-24 13:07 UTC322
    async fn execute(&mut self, command: TimelineCommand, reply: Responder, room: &Room) {
31ce2453c82fatagen2026-09-24 13:07 UTC323
        match command {
fa5b4e09943aatagen2026-10-24 09:33 UTC324
            TimelineCommand::OpenThread {
fa5b4e09943aatagen2026-10-24 09:33 UTC325
                room_id,
fa5b4e09943aatagen2026-10-24 09:33 UTC326
                root_event_id,
fa5b4e09943aatagen2026-10-24 09:33 UTC327
            } => self
fa5b4e09943aatagen2026-10-24 09:33 UTC328
                .threads
fa5b4e09943aatagen2026-10-24 09:33 UTC329
                .open(TimelineId::thread(room_id, root_event_id), reply, room),
fa5b4e09943aatagen2026-10-24 09:33 UTC330
            TimelineCommand::CloseThread {
fa5b4e09943aatagen2026-10-24 09:33 UTC331
                room_id,
fa5b4e09943aatagen2026-10-24 09:33 UTC332
                root_event_id,
fa5b4e09943aatagen2026-10-24 09:33 UTC333
            } => {
fa5b4e09943aatagen2026-10-24 09:33 UTC334
                let id = TimelineId::thread(room_id, root_event_id);
fa5b4e09943aatagen2026-10-24 09:33 UTC335
                reply.done(self.threads.close_one(&id).await);
31ce2453c82fatagen2026-09-24 13:07 UTC336
            }
fa5b4e09943aatagen2026-10-24 09:33 UTC337
            TimelineCommand::OpenPinned(room_id) => {
fa5b4e09943aatagen2026-10-24 09:33 UTC338
                self.threads.open(TimelineId::Pinned(room_id), reply, room);
fa5b4e09943aatagen2026-10-24 09:33 UTC339
            }
fa5b4e09943aatagen2026-10-24 09:33 UTC340
            TimelineCommand::ClosePinned(room_id) => {
fa5b4e09943aatagen2026-10-24 09:33 UTC341
                reply.done(self.threads.close_one(&TimelineId::Pinned(room_id)).await);
31ce2453c82fatagen2026-09-24 13:07 UTC342
            }
31ce2453c82fatagen2026-09-24 13:07 UTC343
            TimelineCommand::Resync {
31ce2453c82fatagen2026-09-24 13:07 UTC344
                timeline_id: TimelineId::Room(_),
31ce2453c82fatagen2026-09-24 13:07 UTC345
            } => reply.done(self.room.resync().await),
31ce2453c82fatagen2026-09-24 13:07 UTC346
            command => {
fa5b4e09943aatagen2026-10-24 09:33 UTC347
                let Some(id) = command
31ce2453c82fatagen2026-09-24 13:07 UTC348
                    .timeline_id()
fa5b4e09943aatagen2026-10-24 09:33 UTC349
                    .filter(|id| !matches!(id, TimelineId::Room(_)))
fa5b4e09943aatagen2026-10-24 09:33 UTC350
                    .cloned()
fa5b4e09943aatagen2026-10-24 09:33 UTC351
                else {
31ce2453c82fatagen2026-09-24 13:07 UTC352
                    return self.room.execute(command, reply, &mut self.work).await;
31ce2453c82fatagen2026-09-24 13:07 UTC353
                };
fa5b4e09943aatagen2026-10-24 09:33 UTC354
                if matches!(id, TimelineId::Pinned(_))
fa5b4e09943aatagen2026-10-24 09:33 UTC355
                    && matches!(
fa5b4e09943aatagen2026-10-24 09:33 UTC356
                        command,
fa5b4e09943aatagen2026-10-24 09:33 UTC357
                        TimelineCommand::Send { .. } | TimelineCommand::Paginate { .. }
fa5b4e09943aatagen2026-10-24 09:33 UTC358
                    )
fa5b4e09943aatagen2026-10-24 09:33 UTC359
                {
fa5b4e09943aatagen2026-10-24 09:33 UTC360
                    return reply.done(Err(Error::new(
fa5b4e09943aatagen2026-10-24 09:33 UTC361
                        ErrorKind::Invalid,
fa5b4e09943aatagen2026-10-24 09:33 UTC362
                        "the pinned timeline can't send or paginate",
fa5b4e09943aatagen2026-10-24 09:33 UTC363
                    )));
fa5b4e09943aatagen2026-10-24 09:33 UTC364
                }
5a52012c9b1datagen2026-09-24 23:45 UTC365
                if let TimelineCommand::MarkRead {
5a52012c9b1datagen2026-09-24 23:45 UTC366
                    timeline_id,
5a52012c9b1datagen2026-09-24 23:45 UTC367
                    event_id,
5a52012c9b1datagen2026-09-24 23:45 UTC368
                } = &command
fa5b4e09943aatagen2026-10-24 09:33 UTC369
                    && !self.threads.open.contains_key(&id)
5a52012c9b1datagen2026-09-24 23:45 UTC370
                {
5a52012c9b1datagen2026-09-24 23:45 UTC371
                    let client = room.client();
5a52012c9b1datagen2026-09-24 23:45 UTC372
                    let (timeline_id, event_id) = (timeline_id.clone(), event_id.clone());
5a52012c9b1datagen2026-09-24 23:45 UTC373
                    self.work.spawn(async move {
5a52012c9b1datagen2026-09-24 23:45 UTC374
                        reply.done(
5a52012c9b1datagen2026-09-24 23:45 UTC375
                            super::commands::mark_read(&client, &timeline_id, event_id).await,
5a52012c9b1datagen2026-09-24 23:45 UTC376
                        );
5a52012c9b1datagen2026-09-24 23:45 UTC377
                    });
5a52012c9b1datagen2026-09-24 23:45 UTC378
                    return;
5a52012c9b1datagen2026-09-24 23:45 UTC379
                }
fa5b4e09943aatagen2026-10-24 09:33 UTC380
                match self.threads.route(id, command, reply) {
31ce2453c82fatagen2026-09-24 13:07 UTC381
                    Routed::Open(session, TimelineCommand::Resync { .. }, reply) => {
31ce2453c82fatagen2026-09-24 13:07 UTC382
                        reply.done(session.resync().await);
31ce2453c82fatagen2026-09-24 13:07 UTC383
                    }
31ce2453c82fatagen2026-09-24 13:07 UTC384
                    Routed::Open(session, command, reply) => {
31ce2453c82fatagen2026-09-24 13:07 UTC385
                        session.execute(command, reply, &mut self.work).await;
31ce2453c82fatagen2026-09-24 13:07 UTC386
                    }
31ce2453c82fatagen2026-09-24 13:07 UTC387
                    Routed::Queued => {}
31ce2453c82fatagen2026-09-24 13:07 UTC388
                }
31ce2453c82fatagen2026-09-24 13:07 UTC389
            }
31ce2453c82fatagen2026-09-24 13:07 UTC390
        }
31ce2453c82fatagen2026-09-24 13:07 UTC391
    }
31ce2453c82fatagen2026-09-24 13:07 UTC392
31ce2453c82fatagen2026-09-24 13:07 UTC393
    async fn thread_built(
31ce2453c82fatagen2026-09-24 13:07 UTC394
        &mut self,
1fe480c16a3catagen2026-09-26 08:44 UTC395
        built: Result<Built, JoinError>,
31ce2453c82fatagen2026-09-24 13:07 UTC396
        room_id: &OwnedRoomId,
31ce2453c82fatagen2026-09-24 13:07 UTC397
        context: &SessionContext,
31ce2453c82fatagen2026-09-24 13:07 UTC398
    ) {
fa5b4e09943aatagen2026-10-24 09:33 UTC399
        let Some((id, queued, timeline)) = self.threads.built(built) else {
31ce2453c82fatagen2026-09-24 13:07 UTC400
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC401
        };
31ce2453c82fatagen2026-09-24 13:07 UTC402
        let timeline = match timeline {
31ce2453c82fatagen2026-09-24 13:07 UTC403
            Ok(timeline) => timeline,
31ce2453c82fatagen2026-09-24 13:07 UTC404
            Err(error) => {
fa5b4e09943aatagen2026-10-24 09:33 UTC405
                tracing::warn!(%error, %room_id, ?id, "timeline build failed");
fa5b4e09943aatagen2026-10-24 09:33 UTC406
                let error = Error::with_source(ErrorKind::Timeline, "timeline build failed", error);
31ce2453c82fatagen2026-09-24 13:07 UTC407
                for (_, reply) in queued {
31ce2453c82fatagen2026-09-24 13:07 UTC408
                    reply.done(Err(error.clone()));
31ce2453c82fatagen2026-09-24 13:07 UTC409
                }
31ce2453c82fatagen2026-09-24 13:07 UTC410
                return;
31ce2453c82fatagen2026-09-24 13:07 UTC411
            }
31ce2453c82fatagen2026-09-24 13:07 UTC412
        };
fa5b4e09943aatagen2026-10-24 09:33 UTC413
        let Ok(session) = TimelineSession::start(id.clone(), timeline, context.clone()).await
fa5b4e09943aatagen2026-10-24 09:33 UTC414
        else {
31ce2453c82fatagen2026-09-24 13:07 UTC415
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC416
        };
fa5b4e09943aatagen2026-10-24 09:33 UTC417
        let session = self.threads.open.entry(id).insert_entry(session).into_mut();
31ce2453c82fatagen2026-09-24 13:07 UTC418
        for (command, reply) in queued {
31ce2453c82fatagen2026-09-24 13:07 UTC419
            match command {
31ce2453c82fatagen2026-09-24 13:07 UTC420
                None => reply.done(Ok(())),
31ce2453c82fatagen2026-09-24 13:07 UTC421
                Some(TimelineCommand::Resync { .. }) => reply.done(session.resync().await),
31ce2453c82fatagen2026-09-24 13:07 UTC422
                Some(command) => session.execute(command, reply, &mut self.work).await,
31ce2453c82fatagen2026-09-24 13:07 UTC423
            }
31ce2453c82fatagen2026-09-24 13:07 UTC424
        }
31ce2453c82fatagen2026-09-24 13:07 UTC425
    }
31ce2453c82fatagen2026-09-24 13:07 UTC426
}
31ce2453c82fatagen2026-09-24 13:07 UTC427
31ce2453c82fatagen2026-09-24 13:07 UTC428
type Built = (
fa5b4e09943aatagen2026-10-24 09:33 UTC429
    TimelineId,
31ce2453c82fatagen2026-09-24 13:07 UTC430
    u64,
31ce2453c82fatagen2026-09-24 13:07 UTC431
    Result<Timeline, matrix_sdk_ui::timeline::Error>,
31ce2453c82fatagen2026-09-24 13:07 UTC432
);
31ce2453c82fatagen2026-09-24 13:07 UTC433
31ce2453c82fatagen2026-09-24 13:07 UTC434
/// `None` is the open itself, everything else waits behind it
31ce2453c82fatagen2026-09-24 13:07 UTC435
type Queued = VecDeque<(Option<TimelineCommand>, Responder)>;
31ce2453c82fatagen2026-09-24 13:07 UTC436
31ce2453c82fatagen2026-09-24 13:07 UTC437
enum Routed<'a> {
31ce2453c82fatagen2026-09-24 13:07 UTC438
    Open(&'a mut TimelineSession, TimelineCommand, Responder),
31ce2453c82fatagen2026-09-24 13:07 UTC439
    Queued,
31ce2453c82fatagen2026-09-24 13:07 UTC440
}
31ce2453c82fatagen2026-09-24 13:07 UTC441
31ce2453c82fatagen2026-09-24 13:07 UTC442
struct OpeningThread {
31ce2453c82fatagen2026-09-24 13:07 UTC443
    generation: u64,
31ce2453c82fatagen2026-09-24 13:07 UTC444
    build: AbortHandle,
31ce2453c82fatagen2026-09-24 13:07 UTC445
    queued: Queued,
31ce2453c82fatagen2026-09-24 13:07 UTC446
}
31ce2453c82fatagen2026-09-24 13:07 UTC447
fa5b4e09943aatagen2026-10-24 09:33 UTC448
/// threads and the pinned timeline, which build off the actor and queue what arrives meanwhile
31ce2453c82fatagen2026-09-24 13:07 UTC449
struct Threads {
fa5b4e09943aatagen2026-10-24 09:33 UTC450
    open: HashMap<TimelineId, TimelineSession>,
fa5b4e09943aatagen2026-10-24 09:33 UTC451
    opening: HashMap<TimelineId, OpeningThread>,
1fe480c16a3catagen2026-09-26 08:44 UTC452
    builds: TaskSet<Built>,
31ce2453c82fatagen2026-09-24 13:07 UTC453
    next_generation: u64,
31ce2453c82fatagen2026-09-24 13:07 UTC454
    queued: usize,
31ce2453c82fatagen2026-09-24 13:07 UTC455
    capacity: usize,
31ce2453c82fatagen2026-09-24 13:07 UTC456
}
31ce2453c82fatagen2026-09-24 13:07 UTC457
31ce2453c82fatagen2026-09-24 13:07 UTC458
impl Threads {
31ce2453c82fatagen2026-09-24 13:07 UTC459
    fn new(capacity: usize) -> Self {
31ce2453c82fatagen2026-09-24 13:07 UTC460
        Self {
31ce2453c82fatagen2026-09-24 13:07 UTC461
            open: HashMap::new(),
31ce2453c82fatagen2026-09-24 13:07 UTC462
            opening: HashMap::new(),
1fe480c16a3catagen2026-09-26 08:44 UTC463
            builds: TaskSet::new(),
31ce2453c82fatagen2026-09-24 13:07 UTC464
            next_generation: 0,
31ce2453c82fatagen2026-09-24 13:07 UTC465
            queued: 0,
31ce2453c82fatagen2026-09-24 13:07 UTC466
            capacity,
31ce2453c82fatagen2026-09-24 13:07 UTC467
        }
31ce2453c82fatagen2026-09-24 13:07 UTC468
    }
31ce2453c82fatagen2026-09-24 13:07 UTC469
fa5b4e09943aatagen2026-10-24 09:33 UTC470
    fn enqueue(&mut self, id: &TimelineId, command: Option<TimelineCommand>, reply: Responder) {
31ce2453c82fatagen2026-09-24 13:07 UTC471
        if self.queued >= self.capacity {
31ce2453c82fatagen2026-09-24 13:07 UTC472
            return reply.done(Err(Error::new(
31ce2453c82fatagen2026-09-24 13:07 UTC473
                ErrorKind::QueueFull,
31ce2453c82fatagen2026-09-24 13:07 UTC474
                "thread command queue is full",
31ce2453c82fatagen2026-09-24 13:07 UTC475
            )));
31ce2453c82fatagen2026-09-24 13:07 UTC476
        }
fa5b4e09943aatagen2026-10-24 09:33 UTC477
        let Some(opening) = self.opening.get_mut(id) else {
31ce2453c82fatagen2026-09-24 13:07 UTC478
            return reply.done(Err(Error::new(
31ce2453c82fatagen2026-09-24 13:07 UTC479
                ErrorKind::TimelineNotOpen,
fa5b4e09943aatagen2026-10-24 09:33 UTC480
                "timeline is not open",
31ce2453c82fatagen2026-09-24 13:07 UTC481
            )));
31ce2453c82fatagen2026-09-24 13:07 UTC482
        };
31ce2453c82fatagen2026-09-24 13:07 UTC483
        opening.queued.push_back((command, reply));
31ce2453c82fatagen2026-09-24 13:07 UTC484
        self.queued += 1;
31ce2453c82fatagen2026-09-24 13:07 UTC485
    }
31ce2453c82fatagen2026-09-24 13:07 UTC486
fa5b4e09943aatagen2026-10-24 09:33 UTC487
    fn open(&mut self, id: TimelineId, reply: Responder, room: &Room) {
fa5b4e09943aatagen2026-10-24 09:33 UTC488
        if self.open.contains_key(&id) {
31ce2453c82fatagen2026-09-24 13:07 UTC489
            return reply.done(Ok(()));
31ce2453c82fatagen2026-09-24 13:07 UTC490
        }
fa5b4e09943aatagen2026-10-24 09:33 UTC491
        if !self.opening.contains_key(&id) {
31ce2453c82fatagen2026-09-24 13:07 UTC492
            let generation = self.next_generation;
31ce2453c82fatagen2026-09-24 13:07 UTC493
            self.next_generation = self.next_generation.wrapping_add(1);
31ce2453c82fatagen2026-09-24 13:07 UTC494
            let room = room.clone();
fa5b4e09943aatagen2026-10-24 09:33 UTC495
            let build_id = id.clone();
31ce2453c82fatagen2026-09-24 13:07 UTC496
            let build = self.builds.spawn(async move {
fa5b4e09943aatagen2026-10-24 09:33 UTC497
                let focus = match &build_id {
fa5b4e09943aatagen2026-10-24 09:33 UTC498
                    TimelineId::Thread { root_event_id, .. } => TimelineFocus::Thread {
fa5b4e09943aatagen2026-10-24 09:33 UTC499
                        root_event_id: root_event_id.clone(),
fa5b4e09943aatagen2026-10-24 09:33 UTC500
                    },
fa5b4e09943aatagen2026-10-24 09:33 UTC501
                    _ => TimelineFocus::PinnedEvents,
31ce2453c82fatagen2026-09-24 13:07 UTC502
                };
fa5b4e09943aatagen2026-10-24 09:33 UTC503
                (build_id, generation, build_timeline(&room, focus).await)
31ce2453c82fatagen2026-09-24 13:07 UTC504
            });
31ce2453c82fatagen2026-09-24 13:07 UTC505
            self.opening.insert(
fa5b4e09943aatagen2026-10-24 09:33 UTC506
                id.clone(),
31ce2453c82fatagen2026-09-24 13:07 UTC507
                OpeningThread {
31ce2453c82fatagen2026-09-24 13:07 UTC508
                    generation,
31ce2453c82fatagen2026-09-24 13:07 UTC509
                    build,
31ce2453c82fatagen2026-09-24 13:07 UTC510
                    queued: VecDeque::new(),
31ce2453c82fatagen2026-09-24 13:07 UTC511
                },
31ce2453c82fatagen2026-09-24 13:07 UTC512
            );
31ce2453c82fatagen2026-09-24 13:07 UTC513
        }
fa5b4e09943aatagen2026-10-24 09:33 UTC514
        self.enqueue(&id, None, reply);
31ce2453c82fatagen2026-09-24 13:07 UTC515
    }
31ce2453c82fatagen2026-09-24 13:07 UTC516
fa5b4e09943aatagen2026-10-24 09:33 UTC517
    fn route(&mut self, id: TimelineId, command: TimelineCommand, reply: Responder) -> Routed<'_> {
fa5b4e09943aatagen2026-10-24 09:33 UTC518
        if !self.open.contains_key(&id) {
fa5b4e09943aatagen2026-10-24 09:33 UTC519
            self.enqueue(&id, Some(command), reply);
31ce2453c82fatagen2026-09-24 13:07 UTC520
            return Routed::Queued;
31ce2453c82fatagen2026-09-24 13:07 UTC521
        }
fa5b4e09943aatagen2026-10-24 09:33 UTC522
        let session = self.open.get_mut(&id).expect("timeline was just checked");
31ce2453c82fatagen2026-09-24 13:07 UTC523
        Routed::Open(session, command, reply)
31ce2453c82fatagen2026-09-24 13:07 UTC524
    }
31ce2453c82fatagen2026-09-24 13:07 UTC525
31ce2453c82fatagen2026-09-24 13:07 UTC526
    /// `None` when the build belongs to a thread that was closed while it ran
31ce2453c82fatagen2026-09-24 13:07 UTC527
    fn built(
31ce2453c82fatagen2026-09-24 13:07 UTC528
        &mut self,
1fe480c16a3catagen2026-09-26 08:44 UTC529
        built: Result<Built, JoinError>,
31ce2453c82fatagen2026-09-24 13:07 UTC530
    ) -> Option<(
fa5b4e09943aatagen2026-10-24 09:33 UTC531
        TimelineId,
31ce2453c82fatagen2026-09-24 13:07 UTC532
        Queued,
31ce2453c82fatagen2026-09-24 13:07 UTC533
        Result<Timeline, matrix_sdk_ui::timeline::Error>,
31ce2453c82fatagen2026-09-24 13:07 UTC534
    )> {
fa5b4e09943aatagen2026-10-24 09:33 UTC535
        let (id, generation, timeline) = match built {
31ce2453c82fatagen2026-09-24 13:07 UTC536
            Ok(built) => built,
31ce2453c82fatagen2026-09-24 13:07 UTC537
            Err(error) if error.is_cancelled() => return None,
1fe480c16a3catagen2026-09-26 08:44 UTC538
            Err(error) => resume_unwind(error),
31ce2453c82fatagen2026-09-24 13:07 UTC539
        };
31ce2453c82fatagen2026-09-24 13:07 UTC540
        if self
31ce2453c82fatagen2026-09-24 13:07 UTC541
            .opening
fa5b4e09943aatagen2026-10-24 09:33 UTC542
            .get(&id)
31ce2453c82fatagen2026-09-24 13:07 UTC543
            .is_none_or(|opening| opening.generation != generation)
31ce2453c82fatagen2026-09-24 13:07 UTC544
        {
31ce2453c82fatagen2026-09-24 13:07 UTC545
            return None;
31ce2453c82fatagen2026-09-24 13:07 UTC546
        }
fa5b4e09943aatagen2026-10-24 09:33 UTC547
        let opening = self.opening.remove(&id)?;
31ce2453c82fatagen2026-09-24 13:07 UTC548
        self.queued -= opening.queued.len();
fa5b4e09943aatagen2026-10-24 09:33 UTC549
        Some((id, opening.queued, timeline))
31ce2453c82fatagen2026-09-24 13:07 UTC550
    }
31ce2453c82fatagen2026-09-24 13:07 UTC551
fa5b4e09943aatagen2026-10-24 09:33 UTC552
    async fn close_one(&mut self, id: &TimelineId) -> Result<(), Error> {
fa5b4e09943aatagen2026-10-24 09:33 UTC553
        if let Some(session) = self.open.remove(id) {
31ce2453c82fatagen2026-09-24 13:07 UTC554
            session.close().await;
31ce2453c82fatagen2026-09-24 13:07 UTC555
            return Ok(());
31ce2453c82fatagen2026-09-24 13:07 UTC556
        }
fa5b4e09943aatagen2026-10-24 09:33 UTC557
        let Some(opening) = self.opening.remove(id) else {
31ce2453c82fatagen2026-09-24 13:07 UTC558
            return Err(Error::new(
31ce2453c82fatagen2026-09-24 13:07 UTC559
                ErrorKind::TimelineNotOpen,
fa5b4e09943aatagen2026-10-24 09:33 UTC560
                "timeline is not open",
31ce2453c82fatagen2026-09-24 13:07 UTC561
            ));
31ce2453c82fatagen2026-09-24 13:07 UTC562
        };
31ce2453c82fatagen2026-09-24 13:07 UTC563
        opening.build.abort();
31ce2453c82fatagen2026-09-24 13:07 UTC564
        self.queued -= opening.queued.len();
31ce2453c82fatagen2026-09-24 13:07 UTC565
        Ok(())
31ce2453c82fatagen2026-09-24 13:07 UTC566
    }
31ce2453c82fatagen2026-09-24 13:07 UTC567
31ce2453c82fatagen2026-09-24 13:07 UTC568
    async fn close(&mut self) {
1fe480c16a3catagen2026-09-26 08:44 UTC569
        self.builds.shutdown().await;
31ce2453c82fatagen2026-09-24 13:07 UTC570
        self.opening.clear();
31ce2453c82fatagen2026-09-24 13:07 UTC571
        for (_, session) in self.open.drain() {
31ce2453c82fatagen2026-09-24 13:07 UTC572
            session.close().await;
31ce2453c82fatagen2026-09-24 13:07 UTC573
        }
31ce2453c82fatagen2026-09-24 13:07 UTC574
    }
31ce2453c82fatagen2026-09-24 13:07 UTC575
}