31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | mod commands; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | mod media; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 3 | mod mutations; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 4 | mod room; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | mod timeline; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | mod watchers; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | use std::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 9 | collections::{HashMap, VecDeque}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 10 | future::Future, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | sync::Arc, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 13 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | use futures_util::future::BoxFuture; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 15 | use matrix_sdk::{Client, SessionChange}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | use matrix_sdk_ui::sync_service::SyncService; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 17 | use ruma::{OwnedRoomId, RoomId}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | use thiserror::Error as ThisError; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 19 | use tokio::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | sync::{Notify, broadcast, mpsc, watch}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | task::{AbortHandle, Id as TaskId, JoinError, JoinSet}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 24 | pub(crate) use media::MediaService; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 25 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | use self::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | commands::Job, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | mutations::{Mutation, MutationJob}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | room::{ActorRequest, Exited, RoomActor}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | timeline::{SessionContext, Supervisor}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | use crate::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | AccountCommand, ClientHandle, Command, ConnectionState, EncryptionCommand, EngineErrorKind, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | EngineFailure, Error, ErrorKind, Event, EventStream, LifecycleCommand, MediaCommand, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | NotificationCommand, RoomCommand, TimelineCommand, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | transport::{DriverPort, Request, Responder, transport_pair}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | #[derive(Clone, Debug, Eq, PartialEq)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | #[non_exhaustive] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | pub struct EngineConfig { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | pub command_capacity: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | pub event_capacity: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | pub initial_history_limit: u16, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | pub max_media_bytes: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | pub max_media_sources: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | impl EngineConfig { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | pub const fn with_command_capacity(mut self, capacity: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | self.command_capacity = capacity; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | pub const fn with_event_capacity(mut self, capacity: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | self.event_capacity = capacity; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | pub const fn with_initial_history_limit(mut self, limit: u16) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | self.initial_history_limit = limit; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | pub const fn with_max_media_bytes(mut self, bytes: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | self.max_media_bytes = bytes; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | pub const fn with_max_media_sources(mut self, sources: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | self.max_media_sources = sources; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | impl Default for EngineConfig { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | fn default() -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | command_capacity: 64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | event_capacity: 128, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | initial_history_limit: 30, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | max_media_bytes: 20 * 1024 * 1024, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | max_media_sources: 2_048, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | } |
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 | #[derive(Debug, ThisError)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | pub enum EngineError { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | #[error("the Matrix client is not authenticated")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | Unauthenticated, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | #[error("command, event, and media source capacities must be nonzero")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | ZeroCapacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | #[error("could not build the sync service")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | SyncService(#[source] matrix_sdk_ui::sync_service::Error), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | #[error("the event receiver has closed")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | EventReceiverClosed, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | #[error("engine task {task} panicked")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | TaskPanicked { task: String }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | impl EngineError { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | fn failure(&self) -> EngineFailure { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | EngineFailure { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | kind: match self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | Self::EventReceiverClosed => EngineErrorKind::EventReceiverClosed, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | Self::TaskPanicked { .. } => EngineErrorKind::Watcher, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | Self::Unauthenticated | Self::ZeroCapacity | Self::SyncService(_) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | EngineErrorKind::Other |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | message: self.to_string(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | } |
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 | #[derive(Clone)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | enum TaskContext { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | /// a panic here costs the caller an `Interrupted` answer and nothing else |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | Command(&'static str), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | /// a panic here takes the engine with it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | Watcher(&'static str), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | room_id: OwnedRoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | impl TaskContext { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | fn label(&self) -> String { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | match self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | Self::Command(name) | Self::Watcher(name) => (*name).to_owned(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | Self::RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | } => format!("room actor {room_id} generation {generation}"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | } |
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 | struct TaskRegistry { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | tasks: JoinSet<()>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 148 | contexts: HashMap<TaskId, TaskContext>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 149 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 150 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 151 | impl TaskRegistry { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 152 | fn spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 153 | &mut self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | context: TaskContext, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | task: impl Future<Output = ()> + Send + 'static, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | ) -> AbortHandle { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | let handle = self.tasks.spawn(task); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 158 | self.contexts.insert(handle.id(), context); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | handle |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 161 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 162 | async fn join_next(&mut self) -> Option<(TaskContext, Result<(), JoinError>)> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 163 | let result = self.tasks.join_next_with_id().await?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 164 | let id = match &result { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 165 | Ok((id, ())) => *id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 166 | Err(error) => error.id(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 167 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 168 | let context = self.contexts.remove(&id)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 169 | Some((context, result.map(drop))) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 170 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 171 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 172 | async fn shutdown(&mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 173 | self.tasks.abort_all(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 174 | while self.tasks.join_next().await.is_some() {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | self.contexts.clear(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 176 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 177 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 178 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 179 | enum RoomSlot { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 180 | Active { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 181 | generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 182 | sender: mpsc::Sender<ActorRequest>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 183 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 184 | /// 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 | 185 | Closing { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 186 | generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 187 | pending: VecDeque<Deferred>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 188 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 189 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 190 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 191 | impl RoomSlot { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 192 | const fn generation(&self) -> u64 { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 193 | match self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | Self::Active { generation, .. } | Self::Closing { generation, .. } => *generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 196 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 197 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 198 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 199 | enum Deferred { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 200 | Open(Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 201 | Close(Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 202 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 203 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 204 | struct Queues { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 205 | mutations: mpsc::Receiver<MutationJob>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 206 | notifications: mpsc::Receiver<Job<NotificationCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 207 | encryption: mpsc::Receiver<Job<EncryptionCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 208 | session_changes: broadcast::Receiver<SessionChange>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 209 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 210 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 211 | pub struct Engine { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 212 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 213 | sync: Arc<SyncService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 214 | port: DriverPort, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | config: EngineConfig, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 216 | media: Arc<MediaService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 217 | tasks: TaskRegistry, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 218 | rooms: HashMap<OwnedRoomId, RoomSlot>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 219 | next_generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 220 | exits: mpsc::UnboundedReceiver<Exited>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 221 | exit_sender: mpsc::UnboundedSender<Exited>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 222 | mutations: mpsc::Sender<MutationJob>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 223 | notifications: mpsc::Sender<Job<NotificationCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 224 | encryption: mpsc::Sender<Job<EncryptionCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 225 | queues: Option<Queues>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | /// one progress forwarder per room that has uploaded, and whether it's subscribed yet |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | uploads: HashMap<OwnedRoomId, (AbortHandle, watch::Receiver<bool>)>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | stop: Arc<Notify>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 230 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 231 | impl Engine { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | /// the escape hatch for a client you built yourself. `Session` is the usual way in |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | pub async fn from_client( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 235 | config: EngineConfig, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 236 | ) -> Result<(Self, ClientHandle, EventStream), EngineError> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 237 | if client.user_id().is_none() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 238 | return Err(EngineError::Unauthenticated); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | if config.command_capacity == 0 |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | || config.event_capacity == 0 |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | || config.max_media_sources == 0 |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | return Err(EngineError::ZeroCapacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 245 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 246 | let session_changes = client.subscribe_to_session_changes(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | let sync = Arc::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 248 | SyncService::builder(client.clone()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 249 | .with_offline_mode() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 250 | .build() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 251 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | .map_err(EngineError::SyncService)?, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 253 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 254 | let media = Arc::new(MediaService::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | config.max_media_bytes, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 257 | config.max_media_sources, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 258 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 259 | let (handle, port, events) = transport_pair( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 261 | sync.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 262 | media.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 263 | config.command_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | config.event_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | client.send_queue().enable_upload_progress(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 267 | let (exit_sender, exits) = mpsc::unbounded_channel(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 268 | let (mutations, mutation_queue) = mpsc::channel(config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 269 | let (notifications, notification_queue) = mpsc::channel(config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 270 | let (encryption, encryption_queue) = mpsc::channel(config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 271 | let engine = Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 272 | client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 273 | sync, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 274 | port, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 275 | config, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 276 | media, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 277 | tasks: TaskRegistry { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 278 | tasks: JoinSet::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 279 | contexts: HashMap::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 280 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 281 | rooms: HashMap::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 282 | next_generation: 0, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 283 | exits, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 284 | exit_sender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 285 | mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 286 | notifications, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 287 | encryption, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 288 | queues: Some(Queues { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 289 | mutations: mutation_queue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 290 | notifications: notification_queue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 291 | encryption: encryption_queue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 292 | session_changes, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 293 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 294 | uploads: HashMap::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 295 | stop: Arc::new(Notify::new()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 296 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 297 | Ok((engine, handle, events)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 298 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 299 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 300 | pub async fn run(mut self) -> Result<(), EngineError> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 301 | let events = self.port.event_sender(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 302 | let user_id = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 303 | .client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 304 | .user_id() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | .expect("authentication was checked during construction") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 306 | .to_owned(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 307 | self.port |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 308 | .connection_sender() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | .send_replace(ConnectionState::Connecting); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | for event in [ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | Event::Authenticated { user_id }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | Event::ConnectionChanged(ConnectionState::Connecting), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 313 | ] { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | if events.send(event).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 315 | self.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | return Err(EngineError::EventReceiverClosed); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | self.start_tasks(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | self.sync.start().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | let mut failure = None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 323 | let mut shutdown_reply = None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 326 | biased; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | () = events.closed() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | failure = Some(EngineError::EventReceiverClosed); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | break; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 330 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 331 | () = self.stop.notified() => break, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 332 | request = self.port.recv() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 333 | let Some(request) = request else { break }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 334 | if let Some(reply) = self.dispatch(request).await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 335 | shutdown_reply = Some(reply); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | break; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 338 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 339 | Some((context, result)) = self.tasks.join_next(), if !self.tasks.tasks.is_empty() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 340 | let Err(error) = result else { continue }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 341 | if !error.is_panic() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 342 | continue; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | if let TaskContext::Command(label) = context { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 345 | tracing::error!(label, %error, "command task panicked"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 346 | continue; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 347 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 348 | failure = Some(EngineError::TaskPanicked { task: context.label() }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 349 | break; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 351 | Some(exited) = self.exits.recv() => self.room_exited(exited).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 352 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 353 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 354 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 355 | if let Some(error) = &failure { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 356 | let _ = events.send(Event::Failed(error.failure())).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 357 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 358 | self.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 359 | let _ = events.send(Event::Stopped).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 360 | if let Some(reply) = shutdown_reply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | reply.done(Ok(())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 362 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | failure.map_or(Ok(()), Err) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 364 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | fn start_tasks(&mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | let queues = self.queues.take().expect("engine tasks start once"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | let client = &self.client; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | let events = || self.port.event_sender(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | let watchers: [(&'static str, BoxFuture<'static, ()>); 9] = [ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | "session change watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | Box::pin(watchers::session( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | queues.session_changes, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | events(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | self.stop.clone(), |
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 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 380 | "connection state watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 381 | Box::pin(watchers::connection( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 382 | self.sync.state(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 383 | self.sync.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 384 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 385 | events(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 386 | self.port.connection_sender(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 387 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 388 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 389 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 390 | "send queue watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 391 | Box::pin(watchers::send_queue_errors( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 392 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 393 | client.send_queue().subscribe_errors(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 394 | self.port.connection_sender().subscribe(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 395 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 396 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 397 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | "encryption state watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | Box::pin(watchers::encryption(client.clone(), events())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 400 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 402 | "room list watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 403 | Box::pin(watchers::rooms(client.clone(), events())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 404 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 405 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 406 | "notification settings watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 407 | Box::pin(watchers::notification_settings(client.clone(), events())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 408 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 409 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 410 | "room command worker", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 411 | Box::pin(mutations::run_scheduler( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 412 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 413 | queues.mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | self.config.command_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 415 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 416 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 417 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 418 | "notification command worker", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 419 | Box::pin(commands::run_notifications( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 420 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 421 | queues.notifications, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 422 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 423 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 424 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 425 | "encryption command worker", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 426 | Box::pin(commands::run_encryption( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 427 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 428 | events(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 429 | queues.encryption, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 430 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 431 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 432 | ]; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 433 | for (label, task) in watchers { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 434 | self.tasks.spawn(TaskContext::Watcher(label), task); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 435 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 436 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 437 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 438 | /// hands back the shutdown request's responder, which is answered once we've stopped |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 439 | async fn dispatch(&mut self, Request { command, reply }: Request) -> Option<Responder> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 440 | let client = self.client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 441 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 442 | Command::Lifecycle(LifecycleCommand::Shutdown) => return Some(reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 443 | Command::Account(AccountCommand::Logout) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 444 | let events = self.port.event_sender(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 445 | let stop = self.stop.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 446 | self.spawn_command("logout", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 447 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 448 | .logout() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 449 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 450 | .map_err(|source| Error::from_sdk("logout failed", source))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 451 | let _ = events.send(Event::LoggedOut).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 452 | stop.notify_one(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 453 | Ok(()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 454 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 455 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 456 | Command::Account(AccountCommand::SetAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 457 | event_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 458 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 459 | }) => enqueue( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 460 | &self.mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 461 | Job { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 462 | command: Mutation::GlobalAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 463 | event_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 464 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 465 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 466 | reply, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 467 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 468 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 469 | Command::Account(AccountCommand::SetDisplayName(name)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 470 | self.spawn_command("set display name", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 471 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 472 | .account() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 473 | .set_display_name(name.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 474 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 475 | .map_err(|source| Error::from_sdk("setting display name failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 476 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 477 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 478 | Command::Account(AccountCommand::SetAvatar(avatar)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 479 | self.spawn_command("set avatar", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 480 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 481 | .account() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 482 | .set_avatar_url(avatar.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 483 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 484 | .map_err(|source| Error::from_sdk("setting avatar failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 485 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 486 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 487 | Command::Account(AccountCommand::UploadAvatar { mimetype, bytes }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 488 | let max_bytes = self.config.max_media_bytes; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 489 | self.spawn_command("upload avatar", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 490 | commands::upload_avatar(&client, &mimetype, bytes, max_bytes).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 491 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 492 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 493 | Command::Account(AccountCommand::RenameDevice { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 494 | device_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 495 | display_name, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 496 | }) => self.spawn_command("rename device", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 497 | commands::rename_device(&client, &device_id, &display_name).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 498 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 499 | Command::Account(AccountCommand::DeleteDevices { device_ids, auth }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 500 | self.spawn_command("delete devices", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 501 | commands::delete_devices(&client, &device_ids, auth).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 502 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 503 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 504 | Command::Room(RoomCommand::Open(room_id)) => self.open_room(room_id, reply).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 505 | Command::Room(RoomCommand::Close(room_id)) => self.close_room(room_id, reply).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 506 | Command::Room(RoomCommand::SetTyping { room_id, typing }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 507 | self.spawn_command("typing", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 508 | commands::set_typing(&client, &room_id, typing).await |
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 | Command::Room(RoomCommand::CancelSend { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 512 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 513 | transaction_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 514 | }) => self.spawn_command("cancel send", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 515 | commands::cancel_send(&client, &room_id, &transaction_id).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 516 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 517 | Command::Room(RoomCommand::RetrySend { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 518 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 519 | transaction_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 520 | }) => self.spawn_command("retry send", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 521 | commands::retry_send(&client, &room_id, &transaction_id).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 522 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 523 | Command::Room(command) => enqueue( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 524 | &self.mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 525 | Job { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 526 | command: Mutation::Room(command), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 527 | reply, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 528 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 529 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 530 | Command::Timeline(command) => self.route_timeline(command, reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 531 | Command::Media(MediaCommand::Upload(upload)) => self.upload(upload, reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 532 | Command::Encryption(command) => enqueue(&self.encryption, Job { command, reply }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 533 | Command::Notification(command) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 534 | enqueue(&self.notifications, Job { command, reply }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 535 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 536 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 537 | None |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 538 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 539 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 540 | fn spawn_command( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 541 | &mut self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 542 | label: &'static str, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 543 | reply: Responder, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 544 | work: impl Future<Output = Result<(), Error>> + Send + 'static, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 545 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 546 | self.tasks.spawn(TaskContext::Command(label), async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 547 | reply.done(work.await); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 548 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 549 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 550 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 551 | fn upload(&mut self, upload: crate::AttachmentUpload, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 552 | let Some(room) = self.client.get_room(upload.timeline_id.room_id()) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 553 | return reply.done(Err(Error::room_unknown())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 554 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 555 | let room_id = room.room_id().to_owned(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 556 | self.uploads |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 557 | .retain(|_, (forwarder, _)| !forwarder.is_finished()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 558 | let mut ready = match self.uploads.get(&room_id) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 559 | Some((_, ready)) => ready.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 560 | None => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 561 | let (subscribed, ready) = watch::channel(false); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 562 | let forwarder = self.tasks.spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 563 | TaskContext::Command("upload watcher"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 564 | watchers::room_uploads(room.clone(), self.port.event_sender(), subscribed), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 565 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 566 | self.uploads.insert(room_id, (forwarder, ready.clone())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 567 | ready |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 568 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 569 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 570 | let max_bytes = self.config.max_media_bytes; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 571 | // queueing before the forwarder listens would lose the first progress ticks |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 572 | self.spawn_command("upload", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 573 | ready |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 574 | .wait_for(|subscribed| *subscribed) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 575 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 576 | .map_err(|_| Error::new(ErrorKind::Store, "reading the media send queue failed"))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 577 | media::queue_upload(room, upload, max_bytes).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 578 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 579 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 580 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 581 | fn route_timeline(&mut self, command: TimelineCommand, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 582 | let sender = match self.rooms.get(command.room_id()) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 583 | Some(RoomSlot::Active { sender, .. }) => Some(sender.clone()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 584 | _ => None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 585 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 586 | match (sender, command) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 587 | (Some(sender), command) => send_actor(&sender, ActorRequest::Execute(command, reply)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 588 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 589 | None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 590 | TimelineCommand::MarkRead { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 591 | timeline_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 592 | event_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 593 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 594 | ) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 595 | let client = self.client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 596 | self.spawn_command("mark read", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 597 | commands::mark_read(&client, &timeline_id, event_id).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 598 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 599 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 600 | (None, _) => reply.done(Err(Error::new(ErrorKind::RoomNotOpen, "room is not open"))), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 601 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 602 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 603 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 604 | async fn open_room(&mut self, room_id: OwnedRoomId, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 605 | match self.rooms.get_mut(&room_id) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 606 | Some(RoomSlot::Active { sender, .. }) => send_actor(sender, ActorRequest::Open(reply)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 607 | Some(RoomSlot::Closing { pending, .. }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 608 | defer(pending, Deferred::Open(reply), self.config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 609 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 610 | None => self.start_actor(room_id, reply).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 611 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 612 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 613 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 614 | async fn close_room(&mut self, room_id: OwnedRoomId, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 615 | let (generation, sender) = match self.rooms.get_mut(&room_id) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 616 | None => return reply.done(Ok(())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 617 | Some(RoomSlot::Closing { pending, .. }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 618 | return defer( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 619 | pending, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 620 | Deferred::Close(reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 621 | self.config.command_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 622 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 623 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 624 | Some(RoomSlot::Active { generation, sender }) => (*generation, sender.clone()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 625 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 626 | match sender.try_send(ActorRequest::Close(reply)) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 627 | // a closed channel means the actor already left on its own, which is closed enough |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 628 | Ok(()) | Err(mpsc::error::TrySendError::Closed(_)) => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 629 | Err(mpsc::error::TrySendError::Full(request)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 630 | return request.into_reply().done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 631 | ErrorKind::RoomBusy, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 632 | "room command queue is full", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 633 | ))); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 634 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 635 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 636 | self.rooms.insert( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 637 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 638 | RoomSlot::Closing { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 639 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 640 | pending: VecDeque::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 641 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 642 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 643 | self.update_room_subscriptions().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 644 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 645 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 646 | async fn start_actor(&mut self, room_id: OwnedRoomId, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 647 | let Some(room) = self.client.get_room(&room_id) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 648 | return reply.done(Err(Error::room_unknown())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 649 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 650 | self.next_generation = self.next_generation.wrapping_add(1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 651 | let generation = self.next_generation; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 652 | let (sender, requests) = mpsc::channel(self.config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 653 | let (supervisor, panics) = Supervisor::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 654 | let actor = RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 655 | room_id: room_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 656 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 657 | room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 658 | requests, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 659 | exits: self.exit_sender.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 660 | context: SessionContext { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 661 | events: self.port.event_sender(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 662 | media: self.media.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 663 | current_user_id: self.client.user_id().map(ToOwned::to_owned), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 664 | initial_history_limit: self.config.initial_history_limit, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 665 | supervisor, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 666 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 667 | panics, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 668 | opening: vec![reply], |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 669 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 670 | self.rooms |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 671 | .insert(room_id.clone(), RoomSlot::Active { generation, sender }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 672 | self.tasks.spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 673 | TaskContext::RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 674 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 675 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 676 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 677 | actor.run(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 678 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 679 | self.update_room_subscriptions().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 680 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 681 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 682 | async fn room_exited( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 683 | &mut self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 684 | Exited { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 685 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 686 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 687 | }: Exited, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 688 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 689 | if self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 690 | .rooms |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 691 | .get(&room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 692 | .is_none_or(|slot| slot.generation() != generation) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 693 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 694 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 695 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 696 | if let Some(RoomSlot::Closing { pending, .. }) = self.rooms.remove(&room_id) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 697 | for deferred in pending { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 698 | match deferred { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 699 | Deferred::Open(reply) => self.open_room(room_id.clone(), reply).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 700 | Deferred::Close(reply) => self.close_room(room_id.clone(), reply).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 701 | } |
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 | self.update_room_subscriptions().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 705 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 706 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 707 | async fn update_room_subscriptions(&self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 708 | let room_ids = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 709 | .rooms |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 710 | .iter() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 711 | .filter(|(_, slot)| matches!(slot, RoomSlot::Active { .. })) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 712 | .map(|(room_id, _)| room_id.as_ref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 713 | .collect::<Vec<&RoomId>>(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 714 | self.sync |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 715 | .room_list_service() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 716 | .subscribe_to_rooms(&room_ids) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 717 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 718 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 719 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 720 | async fn shutdown(&mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 721 | self.port.close(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 722 | self.tasks.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 723 | self.rooms.clear(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 724 | self.uploads.clear(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 725 | self.media.clear().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 726 | self.sync.stop().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 727 | self.port |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 728 | .connection_sender() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 729 | .send_replace(ConnectionState::Stopped); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 730 | let _ = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 731 | .port |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 732 | .event_sender() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 733 | .send(Event::ConnectionChanged(ConnectionState::Stopped)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 734 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 735 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 736 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 737 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 738 | fn send_actor(sender: &mpsc::Sender<ActorRequest>, request: ActorRequest) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 739 | if let Err(error) = sender.try_send(request) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 740 | let (request, error) = match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 741 | mpsc::error::TrySendError::Full(request) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 742 | request, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 743 | Error::new(ErrorKind::RoomBusy, "room command queue is full"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 744 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 745 | mpsc::error::TrySendError::Closed(request) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 746 | request, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 747 | Error::new(ErrorKind::RoomNotOpen, "room is not open"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 748 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 749 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 750 | request.into_reply().done(Err(error)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 751 | } |
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 | fn defer(pending: &mut VecDeque<Deferred>, deferred: Deferred, capacity: usize) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 755 | if pending.len() < capacity { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 756 | return pending.push_back(deferred); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 757 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 758 | let (Deferred::Open(reply) | Deferred::Close(reply)) = deferred; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 759 | reply.done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 760 | ErrorKind::RoomBusy, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 761 | "room lifecycle queue is full", |
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 | fn enqueue<T>(sender: &mpsc::Sender<Job<T>>, job: Job<T>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 766 | if let Err(error) = sender.try_send(job) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 767 | let (job, error) = match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 768 | mpsc::error::TrySendError::Full(job) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 769 | job, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 770 | Error::new(ErrorKind::QueueFull, "command queue is full"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 771 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 772 | mpsc::error::TrySendError::Closed(job) => (job, Error::interrupted()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 773 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 774 | job.reply.done(Err(error)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 775 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 776 | } |