31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | mod commands; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 2 | pub(crate) mod executor; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 3 | mod media; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 4 | mod mutations; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | mod room; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 6 | mod session; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | mod timeline; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | mod watchers; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 9 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 10 | use std::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | collections::{HashMap, VecDeque}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | future::Future, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 13 | panic::AssertUnwindSafe, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | sync::Arc, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 15 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 17 | use futures_util::FutureExt; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 18 | use matrix_sdk::{Client, SendOutsideWasm, SessionChange}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 19 | use matrix_sdk_ui::sync_service::SyncService; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 20 | use ruma::{OwnedDeviceId, OwnedRoomId}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | use thiserror::Error as ThisError; |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 22 | use tokio::sync::{broadcast, mpsc, watch}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 24 | pub(crate) use media::MediaService; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 25 | pub(crate) use session::Persistence; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | use self::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | commands::Job, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 29 | executor::{EngineBoxFuture, TaskSet}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | mutations::{Mutation, MutationJob}, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 31 | room::{ActorNote, ActorRequest, RoomActor}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | timeline::{SessionContext, Supervisor}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | use crate::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | AccountCommand, ClientHandle, Command, ConnectionState, EncryptionCommand, EngineErrorKind, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | EngineFailure, Error, ErrorKind, Event, EventStream, LifecycleCommand, MediaCommand, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 37 | NotificationCommand, RoomCommand, Termination, TimelineCommand, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | transport::{DriverPort, Request, Responder, transport_pair}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | #[derive(Clone, Debug, Eq, PartialEq)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | #[non_exhaustive] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | pub struct EngineConfig { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | pub command_capacity: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | pub event_capacity: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | pub initial_history_limit: u16, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | pub max_media_bytes: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | pub max_media_sources: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | impl EngineConfig { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | pub const fn with_command_capacity(mut self, capacity: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | self.command_capacity = capacity; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | self |
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 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | pub const fn with_event_capacity(mut self, capacity: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | self.event_capacity = capacity; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | pub const fn with_initial_history_limit(mut self, limit: u16) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | self.initial_history_limit = limit; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | pub const fn with_max_media_bytes(mut self, bytes: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | self.max_media_bytes = bytes; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | self |
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 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | pub const fn with_max_media_sources(mut self, sources: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | self.max_media_sources = sources; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | impl Default for EngineConfig { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | fn default() -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | command_capacity: 64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | event_capacity: 128, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | initial_history_limit: 30, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | max_media_bytes: 20 * 1024 * 1024, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | max_media_sources: 2_048, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | #[derive(Debug, ThisError)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | pub enum EngineError { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | #[error("the Matrix client is not authenticated")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | Unauthenticated, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | #[error("command, event, and media source capacities must be nonzero")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | ZeroCapacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | #[error("could not build the sync service")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | SyncService(#[source] matrix_sdk_ui::sync_service::Error), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | #[error("the event receiver has closed")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | EventReceiverClosed, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | #[error("engine task {task} panicked")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | TaskPanicked { task: String }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | impl EngineError { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | fn failure(&self) -> EngineFailure { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | EngineFailure { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | kind: match self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | Self::EventReceiverClosed => EngineErrorKind::EventReceiverClosed, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | Self::TaskPanicked { .. } => EngineErrorKind::Watcher, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | Self::Unauthenticated | Self::ZeroCapacity | Self::SyncService(_) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | EngineErrorKind::Other |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | message: self.to_string(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | } |
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 | #[derive(Clone)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | enum TaskContext { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | /// a panic here costs the caller an `Interrupted` answer and nothing else |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | Command(&'static str), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | /// a panic here takes the engine with it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | Watcher(&'static str), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | room_id: OwnedRoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | generation: u64, |
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 | impl TaskContext { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | fn label(&self) -> String { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | match self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | Self::Command(name) | Self::Watcher(name) => (*name).to_owned(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | Self::RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | } => format!("room actor {room_id} generation {generation}"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 144 | } |
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 | |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 148 | /// tasks catch their own panics, since the sdk's wasm join error drops the payload |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 149 | fn panic_message(payload: Box<dyn std::any::Any + Send>) -> String { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 150 | payload |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 151 | .downcast_ref::<&str>() |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 152 | .map(ToString::to_string) |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 153 | .or_else(|| payload.downcast_ref::<String>().cloned()) |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 154 | .unwrap_or_else(|| "task panicked with a non-string payload".to_owned()) |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 155 | } |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 156 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | struct TaskRegistry { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 158 | tasks: TaskSet<(TaskContext, Option<String>)>, |
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 | impl TaskRegistry { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 162 | fn spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 163 | &mut self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 164 | context: TaskContext, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 165 | task: impl Future<Output = ()> + SendOutsideWasm + 'static, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 166 | ) { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 167 | self.tasks.spawn(async move { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 168 | let panic = AssertUnwindSafe(task) |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 169 | .catch_unwind() |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 170 | .await |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 171 | .err() |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 172 | .map(panic_message); |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 173 | (context, panic) |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 174 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 176 | |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 177 | fn is_empty(&self) -> bool { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 178 | self.tasks.is_empty() |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 179 | } |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 180 | |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 181 | async fn join_next(&mut self) -> Option<(TaskContext, Option<String>)> { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 182 | loop { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 183 | if let Ok(done) = self.tasks.join_next().await? { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 184 | return Some(done); |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 185 | } |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 186 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 187 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 188 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 189 | async fn shutdown(&mut self) { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 190 | self.tasks.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 191 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 192 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 193 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | enum RoomSlot { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | Active { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 196 | generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 197 | sender: mpsc::Sender<ActorRequest>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 198 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 199 | /// opens and closes that arrive while the actor winds down, replayed in order once it's gone |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 200 | Closing { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 201 | generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 202 | pending: VecDeque<Deferred>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 203 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 204 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 205 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 206 | impl RoomSlot { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 207 | const fn generation(&self) -> u64 { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 208 | match self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 209 | Self::Active { generation, .. } | Self::Closing { generation, .. } => *generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 210 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 211 | } |
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 | enum Deferred { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | Open(Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 216 | Close(Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 217 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 218 | |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 219 | /// logouts in flight, and a token rejection held back because one of them caused it |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 220 | #[derive(Default)] |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 221 | pub(super) struct Logouts { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 222 | pub(super) in_flight: usize, |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 223 | pub(super) rejected: Option<Option<OwnedDeviceId>>, |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 224 | } |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 225 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | struct Queues { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | mutations: mpsc::Receiver<MutationJob>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | notifications: mpsc::Receiver<Job<NotificationCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | encryption: mpsc::Receiver<Job<EncryptionCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 230 | session_changes: broadcast::Receiver<SessionChange>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 231 | persistence: Option<Persistence>, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 232 | upload_reports: mpsc::Receiver<watchers::RoomUpload>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 235 | /// requests other devices send us, handed from the sdk's event handler to the encryption worker |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 236 | type IncomingVerifications = |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 237 | mpsc::UnboundedSender<matrix_sdk::encryption::verification::VerificationRequest>; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 238 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | pub struct Engine { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | sync: Arc<SyncService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | port: DriverPort, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | config: EngineConfig, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | media: Arc<MediaService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 245 | tasks: TaskRegistry, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 246 | rooms: HashMap<OwnedRoomId, RoomSlot>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | next_generation: u64, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 248 | notes: mpsc::UnboundedReceiver<ActorNote>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 249 | note_sender: mpsc::UnboundedSender<ActorNote>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 250 | /// the rooms sliding sync should follow, applied off the run loop since it touches the store |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 251 | subscriptions: watch::Sender<Vec<OwnedRoomId>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | mutations: mpsc::Sender<MutationJob>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 253 | notifications: mpsc::Sender<Job<NotificationCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 254 | encryption: mpsc::Sender<Job<EncryptionCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | queues: Option<Queues>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | /// one progress forwarder per room that has uploaded, and whether it's subscribed yet |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 257 | uploads: HashMap<OwnedRoomId, watch::Receiver<bool>>, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 258 | upload_reports: mpsc::Sender<watchers::RoomUpload>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 259 | /// the client can outlive us, so the handler has to come off it when we stop |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 260 | verification_handler: Option<matrix_sdk::event_handler::EventHandlerHandle>, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 261 | stop: mpsc::UnboundedSender<Termination>, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 262 | stops: mpsc::UnboundedReceiver<Termination>, |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 263 | logouts: Arc<watch::Sender<Logouts>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | impl Engine { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 267 | /// the escape hatch for a client you built yourself. `Session` is the usual way in |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 268 | pub async fn from_client( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 269 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 270 | config: EngineConfig, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 271 | ) -> Result<(Self, ClientHandle, EventStream), EngineError> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 272 | let session_changes = client.subscribe_to_session_changes(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 273 | Self::with_persistence(client, config, session_changes, None).await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 274 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 275 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 276 | /// `session_changes` is taken when the client is built, since the sdk can refresh |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 277 | /// before an engine exists |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 278 | pub(crate) async fn with_persistence( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 279 | client: Client, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 280 | config: EngineConfig, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 281 | session_changes: broadcast::Receiver<SessionChange>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 282 | persistence: Option<Persistence>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 283 | ) -> Result<(Self, ClientHandle, EventStream), EngineError> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 284 | if client.user_id().is_none() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 285 | return Err(EngineError::Unauthenticated); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 286 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 287 | if config.command_capacity == 0 |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 288 | || config.event_capacity == 0 |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 289 | || config.max_media_sources == 0 |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 290 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 291 | return Err(EngineError::ZeroCapacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 292 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 293 | let sync = Arc::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 294 | SyncService::builder(client.clone()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 295 | .with_offline_mode() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 296 | .build() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 297 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 298 | .map_err(EngineError::SyncService)?, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 299 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 300 | let media = Arc::new(MediaService::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 301 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 302 | config.max_media_bytes, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 303 | config.max_media_sources, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 304 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | let (handle, port, events) = transport_pair( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 306 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 307 | sync.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 308 | media.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | config.command_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | config.event_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | client.send_queue().enable_upload_progress(true); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 313 | let (note_sender, notes) = mpsc::unbounded_channel(); |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 314 | let (stop, stops) = mpsc::unbounded_channel(); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 315 | let (upload_reports, upload_report_queue) = mpsc::channel(config.event_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | let (mutations, mutation_queue) = mpsc::channel(config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | let (notifications, notification_queue) = mpsc::channel(config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | let (encryption, encryption_queue) = mpsc::channel(config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | let engine = Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | sync, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | port, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 323 | config, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | media, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | tasks: TaskRegistry { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 326 | tasks: TaskSet::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | rooms: HashMap::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | next_generation: 0, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 330 | notes, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 331 | note_sender, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 332 | subscriptions: watch::Sender::new(Vec::new()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 333 | mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 334 | notifications, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 335 | encryption, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | queues: Some(Queues { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | mutations: mutation_queue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 338 | notifications: notification_queue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 339 | encryption: encryption_queue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 340 | session_changes, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 341 | persistence, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 342 | upload_reports: upload_report_queue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | }), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 344 | upload_reports, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 345 | uploads: HashMap::new(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 346 | verification_handler: None, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 347 | stop, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 348 | stops, |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 349 | logouts: Arc::new(watch::Sender::new(Logouts::default())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 351 | Ok((engine, handle, events)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 352 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 353 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 354 | /// boxed because the engine's future is deep enough to blow the compiler's query depth |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 355 | /// in every crate that spawns it, which isn't something a client should have to fix |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 356 | pub fn run(self) -> EngineBoxFuture<'static, Result<Termination, EngineError>> { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 357 | Box::pin(self.run_inner()) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 358 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 359 | |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 360 | async fn run_inner(mut self) -> Result<Termination, EngineError> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | let events = self.port.event_sender(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 362 | let user_id = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | .client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 364 | .user_id() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | .expect("authentication was checked during construction") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | .to_owned(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | self.port |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | .connection_sender() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | .send_replace(ConnectionState::Connecting); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | for event in [ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | Event::Authenticated { user_id }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | Event::ConnectionChanged(ConnectionState::Connecting), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | ] { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | if events.send(event).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | self.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | return Err(EngineError::EventReceiverClosed); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 377 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 378 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 379 | self.start_tasks(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 380 | self.resume_upload_forwarders().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 381 | self.sync.start().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 382 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 383 | let mut shutdown_reply = None; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 384 | let mut handles_dropped = false; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 385 | let mut logouts = self.logouts.subscribe(); |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 386 | let ending = loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 387 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 388 | biased; |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 389 | Some(termination) = self.stops.recv() => break Ok(termination), |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 390 | () = events.closed() => break Err(EngineError::EventReceiverClosed), |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 391 | () = settled(&mut logouts), if handles_dropped => { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 392 | break Ok(Termination::HandlesDropped); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 393 | } |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 394 | Some((context, panic)) = self.tasks.join_next(), if !self.tasks.is_empty() => { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 395 | let Some(message) = panic else { continue }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 396 | if let TaskContext::Command(label) = context { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 397 | tracing::error!(label, panic = %message, "command task panicked"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | continue; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | } |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 400 | break Err(EngineError::TaskPanicked { task: context.label() }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 402 | Some(note) = self.notes.recv() => self.actor_note(note).await, |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 403 | request = self.port.recv(), if !handles_dropped => { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 404 | // nobody's left to ask, but a logout they asked for still deserves to land |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 405 | let Some(request) = request else { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 406 | handles_dropped = true; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 407 | continue; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 408 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 409 | if let Some(reply) = self.dispatch(request).await { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 410 | shutdown_reply = Some(reply); |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 411 | break Ok(Termination::ShutDown); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 412 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 413 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | } |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 415 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 416 | |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 417 | if let Err(error) = &ending { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 418 | let _ = events.send(Event::Failed(error.failure())).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 419 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 420 | self.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 421 | let _ = events.send(Event::Stopped).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 422 | if let Some(reply) = shutdown_reply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 423 | reply.done(Ok(())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 424 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 425 | // everything else still queued answers `Interrupted` as it drops, but a shutdown did happen |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 426 | while let Some(Request { command, reply }) = self.port.try_recv() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 427 | if matches!(command, Command::Lifecycle(LifecycleCommand::Shutdown)) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 428 | reply.done(Ok(())); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 429 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 430 | } |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 431 | ending |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 432 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 433 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 434 | fn start_tasks(&mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 435 | let queues = self.queues.take().expect("engine tasks start once"); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 436 | let (incoming, incoming_verifications) = mpsc::unbounded_channel(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 437 | self.verification_handler = Some(self.verification_handler(incoming)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 438 | let client = &self.client; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 439 | let events = || self.port.event_sender(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 440 | let (notices, notice_queue) = mpsc::unbounded_channel(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 441 | let watchers: [(&'static str, EngineBoxFuture<'static, ()>); 12] = [ |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 442 | ( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 443 | "upload tracker", |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 444 | Box::pin(watchers::uploads( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 445 | client.clone(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 446 | client.send_queue().subscribe(), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 447 | queues.upload_reports, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 448 | events(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 449 | )), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 450 | ), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 451 | ( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 452 | "room subscription watcher", |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 453 | Box::pin(watchers::room_subscriptions( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 454 | self.sync.clone(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 455 | self.subscriptions.subscribe(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 456 | )), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 457 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 458 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 459 | "session change watcher", |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 460 | Box::pin(session::session( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 461 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 462 | queues.session_changes, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 463 | queues.persistence, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 464 | notices, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 465 | client |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 466 | .device_id() |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 467 | .expect("authentication was checked during construction") |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 468 | .to_owned(), |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 469 | self.logouts.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 470 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 471 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 472 | ( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 473 | "session notice watcher", |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 474 | Box::pin(session::session_notices( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 475 | notice_queue, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 476 | events(), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 477 | self.stop.clone(), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 478 | )), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 479 | ), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 480 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 481 | "connection state watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 482 | Box::pin(watchers::connection( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 483 | self.sync.state(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 484 | self.sync.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 485 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 486 | events(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 487 | self.port.connection_sender(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 488 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 489 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 490 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 491 | "send queue watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 492 | Box::pin(watchers::send_queue_errors( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 493 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 494 | client.send_queue().subscribe_errors(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 495 | self.port.connection_sender().subscribe(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 496 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 497 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 498 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 499 | "encryption state watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 500 | Box::pin(watchers::encryption(client.clone(), events())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 501 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 502 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 503 | "room list watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 504 | Box::pin(watchers::rooms(client.clone(), events())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 505 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 506 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 507 | "notification settings watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 508 | Box::pin(watchers::notification_settings(client.clone(), events())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 509 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 510 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 511 | "room command worker", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 512 | Box::pin(mutations::run_scheduler( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 513 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 514 | queues.mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 515 | self.config.command_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 516 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 517 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 518 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 519 | "notification command worker", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 520 | Box::pin(commands::run_notifications( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 521 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 522 | queues.notifications, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 523 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 524 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 525 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 526 | "encryption command worker", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 527 | Box::pin(commands::run_encryption( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 528 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 529 | events(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 530 | queues.encryption, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 531 | incoming_verifications, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 532 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 533 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 534 | ]; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 535 | for (label, task) in watchers { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 536 | self.tasks.spawn(TaskContext::Watcher(label), task); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 537 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 538 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 539 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 540 | fn verification_handler( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 541 | &self, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 542 | incoming: IncomingVerifications, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 543 | ) -> matrix_sdk::event_handler::EventHandlerHandle { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 544 | use matrix_sdk::ruma::events::key::verification::request::ToDeviceKeyVerificationRequestEvent; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 545 | self.client.add_event_handler( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 546 | move |event: ToDeviceKeyVerificationRequestEvent, client: Client| { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 547 | let incoming = incoming.clone(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 548 | async move { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 549 | let request = client |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 550 | .encryption() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 551 | .get_verification_request( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 552 | &event.sender, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 553 | event.content.transaction_id.as_str(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 554 | ) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 555 | .await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 556 | if let Some(request) = request { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 557 | let _ = incoming.send(request); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 558 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 559 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 560 | }, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 561 | ) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 562 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 563 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 564 | /// hands back the shutdown request's responder, which is answered once we've stopped |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 565 | async fn dispatch(&mut self, Request { command, reply }: Request) -> Option<Responder> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 566 | let client = self.client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 567 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 568 | Command::Lifecycle(LifecycleCommand::Shutdown) => return Some(reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 569 | Command::Account(AccountCommand::Logout) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 570 | let events = self.port.event_sender(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 571 | let stop = self.stop.clone(); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 572 | let logouts = self.logouts.clone(); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 573 | logouts.send_modify(|logouts| logouts.in_flight += 1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 574 | self.spawn_command("logout", reply, async move { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 575 | let result = client.logout().await; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 576 | if result.is_ok() { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 577 | let _ = events.send(Event::LoggedOut).await; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 578 | let _ = stop.send(Termination::LoggedOut); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 579 | } |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 580 | let mut rejected = None; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 581 | logouts.send_if_modified(|logouts| { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 582 | if result.is_err() && logouts.in_flight == 1 { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 583 | rejected = logouts.rejected.take(); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 584 | } |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 585 | false |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 586 | }); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 587 | if let Some(soft_logout) = rejected { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 588 | session::reject_session(&events, &stop, soft_logout).await; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 589 | } |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 590 | logouts.send_modify(|logouts| logouts.in_flight -= 1); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 591 | result.map_err(|source| Error::from_sdk("logout failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 592 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 593 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 594 | Command::Account(AccountCommand::SetAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 595 | event_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 596 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 597 | }) => enqueue( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 598 | &self.mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 599 | Job { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 600 | command: Mutation::GlobalAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 601 | event_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 602 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 603 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 604 | reply, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 605 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 606 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 607 | Command::Account(AccountCommand::SetDisplayName(name)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 608 | self.spawn_command("set display name", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 609 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 610 | .account() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 611 | .set_display_name(name.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 612 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 613 | .map_err(|source| Error::from_sdk("setting display name failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 614 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 615 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 616 | Command::Account(AccountCommand::SetAvatar(avatar)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 617 | self.spawn_command("set avatar", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 618 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 619 | .account() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 620 | .set_avatar_url(avatar.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 621 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 622 | .map_err(|source| Error::from_sdk("setting avatar failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 623 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 624 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 625 | Command::Account(AccountCommand::UploadAvatar { mimetype, bytes }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 626 | let max_bytes = self.config.max_media_bytes; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 627 | self.spawn_command("upload avatar", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 628 | commands::upload_avatar(&client, &mimetype, bytes, max_bytes).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 629 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 630 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 631 | Command::Account(AccountCommand::RenameDevice { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 632 | device_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 633 | display_name, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 634 | }) => self.spawn_command("rename device", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 635 | commands::rename_device(&client, &device_id, &display_name).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 636 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 637 | Command::Account(AccountCommand::DeleteDevices { device_ids, auth }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 638 | self.spawn_command("delete devices", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 639 | commands::delete_devices(&client, &device_ids, auth).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 640 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 641 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 642 | Command::Room(RoomCommand::Open(room_id)) => self.open_room(room_id, reply).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 643 | Command::Room(RoomCommand::Close(room_id)) => self.close_room(room_id, reply).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 644 | Command::Room(RoomCommand::SetTyping { room_id, typing }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 645 | self.spawn_command("typing", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 646 | commands::set_typing(&client, &room_id, typing).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 647 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 648 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 649 | Command::Room(RoomCommand::CancelSend { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 650 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 651 | transaction_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 652 | }) => self.spawn_command("cancel send", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 653 | commands::cancel_send(&client, &room_id, &transaction_id).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 654 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 655 | Command::Room(RoomCommand::RetrySend { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 656 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 657 | transaction_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 658 | }) => self.spawn_command("retry send", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 659 | commands::retry_send(&client, &room_id, &transaction_id).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 660 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 661 | Command::Room(command) => enqueue( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 662 | &self.mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 663 | Job { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 664 | command: Mutation::Room(command), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 665 | reply, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 666 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 667 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 668 | Command::Timeline(command) => self.route_timeline(command, reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 669 | Command::Media(MediaCommand::Upload(upload)) => self.upload(upload, reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 670 | Command::Encryption(command) => enqueue(&self.encryption, Job { command, reply }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 671 | Command::Notification(command) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 672 | enqueue(&self.notifications, Job { command, reply }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 673 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 674 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 675 | None |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 676 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 677 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 678 | fn spawn_command( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 679 | &mut self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 680 | label: &'static str, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 681 | reply: Responder, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 682 | work: impl Future<Output = Result<(), Error>> + SendOutsideWasm + 'static, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 683 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 684 | self.tasks.spawn(TaskContext::Command(label), async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 685 | reply.done(work.await); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 686 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 687 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 688 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 689 | fn upload(&mut self, upload: crate::AttachmentUpload, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 690 | let Some(room) = self.client.get_room(upload.timeline_id.room_id()) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 691 | return reply.done(Err(Error::room_unknown())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 692 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 693 | let mut ready = self.upload_forwarder(&room); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 694 | let max_bytes = self.config.max_media_bytes; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 695 | // queueing before the forwarder listens would lose the first progress ticks |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 696 | self.spawn_command("upload", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 697 | ready |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 698 | .wait_for(|subscribed| *subscribed) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 699 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 700 | .map_err(|_| Error::new(ErrorKind::Store, "reading the media send queue failed"))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 701 | media::queue_upload(room, upload, max_bytes).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 702 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 703 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 704 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 705 | fn upload_forwarder(&mut self, room: &matrix_sdk::Room) -> watch::Receiver<bool> { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 706 | self.uploads.retain(|_, ready| ready.has_changed().is_ok()); |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 707 | if let Some(ready) = self.uploads.get(room.room_id()) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 708 | return ready.clone(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 709 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 710 | let (subscribed, ready) = watch::channel(false); |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 711 | self.tasks.spawn( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 712 | TaskContext::Command("upload watcher"), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 713 | watchers::room_uploads(room.clone(), self.upload_reports.clone(), subscribed), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 714 | ); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 715 | self.uploads |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 716 | .insert(room.room_id().to_owned(), ready.clone()); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 717 | ready |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 718 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 719 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 720 | /// the sdk resumes uploads left over from last time, and they deserve progress too |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 721 | async fn resume_upload_forwarders(&mut self) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 722 | let Ok(echoes) = self.client.send_queue().local_echoes().await else { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 723 | return; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 724 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 725 | for room_id in echoes.keys() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 726 | if let Some(room) = self.client.get_room(room_id) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 727 | self.upload_forwarder(&room); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 728 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 729 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 730 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 731 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 732 | fn route_timeline(&mut self, command: TimelineCommand, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 733 | let sender = match self.rooms.get(command.room_id()) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 734 | Some(RoomSlot::Active { sender, .. }) => Some(sender.clone()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 735 | _ => None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 736 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 737 | match (sender, command) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 738 | (Some(sender), command) => send_actor(&sender, ActorRequest::Execute(command, reply)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 739 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 740 | None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 741 | TimelineCommand::MarkRead { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 742 | timeline_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 743 | event_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 744 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 745 | ) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 746 | let client = self.client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 747 | self.spawn_command("mark read", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 748 | commands::mark_read(&client, &timeline_id, event_id).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 749 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 750 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 751 | (None, _) => reply.done(Err(Error::new(ErrorKind::RoomNotOpen, "room is not open"))), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 752 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 753 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 754 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 755 | async fn open_room(&mut self, room_id: OwnedRoomId, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 756 | match self.rooms.get_mut(&room_id) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 757 | Some(RoomSlot::Active { sender, .. }) => send_actor(sender, ActorRequest::Open(reply)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 758 | Some(RoomSlot::Closing { pending, .. }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 759 | defer(pending, Deferred::Open(reply), self.config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 760 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 761 | None => self.start_actor(room_id, reply).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 762 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 763 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 764 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 765 | async fn close_room(&mut self, room_id: OwnedRoomId, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 766 | let (generation, sender) = match self.rooms.get_mut(&room_id) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 767 | None => return reply.done(Ok(())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 768 | Some(RoomSlot::Closing { pending, .. }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 769 | return defer( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 770 | pending, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 771 | Deferred::Close(reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 772 | self.config.command_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 773 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 774 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 775 | Some(RoomSlot::Active { generation, sender }) => (*generation, sender.clone()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 776 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 777 | match sender.try_send(ActorRequest::Close(reply)) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 778 | Ok(()) => {} |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 779 | // the actor already left on its own, which is closed enough |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 780 | Err(mpsc::error::TrySendError::Closed(request)) => request.into_reply().done(Ok(())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 781 | Err(mpsc::error::TrySendError::Full(request)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 782 | return request.into_reply().done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 783 | ErrorKind::RoomBusy, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 784 | "room command queue is full", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 785 | ))); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 786 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 787 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 788 | self.rooms.insert( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 789 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 790 | RoomSlot::Closing { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 791 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 792 | pending: VecDeque::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 793 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 794 | ); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 795 | self.update_room_subscriptions(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 796 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 797 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 798 | async fn start_actor(&mut self, room_id: OwnedRoomId, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 799 | let Some(room) = self.client.get_room(&room_id) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 800 | return reply.done(Err(Error::room_unknown())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 801 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 802 | self.next_generation = self.next_generation.wrapping_add(1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 803 | let generation = self.next_generation; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 804 | let (sender, requests) = mpsc::channel(self.config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 805 | let (supervisor, panics) = Supervisor::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 806 | let actor = RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 807 | room_id: room_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 808 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 809 | room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 810 | requests, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 811 | notes: self.note_sender.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 812 | context: SessionContext { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 813 | events: self.port.event_sender(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 814 | media: self.media.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 815 | current_user_id: self.client.user_id().map(ToOwned::to_owned), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 816 | initial_history_limit: self.config.initial_history_limit, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 817 | supervisor, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 818 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 819 | panics, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 820 | opening: vec![reply], |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 821 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 822 | self.rooms |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 823 | .insert(room_id.clone(), RoomSlot::Active { generation, sender }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 824 | self.tasks.spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 825 | TaskContext::RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 826 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 827 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 828 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 829 | actor.run(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 830 | ); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 831 | self.update_room_subscriptions(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 832 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 833 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 834 | async fn actor_note(&mut self, note: ActorNote) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 835 | let (room_id, generation) = match ¬e { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 836 | ActorNote::Leaving { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 837 | room_id, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 838 | generation, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 839 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 840 | | ActorNote::Exited { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 841 | room_id, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 842 | generation, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 843 | .. |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 844 | } => (room_id.clone(), *generation), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 845 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 846 | if self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 847 | .rooms |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 848 | .get(&room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 849 | .is_none_or(|slot| slot.generation() != generation) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 850 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 851 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 852 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 853 | let ActorNote::Exited { leftovers, .. } = note else { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 854 | if let Some(slot @ RoomSlot::Active { .. }) = self.rooms.get_mut(&room_id) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 855 | *slot = RoomSlot::Closing { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 856 | generation, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 857 | pending: VecDeque::new(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 858 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 859 | self.update_room_subscriptions(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 860 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 861 | return; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 862 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 863 | let pending = match self.rooms.remove(&room_id) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 864 | Some(RoomSlot::Closing { pending, .. }) => pending, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 865 | _ => VecDeque::new(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 866 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 867 | // leftovers were sent before anything deferred, so they go first |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 868 | for request in leftovers { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 869 | match request { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 870 | ActorRequest::Open(reply) => self.open_room(room_id.clone(), reply).await, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 871 | ActorRequest::Close(reply) => reply.done(Ok(())), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 872 | ActorRequest::Execute(command, reply) => self.route_timeline(command, reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 873 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 874 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 875 | for deferred in pending { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 876 | match deferred { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 877 | Deferred::Open(reply) => self.open_room(room_id.clone(), reply).await, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 878 | Deferred::Close(reply) => self.close_room(room_id.clone(), reply).await, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 879 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 880 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 881 | self.update_room_subscriptions(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 882 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 883 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 884 | fn update_room_subscriptions(&self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 885 | let room_ids = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 886 | .rooms |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 887 | .iter() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 888 | .filter(|(_, slot)| matches!(slot, RoomSlot::Active { .. })) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 889 | .map(|(room_id, _)| room_id.clone()) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 890 | .collect(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 891 | self.subscriptions.send_replace(room_ids); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 892 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 893 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 894 | async fn shutdown(&mut self) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 895 | if let Some(handler) = self.verification_handler.take() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 896 | self.client.remove_event_handler(handler); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 897 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 898 | self.port.close(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 899 | self.tasks.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 900 | self.rooms.clear(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 901 | self.uploads.clear(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 902 | self.media.clear().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 903 | self.sync.stop().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 904 | self.port |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 905 | .connection_sender() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 906 | .send_replace(ConnectionState::Stopped); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 907 | let _ = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 908 | .port |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 909 | .event_sender() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 910 | .send(Event::ConnectionChanged(ConnectionState::Stopped)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 911 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 912 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 913 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 914 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 915 | fn send_actor(sender: &mpsc::Sender<ActorRequest>, request: ActorRequest) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 916 | if let Err(error) = sender.try_send(request) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 917 | let (request, error) = match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 918 | mpsc::error::TrySendError::Full(request) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 919 | request, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 920 | Error::new(ErrorKind::RoomBusy, "room command queue is full"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 921 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 922 | mpsc::error::TrySendError::Closed(request) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 923 | request, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 924 | Error::new(ErrorKind::RoomNotOpen, "room is not open"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 925 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 926 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 927 | request.into_reply().done(Err(error)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 928 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 929 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 930 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 931 | fn defer(pending: &mut VecDeque<Deferred>, deferred: Deferred, capacity: usize) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 932 | if pending.len() < capacity { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 933 | return pending.push_back(deferred); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 934 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 935 | let (Deferred::Open(reply) | Deferred::Close(reply)) = deferred; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 936 | reply.done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 937 | ErrorKind::RoomBusy, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 938 | "room lifecycle queue is full", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 939 | ))); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 940 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 941 | |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 942 | async fn settled(logouts: &mut watch::Receiver<Logouts>) { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 943 | let _ = logouts.wait_for(|logouts| logouts.in_flight == 0).await; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 944 | } |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 945 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 946 | fn enqueue<T>(sender: &mpsc::Sender<Job<T>>, job: Job<T>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 947 | if let Err(error) = sender.try_send(job) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 948 | let (job, error) = match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 949 | mpsc::error::TrySendError::Full(job) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 950 | job, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 951 | Error::new(ErrorKind::QueueFull, "command queue is full"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 952 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 953 | mpsc::error::TrySendError::Closed(job) => (job, Error::interrupted()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 954 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 955 | job.reply.done(Err(error)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 956 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 957 | } |