96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1 | use std::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2 | borrow::Cow, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | collections::{HashMap, HashSet, VecDeque}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | future::Future, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | io::Read as _, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 6 | panic::AssertUnwindSafe, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 7 | sync::Arc, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 8 | time::Duration, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | use futures_util::{FutureExt, StreamExt}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | use matrix_sdk::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | Client, Room, SessionChange, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | room::edit::EditedContent, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | room::reply::{EnforceThread, Reply as SdkReply}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | ruma::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | OwnedDeviceId, OwnedEventId, OwnedRoomId, RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | api::client::receipt::create_receipt::v3::ReceiptType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | events::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | AnyGlobalAccountDataEventContent, AnyMessageLikeEventContent, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | GlobalAccountDataEventType, Mentions, RoomAccountDataEventType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | receipt::ReceiptThread, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | room::message::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | AddMentions, MessageType, Relation, ReplyWithinThread, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | RoomMessageEventContentWithoutRelation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | serde::Raw, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | send_queue::{LocalEchoContent, RoomSendQueueUpdate, SendHandle}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | use matrix_sdk_ui::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | sync_service::{State as SyncState, SyncService}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | timeline::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | RoomExt, Timeline, TimelineEventItemId, TimelineFocus, TimelineReadReceiptTracking, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | use thiserror::Error; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | use tokio::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | sync::{Mutex, mpsc, oneshot, watch}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | task::{AbortHandle, Id as TaskId, JoinHandle, JoinSet}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | use crate::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | AccountCommand, AttachmentUpload, ClientHandle, Command, CommandErrorKind, CommandFailure, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | CommandId, CommandKind, ConnectionState, CreateRoomOptions, EncryptionCommand, EngineErrorKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | EngineFailure, Event, EventStream, HistoryState, InteractiveAuth, InteractiveAuthChallenge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | InteractiveAuthOutcome, LifecycleCommand, MediaCommand, MediaId, MediaSize, MessageContent, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | MessageEditContent, MessageMentions, NotificationCommand, PowerLevelChanges, RichText, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | RoomCommand, RoomNotificationMode, RoomPreset, RoomVisibility, TimelineCommand, TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | TypingState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | encryption::{VerificationController, encryption_state}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | error::{CommandError, classify_sdk_error}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | normalise::{MediaRegistration, MediaScope, normalise_timeline, normalise_timeline_diffs}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | transport::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | ClientQueryError, CommandEnvelope, CommandResultError, DriverPort, DriverRequest, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | EventSender, SendEventError, transport_pair, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | upload::prepare_upload, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | use matrix_sdk::ruma::api::{Metadata, OutgoingRequest, auth_scheme::SendAccessToken}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | use matrix_sdk_crypto::{AttachmentDecryptor, MediaEncryptionInfo}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | #[derive(Clone, Debug, Eq, PartialEq)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | pub struct EngineConfig { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | pub command_capacity: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | pub event_capacity: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | pub initial_history_limit: u16, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | pub max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | pub max_media_sources: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | impl EngineConfig { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | pub const fn with_command_capacity(mut self, capacity: usize) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | self.command_capacity = capacity; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 81 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 82 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | pub const fn with_event_capacity(mut self, capacity: usize) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 84 | self.event_capacity = capacity; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 88 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | pub const fn with_initial_history_limit(mut self, limit: u16) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 90 | self.initial_history_limit = limit; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 92 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 93 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 94 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | pub const fn with_max_media_bytes(mut self, bytes: usize) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 96 | self.max_media_bytes = bytes; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 97 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 98 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 99 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 100 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 101 | pub const fn with_max_media_sources(mut self, sources: usize) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 102 | self.max_media_sources = sources; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 103 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 104 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 106 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | impl Default for EngineConfig { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | fn default() -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | command_capacity: 64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | event_capacity: 128, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | initial_history_limit: 30, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | max_media_bytes: 20 * 1024 * 1024, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | max_media_sources: 2_048, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 117 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 118 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | #[derive(Debug, Error)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | pub enum EngineError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | #[error("the Matrix client is not authenticated")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | #[error("command, event, and media source capacities must be nonzero")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | ZeroCapacity, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | #[error("could not build the sync service")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | SyncService(#[source] matrix_sdk_ui::sync_service::Error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | #[error("the event receiver has closed")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | EventReceiverClosed, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | #[error("engine task {task} panicked")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | TaskPanicked { task: String }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | impl EngineError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | fn failure(&self) -> EngineFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | EngineFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | kind: match self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | Self::EventReceiverClosed => EngineErrorKind::EventReceiverClosed, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | Self::Unauthenticated |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | | Self::ZeroCapacity |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | | Self::SyncService(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | | Self::TaskPanicked { .. } => EngineErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | message: self.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 146 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 147 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 148 | type UploadWatchers = HashMap< |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | AbortHandle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | watch::Receiver<Option<Result<(), CommandErrorKind>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | >; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 155 | const SYNC_RESTART_MIN: Duration = Duration::from_secs(1); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 156 | const SYNC_RESTART_MAX: Duration = Duration::from_secs(60); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 157 | const SEND_QUEUE_RETRY: Duration = Duration::from_secs(5); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 158 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | type MediaFlights = Arc<Mutex<HashMap<(MediaId, MediaSize), Vec<CommandId>>>>; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | pub struct Engine { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | sync: Arc<SyncService>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | port: DriverPort, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | rooms: HashMap<OwnedRoomId, RoomActorState>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | next_room_generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | room_actor_events: mpsc::UnboundedReceiver<RoomActorEvent>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | room_actor_event_sender: mpsc::UnboundedSender<RoomActorEvent>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | upload_watchers: UploadWatchers, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | tasks: TaskRegistry, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | session_changes: Option<tokio::sync::broadcast::Receiver<SessionChange>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | initial_history_limit: u16, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 173 | room_command_capacity: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 174 | max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | media: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | notification_commands: mpsc::Sender<CommandJob<NotificationCommand>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | notification_command_receiver: Option<mpsc::Receiver<CommandJob<NotificationCommand>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 178 | encryption_commands: mpsc::Sender<EncryptionWork>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | encryption_command_receiver: Option<mpsc::Receiver<EncryptionWork>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 180 | verification: Arc<Mutex<VerificationController>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 181 | room_commands: mpsc::Sender<RoomJob>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 182 | room_command_receiver: Option<mpsc::Receiver<RoomJob>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 183 | global_account_gates: HashMap<String, oneshot::Receiver<()>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 184 | room_account_gates: HashMap<(OwnedRoomId, String), oneshot::Receiver<()>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 185 | media_flights: MediaFlights, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 186 | stop: Arc<tokio::sync::Notify>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 187 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 188 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 189 | struct CommandJob<T> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 190 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 191 | command: T, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 192 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 193 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 194 | enum EncryptionWork { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 195 | Command(CommandJob<EncryptionCommand>), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 196 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 197 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 198 | struct RoomJob { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 199 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 200 | command: RoomCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 201 | reply: Option<oneshot::Sender<Result<OwnedRoomId, CommandResultError>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 202 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 203 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 204 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 205 | enum RoomMutationKey { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 206 | Room(OwnedRoomId), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 207 | Target(String), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 208 | Direct(ruma::OwnedUserId), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 209 | Independent(CommandId), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 210 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 211 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 212 | struct RoomJobCompletion { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 213 | key: RoomMutationKey, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 214 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 215 | reply: Option<oneshot::Sender<Result<OwnedRoomId, CommandResultError>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 216 | result: Result<Option<OwnedRoomId>, CommandError>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 217 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 218 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 219 | #[derive(Clone)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 220 | enum TaskContext { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 221 | Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 222 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 223 | kind: CommandKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 224 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 225 | Watcher(&'static str), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 226 | RoomActor { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 227 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 228 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 229 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 230 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 231 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 232 | impl TaskContext { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 233 | fn label(&self) -> String { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 234 | match self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 235 | Self::Command { id, kind } => format!("{kind:?} command {}", id.get()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 236 | Self::Watcher(name) => (*name).to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 237 | Self::RoomActor { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 238 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 239 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 240 | } => format!("room actor {room_id} generation {generation}"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 241 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 242 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 243 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 244 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 245 | struct TaskRegistry { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 246 | tasks: JoinSet<()>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 247 | contexts: HashMap<TaskId, TaskContext>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 248 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 249 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 250 | impl TaskRegistry { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 251 | fn new() -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 252 | Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 253 | tasks: JoinSet::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 254 | contexts: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 255 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 256 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 257 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 258 | fn is_empty(&self) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 259 | self.tasks.is_empty() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 260 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 261 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 262 | fn spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 263 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 264 | context: TaskContext, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 265 | future: impl Future<Output = ()> + Send + 'static, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 266 | ) -> AbortHandle { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | let handle = self.tasks.spawn(future); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 268 | self.contexts.insert(handle.id(), context); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 269 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 270 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 271 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 272 | async fn join_next(&mut self) -> Option<(TaskContext, Result<(), tokio::task::JoinError>)> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 273 | let result = self.tasks.join_next_with_id().await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 274 | let id = match &result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 275 | Ok((id, ())) => *id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 276 | Err(error) => error.id(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 277 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 278 | let context = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 279 | .contexts |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 280 | .remove(&id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 281 | .expect("every registered task has context"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 282 | Some((context, result.map(|(_, ())| ()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 283 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 284 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 285 | async fn shutdown(&mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 286 | self.tasks.abort_all(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 287 | while self.tasks.join_next().await.is_some() {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 288 | self.contexts.clear(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 289 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 290 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 291 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 292 | enum RoomActorState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 293 | Opening { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 294 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 295 | sender: mpsc::Sender<RoomActorRequest>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 296 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 297 | Open { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 298 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 299 | sender: mpsc::Sender<RoomActorRequest>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 300 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 301 | Closing { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 302 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 303 | pending: VecDeque<DeferredRoomLifecycle>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 304 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 305 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 306 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 307 | enum DeferredRoomLifecycle { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 308 | Open(CommandId), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 309 | Close(CommandId), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 310 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 311 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 312 | impl RoomActorState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 313 | const fn generation(&self) -> u64 { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 314 | match self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 315 | Self::Opening { generation, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 316 | | Self::Open { generation, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 317 | | Self::Closing { generation, .. } => *generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 318 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 319 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 320 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 321 | fn sender(&self) -> Option<&mpsc::Sender<RoomActorRequest>> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 322 | match self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 323 | Self::Opening { sender, .. } | Self::Open { sender, .. } => Some(sender), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 324 | Self::Closing { .. } => None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 325 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 326 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 327 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 328 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 329 | enum RoomActorEvent { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 330 | Ready { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 331 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 332 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 333 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 334 | Exited { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 335 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 336 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 337 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 338 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 339 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 340 | enum EngineActivity { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 341 | Request(Option<Box<DriverRequest>>), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 342 | Task(Option<(TaskContext, Result<(), tokio::task::JoinError>)>), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 343 | RoomActor(Option<RoomActorEvent>), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 344 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 345 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 346 | /// least recently seen goes first. stale order slots are skipped by stamp rather than hunted down |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 347 | struct MediaRegistry { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 348 | entries: HashMap<MediaId, (MediaRegistration, u64)>, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 349 | order: VecDeque<(MediaId, u64)>, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 350 | next_stamp: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 351 | limit: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 352 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 353 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 354 | impl MediaRegistry { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 355 | fn new(limit: usize) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 356 | Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 357 | entries: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 358 | order: VecDeque::new(), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 359 | next_stamp: 0, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 360 | limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 361 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 362 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 363 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 364 | fn get(&self, id: &MediaId) -> Option<&MediaRegistration> { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 365 | self.entries.get(id).map(|(media, _)| media) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 366 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 367 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 368 | fn insert_all(&mut self, media: Vec<MediaRegistration>) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 369 | for media in media { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 370 | let stamp = self.next_stamp; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 371 | self.next_stamp += 1; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 372 | self.order.push_back((media.id.clone(), stamp)); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 373 | self.entries.insert(media.id.clone(), (media, stamp)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 374 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 375 | while self.entries.len() > self.limit { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 376 | let Some((id, stamp)) = self.order.pop_front() else { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 377 | break; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 378 | }; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 379 | if self |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 380 | .entries |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 381 | .get(&id) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 382 | .is_some_and(|(_, current)| *current == stamp) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 383 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 384 | self.entries.remove(&id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 385 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 386 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 387 | if self.order.len() > self.limit.saturating_mul(4) { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 388 | let entries = &self.entries; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 389 | self.order |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 390 | .retain(|(id, stamp)| entries.get(id).is_some_and(|(_, current)| current == stamp)); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 391 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 392 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 393 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 394 | fn release(&mut self, scope: &MediaScope) { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 395 | self.entries.retain(|id, _| !scope.owns(id)); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 396 | let entries = &self.entries; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 397 | self.order.retain(|(id, _)| entries.contains_key(id)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 398 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 399 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 400 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 401 | enum RoomActorRequest { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 402 | Opened { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 403 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 404 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 405 | Execute { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 406 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 407 | command: TimelineCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 408 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 409 | Close { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 410 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 411 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 412 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 413 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 414 | struct RoomActor { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 415 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 416 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 417 | room: Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 418 | receiver: mpsc::Receiver<RoomActorRequest>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 419 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 420 | engine_events: mpsc::UnboundedSender<RoomActorEvent>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 421 | media: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 422 | initial_history_limit: u16, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 423 | open_ids: Vec<CommandId>, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 424 | background: JoinSet<(CommandId, Result<(), CommandError>)>, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 425 | background_ids: HashMap<TaskId, CommandId>, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 426 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 427 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 428 | type BackgroundResult = |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 429 | Result<(TaskId, (CommandId, Result<(), CommandError>)), tokio::task::JoinError>; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 430 | type BackgroundWork = std::pin::Pin<Box<dyn Future<Output = Result<(), CommandError>> + Send>>; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 431 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 432 | /// network-bound work runs beside the actor so one slow redaction doesn't queue the room behind it |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 433 | enum TimelineOutcome { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 434 | Done(Result<(), CommandError>), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 435 | Background(BackgroundWork), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 436 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 437 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 438 | struct TimelineSession { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 439 | id: TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 440 | timeline: Arc<Timeline>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 441 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 442 | watcher: JoinHandle<()>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 443 | media: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 444 | current_user_id: Option<ruma::OwnedUserId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 445 | scope: MediaScope, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 446 | pagination_ids: Arc<Mutex<HashSet<CommandId>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 447 | pagination_sender: mpsc::Sender<PaginationRequest>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 448 | pagination_worker: JoinHandle<()>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 449 | pagination_results: Option<mpsc::UnboundedSender<(CommandId, Result<(), CommandError>)>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 450 | pagination_result_task: JoinHandle<()>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 451 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 452 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 453 | struct PaginationRequest { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 454 | timeline: Arc<Timeline>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 455 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 456 | timeline_id: TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 457 | limit: u16, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 458 | command_id: Option<CommandId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 459 | results: mpsc::UnboundedSender<(CommandId, Result<(), CommandError>)>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 460 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 461 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 462 | impl TimelineSession { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 463 | async fn start( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 464 | id: TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 465 | timeline: Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 466 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 467 | media: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 468 | current_user_id: Option<ruma::OwnedUserId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 469 | initial_history_limit: u16, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 470 | ) -> Result<Self, SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 471 | let timeline = Arc::new(timeline); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 472 | let scope = MediaScope::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 473 | let watcher = start_timeline_watcher( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 474 | id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 475 | timeline.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 476 | events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 477 | media.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 478 | current_user_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 479 | scope.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 480 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 481 | .await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 482 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 483 | let (pagination_sender, mut pagination_receiver) = mpsc::channel::<PaginationRequest>(32); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 484 | let pagination_worker = tokio::spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 485 | while let Some(request) = pagination_receiver.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 486 | let command_id = request.command_id; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 487 | let results = request.results.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 488 | if AssertUnwindSafe(run_pagination(request)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 489 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 490 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 491 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 492 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 493 | tracing::warn!(?command_id, "pagination task panicked"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 494 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 495 | let _ = results.send(( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 496 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 497 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 498 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 499 | "pagination failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 500 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 501 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 502 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 503 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 504 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 505 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 506 | let pagination_ids = Arc::new(Mutex::new(HashSet::new())); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 507 | let (pagination_results, mut result_receiver) = mpsc::unbounded_channel(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 508 | let result_events = events.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 509 | let result_ids = pagination_ids.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 510 | let pagination_result_task = tokio::spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 511 | while let Some((id, result)) = result_receiver.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 512 | if result_ids.lock().await.remove(&id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 513 | let _ = emit_command_result(&result_events, id, CommandKind::Timeline, result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 514 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 515 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 516 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 517 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 518 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 519 | let mut session = Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 520 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 521 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 522 | events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 523 | watcher, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 524 | media, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 525 | current_user_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 526 | scope, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 527 | pagination_sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 528 | pagination_worker, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 529 | pagination_ids, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 530 | pagination_results: Some(pagination_results), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 531 | pagination_result_task, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 532 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 533 | if initial_history_limit > 0 { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 534 | session.paginate(initial_history_limit, None).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 535 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 536 | Ok(session) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 537 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 538 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 539 | async fn resync(&mut self) -> Result<(), SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 540 | self.watcher.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 541 | let _ = (&mut self.watcher).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 542 | self.watcher = start_timeline_watcher( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 543 | self.id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 544 | self.timeline.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 545 | self.events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 546 | self.media.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 547 | self.current_user_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 548 | self.scope.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 549 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 550 | .await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 551 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 552 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 553 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 554 | async fn paginate(&mut self, limit: u16, command_id: Option<CommandId>) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 555 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 556 | self.pagination_ids.lock().await.insert(id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 557 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 558 | let request = PaginationRequest { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 559 | timeline: self.timeline.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 560 | events: self.events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 561 | timeline_id: self.id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 562 | limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 563 | command_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 564 | results: self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 565 | .pagination_results |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 566 | .as_ref() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 567 | .expect("pagination session is open") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 568 | .clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 569 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 570 | if let Err(error) = self.pagination_sender.try_send(request) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 571 | let kind = match error { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 572 | mpsc::error::TrySendError::Full(_) => CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 573 | mpsc::error::TrySendError::Closed(_) => CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 574 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 575 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 576 | self.pagination_ids.lock().await.remove(&id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 577 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 578 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 579 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 580 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 581 | Err(CommandError::new(kind, "pagination queue is unavailable")), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 582 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 583 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 584 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 585 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 586 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 587 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 588 | async fn close(mut self, kind: CommandErrorKind, message: &'static str) { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 589 | self.media.lock().await.release(&self.scope); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 590 | self.pagination_worker.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 591 | let _ = (&mut self.pagination_worker).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 592 | drop(self.pagination_results.take()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 593 | let _ = (&mut self.pagination_result_task).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 594 | let pending = self.pagination_ids.lock().await.drain().collect::<Vec<_>>(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 595 | for id in pending { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 596 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 597 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 598 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 599 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 600 | Err(CommandError::new(kind, message)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 601 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 602 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 603 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 604 | self.watcher.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 605 | let _ = (&mut self.watcher).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 606 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 607 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 608 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 609 | async fn start_timeline_watcher( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 610 | id: TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 611 | timeline: Arc<Timeline>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 612 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 613 | media: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 614 | current_user_id: Option<ruma::OwnedUserId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 615 | scope: MediaScope, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 616 | ) -> Result<JoinHandle<()>, SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 617 | let (items, mut stream) = timeline.subscribe().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 618 | let normalised = normalise_timeline(items.iter(), current_user_id.as_deref(), &scope); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 619 | media.lock().await.insert_all(normalised.media); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 620 | events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 621 | .send(Event::TimelineReset { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 622 | timeline_id: id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 623 | items: normalised.items, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 624 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 625 | .await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 626 | Ok(tokio::spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 627 | while let Some(diffs) = stream.next().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 628 | let normalised = normalise_timeline_diffs(diffs, current_user_id.as_deref(), &scope); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 629 | media.lock().await.insert_all(normalised.media); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 630 | if events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 631 | .send(Event::TimelineChanged { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 632 | timeline_id: id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 633 | diffs: normalised.diffs, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 634 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 635 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 636 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 637 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 638 | tracing::debug!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 639 | ?id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 640 | "timeline watcher stopped because the event receiver closed" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 641 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 642 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 643 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 644 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 645 | tracing::debug!(?id, "timeline watcher stopped"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 646 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 647 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 648 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 649 | impl Drop for TimelineSession { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 650 | fn drop(&mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 651 | self.watcher.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 652 | self.pagination_worker.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 653 | self.pagination_result_task.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 654 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 655 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 656 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 657 | impl Engine { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 658 | pub async fn from_client( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 659 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 660 | config: EngineConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 661 | ) -> Result<(Self, ClientHandle, EventStream), EngineError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 662 | if client.user_id().is_none() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 663 | return Err(EngineError::Unauthenticated); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 664 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 665 | if config.command_capacity == 0 |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 666 | || config.event_capacity == 0 |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 667 | || config.max_media_sources == 0 |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 668 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 669 | return Err(EngineError::ZeroCapacity); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 670 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 671 | let session_changes = client.subscribe_to_session_changes(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 672 | let sync = Arc::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 673 | SyncService::builder(client.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 674 | .with_offline_mode() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 675 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 676 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 677 | .map_err(EngineError::SyncService)?, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 678 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 679 | let (handle, port, events) = transport_pair( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 680 | client.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 681 | sync.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 682 | config.command_capacity, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 683 | config.event_capacity, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 684 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 685 | client.send_queue().enable_upload_progress(true); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 686 | let (room_actor_event_sender, room_actor_events) = mpsc::unbounded_channel(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 687 | let (notification_commands, notification_command_receiver) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 688 | mpsc::channel(config.command_capacity); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 689 | let (encryption_commands, encryption_command_receiver) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 690 | mpsc::channel(config.command_capacity); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 691 | let (room_commands, room_command_receiver) = mpsc::channel(config.command_capacity); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 692 | Ok(( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 693 | Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 694 | client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 695 | sync, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 696 | port, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 697 | rooms: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 698 | next_room_generation: 0, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 699 | room_actor_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 700 | room_actor_event_sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 701 | upload_watchers: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 702 | tasks: TaskRegistry::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 703 | session_changes: Some(session_changes), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 704 | initial_history_limit: config.initial_history_limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 705 | room_command_capacity: config.command_capacity, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 706 | max_media_bytes: config.max_media_bytes, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 707 | media: Arc::new(Mutex::new(MediaRegistry::new(config.max_media_sources))), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 708 | notification_commands, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 709 | notification_command_receiver: Some(notification_command_receiver), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 710 | encryption_commands, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 711 | encryption_command_receiver: Some(encryption_command_receiver), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 712 | verification: Arc::new(Mutex::new(VerificationController::default())), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 713 | room_commands, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 714 | room_command_receiver: Some(room_command_receiver), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 715 | global_account_gates: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 716 | room_account_gates: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 717 | media_flights: Arc::new(Mutex::new(HashMap::new())), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 718 | stop: Arc::new(tokio::sync::Notify::new()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 719 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 720 | handle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 721 | events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 722 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 723 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 724 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 725 | pub async fn run(mut self) -> Result<(), EngineError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 726 | let sender = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 727 | let user_id = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 728 | .client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 729 | .user_id() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 730 | .expect("authentication was checked during construction") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 731 | .to_owned(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 732 | if sender.send(Event::Authenticated { user_id }).await.is_err() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 733 | self.shutdown().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 734 | return Err(EngineError::EventReceiverClosed); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 735 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 736 | self.port |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 737 | .connection_sender() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 738 | .send_replace(ConnectionState::Connecting); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 739 | if sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 740 | .send(Event::ConnectionChanged(ConnectionState::Connecting)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 741 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 742 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 743 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 744 | self.shutdown().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 745 | return Err(EngineError::EventReceiverClosed); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 746 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 747 | self.start_watchers(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 748 | self.start_domain_workers(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 749 | self.sync.start().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 750 | let mut terminal_error = None; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 751 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 752 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 753 | let activity = tokio::select! { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 754 | biased; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 755 | _ = sender.closed() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 756 | terminal_error = Some(EngineError::EventReceiverClosed); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 757 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 758 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 759 | () = self.stop.notified() => break, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 760 | request = self.port.recv() => EngineActivity::Request(request.map(Box::new)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 761 | completed = self.tasks.join_next(), if !self.tasks.is_empty() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 762 | EngineActivity::Task(completed) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 763 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 764 | event = self.room_actor_events.recv() => EngineActivity::RoomActor(event), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 765 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 766 | match activity { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 767 | EngineActivity::Request(Some(request)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 768 | let Some(envelope) = self.dispatch_request(*request).await else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 769 | continue; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 770 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 771 | let id = envelope.id; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 772 | let command = envelope.command; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 773 | if matches!(command, Command::Lifecycle(LifecycleCommand::Shutdown)) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 774 | let _ = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 775 | emit_command_result(&sender, id, CommandKind::Lifecycle, Ok(())).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 776 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 777 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 778 | let kind = command.kind(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 779 | tracing::debug!(?id, ?kind, "dispatching command"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 780 | let result = self.execute(id, command).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 781 | if Self::emit_sync_result(&sender, id, kind, result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 782 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 783 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 784 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 785 | terminal_error = Some(EngineError::EventReceiverClosed); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 786 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 787 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 788 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 789 | EngineActivity::Request(None) => break, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 790 | EngineActivity::Task(Some((context, result))) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 791 | if let Err(error) = result |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 792 | && error.is_panic() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 793 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 794 | match context { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 795 | TaskContext::Command { id, kind } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 796 | if sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 797 | .send(Event::CommandFailed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 798 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 799 | command: kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 800 | kind: CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 801 | message: "command task panicked".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 802 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 803 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 804 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 805 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 806 | terminal_error = Some(EngineError::EventReceiverClosed); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 807 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 808 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 809 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 810 | context => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 811 | terminal_error = Some(EngineError::TaskPanicked { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 812 | task: context.label(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 813 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 814 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 815 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 816 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 817 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 818 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 819 | EngineActivity::Task(None) => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 820 | EngineActivity::RoomActor(Some(event)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 821 | if let Err(error) = self.handle_room_actor_event(event).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 822 | terminal_error = Some(error); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 823 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 824 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 825 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 826 | EngineActivity::RoomActor(None) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 827 | terminal_error = Some(EngineError::TaskPanicked { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 828 | task: "room actor event channel".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 829 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 830 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 831 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 832 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 833 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 834 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 835 | if let Some(error) = terminal_error.as_ref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 836 | let _ = sender.send(Event::Failed(error.failure())).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 837 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 838 | self.shutdown().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 839 | let _ = sender.send(Event::Stopped).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 840 | if let Some(error) = terminal_error { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 841 | Err(error) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 842 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 843 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 844 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 845 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 846 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 847 | async fn emit_sync_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 848 | sender: &EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 849 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 850 | kind: CommandKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 851 | result: Result<bool, CommandError>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 852 | ) -> Result<(), SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 853 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 854 | Ok(true) => Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 855 | Ok(false) => emit_command_result(sender, id, kind, Ok(())).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 856 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 857 | tracing::warn!(?id, ?kind, error = ?error, "command failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 858 | emit_command_result(sender, id, kind, Err(error)).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 859 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 860 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 861 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 862 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 863 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 864 | fn spawn_logout(&mut self, id: CommandId) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 865 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 866 | let client = self.client.clone(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 867 | let stop = self.stop.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 868 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 869 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 870 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 871 | kind: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 872 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 873 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 874 | match client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 875 | .logout() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 876 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 877 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 878 | .map_err(|source| CommandError::from_sdk("logout failed", source)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 879 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 880 | Ok(()) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 881 | let _ = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 882 | emit_command_result(&events, id, CommandKind::Account, Ok(())).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 883 | let _ = events.send(Event::LoggedOut).await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 884 | stop.notify_one(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 885 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 886 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 887 | let _ = emit_command_result(&events, id, CommandKind::Account, Err(error)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 888 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 889 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 890 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 891 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 892 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 893 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 894 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 895 | async fn dispatch_request(&mut self, request: DriverRequest) -> Option<CommandEnvelope> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 896 | match request { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 897 | DriverRequest::Command(command) => Some(command), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 898 | DriverRequest::DeleteDevices { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 899 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 900 | device_ids, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 901 | auth, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 902 | reply, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 903 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 904 | self.spawn_delete_devices(id, device_ids, auth, Some(reply)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 905 | None |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 906 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 907 | DriverRequest::RoomResult { id, command, reply } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 908 | if let Err(error) = self.spawn_room_command_with_reply(id, command, Some(reply)) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 909 | tracing::warn!(?id, error = ?error, "room result request failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 910 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 911 | None |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 912 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 913 | DriverRequest::SetGlobalAccountData { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 914 | event_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 915 | content, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 916 | reply, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 917 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 918 | self.spawn_global_account_data_write(event_type, content, reply); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 919 | None |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 920 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 921 | DriverRequest::SetRoomAccountData { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 922 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 923 | event_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 924 | content, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 925 | reply, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 926 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 927 | self.spawn_room_account_data_write(room_id, event_type, content, reply); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 928 | None |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 929 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 930 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 931 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 932 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 933 | fn spawn_mark_read(&mut self, id: CommandId, timeline_id: TimelineId, event_id: OwnedEventId) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 934 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 935 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 936 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 937 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 938 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 939 | kind: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 940 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 941 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 942 | let result = mark_read(&client, &timeline_id, event_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 943 | let _ = emit_command_result(&events, id, CommandKind::Room, result).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 944 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 945 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 946 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 947 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 948 | fn spawn_typing(&mut self, id: CommandId, room_id: OwnedRoomId, typing: bool) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 949 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 950 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 951 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 952 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 953 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 954 | kind: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 955 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 956 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 957 | let result = set_typing(&client, &room_id, typing).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 958 | let _ = emit_command_result(&events, id, CommandKind::Room, result).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 959 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 960 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 961 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 962 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 963 | fn spawn_cancel_send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 964 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 965 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 966 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 967 | transaction_id: matrix_sdk::ruma::OwnedTransactionId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 968 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 969 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 970 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 971 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 972 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 973 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 974 | kind: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 975 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 976 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 977 | let result = cancel_send(&client, &room_id, &transaction_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 978 | let _ = emit_command_result(&events, id, CommandKind::Room, result).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 979 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 980 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 981 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 982 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 983 | fn spawn_retry_send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 984 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 985 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 986 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 987 | transaction_id: matrix_sdk::ruma::OwnedTransactionId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 988 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 989 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 990 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 991 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 992 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 993 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 994 | kind: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 995 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 996 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 997 | let result = retry_send(&client, &room_id, &transaction_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 998 | let _ = emit_command_result(&events, id, CommandKind::Room, result).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 999 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1000 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1001 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1002 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1003 | fn spawn_delete_devices( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1004 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1005 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1006 | device_ids: Vec<OwnedDeviceId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1007 | auth: Option<InteractiveAuth>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1008 | reply: Option<oneshot::Sender<Result<InteractiveAuthOutcome, CommandResultError>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1009 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1010 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1011 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1012 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1013 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1014 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1015 | kind: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1016 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1017 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1018 | let auth = match auth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1019 | Some(auth) => match auth.into_sdk( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1020 | client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1021 | .user_id() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1022 | .expect("authentication was checked during construction"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1023 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1024 | Ok(auth) => Some(auth), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1025 | Err(message) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1026 | let failure = CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1027 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1028 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1029 | kind: CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1030 | message: format!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1031 | "interactive authentication data is invalid, {message}" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1032 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1033 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1034 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1035 | let _ = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1036 | reply.send(Err(CommandResultError::Failed(failure.clone()))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1037 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1038 | let _ = events.send(Event::CommandFailed(failure)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1039 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1040 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1041 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1042 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1043 | None => None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1044 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1045 | let result = client.delete_devices(&device_ids, auth).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1046 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1047 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1048 | Ok(_) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1049 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1050 | let _ = reply.send(Ok(InteractiveAuthOutcome::Completed)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1051 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1052 | let _ = emit_command_result(&events, id, CommandKind::Account, Ok(())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1053 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1054 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1055 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1056 | Err(source) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1057 | if let Some(info) = source.as_uiaa_response() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1058 | let challenge = InteractiveAuthChallenge::from(info); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1059 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1060 | let _ = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1061 | reply.send(Ok(InteractiveAuthOutcome::Challenge(challenge))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1062 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1063 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1064 | .send(Event::InteractiveAuthRequired { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1065 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1066 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1067 | challenge: challenge.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1068 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1069 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1070 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1071 | .send(Event::CommandFailed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1072 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1073 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1074 | kind: CommandErrorKind::InteractiveAuthRequired, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1075 | message: "interactive authentication is required" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1076 | .to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1077 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1078 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1079 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1080 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1081 | let error = CommandError::from_http("deleting devices failed", source); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1082 | let failure = CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1083 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1084 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1085 | kind: error.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1086 | message: error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1087 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1088 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1089 | let _ = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1090 | reply.send(Err(CommandResultError::Failed(failure.clone()))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1091 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1092 | let _ = events.send(Event::CommandFailed(failure)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1093 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1094 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1095 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1096 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1097 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1098 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1099 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1100 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1101 | fn spawn_rename_device( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1102 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1103 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1104 | device_id: OwnedDeviceId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1105 | display_name: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1106 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1107 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1108 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1109 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1110 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1111 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1112 | kind: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1113 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1114 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1115 | match client.rename_device(&device_id, &display_name).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1116 | Ok(_) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1117 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1118 | .send(Event::CommandSucceeded { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1119 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1120 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1121 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1122 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1123 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1124 | Err(source) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1125 | let error = CommandError::from_http("renaming device failed", source); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1126 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1127 | .send(Event::CommandFailed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1128 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1129 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1130 | kind: error.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1131 | message: error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1132 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1133 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1134 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1135 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1136 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1137 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1138 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1139 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1140 | fn spawn_global_account_data_write( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1141 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1142 | event_type: GlobalAccountDataEventType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1143 | content: Raw<AnyGlobalAccountDataEventContent>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1144 | reply: oneshot::Sender<Result<(), ClientQueryError>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1145 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1146 | let (done, next) = oneshot::channel(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1147 | let previous = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1148 | .global_account_gates |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1149 | .insert(event_type.to_string(), next); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1150 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1151 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1152 | TaskContext::Watcher("global account data writer"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1153 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1154 | if let Some(previous) = previous { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1155 | let _ = previous.await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1156 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1157 | let result = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1158 | .account() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1159 | .set_account_data_raw(event_type, content) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1160 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1161 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1162 | .map_err(ClientQueryError::Sdk); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1163 | let _ = reply.send(result); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1164 | let _ = done.send(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1165 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1166 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1167 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1168 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1169 | fn spawn_room_account_data_write( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1170 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1171 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1172 | event_type: RoomAccountDataEventType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1173 | content: Raw<matrix_sdk::ruma::events::AnyRoomAccountDataEventContent>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1174 | reply: oneshot::Sender<Result<(), ClientQueryError>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1175 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1176 | let (done, next) = oneshot::channel(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1177 | let previous = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1178 | .room_account_gates |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1179 | .insert((room_id.clone(), event_type.to_string()), next); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1180 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1181 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1182 | TaskContext::Watcher("room account data writer"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1183 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1184 | if let Some(previous) = previous { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1185 | let _ = previous.await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1186 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1187 | let result = match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1188 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1189 | .set_account_data_raw(event_type, content) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1190 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1191 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1192 | .map_err(ClientQueryError::Sdk), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1193 | None => Err(ClientQueryError::RoomUnknown(room_id)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1194 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1195 | let _ = reply.send(result); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1196 | let _ = done.send(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1197 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1198 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1199 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1200 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1201 | fn start_domain_workers(&mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1202 | let mut notification_commands = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1203 | .notification_command_receiver |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1204 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1205 | .expect("notification worker has not started"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1206 | let notification_client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1207 | let notification_events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1208 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1209 | TaskContext::Watcher("notification command worker"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1210 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1211 | while let Some(CommandJob { id, command }) = notification_commands.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1212 | let result = AssertUnwindSafe(execute_notification_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1213 | ¬ification_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1214 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1215 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1216 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1217 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1218 | .unwrap_or_else(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1219 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1220 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1221 | "notification command panicked", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1222 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1223 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1224 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1225 | ¬ification_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1226 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1227 | CommandKind::Notification, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1228 | result, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1229 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1230 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1231 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1232 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1233 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1234 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1235 | let mut encryption_commands = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1236 | .encryption_command_receiver |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1237 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1238 | .expect("encryption worker has not started"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1239 | let encryption_client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1240 | let encryption_events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1241 | let verification = self.verification.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1242 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1243 | TaskContext::Watcher("encryption command worker"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1244 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1245 | while let Some(work) = encryption_commands.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1246 | match work { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1247 | EncryptionWork::Command(CommandJob { id, command }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1248 | if matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1249 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1250 | EncryptionCommand::Recover(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1251 | | EncryptionCommand::RecoverKeyBackup(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1252 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1253 | let result = AssertUnwindSafe(execute_encryption_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1254 | &encryption_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1255 | &encryption_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1256 | &mut VerificationController::default(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1257 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1258 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1259 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1260 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1261 | .unwrap_or_else(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1262 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1263 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1264 | "encryption command panicked", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1265 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1266 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1267 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1268 | &encryption_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1269 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1270 | CommandKind::Encryption, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1271 | result, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1272 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1273 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1274 | continue; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1275 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1276 | let is_key_backup = matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1277 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1278 | EncryptionCommand::CreateKeyBackup |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1279 | | EncryptionCommand::EnableKeyBackup |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1280 | | EncryptionCommand::DisableKeyBackup { .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1281 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1282 | let result = if is_key_backup { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1283 | AssertUnwindSafe(execute_key_backup_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1284 | &encryption_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1285 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1286 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1287 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1288 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1289 | .unwrap_or_else(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1290 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1291 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1292 | "encryption command panicked", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1293 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1294 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1295 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1296 | let mut verification = verification.lock().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1297 | let result = AssertUnwindSafe(execute_encryption_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1298 | &encryption_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1299 | &encryption_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1300 | &mut verification, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1301 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1302 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1303 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1304 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1305 | .unwrap_or_else(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1306 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1307 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1308 | "encryption command panicked", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1309 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1310 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1311 | drop(verification); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1312 | result |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1313 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1314 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1315 | &encryption_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1316 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1317 | CommandKind::Encryption, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1318 | result, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1319 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1320 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1321 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1322 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1323 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1324 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1325 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1326 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1327 | let room_commands = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1328 | .room_command_receiver |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1329 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1330 | .expect("room command worker has not started"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1331 | let room_client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1332 | let room_events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1333 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1334 | TaskContext::Watcher("room command worker"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1335 | run_room_command_worker( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1336 | room_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1337 | room_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1338 | room_commands, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1339 | self.room_command_capacity, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1340 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1341 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1342 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1343 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1344 | fn start_watchers(&mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1345 | let mut session_changes = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1346 | .session_changes |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1347 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1348 | .expect("session watcher has not started"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1349 | let session_sender = self.port.event_sender(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1350 | let stop = self.stop.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1351 | self.tasks |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1352 | .spawn(TaskContext::Watcher("session change watcher"), async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1353 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1354 | let event = match session_changes.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1355 | Ok(SessionChange::TokensRefreshed) => Event::SessionTokensChanged, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1356 | Ok(SessionChange::UnknownToken(data)) => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1357 | let _ = session_sender |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1358 | .send(Event::SessionAccessRejected { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1359 | soft_logout: data.soft_logout, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1360 | }) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1361 | .await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1362 | stop.notify_one(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1363 | break; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1364 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1365 | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1366 | Event::SessionTokensChanged |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1367 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1368 | Err(tokio::sync::broadcast::error::RecvError::Closed) => break, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1369 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1370 | if session_sender.send(event).await.is_err() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1371 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1372 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1373 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1374 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1375 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1376 | let mut sync_states = self.sync.state(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1377 | let state_sender = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1378 | let connection_sender = self.port.connection_sender(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1379 | let sync = self.sync.clone(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1380 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1381 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1382 | TaskContext::Watcher("connection state watcher"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1383 | async move { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1384 | let mut backoff = SYNC_RESTART_MIN; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1385 | while let Some(state) = sync_states.next().await { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1386 | let (state, restart) = match state { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1387 | SyncState::Idle => (ConnectionState::Idle, false), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1388 | SyncState::Running => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1389 | backoff = SYNC_RESTART_MIN; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1390 | // the sdk parks a room queue on any send error and never unparks it |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1391 | client.send_queue().set_enabled(true).await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1392 | (ConnectionState::Online, false) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1393 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1394 | SyncState::Offline => (ConnectionState::Offline, false), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1395 | SyncState::Terminated => (ConnectionState::Offline, true), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1396 | SyncState::Error(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1397 | tracing::warn!(error = %error, "sync service reported an error"); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1398 | (ConnectionState::Offline, true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1399 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1400 | }; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1401 | if *connection_sender.borrow() != state { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1402 | connection_sender.send_replace(state); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1403 | if state_sender |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1404 | .send(Event::ConnectionChanged(state)) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1405 | .await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1406 | .is_err() |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1407 | { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1408 | break; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1409 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1410 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1411 | if restart { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1412 | tokio::time::sleep(backoff).await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1413 | backoff = (backoff * 2).min(SYNC_RESTART_MAX); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1414 | sync.start().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1415 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1416 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1417 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1418 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1419 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1420 | let client = self.client.clone(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1421 | let online = self.port.connection_sender().subscribe(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1422 | self.tasks |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1423 | .spawn(TaskContext::Watcher("send queue watcher"), async move { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1424 | let mut errors = client.send_queue().subscribe_errors(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1425 | let mut retries = JoinSet::new(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1426 | loop { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1427 | let error = tokio::select! { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1428 | error = errors.recv() => error, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1429 | Some(_) = retries.join_next(), if !retries.is_empty() => continue, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1430 | }; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1431 | match error { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1432 | Ok(error) if error.is_recoverable => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1433 | let client = client.clone(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1434 | let online = online.clone(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1435 | retries.spawn(async move { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1436 | tokio::time::sleep(SEND_QUEUE_RETRY).await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1437 | if *online.borrow() == ConnectionState::Online |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1438 | && let Some(room) = client.get_room(&error.room_id) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1439 | { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1440 | room.send_queue().set_enabled(true); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1441 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1442 | }); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1443 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1444 | Ok(_) | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {} |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1445 | Err(tokio::sync::broadcast::error::RecvError::Closed) => break, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1446 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1447 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1448 | }); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1449 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1450 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1451 | let encryption_sender = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1452 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1453 | TaskContext::Watcher("encryption state watcher"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1454 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1455 | let Some(device_id) = client.device_id().map(ToOwned::to_owned) else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1456 | tracing::warn!("encryption watcher stopped because the client has no device id"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1457 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1458 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1459 | let recovery = client.encryption().recovery(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1460 | let backups = client.encryption().backups(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1461 | let mut recovery_stream = recovery.state_stream(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1462 | let mut backup_stream = backups.state_stream(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1463 | let mut verification_stream = client.encryption().verification_state(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1464 | let mut recovery_state = recovery.state(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1465 | let mut backup_state = backups.state(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1466 | let mut verification_state = verification_stream.get(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1467 | if encryption_sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1468 | .send(Event::EncryptionChanged(encryption_state( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1469 | device_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1470 | recovery_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1471 | verification_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1472 | backup_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1473 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1474 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1475 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1476 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1477 | tracing::debug!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1478 | "encryption watcher stopped because the event receiver closed" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1479 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1480 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1481 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1482 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1483 | tokio::select! { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1484 | update = recovery_stream.next() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1485 | let Some(update) = update else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1486 | tracing::debug!("encryption watcher stopped because the recovery stream ended"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1487 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1488 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1489 | recovery_state = update; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1490 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1491 | update = verification_stream.next() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1492 | let Some(update) = update else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1493 | tracing::debug!("encryption watcher stopped because the verification stream ended"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1494 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1495 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1496 | verification_state = update; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1497 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1498 | update = backup_stream.next() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1499 | let Some(update) = update else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1500 | tracing::debug!("backup watcher stopped because the backup state stream ended"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1501 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1502 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1503 | backup_state = update.unwrap_or_else(|_| backups.state()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1504 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1505 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1506 | if encryption_sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1507 | .send(Event::EncryptionChanged(encryption_state( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1508 | device_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1509 | recovery_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1510 | verification_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1511 | backup_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1512 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1513 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1514 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1515 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1516 | tracing::debug!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1517 | "encryption watcher stopped because the event receiver closed" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1518 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1519 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1520 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1521 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1522 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1523 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1524 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1525 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1526 | let room_sender = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1527 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1528 | TaskContext::Watcher("room list watcher"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1529 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1530 | let (_, mut room_stream) = client.rooms_stream(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1531 | let mut info_stream = client.room_info_notable_update_receiver(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1532 | if room_sender.send(Event::RoomsChanged).await.is_err() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1533 | tracing::debug!("room watcher stopped because the event receiver closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1534 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1535 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1536 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1537 | tokio::select! { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1538 | update = room_stream.next() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1539 | if update.is_none() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1540 | tracing::debug!("room watcher stopped because the room stream ended"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1541 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1542 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1543 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1544 | update = info_stream.recv() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1545 | if update.is_err() && info_stream.is_closed() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1546 | tracing::debug!("room watcher stopped because the room info stream ended"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1547 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1548 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1549 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1550 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1551 | if room_sender.send(Event::RoomsChanged).await.is_err() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1552 | tracing::debug!("room watcher stopped because the event receiver closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1553 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1554 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1555 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1556 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1557 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1558 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1559 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1560 | let notification_sender = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1561 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1562 | TaskContext::Watcher("notification settings watcher"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1563 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1564 | let settings = client.notification_settings().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1565 | let mut changes = settings.subscribe_to_changes(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1566 | while let Ok(()) | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1567 | changes.recv().await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1568 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1569 | if notification_sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1570 | .send(Event::NotificationSettingsChanged) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1571 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1572 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1573 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1574 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1575 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1576 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1577 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1578 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1579 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1580 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1581 | async fn execute(&mut self, id: CommandId, command: Command) -> Result<bool, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1582 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1583 | Command::Account(AccountCommand::Logout) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1584 | self.spawn_logout(id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1585 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1586 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1587 | Command::Account(AccountCommand::RenameDevice { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1588 | device_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1589 | display_name, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1590 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1591 | self.spawn_rename_device(id, device_id, display_name); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1592 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1593 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1594 | Command::Account(AccountCommand::DeleteDevices { device_ids, auth }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1595 | self.spawn_delete_devices(id, device_ids, auth, None); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1596 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1597 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1598 | Command::Room(RoomCommand::Open(room_id)) => self.open_room(id, room_id).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1599 | Command::Room(RoomCommand::Close(room_id)) => self.close_room(id, &room_id).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1600 | Command::Room(RoomCommand::SetTyping { room_id, typing }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1601 | self.spawn_typing(id, room_id, typing); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1602 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1603 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1604 | Command::Room(RoomCommand::Join { room, via }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1605 | self.spawn_room_command(id, RoomCommand::Join { room, via }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1606 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1607 | Command::Room(RoomCommand::Leave(room_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1608 | self.spawn_room_command(id, RoomCommand::Leave(room_id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1609 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1610 | Command::Room(RoomCommand::Forget(room_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1611 | self.spawn_room_command(id, RoomCommand::Forget(room_id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1612 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1613 | Command::Room(RoomCommand::AcceptInvite(room_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1614 | self.spawn_room_command(id, RoomCommand::AcceptInvite(room_id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1615 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1616 | Command::Room(RoomCommand::RejectInvite(room_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1617 | self.spawn_room_command(id, RoomCommand::RejectInvite(room_id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1618 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1619 | Command::Room(command @ RoomCommand::InviteUser { .. }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1620 | | Command::Room(command @ RoomCommand::KickUser { .. }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1621 | | Command::Room(command @ RoomCommand::BanUser { .. }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1622 | | Command::Room(command @ RoomCommand::UnbanUser { .. }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1623 | | Command::Room(command @ RoomCommand::SetPowerLevels { .. }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1624 | | Command::Room(command @ RoomCommand::SetUserPowerLevel { .. }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1625 | self.spawn_room_command(id, command) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1626 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1627 | Command::Room(RoomCommand::Create(options)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1628 | self.spawn_room_command(id, RoomCommand::Create(options)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1629 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1630 | Command::Room(RoomCommand::CreateDirect(user_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1631 | self.spawn_room_command(id, RoomCommand::CreateDirect(user_id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1632 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1633 | Command::Room(RoomCommand::CancelSend { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1634 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1635 | transaction_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1636 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1637 | self.spawn_cancel_send(id, room_id, transaction_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1638 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1639 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1640 | Command::Room(RoomCommand::RetrySend { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1641 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1642 | transaction_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1643 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1644 | self.spawn_retry_send(id, room_id, transaction_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1645 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1646 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1647 | Command::Timeline(command) => self.route_timeline(id, command), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1648 | Command::Media(MediaCommand::Fetch { id: media_id, size }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1649 | self.spawn_media_fetch(id, media_id, size); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1650 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1651 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1652 | Command::Media(MediaCommand::Upload { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1653 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1654 | filename, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1655 | mimetype, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1656 | bytes, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1657 | caption, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1658 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1659 | let mut upload = AttachmentUpload::new(timeline_id, filename, mimetype, bytes); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1660 | upload.caption = caption; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1661 | self.spawn_media_upload(id, upload).await.map(|()| true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1662 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1663 | Command::Media(MediaCommand::UploadAttachment(upload)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1664 | self.spawn_media_upload(id, upload).await.map(|()| true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1665 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1666 | Command::Encryption(command) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1667 | self.encryption_commands |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1668 | .try_send(EncryptionWork::Command(CommandJob { id, command })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1669 | .map_err(|error| domain_queue_error(error, "encryption"))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1670 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1671 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1672 | Command::Notification(command) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1673 | self.notification_commands |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1674 | .try_send(CommandJob { id, command }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1675 | .map_err(|error| domain_queue_error(error, "notification"))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1676 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1677 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1678 | Command::Lifecycle(LifecycleCommand::Shutdown) => Ok(false), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1679 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1680 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1681 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1682 | async fn open_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1683 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1684 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1685 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1686 | ) -> Result<bool, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1687 | match self.rooms.get_mut(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1688 | Some(RoomActorState::Opening { sender, .. }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1689 | send_room_actor_request(sender, RoomActorRequest::Opened { id })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1690 | return Ok(true); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1691 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1692 | Some(RoomActorState::Open { .. }) => return Ok(false), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1693 | Some(RoomActorState::Closing { pending, .. }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1694 | if pending.len() >= self.room_command_capacity { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1695 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1696 | CommandErrorKind::RoomBusy, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1697 | "room lifecycle queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1698 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1699 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1700 | pending.push_back(DeferredRoomLifecycle::Open(id)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1701 | return Ok(true); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1702 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1703 | None => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1704 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1705 | self.start_room_actor(room_id, vec![id]).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1706 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1707 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1708 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1709 | async fn start_room_actor( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1710 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1711 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1712 | open_ids: Vec<CommandId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1713 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1714 | let room = self.client.get_room(&room_id).ok_or_else(|| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1715 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1716 | CommandErrorKind::RoomUnknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1717 | "room is not available in the local store", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1718 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1719 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1720 | self.next_room_generation = self.next_room_generation.wrapping_add(1); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1721 | let generation = self.next_room_generation; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1722 | let (sender, receiver) = mpsc::channel(self.room_command_capacity); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1723 | let actor = RoomActor { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1724 | room_id: room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1725 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1726 | room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1727 | receiver, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1728 | events: self.port.event_sender(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1729 | engine_events: self.room_actor_event_sender.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1730 | media: self.media.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1731 | initial_history_limit: self.initial_history_limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1732 | open_ids, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1733 | background: JoinSet::new(), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1734 | background_ids: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1735 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1736 | let current_user_id = self.client.user_id().map(ToOwned::to_owned); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1737 | self.rooms.insert( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1738 | room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1739 | RoomActorState::Opening { generation, sender }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1740 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1741 | let actor_room_id = room_id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1742 | let engine_events = self.room_actor_event_sender.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1743 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1744 | TaskContext::RoomActor { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1745 | room_id: room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1746 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1747 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1748 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1749 | actor.run(current_user_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1750 | let _ = engine_events.send(RoomActorEvent::Exited { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1751 | room_id: actor_room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1752 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1753 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1754 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1755 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1756 | self.update_room_subscriptions().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1757 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1758 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1759 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1760 | fn spawn_room_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1761 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1762 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1763 | command: RoomCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1764 | ) -> Result<bool, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1765 | self.spawn_room_command_with_reply(id, command, None)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1766 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1767 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1768 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1769 | fn spawn_room_command_with_reply( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1770 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1771 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1772 | command: RoomCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1773 | reply: Option<oneshot::Sender<Result<OwnedRoomId, CommandResultError>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1774 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1775 | let job = RoomJob { id, command, reply }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1776 | self.room_commands.try_send(job).map_err(|error| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1777 | let (job, kind, message) = match error { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1778 | mpsc::error::TrySendError::Full(job) => ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1779 | job, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1780 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1781 | "room command queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1782 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1783 | mpsc::error::TrySendError::Closed(job) => ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1784 | job, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1785 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1786 | "room command worker has stopped", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1787 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1788 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1789 | let error = CommandError::new(kind, message); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1790 | if let Some(reply) = job.reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1791 | let _ = reply.send(Err(CommandResultError::Failed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1792 | id: job.id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1793 | command: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1794 | kind: error.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1795 | message: error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1796 | }))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1797 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1798 | error |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1799 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1800 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1801 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1802 | fn route_timeline( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1803 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1804 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1805 | command: TimelineCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1806 | ) -> Result<bool, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1807 | let sender = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1808 | .rooms |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1809 | .get(command.room_id()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1810 | .and_then(RoomActorState::sender) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1811 | .cloned(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1812 | match (sender, command) { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1813 | (Some(sender), command) => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1814 | send_room_actor_request(&sender, RoomActorRequest::Execute { id, command })?; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1815 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1816 | ( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1817 | None, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1818 | TimelineCommand::MarkRead { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1819 | timeline_id, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1820 | event_id, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1821 | }, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1822 | ) => self.spawn_mark_read(id, timeline_id, event_id), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1823 | (None, _) => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1824 | return Err(CommandError::new( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1825 | CommandErrorKind::RoomNotOpen, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1826 | "room is not open", |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1827 | )); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1828 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 1829 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1830 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1831 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1832 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1833 | async fn close_room(&mut self, id: CommandId, room_id: &RoomId) -> Result<bool, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1834 | let Some(state) = self.rooms.get_mut(room_id) else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1835 | return Ok(false); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1836 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1837 | if let RoomActorState::Closing { pending, .. } = state { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1838 | if pending.len() >= self.room_command_capacity { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1839 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1840 | CommandErrorKind::RoomBusy, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1841 | "room lifecycle queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1842 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1843 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1844 | pending.push_back(DeferredRoomLifecycle::Close(id)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1845 | return Ok(true); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1846 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1847 | let Some(sender) = state.sender().cloned() else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1848 | return Ok(false); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1849 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1850 | let generation = state.generation(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1851 | send_room_actor_request(&sender, RoomActorRequest::Close { id })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1852 | self.rooms.insert( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1853 | room_id.to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1854 | RoomActorState::Closing { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1855 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1856 | pending: VecDeque::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1857 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1858 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1859 | self.update_room_subscriptions().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1860 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1861 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1862 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1863 | async fn handle_room_actor_event(&mut self, event: RoomActorEvent) -> Result<(), EngineError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1864 | match event { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1865 | RoomActorEvent::Ready { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1866 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1867 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1868 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1869 | let stale = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1870 | .rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1871 | .get(&room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1872 | .is_none_or(|state| state.generation() != generation); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1873 | if stale { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1874 | return Ok(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1875 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1876 | let Some(state) = self.rooms.remove(&room_id) else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1877 | return Ok(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1878 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1879 | match state { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1880 | RoomActorState::Opening { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1881 | generation: current, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1882 | sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1883 | } if current == generation => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1884 | self.rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1885 | .insert(room_id, RoomActorState::Open { generation, sender }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1886 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1887 | state => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1888 | self.rooms.insert(room_id, state); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1889 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1890 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1891 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1892 | RoomActorEvent::Exited { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1893 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1894 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1895 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1896 | if self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1897 | .rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1898 | .get(&room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1899 | .is_none_or(|state| state.generation() != generation) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1900 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1901 | return Ok(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1902 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1903 | let state = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1904 | .rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1905 | .remove(&room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1906 | .expect("room generation was checked"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1907 | let pending = match state { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1908 | RoomActorState::Closing { pending, .. } => pending, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1909 | RoomActorState::Opening { .. } | RoomActorState::Open { .. } => VecDeque::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1910 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1911 | for operation in pending { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1912 | let (id, result) = match operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1913 | DeferredRoomLifecycle::Open(id) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1914 | (id, self.open_room(id, room_id.clone()).await) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1915 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1916 | DeferredRoomLifecycle::Close(id) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1917 | (id, self.close_room(id, &room_id).await) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1918 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1919 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1920 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1921 | Ok(true) => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1922 | Ok(false) => emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1923 | &self.port.event_sender(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1924 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1925 | CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1926 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1927 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1928 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1929 | .map_err(|_| EngineError::EventReceiverClosed)?, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1930 | Err(error) => emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1931 | &self.port.event_sender(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1932 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1933 | CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1934 | Err(error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1935 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1936 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1937 | .map_err(|_| EngineError::EventReceiverClosed)?, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1938 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1939 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1940 | self.update_room_subscriptions().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1941 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1942 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1943 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1944 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1945 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1946 | async fn update_room_subscriptions(&self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1947 | let room_ids = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1948 | .rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1949 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1950 | .filter(|(_, state)| !matches!(state, RoomActorState::Closing { .. })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1951 | .map(|(room_id, _)| room_id.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1952 | .collect::<Vec<_>>(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1953 | let room_refs = room_ids.iter().map(AsRef::as_ref).collect::<Vec<_>>(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1954 | self.sync |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1955 | .room_list_service() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1956 | .subscribe_to_rooms(&room_refs) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1957 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1958 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1959 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1960 | fn spawn_media_fetch(&mut self, command_id: CommandId, media_id: MediaId, size: MediaSize) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1961 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1962 | let media = self.media.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1963 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1964 | let max_media_bytes = self.max_media_bytes; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1965 | let flights = self.media_flights.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1966 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1967 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1968 | id: command_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1969 | kind: CommandKind::Media, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1970 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1971 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1972 | let leader = { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1973 | let mut flights = flights.lock().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1974 | let waiters = flights.entry((media_id.clone(), size)).or_default(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1975 | waiters.push(command_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1976 | waiters.len() == 1 |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1977 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1978 | if !leader { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1979 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1980 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1981 | let result = fetch_media( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1982 | client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1983 | media, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1984 | events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1985 | max_media_bytes, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1986 | media_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1987 | size, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1988 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1989 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1990 | let waiters = { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1991 | let mut flights = flights.lock().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1992 | flights.remove(&(media_id, size)).unwrap_or_default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1993 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1994 | let result = match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1995 | Ok(()) => Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1996 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1997 | let kind = error.kind; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1998 | let message = error.to_string(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1999 | Err((kind, message)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2000 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2001 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2002 | for waiter in waiters { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2003 | let waiter_result = match &result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2004 | Ok(()) => Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2005 | Err((kind, message)) => Err(CommandError::new(*kind, message.clone())), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2006 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2007 | if emit_command_result(&events, waiter, CommandKind::Media, waiter_result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2008 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2009 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2010 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2011 | tracing::debug!(?waiter, "media fetch result was not delivered"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2012 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2013 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2014 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2015 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2016 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2017 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2018 | async fn spawn_media_upload( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2019 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2020 | command_id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2021 | upload: AttachmentUpload, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2022 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2023 | let room_id = upload.timeline_id.room_id().clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2024 | let room = self.client.get_room(&room_id).ok_or_else(|| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2025 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2026 | CommandErrorKind::RoomUnknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2027 | "room is not available in the local store", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2028 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2029 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2030 | if self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2031 | .upload_watchers |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2032 | .get(&room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2033 | .is_some_and(|(watcher, _)| watcher.is_finished()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2034 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2035 | self.upload_watchers.remove(&room_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2036 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2037 | let ready = if let Some((_, ready)) = self.upload_watchers.get(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2038 | ready.clone() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2039 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2040 | let (ready_sender, ready) = watch::channel(None); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2041 | let watched_room_id = room_id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2042 | let progress_room_id = room_id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2043 | let queued_room = room.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2044 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2045 | let watcher = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2046 | self.tasks |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2047 | .spawn(TaskContext::Watcher("media upload watcher"), async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2048 | let (_, mut updates) = match queued_room.send_queue().subscribe().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2049 | Ok(subscription) => subscription, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2050 | Err(source) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2051 | tracing::warn!(room_id = %progress_room_id, error = %source, "reading media send queue failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2052 | ready_sender.send_replace(Some(Err(CommandErrorKind::Store))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2053 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2054 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2055 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2056 | ready_sender.send_replace(Some(Ok(()))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2057 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2058 | match updates.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2059 | Ok(RoomSendQueueUpdate::MediaUpload { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2060 | related_to, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2061 | file, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2062 | progress, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2063 | .. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2064 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2065 | if events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2066 | .send(Event::MediaUploadProgress { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2067 | room_id: progress_room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2068 | transaction_id: related_to, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2069 | uploaded_bytes: progress.current as u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2070 | total_bytes: progress.total as u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2071 | complete: file.is_some() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2072 | && progress.current == progress.total, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2073 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2074 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2075 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2076 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2077 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2078 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2079 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2080 | Ok(RoomSendQueueUpdate::SendError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2081 | transaction_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2082 | error, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2083 | is_recoverable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2084 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2085 | let is_attachment = queued_room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2086 | .send_queue() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2087 | .subscribe() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2088 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2089 | .is_ok_and(|(echoes, _)| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2090 | echoes.iter().any(|echo| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2091 | echo.transaction_id == transaction_id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2092 | && is_attachment_echo(&echo.content) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2093 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2094 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2095 | if is_attachment |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2096 | && events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2097 | .send(Event::MediaUploadFailed { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2098 | room_id: progress_room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2099 | transaction_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2100 | kind: classify_sdk_error(&error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2101 | recoverable: is_recoverable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2102 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2103 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2104 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2105 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2106 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2107 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2108 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2109 | Ok(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2110 | | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2111 | Err(tokio::sync::broadcast::error::RecvError::Closed) => break, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2112 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2113 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2114 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2115 | self.upload_watchers |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2116 | .insert(watched_room_id, (watcher, ready.clone())); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2117 | ready |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2118 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2119 | let max_media_bytes = self.max_media_bytes; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2120 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2121 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2122 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2123 | id: command_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2124 | kind: CommandKind::Media, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2125 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2126 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2127 | let mut ready = ready; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2128 | let result = async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2129 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2130 | if let Some(result) = *ready.borrow_and_update() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2131 | result.map_err(|kind| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2132 | CommandError::new(kind, "reading media send queue failed") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2133 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2134 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2135 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2136 | ready.changed().await.map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2137 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2138 | CommandErrorKind::Store, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2139 | "media send queue watcher stopped", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2140 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2141 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2142 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2143 | queue_media_upload(room, upload, max_media_bytes).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2144 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2145 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2146 | if emit_command_result(&events, command_id, CommandKind::Media, result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2147 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2148 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2149 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2150 | tracing::debug!(?command_id, "media upload result was not delivered"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2151 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2152 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2153 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2154 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2155 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2156 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2157 | async fn shutdown(&mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2158 | self.tasks.shutdown().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2159 | self.verification.lock().await.stop().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2160 | self.rooms.clear(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2161 | self.upload_watchers.clear(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2162 | let mut media = self.media.lock().await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2163 | media.entries.clear(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2164 | media.order.clear(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2165 | drop(media); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2166 | self.sync.stop().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2167 | self.port |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2168 | .connection_sender() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2169 | .send_replace(ConnectionState::Stopped); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2170 | let _ = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2171 | .port |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2172 | .send(Event::ConnectionChanged(ConnectionState::Stopped)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2173 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2174 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2175 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2176 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2177 | async fn mark_read( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2178 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2179 | timeline_id: &TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2180 | event_id: OwnedEventId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2181 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2182 | let room = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2183 | .get_room(timeline_id.room_id()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2184 | .ok_or_else(room_unknown)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2185 | let receipt_thread = match timeline_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2186 | TimelineId::Room(_) => ReceiptThread::Main, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2187 | TimelineId::Thread { root_event_id, .. } => ReceiptThread::Thread(root_event_id.clone()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2188 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2189 | room.send_single_receipt(ReceiptType::Read, receipt_thread, event_id.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2190 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2191 | .map_err(|source| CommandError::from_sdk("sending read receipt failed", source))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2192 | if matches!(timeline_id, TimelineId::Room(_)) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2193 | room.send_single_receipt(ReceiptType::FullyRead, ReceiptThread::Unthreaded, event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2194 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2195 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2196 | CommandError::from_sdk("sending fully read receipt failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2197 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2198 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2199 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2200 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2201 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2202 | async fn set_typing(client: &Client, room_id: &RoomId, typing: bool) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2203 | let room = client.get_room(room_id).ok_or_else(room_unknown)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2204 | room.typing_notice(typing) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2205 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2206 | .map_err(|source| CommandError::from_sdk("typing update failed", source)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2207 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2208 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2209 | fn is_attachment_echo(content: &LocalEchoContent) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2210 | let LocalEchoContent::Event { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2211 | serialized_event, .. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2212 | } = content |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2213 | else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2214 | return false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2215 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2216 | matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2217 | serialized_event.deserialize(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2218 | Ok(AnyMessageLikeEventContent::RoomMessage(content)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2219 | if matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2220 | content.msgtype, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2221 | MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2222 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2223 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2224 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2225 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2226 | async fn send_handle( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2227 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2228 | room_id: &RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2229 | transaction_id: &matrix_sdk::ruma::TransactionId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2230 | ) -> Result<SendHandle, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2231 | let room = client.get_room(room_id).ok_or_else(room_unknown)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2232 | let (echoes, _) = room.send_queue().subscribe().await.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2233 | CommandError::with_source(CommandErrorKind::Store, "reading send queue failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2234 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2235 | echoes |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2236 | .into_iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2237 | .find_map(|echo| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2238 | if echo.transaction_id.as_str() != transaction_id.as_str() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2239 | return None; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2240 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2241 | match echo.content { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2242 | LocalEchoContent::Event { send_handle, .. } => Some(send_handle), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2243 | _ => None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2244 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2245 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2246 | .ok_or_else(|| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2247 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2248 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2249 | "send is no longer pending or is not a message", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2250 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2251 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2252 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2253 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2254 | async fn cancel_send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2255 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2256 | room_id: &RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2257 | transaction_id: &matrix_sdk::ruma::TransactionId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2258 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2259 | let handle = send_handle(client, room_id, transaction_id).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2260 | match handle.abort().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2261 | Ok(true) => Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2262 | Ok(false) => Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2263 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2264 | "send could not be cancelled because it has already been sent", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2265 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2266 | Err(source) => Err(CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2267 | CommandErrorKind::Store, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2268 | "cancelling send failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2269 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2270 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2271 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2272 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2273 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2274 | async fn retry_send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2275 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2276 | room_id: &RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2277 | transaction_id: &matrix_sdk::ruma::TransactionId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2278 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2279 | let handle = send_handle(client, room_id, transaction_id).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2280 | handle.unwedge().await.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2281 | CommandError::with_source(CommandErrorKind::Store, "retrying send failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2282 | })?; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2283 | if let Some(room) = client.get_room(room_id) { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2284 | room.send_queue().set_enabled(true); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2285 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2286 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2287 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2288 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2289 | fn send_room_actor_request( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2290 | sender: &mpsc::Sender<RoomActorRequest>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2291 | request: RoomActorRequest, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2292 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2293 | sender.try_send(request).map_err(|error| match error { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2294 | mpsc::error::TrySendError::Full(_) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2295 | CommandError::new(CommandErrorKind::RoomBusy, "room command queue is full") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2296 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2297 | mpsc::error::TrySendError::Closed(_) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2298 | CommandError::new(CommandErrorKind::RoomNotOpen, "room is not open") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2299 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2300 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2301 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2302 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2303 | fn domain_queue_error<T>(error: mpsc::error::TrySendError<T>, domain: &str) -> CommandError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2304 | match error { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2305 | mpsc::error::TrySendError::Full(_) => CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2306 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2307 | format!("{domain} command queue is full"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2308 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2309 | mpsc::error::TrySendError::Closed(_) => CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2310 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2311 | format!("{domain} command worker has stopped"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2312 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2313 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2314 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2315 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2316 | async fn run_room_command_worker( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2317 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2318 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2319 | mut commands: mpsc::Receiver<RoomJob>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2320 | queue_limit: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2321 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2322 | let mut queues = HashMap::<RoomMutationKey, VecDeque<RoomJob>>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2323 | let mut running = HashSet::<RoomMutationKey>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2324 | let mut tasks = JoinSet::<RoomJobCompletion>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2325 | let mut receiving = true; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2326 | let mut queued = 0_usize; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2327 | while receiving || !tasks.is_empty() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2328 | tokio::select! { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2329 | biased; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2330 | completed = tasks.join_next(), if !tasks.is_empty() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2331 | let completion = match completed { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2332 | Some(Ok(completion)) => completion, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2333 | Some(Err(error)) => panic!("room command task failed: {error}"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2334 | None => continue, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2335 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2336 | let key = completion.key.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2337 | emit_room_job_result(&events, completion).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2338 | let next = queues.get_mut(&key).and_then(VecDeque::pop_front); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2339 | if next.is_some() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2340 | queued -= 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2341 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2342 | if queues.get(&key).is_some_and(VecDeque::is_empty) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2343 | queues.remove(&key); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2344 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2345 | if let Some(job) = next { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2346 | spawn_room_job(&mut tasks, client.clone(), key, job); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2347 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2348 | running.remove(&key); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2349 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2350 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2351 | job = commands.recv(), if receiving => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2352 | match job { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2353 | Some(job) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2354 | let key = room_mutation_key(job.id, &job.command); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2355 | if running.insert(key.clone()) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2356 | spawn_room_job(&mut tasks, client.clone(), key, job); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2357 | } else if queued < queue_limit { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2358 | queues.entry(key).or_default().push_back(job); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2359 | queued += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2360 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2361 | let failure = CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2362 | id: job.id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2363 | command: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2364 | kind: CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2365 | message: "room command queue is full".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2366 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2367 | if let Some(reply) = job.reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2368 | let _ = reply.send(Err(CommandResultError::Failed(failure))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2369 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2370 | let _ = events.send(Event::CommandFailed(failure)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2371 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2372 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2373 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2374 | None => receiving = false, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2375 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2376 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2377 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2378 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2379 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2380 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2381 | fn spawn_room_job( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2382 | tasks: &mut JoinSet<RoomJobCompletion>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2383 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2384 | key: RoomMutationKey, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2385 | job: RoomJob, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2386 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2387 | tasks.spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2388 | let RoomJob { id, command, reply } = job; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2389 | let result = AssertUnwindSafe(execute_room_command(&client, command)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2390 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2391 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2392 | .unwrap_or_else(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2393 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2394 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2395 | "room command panicked", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2396 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2397 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2398 | RoomJobCompletion { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2399 | key, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2400 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2401 | reply, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2402 | result, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2403 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2404 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2405 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2406 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2407 | fn room_mutation_key(id: CommandId, command: &RoomCommand) -> RoomMutationKey { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2408 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2409 | RoomCommand::Join { room, .. } if room.is_room_id() => RoomMutationKey::Room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2410 | OwnedRoomId::try_from(room.clone()).expect("room ID variant was checked"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2411 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2412 | RoomCommand::Join { room, .. } => RoomMutationKey::Target(room.to_string()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2413 | RoomCommand::Leave(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2414 | | RoomCommand::Forget(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2415 | | RoomCommand::AcceptInvite(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2416 | | RoomCommand::RejectInvite(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2417 | | RoomCommand::InviteUser { room_id, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2418 | | RoomCommand::KickUser { room_id, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2419 | | RoomCommand::BanUser { room_id, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2420 | | RoomCommand::UnbanUser { room_id, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2421 | | RoomCommand::SetPowerLevels { room_id, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2422 | | RoomCommand::SetUserPowerLevel { room_id, .. } => RoomMutationKey::Room(room_id.clone()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2423 | RoomCommand::CreateDirect(user_id) => RoomMutationKey::Direct(user_id.clone()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2424 | _ => RoomMutationKey::Independent(id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2425 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2426 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2427 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2428 | async fn emit_room_job_result(events: &EventSender, completion: RoomJobCompletion) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2429 | let RoomJobCompletion { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2430 | id, reply, result, .. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2431 | } = completion; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2432 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2433 | Ok(Some(room_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2434 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2435 | let _ = reply.send(Ok(room_id.clone())); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2436 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2437 | let _ = emit_command_result_with_room(events, id, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2438 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2439 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2440 | Ok(None) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2441 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2442 | let _ = reply.send(Err(CommandResultError::Failed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2443 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2444 | command: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2445 | kind: CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2446 | message: "room operation did not return a room ID".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2447 | }))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2448 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2449 | let _ = emit_command_result(events, id, CommandKind::Room, Ok(())).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2450 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2451 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2452 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2453 | let failure = CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2454 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2455 | command: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2456 | kind: error.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2457 | message: error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2458 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2459 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2460 | let _ = reply.send(Err(CommandResultError::Failed(failure))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2461 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2462 | let _ = events.send(Event::CommandFailed(failure)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2463 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2464 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2465 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2466 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2467 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2468 | async fn execute_room_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2469 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2470 | command: RoomCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2471 | ) -> Result<Option<OwnedRoomId>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2472 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2473 | RoomCommand::Join { room, via } => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2474 | .join_room_by_id_or_alias(&room, &via) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2475 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2476 | .map(|room| Some(room.room_id().to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2477 | .map_err(|source| CommandError::from_sdk("joining room failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2478 | RoomCommand::Leave(room_id) => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2479 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2480 | .leave() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2481 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2482 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2483 | .map_err(|source| CommandError::from_sdk("leaving room failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2484 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2485 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2486 | RoomCommand::Forget(room_id) => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2487 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2488 | .forget() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2489 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2490 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2491 | .map_err(|source| CommandError::from_sdk("forgetting room failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2492 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2493 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2494 | RoomCommand::AcceptInvite(room_id) => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2495 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2496 | .join() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2497 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2498 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2499 | .map_err(|source| CommandError::from_sdk("accepting invite failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2500 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2501 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2502 | RoomCommand::RejectInvite(room_id) => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2503 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2504 | .leave() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2505 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2506 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2507 | .map_err(|source| CommandError::from_sdk("rejecting invite failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2508 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2509 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2510 | RoomCommand::InviteUser { room_id, user_id } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2511 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2512 | .invite_user_by_id(&user_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2513 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2514 | .map(|()| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2515 | .map_err(|source| CommandError::from_sdk("inviting user failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2516 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2517 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2518 | RoomCommand::KickUser { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2519 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2520 | user_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2521 | reason, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2522 | } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2523 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2524 | .kick_user(&user_id, reason.as_deref()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2525 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2526 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2527 | .map_err(|source| CommandError::from_sdk("kicking user failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2528 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2529 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2530 | RoomCommand::BanUser { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2531 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2532 | user_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2533 | reason, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2534 | } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2535 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2536 | .ban_user(&user_id, reason.as_deref()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2537 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2538 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2539 | .map_err(|source| CommandError::from_sdk("banning user failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2540 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2541 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2542 | RoomCommand::UnbanUser { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2543 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2544 | user_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2545 | reason, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2546 | } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2547 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2548 | .unban_user(&user_id, reason.as_deref()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2549 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2550 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2551 | .map_err(|source| CommandError::from_sdk("unbanning user failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2552 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2553 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2554 | RoomCommand::SetPowerLevels { room_id, changes } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2555 | Some(room) => apply_power_level_changes(&room, changes) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2556 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2557 | .map(|_| None), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2558 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2559 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2560 | RoomCommand::SetUserPowerLevel { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2561 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2562 | user_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2563 | level, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2564 | } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2565 | Some(room) => match matrix_sdk::ruma::Int::try_from(level) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2566 | Ok(level) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2567 | .update_power_levels(vec![(&user_id, level)]) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2568 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2569 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2570 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2571 | CommandError::from_sdk("updating user power level failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2572 | }), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2573 | Err(_) => Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2574 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2575 | "power level is outside the Matrix integer range", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2576 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2577 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2578 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2579 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2580 | RoomCommand::Create(options) => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2581 | .create_room(create_room_request(options)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2582 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2583 | .map(|room| Some(room.room_id().to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2584 | .map_err(|source| CommandError::from_sdk("creating room failed", source)), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2585 | RoomCommand::CreateDirect(user_id) => match client.get_dm_room(&user_id) { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2586 | Some(room) => Ok(Some(room.room_id().to_owned())), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2587 | None => client |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2588 | .create_dm(&user_id) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2589 | .await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2590 | .map(|room| Some(room.room_id().to_owned())) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2591 | .map_err(|source| CommandError::from_sdk("creating direct room failed", source)), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2592 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2593 | _ => Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2594 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2595 | "unsupported room command", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2596 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2597 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2598 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2599 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2600 | fn room_unknown() -> CommandError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2601 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2602 | CommandErrorKind::RoomUnknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2603 | "room is not available in the local store", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2604 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2605 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2606 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2607 | const fn sdk_notification_mode( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2608 | mode: RoomNotificationMode, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2609 | ) -> matrix_sdk::notification_settings::RoomNotificationMode { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2610 | match mode { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2611 | RoomNotificationMode::AllMessages => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2612 | matrix_sdk::notification_settings::RoomNotificationMode::AllMessages |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2613 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2614 | RoomNotificationMode::MentionsAndKeywordsOnly => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2615 | matrix_sdk::notification_settings::RoomNotificationMode::MentionsAndKeywordsOnly |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2616 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2617 | RoomNotificationMode::Mute => matrix_sdk::notification_settings::RoomNotificationMode::Mute, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2618 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2619 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2620 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2621 | async fn execute_notification_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2622 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2623 | command: NotificationCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2624 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2625 | use matrix_sdk::notification_settings::{IsEncrypted, IsOneToOne}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2626 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2627 | let settings = client.notification_settings().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2628 | let result = match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2629 | NotificationCommand::SetRoomMode { room_id, mode } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2630 | settings |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2631 | .set_room_notification_mode(&room_id, sdk_notification_mode(mode)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2632 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2633 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2634 | NotificationCommand::ResetRoomMode(room_id) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2635 | settings.delete_user_defined_room_rules(&room_id).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2636 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2637 | NotificationCommand::SetDefaultMode { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2638 | encrypted, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2639 | one_to_one, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2640 | mode, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2641 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2642 | settings |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2643 | .set_default_room_notification_mode( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2644 | IsEncrypted::from(encrypted), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2645 | IsOneToOne::from(one_to_one), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2646 | sdk_notification_mode(mode), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2647 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2648 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2649 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2650 | NotificationCommand::AddKeyword(keyword) => settings.add_keyword(keyword).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2651 | NotificationCommand::RemoveKeyword(keyword) => settings.remove_keyword(&keyword).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2652 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2653 | result.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2654 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2655 | CommandErrorKind::Server, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2656 | "updating notification settings failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2657 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2658 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2659 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2660 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2661 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2662 | async fn execute_encryption_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2663 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2664 | events: &EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2665 | verification: &mut VerificationController, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2666 | command: EncryptionCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2667 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2668 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2669 | EncryptionCommand::Recover(key) => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2670 | .encryption() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2671 | .recovery() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2672 | .recover(&key) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2673 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2674 | .map_err(|source| CommandError::from_recovery("account recovery failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2675 | EncryptionCommand::StartVerification(target) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2676 | verification.start(client, events.clone(), target).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2677 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2678 | EncryptionCommand::Verify(action) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2679 | verification.apply(client, events.clone(), action).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2680 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2681 | command => execute_key_backup_command(client, command).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2682 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2683 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2684 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2685 | async fn execute_key_backup_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2686 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2687 | command: EncryptionCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2688 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2689 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2690 | EncryptionCommand::CreateKeyBackup => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2691 | .encryption() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2692 | .backups() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2693 | .create() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2694 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2695 | .map_err(|source| CommandError::from_sdk("creating key backup failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2696 | EncryptionCommand::EnableKeyBackup => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2697 | .encryption() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2698 | .recovery() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2699 | .enable_backup() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2700 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2701 | .map_err(|source| CommandError::from_recovery("enabling key backup failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2702 | EncryptionCommand::DisableKeyBackup { delete_from_server } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2703 | let backups = client.encryption().backups(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2704 | if delete_from_server { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2705 | backups.disable_and_delete().await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2706 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2707 | backups.disable().await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2708 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2709 | .map_err(|source| CommandError::from_sdk("disabling key backup failed", source)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2710 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2711 | EncryptionCommand::RecoverKeyBackup(key) => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2712 | .encryption() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2713 | .recovery() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2714 | .recover_and_fix_backup(&key) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2715 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2716 | .map_err(|source| CommandError::from_recovery("recovering key backup failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2717 | _ => unreachable!("only key backup commands are spawned here"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2718 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2719 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2720 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2721 | async fn apply_power_level_changes( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2722 | room: &Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2723 | changes: PowerLevelChanges, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2724 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2725 | use matrix_sdk::ruma::events::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2726 | MessageLikeEventType, StateEventType, room::power_levels::RoomPowerLevelsEventContent, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2727 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2728 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2729 | let mut levels = room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2730 | .power_levels() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2731 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2732 | .map_err(|source| CommandError::from_sdk("reading power levels failed", source.into()))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2733 | if let Some(value) = changes.ban { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2734 | levels.ban = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2735 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2736 | if let Some(value) = changes.invite { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2737 | levels.invite = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2738 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2739 | if let Some(value) = changes.kick { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2740 | levels.kick = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2741 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2742 | if let Some(value) = changes.redact { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2743 | levels.redact = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2744 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2745 | if let Some(value) = changes.events_default { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2746 | levels.events_default = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2747 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2748 | if let Some(value) = changes.state_default { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2749 | levels.state_default = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2750 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2751 | if let Some(value) = changes.users_default { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2752 | levels.users_default = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2753 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2754 | if let Some(value) = changes.room_notification { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2755 | levels.notifications.room = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2756 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2757 | for (event_type, value) in [ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2758 | (StateEventType::RoomName.into(), changes.room_name), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2759 | (StateEventType::RoomAvatar.into(), changes.room_avatar), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2760 | (StateEventType::RoomTopic.into(), changes.room_topic), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2761 | (StateEventType::SpaceChild.into(), changes.space_child), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2762 | (MessageLikeEventType::Beacon.into(), changes.beacon), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2763 | (StateEventType::BeaconInfo.into(), changes.beacon_info), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2764 | ] { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2765 | if let Some(value) = value { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2766 | levels.events.insert(event_type, matrix_power_level(value)?); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2767 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2768 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2769 | let content = RoomPowerLevelsEventContent::try_from(levels).map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2770 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2771 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2772 | "normalizing power levels failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2773 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2774 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2775 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2776 | room.send_state_event(content) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2777 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2778 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2779 | .map_err(|source| CommandError::from_sdk("updating power levels failed", source)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2780 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2781 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2782 | fn matrix_power_level(value: i64) -> Result<matrix_sdk::ruma::Int, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2783 | value.try_into().map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2784 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2785 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2786 | "power level is outside the Matrix integer range", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2787 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2788 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2789 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2790 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2791 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2792 | fn create_room_request( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2793 | options: CreateRoomOptions, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2794 | ) -> matrix_sdk::ruma::api::client::room::create_room::v3::Request { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2795 | use matrix_sdk::ruma::api::client::room::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2796 | Visibility as SdkVisibility, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2797 | create_room::v3::{Request, RoomPreset as SdkPreset}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2798 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2799 | let mut request = Request::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2800 | request.name = options.name; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2801 | request.topic = options.topic; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2802 | request.room_alias_name = options.alias_localpart; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2803 | request.invite = options.invite; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2804 | request.is_direct = options.direct; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2805 | request.visibility = match options.visibility { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2806 | RoomVisibility::Private => SdkVisibility::Private, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2807 | RoomVisibility::Public => SdkVisibility::Public, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2808 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2809 | request.preset = options.preset.map(|preset| match preset { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2810 | RoomPreset::PrivateChat => SdkPreset::PrivateChat, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2811 | RoomPreset::TrustedPrivateChat => SdkPreset::TrustedPrivateChat, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2812 | RoomPreset::PublicChat => SdkPreset::PublicChat, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2813 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2814 | request |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2815 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2816 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2817 | async fn queue_media_upload( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2818 | room: Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2819 | upload: AttachmentUpload, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2820 | max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2821 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2822 | let (upload, mime, mut config) = prepare_upload(upload, max_media_bytes)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2823 | if let TimelineId::Thread { root_event_id, .. } = &upload.timeline_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2824 | config.reply = Some(SdkReply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2825 | event_id: root_event_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2826 | enforce_thread: EnforceThread::Threaded(ReplyWithinThread::No), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2827 | add_mentions: AddMentions::No, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2828 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2829 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2830 | room.send_queue() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2831 | .send_attachment(upload.filename, mime, upload.bytes, config) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2832 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2833 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2834 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2835 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2836 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2837 | "media upload could not be queued", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2838 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2839 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2840 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2841 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2842 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2843 | async fn fetch_media( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2844 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2845 | registry: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2846 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2847 | max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2848 | media_id: MediaId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2849 | size: MediaSize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2850 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2851 | let media = registry |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2852 | .lock() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2853 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2854 | .get(&media_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2855 | .cloned() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2856 | .ok_or_else(|| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2857 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2858 | CommandErrorKind::MediaUnknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2859 | "media is not registered in a retained timeline", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2860 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2861 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2862 | let (source, format, mimetype, declared_size) = match size { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2863 | MediaSize::Full => ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2864 | media.full.source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2865 | MediaFormat::File, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2866 | media.full.mimetype, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2867 | media.full.declared_size, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2868 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2869 | MediaSize::Thumbnail => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2870 | if let Some(thumbnail) = media.thumbnail { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2871 | ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2872 | thumbnail.source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2873 | MediaFormat::File, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2874 | thumbnail.mimetype, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2875 | thumbnail.declared_size, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2876 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2877 | } else if matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2878 | media.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2879 | crate::MediaKind::Image | crate::MediaKind::Video |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2880 | ) && matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2881 | &media.full.source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2882 | matrix_sdk::ruma::events::room::MediaSource::Plain(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2883 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2884 | ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2885 | media.full.source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2886 | MediaFormat::Thumbnail(MediaThumbnailSettings::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2887 | 800_u32.into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2888 | 800_u32.into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2889 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2890 | media.full.mimetype, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2891 | None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2892 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2893 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2894 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2895 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2896 | "the requested media size is unavailable", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2897 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2898 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2899 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2900 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2901 | if declared_size.is_some_and(|size| size > max_media_bytes as u64) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2902 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2903 | CommandErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2904 | "media exceeds the configured byte limit", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2905 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2906 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2907 | let request = MediaRequestParameters { source, format }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2908 | if let Some(cached) = cached_media(&client, &request).await? { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2909 | if cached.len() > max_media_bytes { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2910 | remove_oversize_media(&client, &request).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2911 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2912 | CommandErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2913 | "cached media exceeds the configured byte limit", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2914 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2915 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2916 | events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2917 | .send(Event::MediaReady { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2918 | media_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2919 | size, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2920 | bytes: Arc::from(cached), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2921 | mimetype: mimetype.unwrap_or_else(|| "application/octet-stream".into()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2922 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2923 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2924 | .map_err(send_error)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2925 | return Ok(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2926 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2927 | let bytes = download_bounded(&client, &request, max_media_bytes).await?; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2928 | // the sdk caches plaintext under the encrypted key, so we do too or nobody can read anything |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2929 | let bytes = maybe_decrypt_media(bytes, &request, max_media_bytes)?; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 2930 | cache_media(&client, &request, &bytes).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2931 | events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2932 | .send(Event::MediaReady { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2933 | media_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2934 | size, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2935 | bytes: Arc::from(bytes), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2936 | mimetype: mimetype.unwrap_or_else(|| "application/octet-stream".into()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2937 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2938 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2939 | .map_err(send_error) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2940 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2941 | async fn cached_media( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2942 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2943 | request: &MediaRequestParameters, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2944 | ) -> Result<Option<Vec<u8>>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2945 | client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2946 | .media_store() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2947 | .lock() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2948 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2949 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2950 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2951 | CommandErrorKind::Store, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2952 | "reading media cache failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2953 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2954 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2955 | })? |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2956 | .get_media_content(request) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2957 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2958 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2959 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2960 | CommandErrorKind::Store, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2961 | "reading media cache failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2962 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2963 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2964 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2965 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2966 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2967 | async fn remove_oversize_media(client: &Client, request: &MediaRequestParameters) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2968 | if let Ok(store) = client.media_store().lock().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2969 | let _ = store.remove_media_content(request).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2970 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2971 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2972 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2973 | async fn cache_media(client: &Client, request: &MediaRequestParameters, bytes: &[u8]) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2974 | if let Ok(store) = client.media_store().lock().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2975 | let _ = store |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2976 | .add_media_content( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2977 | request, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2978 | bytes.to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2979 | matrix_sdk::media::store::IgnoreMediaRetentionPolicy::No, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2980 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2981 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2982 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2983 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2984 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2985 | fn maybe_decrypt_media( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2986 | bytes: Vec<u8>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2987 | request: &MediaRequestParameters, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2988 | max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2989 | ) -> Result<Vec<u8>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2990 | let matrix_sdk::ruma::events::room::MediaSource::Encrypted(file) = &request.source else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2991 | return Ok(bytes); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2992 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2993 | let info: MediaEncryptionInfo = (**file).clone().into(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2994 | let mut cursor = std::io::Cursor::new(bytes); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2995 | let mut decryptor = AttachmentDecryptor::new(&mut cursor, info).map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2996 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2997 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2998 | "media decryption failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2999 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3000 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3001 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3002 | let mut decrypted = Vec::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3003 | let mut chunk = [0_u8; 8192]; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3004 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3005 | match decryptor.read(&mut chunk) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3006 | Ok(0) => break, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3007 | Ok(read) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3008 | if read > max_media_bytes.saturating_sub(decrypted.len()) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3009 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3010 | CommandErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3011 | "decrypted media exceeds the configured byte limit", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3012 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3013 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3014 | decrypted.extend_from_slice(&chunk[..read]); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3015 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3016 | Err(source) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3017 | return Err(CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3018 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3019 | "media decryption failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3020 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3021 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3022 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3023 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3024 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3025 | Ok(decrypted) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3026 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3027 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3028 | async fn download_bounded( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3029 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3030 | request: &MediaRequestParameters, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3031 | max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3032 | ) -> Result<Vec<u8>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3033 | if is_local_media(&request.source) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3034 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3035 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3036 | "local media is not in the media cache", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3037 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3038 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3039 | let mut refreshed = false; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3040 | let request = loop { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3041 | let http_request = media_http_request(client, request).await?; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3042 | let response = client |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3043 | .http_client() |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3044 | .execute(request_from_http(client, http_request)?) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3045 | .await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3046 | .map_err(|source| { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3047 | CommandError::with_source( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3048 | CommandErrorKind::Network, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3049 | "media download failed", |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3050 | source, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3051 | ) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3052 | })?; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3053 | if response.status() != matrix_sdk::reqwest::StatusCode::UNAUTHORIZED { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3054 | break response; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3055 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3056 | if refreshed |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3057 | || client |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3058 | .session_tokens() |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3059 | .is_none_or(|tokens| tokens.refresh_token.is_none()) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3060 | { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3061 | return Err(CommandError::new( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3062 | CommandErrorKind::Unauthenticated, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3063 | "media download was not authorized", |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3064 | )); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3065 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3066 | client.refresh_access_token().await.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3067 | CommandError::from_refresh_token("refreshing media access token failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3068 | })?; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3069 | refreshed = true; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3070 | }; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3071 | let status = request.status(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3072 | if !status.is_success() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3073 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3074 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3075 | format!("media download failed with status {status}"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3076 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3077 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3078 | if request |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3079 | .content_length() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3080 | .is_some_and(|length| length > max_media_bytes as u64) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3081 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3082 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3083 | CommandErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3084 | "media exceeds the configured byte limit", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3085 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3086 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3087 | let mut stream = request.bytes_stream(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3088 | let mut bytes = Vec::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3089 | while let Some(chunk) = stream.next().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3090 | let chunk = chunk.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3091 | CommandError::with_source(CommandErrorKind::Network, "media download failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3092 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3093 | if chunk.len() > max_media_bytes.saturating_sub(bytes.len()) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3094 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3095 | CommandErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3096 | "downloaded media exceeds the configured byte limit", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3097 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3098 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3099 | bytes.extend_from_slice(&chunk); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3100 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3101 | Ok(bytes) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3102 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3103 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3104 | fn is_local_media(source: &matrix_sdk::ruma::events::room::MediaSource) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3105 | let uri = match source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3106 | matrix_sdk::ruma::events::room::MediaSource::Plain(uri) => uri, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3107 | matrix_sdk::ruma::events::room::MediaSource::Encrypted(file) => &file.url, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3108 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3109 | let Ok(server) = uri.server_name() else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3110 | return false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3111 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3112 | server.as_str() == "send-queue.localhost" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3113 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3114 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3115 | async fn media_http_request( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3116 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3117 | request: &MediaRequestParameters, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3118 | ) -> Result<http::Request<Vec<u8>>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3119 | use matrix_sdk::ruma::api::client::{authenticated_media, media}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3120 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3121 | let versions = client.supported_versions().await.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3122 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3123 | CommandErrorKind::Network, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3124 | "reading supported server versions failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3125 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3126 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3127 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3128 | let use_auth = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3129 | authenticated_media::get_content::v1::Request::PATH_BUILDER.is_supported(&versions); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3130 | let base = client.homeserver().to_string(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3131 | let token = client.access_token(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3132 | let build = |request: http::Request<Vec<u8>>| -> Result<http::Request<Vec<u8>>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3133 | let mut request = request; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3134 | if let Some(token) = token.as_deref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3135 | request.headers_mut().insert( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3136 | http::header::AUTHORIZATION, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3137 | format!("Bearer {token}").parse().map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3138 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3139 | CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3140 | "stored access token is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3141 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3142 | })?, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3143 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3144 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3145 | Ok(request) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3146 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3147 | match &request.source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3148 | matrix_sdk::ruma::events::room::MediaSource::Encrypted(file) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3149 | if use_auth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3150 | let endpoint = authenticated_media::get_content::v1::Request::from_uri(&file.url) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3151 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3152 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3153 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3154 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3155 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3156 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3157 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3158 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3159 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3160 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3161 | SendAccessToken::IfRequired(token.as_deref().unwrap_or("")), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3162 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3163 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3164 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3165 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3166 | CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3167 | "media request could not be authorized", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3168 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3169 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3170 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3171 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3172 | #[allow(deprecated)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3173 | let endpoint = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3174 | media::get_content::v3::Request::from_url(&file.url).map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3175 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3176 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3177 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3178 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3179 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3180 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3181 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3182 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3183 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3184 | SendAccessToken::None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3185 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3186 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3187 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3188 | CommandError::new(CommandErrorKind::Other, "media request is invalid") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3189 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3190 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3191 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3192 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3193 | matrix_sdk::ruma::events::room::MediaSource::Plain(uri) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3194 | if let MediaFormat::Thumbnail(settings) = &request.format { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3195 | if use_auth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3196 | let mut endpoint = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3197 | authenticated_media::get_content_thumbnail::v1::Request::from_uri( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3198 | uri, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3199 | settings.width, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3200 | settings.height, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3201 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3202 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3203 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3204 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3205 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3206 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3207 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3208 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3209 | endpoint.method = Some(settings.method.clone()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3210 | endpoint.animated = Some(settings.animated); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3211 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3212 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3213 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3214 | SendAccessToken::IfRequired(token.as_deref().unwrap_or("")), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3215 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3216 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3217 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3218 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3219 | CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3220 | "media request could not be authorized", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3221 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3222 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3223 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3224 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3225 | #[allow(deprecated)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3226 | let endpoint = { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3227 | let mut endpoint = media::get_content_thumbnail::v3::Request::from_url( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3228 | uri, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3229 | settings.width, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3230 | settings.height, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3231 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3232 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3233 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3234 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3235 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3236 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3237 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3238 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3239 | endpoint.method = Some(settings.method.clone()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3240 | endpoint.animated = Some(settings.animated); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3241 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3242 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3243 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3244 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3245 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3246 | SendAccessToken::None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3247 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3248 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3249 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3250 | CommandError::new(CommandErrorKind::Other, "media request is invalid") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3251 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3252 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3253 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3254 | } else if use_auth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3255 | let endpoint = authenticated_media::get_content::v1::Request::from_uri(uri) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3256 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3257 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3258 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3259 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3260 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3261 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3262 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3263 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3264 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3265 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3266 | SendAccessToken::IfRequired(token.as_deref().unwrap_or("")), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3267 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3268 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3269 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3270 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3271 | CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3272 | "media request could not be authorized", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3273 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3274 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3275 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3276 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3277 | #[allow(deprecated)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3278 | let endpoint = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3279 | media::get_content::v3::Request::from_url(uri).map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3280 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3281 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3282 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3283 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3284 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3285 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3286 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3287 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3288 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3289 | SendAccessToken::None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3290 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3291 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3292 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3293 | CommandError::new(CommandErrorKind::Other, "media request is invalid") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3294 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3295 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3296 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3297 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3298 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3299 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3300 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3301 | fn request_from_http( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3302 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3303 | request: http::Request<Vec<u8>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3304 | ) -> Result<matrix_sdk::reqwest::Request, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3305 | let (parts, _) = request.into_parts(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3306 | let mut builder = client.http_client().get(parts.uri.to_string()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3307 | for (name, value) in parts.headers.iter() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3308 | builder = builder.header(name.as_str(), value.as_bytes()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3309 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3310 | builder |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3311 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3312 | .map_err(|_| CommandError::new(CommandErrorKind::Network, "media request is invalid")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3313 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3314 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3315 | impl RoomActor { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3316 | async fn finish( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3317 | &mut self, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3318 | id: CommandId, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3319 | outcome: TimelineOutcome, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3320 | ) -> Result<(), SendEventError> { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3321 | match outcome { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3322 | TimelineOutcome::Done(result) => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3323 | emit_command_result(&self.events, id, CommandKind::Timeline, result).await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3324 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3325 | TimelineOutcome::Background(work) => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3326 | let task = self.background.spawn(async move { (id, work.await) }); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3327 | self.background_ids.insert(task.id(), id); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3328 | Ok(()) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3329 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3330 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3331 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3332 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3333 | async fn reap(&mut self, completed: BackgroundResult) -> Result<(), SendEventError> { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3334 | let (id, result) = match completed { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3335 | Ok((task_id, completed)) => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3336 | self.background_ids.remove(&task_id); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3337 | completed |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3338 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3339 | Err(error) => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3340 | let Some(id) = self.background_ids.remove(&error.id()) else { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3341 | return Ok(()); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3342 | }; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3343 | ( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3344 | id, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3345 | Err(CommandError::new( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3346 | CommandErrorKind::Other, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3347 | "timeline operation panicked", |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3348 | )), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3349 | ) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3350 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3351 | }; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3352 | emit_command_result(&self.events, id, CommandKind::Timeline, result).await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3353 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3354 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3355 | async fn run(mut self, current_user_id: Option<ruma::OwnedUserId>) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3356 | let timeline = match self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3357 | .room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3358 | .timeline_builder() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3359 | .with_focus(TimelineFocus::Live { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3360 | hide_threaded_events: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3361 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3362 | .track_read_marker_and_receipts(TimelineReadReceiptTracking::AllEvents) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3363 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3364 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3365 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3366 | Ok(timeline) => timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3367 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3368 | tracing::warn!(error = %error, room_id = %self.room_id, "room actor could not build its timeline"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3369 | let message = format!("room timeline build failed: {error}"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3370 | for id in self.open_ids.drain(..) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3371 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3372 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3373 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3374 | CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3375 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3376 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3377 | message.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3378 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3379 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3380 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3381 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3382 | self.receiver.close(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3383 | while let Some(request) = self.receiver.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3384 | let (id, command) = match request { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3385 | RoomActorRequest::Opened { id } | RoomActorRequest::Close { id } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3386 | (id, CommandKind::Room) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3387 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3388 | RoomActorRequest::Execute { id, .. } => (id, CommandKind::Timeline), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3389 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3390 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3391 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3392 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3393 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3394 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3395 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3396 | "room timeline build failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3397 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3398 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3399 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3400 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3401 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3402 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3403 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3404 | let room_timeline_id = TimelineId::Room(self.room_id.clone()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3405 | let mut room_timeline = match TimelineSession::start( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3406 | room_timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3407 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3408 | self.events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3409 | self.media.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3410 | current_user_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3411 | self.initial_history_limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3412 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3413 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3414 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3415 | Ok(timeline) => timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3416 | Err(_) => return, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3417 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3418 | let _ = self.engine_events.send(RoomActorEvent::Ready { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3419 | room_id: self.room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3420 | generation: self.generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3421 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3422 | for id in self.open_ids.drain(..) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3423 | if emit_command_result(&self.events, id, CommandKind::Room, Ok(())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3424 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3425 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3426 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3427 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3428 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3429 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3430 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3431 | let (typing_guard, mut typing_rx) = self.room.subscribe_to_typing_notifications(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3432 | let typing_sender = self.events.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3433 | let typing_room_id = self.room_id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3434 | let mut room_tasks = JoinSet::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3435 | room_tasks.spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3436 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3437 | match typing_rx.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3438 | Ok(users) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3439 | let state = TypingState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3440 | room_id: typing_room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3441 | users, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3442 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3443 | if typing_sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3444 | .send(Event::TypingChanged(state)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3445 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3446 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3447 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3448 | tracing::debug!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3449 | room_id = %typing_room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3450 | "typing watcher stopped because the event receiver closed" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3451 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3452 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3453 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3454 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3455 | Err(tokio::sync::broadcast::error::RecvError::Lagged(skipped)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3456 | tracing::warn!(room_id = %typing_room_id, skipped, "typing watcher lagged"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3457 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3458 | Err(tokio::sync::broadcast::error::RecvError::Closed) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3459 | tracing::debug!(room_id = %typing_room_id, "typing watcher stopped"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3460 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3461 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3462 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3463 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3464 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3465 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3466 | enum QueuedThreadCommand { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3467 | Open { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3468 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3469 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3470 | Execute { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3471 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3472 | command: TimelineCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3473 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3474 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3475 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3476 | struct OpeningThread { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3477 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3478 | abort: AbortHandle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3479 | task_id: TaskId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3480 | queued: VecDeque<QueuedThreadCommand>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3481 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3482 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3483 | let mut thread_timelines = HashMap::<OwnedEventId, TimelineSession>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3484 | let mut opening_threads = HashMap::<OwnedEventId, OpeningThread>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3485 | let mut thread_task_ids = HashMap::<TaskId, OwnedEventId>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3486 | let mut thread_open_tasks = JoinSet::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3487 | let mut next_thread_generation = 0_u64; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3488 | let mut close_id = None; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3489 | let thread_queue_cap = self.receiver.max_capacity(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3490 | let mut queued_thread_count = 0; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3491 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3492 | let mut room_info = self.room.subscribe_info(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3493 | 'actor: loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3494 | tokio::select! { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3495 | Some(info) = room_info.next() => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3496 | if matches!(info.state(), matrix_sdk::RoomState::Left | matrix_sdk::RoomState::Banned) { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3497 | tracing::debug!(room_id = %self.room_id, "room actor stopped because we left the room"); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3498 | break 'actor; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3499 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3500 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3501 | request = self.receiver.recv() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3502 | let Some(request) = request else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3503 | tracing::debug!(room_id = %self.room_id, "room actor stopped because its request channel closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3504 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3505 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3506 | match request { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3507 | RoomActorRequest::Opened { id } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3508 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3509 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3510 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3511 | CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3512 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3513 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3514 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3515 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3516 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3517 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3518 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3519 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3520 | RoomActorRequest::Execute { id, command } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3521 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3522 | TimelineCommand::OpenThread { root_event_id, .. } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3523 | if thread_timelines.contains_key(&root_event_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3524 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3525 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3526 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3527 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3528 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3529 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3530 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3531 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3532 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3533 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3534 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3535 | } else if let Some(opening) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3536 | opening_threads.get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3537 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3538 | if queued_thread_count >= thread_queue_cap { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3539 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3540 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3541 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3542 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3543 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3544 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3545 | "thread command queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3546 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3547 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3548 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3549 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3550 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3551 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3552 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3553 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3554 | opening |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3555 | .queued |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3556 | .push_back(QueuedThreadCommand::Open { id }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3557 | queued_thread_count += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3558 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3559 | } else if queued_thread_count >= thread_queue_cap { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3560 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3561 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3562 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3563 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3564 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3565 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3566 | "thread command queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3567 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3568 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3569 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3570 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3571 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3572 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3573 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3574 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3575 | let generation = next_thread_generation; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3576 | next_thread_generation = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3577 | next_thread_generation.wrapping_add(1); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3578 | let task_root = root_event_id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3579 | let room = self.room.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3580 | let abort = thread_open_tasks.spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3581 | let result = room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3582 | .timeline_builder() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3583 | .with_focus(TimelineFocus::Thread { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3584 | root_event_id: root_event_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3585 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3586 | .track_read_marker_and_receipts( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3587 | TimelineReadReceiptTracking::AllEvents, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3588 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3589 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3590 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3591 | (root_event_id, generation, result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3592 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3593 | let task_id = abort.id(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3594 | thread_task_ids.insert(task_id, task_root.clone()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3595 | opening_threads.insert( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3596 | task_root, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3597 | OpeningThread { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3598 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3599 | abort, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3600 | task_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3601 | queued: VecDeque::from([ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3602 | QueuedThreadCommand::Open { id }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3603 | ]), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3604 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3605 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3606 | queued_thread_count += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3607 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3608 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3609 | TimelineCommand::CloseThread { root_event_id, .. } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3610 | if let Some(timeline) = thread_timelines.remove(&root_event_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3611 | timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3612 | .close( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3613 | CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3614 | "thread closed while pagination was in progress", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3615 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3616 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3617 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3618 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3619 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3620 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3621 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3622 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3623 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3624 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3625 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3626 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3627 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3628 | } else if let Some(opening) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3629 | opening_threads.remove(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3630 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3631 | thread_task_ids.remove(&opening.task_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3632 | queued_thread_count -= opening.queued.len(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3633 | opening.abort.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3634 | for queued in opening.queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3635 | let queued_id = match queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3636 | QueuedThreadCommand::Open { id } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3637 | QueuedThreadCommand::Execute { id, .. } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3638 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3639 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3640 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3641 | queued_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3642 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3643 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3644 | CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3645 | "thread was closed before it finished opening", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3646 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3647 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3648 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3649 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3650 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3651 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3652 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3653 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3654 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3655 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3656 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3657 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3658 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3659 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3660 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3661 | } else if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3662 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3663 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3664 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3665 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3666 | CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3667 | "thread timeline is not open", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3668 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3669 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3670 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3671 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3672 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3673 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3674 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3675 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3676 | TimelineCommand::Paginate { timeline_id, limit } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3677 | match timeline_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3678 | TimelineId::Room(_) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3679 | room_timeline.paginate(limit, Some(id)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3680 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3681 | TimelineId::Thread { room_id, root_event_id } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3682 | if let Some(timeline) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3683 | thread_timelines.get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3684 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3685 | timeline.paginate(limit, Some(id)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3686 | } else if let Some(opening) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3687 | opening_threads.get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3688 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3689 | if queued_thread_count >= thread_queue_cap { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3690 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3691 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3692 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3693 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3694 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3695 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3696 | "thread command queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3697 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3698 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3699 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3700 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3701 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3702 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3703 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3704 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3705 | opening.queued.push_back( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3706 | QueuedThreadCommand::Execute { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3707 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3708 | command: |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3709 | TimelineCommand::Paginate { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3710 | timeline_id: |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3711 | TimelineId::Thread { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3712 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3713 | root_event_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3714 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3715 | limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3716 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3717 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3718 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3719 | queued_thread_count += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3720 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3721 | } else if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3722 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3723 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3724 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3725 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3726 | CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3727 | "timeline is not open", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3728 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3729 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3730 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3731 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3732 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3733 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3734 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3735 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3736 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3737 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3738 | command => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3739 | let thread_target = match command |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3740 | .timeline_id() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3741 | .expect("non-lifecycle timeline command has a timeline id") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3742 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3743 | TimelineId::Room(_) => None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3744 | TimelineId::Thread { root_event_id, .. } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3745 | Some(root_event_id.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3746 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3747 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3748 | match thread_target { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3749 | None => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3750 | let result = execute_timeline_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3751 | &mut room_timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3752 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3753 | None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3754 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3755 | .await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3756 | if self.finish(id, result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3757 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3758 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3759 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3760 | tracing::debug!(room_id = %self.room_id, "room actor stopped because the event receiver closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3761 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3762 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3763 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3764 | Some(root_event_id) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3765 | if let Some(timeline) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3766 | thread_timelines.get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3767 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3768 | let thread_root = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3769 | timeline.id.thread_root().cloned(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3770 | let result = execute_timeline_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3771 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3772 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3773 | thread_root.as_ref(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3774 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3775 | .await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3776 | if self.finish(id, result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3777 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3778 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3779 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3780 | tracing::debug!(room_id = %self.room_id, "room actor stopped because the event receiver closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3781 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3782 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3783 | } else if let Some(opening) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3784 | opening_threads.get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3785 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3786 | if queued_thread_count >= thread_queue_cap { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3787 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3788 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3789 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3790 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3791 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3792 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3793 | "thread command queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3794 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3795 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3796 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3797 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3798 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3799 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3800 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3801 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3802 | opening.queued.push_back( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3803 | QueuedThreadCommand::Execute { id, command }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3804 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3805 | queued_thread_count += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3806 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3807 | } else if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3808 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3809 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3810 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3811 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3812 | CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3813 | "timeline is not open", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3814 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3815 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3816 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3817 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3818 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3819 | tracing::debug!(room_id = %self.room_id, "room actor stopped because the event receiver closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3820 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3821 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3822 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3823 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3824 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3825 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3826 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3827 | RoomActorRequest::Close { id } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3828 | close_id = Some(id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3829 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3830 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3831 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3832 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3833 | completed = thread_open_tasks.join_next(), if !thread_open_tasks.is_empty() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3834 | match completed { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3835 | Some(Ok((root_event_id, generation, result))) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3836 | if opening_threads |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3837 | .get(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3838 | .is_none_or(|opening| opening.generation != generation) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3839 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3840 | continue; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3841 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3842 | let opening = opening_threads |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3843 | .remove(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3844 | .expect("thread generation was checked"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3845 | thread_task_ids.remove(&opening.task_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3846 | queued_thread_count -= opening.queued.len(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3847 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3848 | Ok(timeline) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3849 | let timeline_id = TimelineId::thread( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3850 | self.room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3851 | root_event_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3852 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3853 | match TimelineSession::start( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3854 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3855 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3856 | self.events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3857 | self.media.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3858 | current_user_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3859 | self.initial_history_limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3860 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3861 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3862 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3863 | Ok(session) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3864 | thread_timelines |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3865 | .insert(root_event_id.clone(), session); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3866 | let timeline = thread_timelines |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3867 | .get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3868 | .expect("thread session was inserted"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3869 | for queued in opening.queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3870 | match queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3871 | QueuedThreadCommand::Open { id } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3872 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3873 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3874 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3875 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3876 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3877 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3878 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3879 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3880 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3881 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3882 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3883 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3884 | QueuedThreadCommand::Execute { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3885 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3886 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3887 | } => match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3888 | TimelineCommand::Paginate { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3889 | limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3890 | .. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3891 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3892 | timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3893 | .paginate(limit, Some(id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3894 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3895 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3896 | command => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3897 | let thread_root = timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3898 | .id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3899 | .thread_root() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3900 | .cloned(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3901 | let result = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3902 | execute_timeline_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3903 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3904 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3905 | thread_root.as_ref(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3906 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3907 | .await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3908 | if self.finish(id, result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3909 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3910 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3911 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3912 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3913 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3914 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3915 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3916 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3917 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3918 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3919 | Err(_) => break 'actor, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3920 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3921 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3922 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3923 | tracing::warn!(error = %error, room_id = %self.room_id, %root_event_id, "thread timeline build failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3924 | let message = format!("thread timeline build failed: {error}"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3925 | for queued in opening.queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3926 | let queued_id = match queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3927 | QueuedThreadCommand::Open { id } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3928 | QueuedThreadCommand::Execute { id, .. } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3929 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3930 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3931 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3932 | queued_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3933 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3934 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3935 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3936 | message.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3937 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3938 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3939 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3940 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3941 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3942 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3943 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3944 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3945 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3946 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3947 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3948 | Some(Err(error)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3949 | if let Some(root_event_id) = thread_task_ids.remove(&error.id()) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3950 | tracing::warn!(error = %error, room_id = %self.room_id, %root_event_id, "thread open task failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3951 | if let Some(opening) = opening_threads.remove(&root_event_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3952 | queued_thread_count -= opening.queued.len(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3953 | for queued in opening.queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3954 | let queued_id = match queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3955 | QueuedThreadCommand::Open { id } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3956 | QueuedThreadCommand::Execute { id, .. } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3957 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3958 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3959 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3960 | queued_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3961 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3962 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3963 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3964 | "thread timeline build task failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3965 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3966 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3967 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3968 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3969 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3970 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3971 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3972 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3973 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3974 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3975 | tracing::debug!(error = %error, room_id = %self.room_id, "thread open task was aborted"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3976 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3977 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3978 | None => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3979 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3980 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3981 | Some(completed) = self.background.join_next_with_id(), if !self.background.is_empty() => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3982 | if self.reap(completed).await.is_err() { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3983 | break 'actor; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3984 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 3985 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3986 | completed = room_tasks.join_next(), if !room_tasks.is_empty() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3987 | match completed { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3988 | Some(Err(error)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3989 | tracing::warn!(error = %error, room_id = %self.room_id, "room actor task failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3990 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3991 | Some(Ok(())) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3992 | tracing::warn!(room_id = %self.room_id, "room actor task stopped unexpectedly"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3993 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3994 | None => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3995 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3996 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3997 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3998 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3999 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4000 | while let Some(completed) = self.background.join_next_with_id().await { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4001 | let _ = self.reap(completed).await; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4002 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4003 | drop(typing_guard); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4004 | room_tasks.abort_all(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4005 | while room_tasks.join_next().await.is_some() {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4006 | thread_task_ids.clear(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4007 | let pending_opens = opening_threads |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4008 | .drain() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4009 | .flat_map(|(_, opening)| opening.queued) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4010 | .collect::<Vec<_>>(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4011 | for queued in pending_opens { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4012 | let queued_id = match queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4013 | QueuedThreadCommand::Open { id } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4014 | QueuedThreadCommand::Execute { id, .. } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4015 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4016 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4017 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4018 | queued_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4019 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4020 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4021 | CommandErrorKind::RoomNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4022 | "room closed while thread timeline was opening", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4023 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4024 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4025 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4026 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4027 | thread_open_tasks.abort_all(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4028 | while thread_open_tasks.join_next().await.is_some() {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4029 | for (_, timeline) in thread_timelines { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4030 | timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4031 | .close( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4032 | CommandErrorKind::RoomNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4033 | "room closed while thread pagination was in progress", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4034 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4035 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4036 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4037 | room_timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4038 | .close( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4039 | CommandErrorKind::RoomNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4040 | "room closed while pagination was in progress", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4041 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4042 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4043 | if let Some(id) = close_id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4044 | && emit_command_result(&self.events, id, CommandKind::Room, Ok(())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4045 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4046 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4047 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4048 | tracing::debug!(room_id = %self.room_id, "room close acknowledgement was not delivered"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4049 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4050 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4051 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4052 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4053 | async fn execute_timeline_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4054 | session: &mut TimelineSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4055 | command: TimelineCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4056 | thread_root: Option<&OwnedEventId>, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4057 | ) -> TimelineOutcome { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4058 | let timeline = session.timeline.clone(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4059 | let work: BackgroundWork = match command { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4060 | TimelineCommand::Redact { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4061 | event_id, reason, .. |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4062 | } => Box::pin(async move { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4063 | timeline |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4064 | .redact(&TimelineEventItemId::EventId(event_id), reason.as_deref()) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4065 | .await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4066 | .map_err(timeline_error) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4067 | }), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4068 | TimelineCommand::ToggleReaction { event_id, key, .. } => Box::pin(async move { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4069 | timeline |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4070 | .toggle_reaction(&TimelineEventItemId::EventId(event_id), &key) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4071 | .await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4072 | .map(|_| ()) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4073 | .map_err(timeline_error) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4074 | }), |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4075 | TimelineCommand::MarkRead { event_id, .. } => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4076 | let thread = thread_root.is_some(); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4077 | Box::pin(async move { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4078 | let receipt_failed = |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4079 | |source| CommandError::from_sdk("sending read receipt failed", source); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4080 | timeline |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4081 | .send_single_receipt(ReceiptType::Read, event_id.clone()) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4082 | .await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4083 | .map_err(receipt_failed)?; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4084 | if !thread { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4085 | timeline |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4086 | .send_single_receipt(ReceiptType::FullyRead, event_id) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4087 | .await |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4088 | .map_err(receipt_failed)?; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4089 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4090 | Ok(()) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4091 | }) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4092 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4093 | command => { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4094 | return TimelineOutcome::Done( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4095 | execute_serial_timeline_command(session, command, thread_root).await, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4096 | ); |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4097 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4098 | }; |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4099 | TimelineOutcome::Background(work) |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4100 | } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4101 | |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4102 | async fn execute_serial_timeline_command( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4103 | session: &mut TimelineSession, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4104 | command: TimelineCommand, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4105 | thread_root: Option<&OwnedEventId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4106 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4107 | let timeline = &session.timeline; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4108 | match command { |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4109 | TimelineCommand::OpenThread { .. } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4110 | | TimelineCommand::CloseThread { .. } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4111 | | TimelineCommand::Redact { .. } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4112 | | TimelineCommand::ToggleReaction { .. } |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4113 | | TimelineCommand::MarkRead { .. } => Err(CommandError::new( |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4114 | CommandErrorKind::Other, |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4115 | "timeline command was misrouted", |
fa9114fcf75f | atagen | 2026-09-24 12:51 UTC | 4116 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4117 | TimelineCommand::Resync { .. } => session.resync().await.map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4118 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4119 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4120 | "timeline resync could not emit a reset", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4121 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4122 | }), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4123 | TimelineCommand::Paginate { .. } => Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4124 | TimelineCommand::Send { content, .. } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4125 | let body = match &content { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4126 | MessageContent::Text { content, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4127 | | MessageContent::Emote { content, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4128 | | MessageContent::Reply { content, .. } => &content.body, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4129 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4130 | if body.trim().is_empty() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4131 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4132 | CommandErrorKind::EmptyBody, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4133 | "message body is empty", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4134 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4135 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4136 | match content { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4137 | MessageContent::Text { content, mentions } => timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4138 | .send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4139 | message_content(content, mentions, false) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4140 | .with_relation(None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4141 | .into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4142 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4143 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4144 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4145 | .map_err(timeline_error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4146 | MessageContent::Emote { content, mentions } => timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4147 | .send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4148 | message_content(content, mentions, true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4149 | .with_relation(None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4150 | .into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4151 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4152 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4153 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4154 | .map_err(timeline_error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4155 | MessageContent::Reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4156 | event_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4157 | content, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4158 | mentions, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4159 | notify_replied_to, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4160 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4161 | let content = timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4162 | .room() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4163 | .make_reply_event( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4164 | message_content(content, mentions, false), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4165 | SdkReply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4166 | event_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4167 | enforce_thread: if thread_root.is_some() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4168 | EnforceThread::Threaded(ReplyWithinThread::Yes) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4169 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4170 | EnforceThread::MaybeThreaded |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4171 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4172 | add_mentions: if notify_replied_to { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4173 | AddMentions::Yes |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4174 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4175 | AddMentions::No |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4176 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4177 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4178 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4179 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4180 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4181 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4182 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4183 | "reply operation failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4184 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4185 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4186 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4187 | if let Some(thread_root) = thread_root |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4188 | && !matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4189 | content.relates_to.as_ref(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4190 | Some(Relation::Thread(thread)) if &thread.event_id == thread_root |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4191 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4192 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4193 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4194 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4195 | "reply target does not belong to the selected thread", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4196 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4197 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4198 | timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4199 | .send(content.into()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4200 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4201 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4202 | .map_err(timeline_error) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4203 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4204 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4205 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4206 | TimelineCommand::Edit { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4207 | event_id, content, .. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4208 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4209 | let (content, mentions, emote) = match content { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4210 | MessageEditContent::Text { content, mentions } => (content, mentions, false), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4211 | MessageEditContent::Emote { content, mentions } => (content, mentions, true), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4212 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4213 | if content.body.trim().is_empty() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4214 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4215 | CommandErrorKind::EmptyBody, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4216 | "message body is empty", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4217 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4218 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4219 | timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4220 | .edit( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4221 | &TimelineEventItemId::EventId(event_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4222 | EditedContent::RoomMessage(message_content(content, mentions, emote)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4223 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4224 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4225 | .map_err(timeline_error) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4226 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4227 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4228 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4229 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4230 | fn message_content( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4231 | content: RichText, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4232 | mentions: MessageMentions, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4233 | emote: bool, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4234 | ) -> RoomMessageEventContentWithoutRelation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4235 | let content = match content.formatted_body { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4236 | Some(formatted_body) if emote => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4237 | RoomMessageEventContentWithoutRelation::emote_html(content.body, formatted_body) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4238 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4239 | Some(formatted_body) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4240 | RoomMessageEventContentWithoutRelation::text_html(content.body, formatted_body) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4241 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4242 | None if emote => RoomMessageEventContentWithoutRelation::emote_plain(content.body), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4243 | None => RoomMessageEventContentWithoutRelation::text_plain(content.body), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4244 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4245 | let content = content.add_mentions(Mentions::with_user_ids(mentions.user_ids)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4246 | if mentions.room { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4247 | content.add_mentions(Mentions::with_room_mention()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4248 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4249 | content |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4250 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4251 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4252 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4253 | async fn run_pagination(request: PaginationRequest) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4254 | let PaginationRequest { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4255 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4256 | events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4257 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4258 | limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4259 | command_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4260 | results, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4261 | } = request; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4262 | if events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4263 | .send(Event::HistoryChanged { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4264 | timeline_id: timeline_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4265 | state: HistoryState::Loading, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4266 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4267 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4268 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4269 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4270 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4271 | let _ = results.send(( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4272 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4273 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4274 | CommandErrorKind::EventReceiverClosed, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4275 | "event receiver has closed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4276 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4277 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4278 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4279 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4280 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4281 | let result = timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4282 | .paginate_backwards(limit.max(1)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4283 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4284 | .map_err(timeline_error); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4285 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4286 | Ok(at_start) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4287 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4288 | .send(Event::HistoryChanged { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4289 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4290 | state: if at_start { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4291 | HistoryState::StartReached |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4292 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4293 | HistoryState::Idle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4294 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4295 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4296 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4297 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4298 | let _ = results.send((id, Ok(()))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4299 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4300 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4301 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4302 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4303 | .send(Event::HistoryChanged { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4304 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4305 | state: HistoryState::Failed(error.to_string()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4306 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4307 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4308 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4309 | let _ = results.send((id, Err(error))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4310 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4311 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4312 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4313 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4314 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4315 | async fn emit_command_result_with_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4316 | events: &EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4317 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4318 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4319 | ) -> Result<(), SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4320 | events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4321 | .send(Event::CommandSucceededWithRoom { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4322 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4323 | command: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4324 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4325 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4326 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4327 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4328 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4329 | async fn emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4330 | events: &EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4331 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4332 | command: CommandKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4333 | result: Result<(), CommandError>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4334 | ) -> Result<(), SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4335 | let event = match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4336 | Ok(()) => Event::CommandSucceeded { id, command }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4337 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4338 | tracing::warn!(?id, ?command, error = ?error, "room actor command failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4339 | Event::CommandFailed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4340 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4341 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4342 | kind: error.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4343 | message: error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4344 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4345 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4346 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4347 | events.send(event).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4348 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4349 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4350 | fn send_error(source: SendEventError) -> CommandError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4351 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4352 | CommandErrorKind::EventReceiverClosed, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4353 | "event receiver has closed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4354 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4355 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4356 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4357 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4358 | fn timeline_error(source: matrix_sdk_ui::timeline::Error) -> CommandError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4359 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4360 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4361 | "timeline operation failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4362 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4363 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4364 | } |