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