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