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, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 7 | sync::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | Arc, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | atomic::{AtomicBool, Ordering}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 13 | use futures_util::{FutureExt, StreamExt}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | use matrix_sdk::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | Client, Room, SessionChange, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | room::edit::EditedContent, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | room::reply::{EnforceThread, Reply as SdkReply}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | ruma::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | OwnedDeviceId, OwnedEventId, OwnedRoomId, RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | api::client::receipt::create_receipt::v3::ReceiptType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | events::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | AnyGlobalAccountDataEventContent, AnyMessageLikeEventContent, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | GlobalAccountDataEventType, Mentions, RoomAccountDataEventType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | receipt::ReceiptThread, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | room::message::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | AddMentions, MessageType, Relation, ReplyWithinThread, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | RoomMessageEventContentWithoutRelation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | serde::Raw, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 33 | send_queue::{LocalEchoContent, RoomSendQueueUpdate, SendHandle}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | use matrix_sdk_ui::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | sync_service::{State as SyncState, SyncService}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | timeline::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | RoomExt, Timeline, TimelineEventItemId, TimelineFocus, TimelineReadReceiptTracking, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | use thiserror::Error; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | use tokio::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | sync::{Mutex, mpsc, oneshot, watch}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | task::{AbortHandle, Id as TaskId, JoinHandle, JoinSet}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | use crate::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | AccountCommand, AttachmentUpload, ClientHandle, Command, CommandErrorKind, CommandFailure, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | CommandId, CommandKind, ConnectionState, CreateRoomOptions, EncryptionCommand, EngineErrorKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | EngineFailure, Event, EventStream, HistoryState, InteractiveAuth, InteractiveAuthChallenge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | InteractiveAuthOutcome, LifecycleCommand, MediaCommand, MediaId, MediaSize, MessageContent, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | MessageEditContent, MessageMentions, NotificationCommand, PowerLevelChanges, RichText, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | RoomCommand, RoomNotificationMode, RoomPreset, RoomVisibility, TimelineCommand, TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 54 | TypingState, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 55 | encryption::{VerificationController, encryption_state}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 56 | error::{CommandError, classify_sdk_error}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | normalise::{MediaRegistration, MediaScope, normalise_timeline, normalise_timeline_diffs}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | transport::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | ClientQueryError, CommandEnvelope, CommandResultError, DriverPort, DriverRequest, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 60 | EventSender, SendEventError, transport_pair, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | upload::prepare_upload, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | use matrix_sdk::ruma::api::{Metadata, OutgoingRequest, auth_scheme::SendAccessToken}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | use matrix_sdk_crypto::{AttachmentDecryptor, MediaEncryptionInfo}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | #[derive(Clone, Debug, Eq, PartialEq)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | #[non_exhaustive] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | pub struct EngineConfig { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | pub command_capacity: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | pub event_capacity: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | pub initial_history_limit: u16, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | pub max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | pub max_media_sources: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | impl EngineConfig { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | pub const fn with_command_capacity(mut self, capacity: usize) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 80 | self.command_capacity = capacity; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 81 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 82 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 83 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 84 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 85 | pub const fn with_event_capacity(mut self, capacity: usize) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | self.event_capacity = capacity; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 87 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 88 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 89 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 90 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | pub const fn with_initial_history_limit(mut self, limit: u16) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 92 | self.initial_history_limit = limit; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 93 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 94 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 96 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 97 | pub const fn with_max_media_bytes(mut self, bytes: usize) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 98 | self.max_media_bytes = bytes; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 99 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 100 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 101 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 102 | #[must_use] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 103 | pub const fn with_max_media_sources(mut self, sources: usize) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 104 | self.max_media_sources = sources; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 105 | self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 106 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 107 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | impl Default for EngineConfig { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | fn default() -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | command_capacity: 64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | event_capacity: 128, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | initial_history_limit: 30, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | max_media_bytes: 20 * 1024 * 1024, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | max_media_sources: 2_048, |
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 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | #[derive(Debug, Error)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | pub enum EngineError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | #[error("the Matrix client is not authenticated")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | #[error("command, event, and media source capacities must be nonzero")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | ZeroCapacity, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | #[error("could not build the sync service")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 128 | SyncService(#[source] matrix_sdk_ui::sync_service::Error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 129 | #[error("the event receiver has closed")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 130 | EventReceiverClosed, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 131 | #[error("engine task {task} panicked")] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 132 | TaskPanicked { task: String }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 133 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 134 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 135 | impl EngineError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 136 | fn failure(&self) -> EngineFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 137 | EngineFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 138 | kind: match self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 139 | Self::EventReceiverClosed => EngineErrorKind::EventReceiverClosed, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 140 | Self::Unauthenticated |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 141 | | Self::ZeroCapacity |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 142 | | Self::SyncService(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 143 | | Self::TaskPanicked { .. } => EngineErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 144 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 145 | message: self.to_string(), |
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 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 149 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 150 | type UploadWatchers = HashMap< |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 151 | OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 152 | ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 153 | AbortHandle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 154 | watch::Receiver<Option<Result<(), CommandErrorKind>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 155 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 156 | >; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 157 | type MediaFlights = Arc<Mutex<HashMap<(MediaId, MediaSize), Vec<CommandId>>>>; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 158 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 159 | pub struct Engine { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 160 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 161 | sync: Arc<SyncService>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 162 | port: DriverPort, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 163 | rooms: HashMap<OwnedRoomId, RoomActorState>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 164 | next_room_generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 165 | room_actor_events: mpsc::UnboundedReceiver<RoomActorEvent>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 166 | room_actor_event_sender: mpsc::UnboundedSender<RoomActorEvent>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 167 | upload_watchers: UploadWatchers, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 168 | tasks: TaskRegistry, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 169 | session_changes: Option<tokio::sync::broadcast::Receiver<SessionChange>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 170 | initial_history_limit: u16, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 171 | room_command_capacity: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 172 | max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 173 | media: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 174 | notification_commands: mpsc::Sender<CommandJob<NotificationCommand>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 175 | notification_command_receiver: Option<mpsc::Receiver<CommandJob<NotificationCommand>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 176 | encryption_commands: mpsc::Sender<EncryptionWork>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 177 | encryption_command_receiver: Option<mpsc::Receiver<EncryptionWork>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 178 | verification: Arc<Mutex<VerificationController>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 179 | room_commands: mpsc::Sender<RoomJob>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 180 | room_command_receiver: Option<mpsc::Receiver<RoomJob>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 181 | global_account_gates: HashMap<String, oneshot::Receiver<()>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 182 | room_account_gates: HashMap<(OwnedRoomId, String), oneshot::Receiver<()>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 183 | media_flights: MediaFlights, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 184 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 185 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 186 | struct CommandJob<T> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 187 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 188 | command: T, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 189 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 190 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 191 | enum EncryptionWork { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 192 | Command(CommandJob<EncryptionCommand>), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 193 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 194 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 195 | struct RoomJob { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 196 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 197 | command: RoomCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 198 | reply: Option<oneshot::Sender<Result<OwnedRoomId, CommandResultError>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 199 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 200 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 201 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 202 | enum RoomMutationKey { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 203 | Room(OwnedRoomId), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 204 | Target(String), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 205 | Direct(ruma::OwnedUserId), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 206 | Independent(CommandId), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 207 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 208 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 209 | struct RoomJobCompletion { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 210 | key: RoomMutationKey, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 211 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 212 | reply: Option<oneshot::Sender<Result<OwnedRoomId, CommandResultError>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 213 | result: Result<Option<OwnedRoomId>, CommandError>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 214 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 215 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 216 | #[derive(Clone)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 217 | enum TaskContext { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 218 | Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 219 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 220 | kind: CommandKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 221 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 222 | Watcher(&'static str), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 223 | RoomActor { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 224 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 225 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 226 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 227 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 228 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 229 | impl TaskContext { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 230 | fn label(&self) -> String { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 231 | match self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 232 | Self::Command { id, kind } => format!("{kind:?} command {}", id.get()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 233 | Self::Watcher(name) => (*name).to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 234 | Self::RoomActor { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 235 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 236 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 237 | } => format!("room actor {room_id} generation {generation}"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 238 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 239 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 240 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 241 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 242 | struct TaskRegistry { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 243 | tasks: JoinSet<()>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 244 | contexts: HashMap<TaskId, TaskContext>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 245 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 246 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 247 | impl TaskRegistry { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 248 | fn new() -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 249 | Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 250 | tasks: JoinSet::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 251 | contexts: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 252 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 253 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 254 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 255 | fn is_empty(&self) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 256 | self.tasks.is_empty() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 257 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 258 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 259 | fn spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 260 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 261 | context: TaskContext, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 262 | future: impl Future<Output = ()> + Send + 'static, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 263 | ) -> AbortHandle { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 264 | let handle = self.tasks.spawn(future); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 265 | self.contexts.insert(handle.id(), context); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 266 | handle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 267 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 268 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 269 | async fn join_next(&mut self) -> Option<(TaskContext, Result<(), tokio::task::JoinError>)> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 270 | let result = self.tasks.join_next_with_id().await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 271 | let id = match &result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 272 | Ok((id, ())) => *id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 273 | Err(error) => error.id(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 274 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 275 | let context = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 276 | .contexts |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 277 | .remove(&id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 278 | .expect("every registered task has context"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 279 | Some((context, result.map(|(_, ())| ()))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 280 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 281 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 282 | async fn shutdown(&mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 283 | self.tasks.abort_all(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 284 | while self.tasks.join_next().await.is_some() {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 285 | self.contexts.clear(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 286 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 287 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 288 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 289 | enum RoomActorState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 290 | Opening { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 291 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 292 | sender: mpsc::Sender<RoomActorRequest>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 293 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 294 | Open { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 295 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 296 | sender: mpsc::Sender<RoomActorRequest>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 297 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 298 | Closing { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 299 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 300 | pending: VecDeque<DeferredRoomLifecycle>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 301 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 302 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 303 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 304 | enum DeferredRoomLifecycle { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 305 | Open(CommandId), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 306 | Close(CommandId), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 307 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 308 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 309 | impl RoomActorState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 310 | const fn generation(&self) -> u64 { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 311 | match self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 312 | Self::Opening { generation, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 313 | | Self::Open { generation, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 314 | | Self::Closing { generation, .. } => *generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 315 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 316 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 317 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 318 | fn sender(&self) -> Option<&mpsc::Sender<RoomActorRequest>> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 319 | match self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 320 | Self::Opening { sender, .. } | Self::Open { sender, .. } => Some(sender), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 321 | Self::Closing { .. } => None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 322 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 323 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 324 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 325 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 326 | enum RoomActorEvent { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 327 | Ready { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 328 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 329 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 330 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 331 | Exited { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 332 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 333 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 334 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 335 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 336 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 337 | enum EngineActivity { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 338 | Request(Option<Box<DriverRequest>>), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 339 | Task(Option<(TaskContext, Result<(), tokio::task::JoinError>)>), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 340 | RoomActor(Option<RoomActorEvent>), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 341 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 342 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 343 | struct MediaRegistry { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 344 | entries: HashMap<MediaId, MediaRegistration>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 345 | order: VecDeque<MediaId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 346 | limit: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 347 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 348 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 349 | impl MediaRegistry { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 350 | fn new(limit: usize) -> Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 351 | Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 352 | entries: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 353 | order: VecDeque::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 354 | limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 355 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 356 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 357 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 358 | fn insert_all(&mut self, media: Vec<MediaRegistration>) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 359 | for media in media { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 360 | if !self.entries.contains_key(&media.id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 361 | self.order.push_back(media.id.clone()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 362 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 363 | self.entries.insert(media.id.clone(), media); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 364 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 365 | while self.entries.len() > self.limit { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 366 | if let Some(id) = self.order.pop_front() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 367 | self.entries.remove(&id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 368 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 369 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 370 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 371 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 372 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 373 | enum RoomActorRequest { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 374 | Opened { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 375 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 376 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 377 | Execute { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 378 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 379 | command: TimelineCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 380 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 381 | Close { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 382 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 383 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 384 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 385 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 386 | struct RoomActor { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 387 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 388 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 389 | room: Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 390 | receiver: mpsc::Receiver<RoomActorRequest>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 391 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 392 | engine_events: mpsc::UnboundedSender<RoomActorEvent>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 393 | media: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 394 | initial_history_limit: u16, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 395 | open_ids: Vec<CommandId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 396 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 397 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 398 | struct TimelineSession { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 399 | id: TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 400 | timeline: Arc<Timeline>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 401 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 402 | watcher: JoinHandle<()>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 403 | media: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 404 | current_user_id: Option<ruma::OwnedUserId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 405 | scope: MediaScope, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 406 | pagination_ids: Arc<Mutex<HashSet<CommandId>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 407 | pagination_sender: mpsc::Sender<PaginationRequest>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 408 | pagination_worker: JoinHandle<()>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 409 | pagination_results: Option<mpsc::UnboundedSender<(CommandId, Result<(), CommandError>)>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 410 | pagination_result_task: JoinHandle<()>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 411 | reached_start: Arc<AtomicBool>, |
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 PaginationRequest { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 415 | timeline: Arc<Timeline>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 416 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 417 | timeline_id: TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 418 | reached_start: Arc<AtomicBool>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 419 | limit: u16, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 420 | command_id: Option<CommandId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 421 | results: mpsc::UnboundedSender<(CommandId, Result<(), CommandError>)>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 422 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 423 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 424 | impl TimelineSession { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 425 | async fn start( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 426 | id: TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 427 | timeline: Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 428 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 429 | media: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 430 | current_user_id: Option<ruma::OwnedUserId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 431 | initial_history_limit: u16, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 432 | ) -> Result<Self, SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 433 | let timeline = Arc::new(timeline); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 434 | let scope = MediaScope::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 435 | let watcher = start_timeline_watcher( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 436 | id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 437 | timeline.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 438 | events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 439 | media.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 440 | current_user_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 441 | scope.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 442 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 443 | .await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 444 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 445 | let (pagination_sender, mut pagination_receiver) = mpsc::channel::<PaginationRequest>(32); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 446 | let pagination_worker = tokio::spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 447 | while let Some(request) = pagination_receiver.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 448 | let command_id = request.command_id; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 449 | let results = request.results.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 450 | if AssertUnwindSafe(run_pagination(request)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 451 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 452 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 453 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 454 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 455 | tracing::warn!(?command_id, "pagination task panicked"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 456 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 457 | let _ = results.send(( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 458 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 459 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 460 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 461 | "pagination failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 462 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 463 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 464 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 465 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 466 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 467 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 468 | let pagination_ids = Arc::new(Mutex::new(HashSet::new())); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 469 | let (pagination_results, mut result_receiver) = mpsc::unbounded_channel(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 470 | let result_events = events.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 471 | let result_ids = pagination_ids.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 472 | let pagination_result_task = tokio::spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 473 | while let Some((id, result)) = result_receiver.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 474 | if result_ids.lock().await.remove(&id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 475 | let _ = emit_command_result(&result_events, id, CommandKind::Timeline, result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 476 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 477 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 478 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 479 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 480 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 481 | let mut session = Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 482 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 483 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 484 | events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 485 | watcher, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 486 | media, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 487 | current_user_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 488 | scope, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 489 | pagination_sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 490 | pagination_worker, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 491 | pagination_ids, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 492 | pagination_results: Some(pagination_results), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 493 | pagination_result_task, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 494 | reached_start: Arc::new(AtomicBool::new(false)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 495 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 496 | if initial_history_limit > 0 { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 497 | session.paginate(initial_history_limit, None).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 498 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 499 | Ok(session) |
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 | async fn resync(&mut self) -> Result<(), SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 503 | self.watcher.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 504 | let _ = (&mut self.watcher).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 505 | self.watcher = start_timeline_watcher( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 506 | self.id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 507 | self.timeline.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 508 | self.events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 509 | self.media.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 510 | self.current_user_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 511 | self.scope.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 512 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 513 | .await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 514 | Ok(()) |
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 | async fn paginate(&mut self, limit: u16, command_id: Option<CommandId>) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 518 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 519 | self.pagination_ids.lock().await.insert(id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 520 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 521 | let request = PaginationRequest { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 522 | timeline: self.timeline.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 523 | events: self.events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 524 | timeline_id: self.id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 525 | reached_start: self.reached_start.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 526 | limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 527 | command_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 528 | results: self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 529 | .pagination_results |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 530 | .as_ref() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 531 | .expect("pagination session is open") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 532 | .clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 533 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 534 | if let Err(error) = self.pagination_sender.try_send(request) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 535 | let kind = match error { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 536 | mpsc::error::TrySendError::Full(_) => CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 537 | mpsc::error::TrySendError::Closed(_) => CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 538 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 539 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 540 | self.pagination_ids.lock().await.remove(&id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 541 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 542 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 543 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 544 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 545 | Err(CommandError::new(kind, "pagination queue is unavailable")), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 546 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 547 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 548 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 549 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 550 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 551 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 552 | async fn close(mut self, kind: CommandErrorKind, message: &'static str) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 553 | self.pagination_worker.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 554 | let _ = (&mut self.pagination_worker).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 555 | drop(self.pagination_results.take()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 556 | let _ = (&mut self.pagination_result_task).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 557 | let pending = self.pagination_ids.lock().await.drain().collect::<Vec<_>>(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 558 | for id in pending { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 559 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 560 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 561 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 562 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 563 | Err(CommandError::new(kind, message)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 564 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 565 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 566 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 567 | self.watcher.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 568 | let _ = (&mut self.watcher).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 569 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 570 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 571 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 572 | async fn start_timeline_watcher( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 573 | id: TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 574 | timeline: Arc<Timeline>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 575 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 576 | media: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 577 | current_user_id: Option<ruma::OwnedUserId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 578 | scope: MediaScope, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 579 | ) -> Result<JoinHandle<()>, SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 580 | let (items, mut stream) = timeline.subscribe().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 581 | let normalised = normalise_timeline(items.iter(), current_user_id.as_deref(), &scope); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 582 | media.lock().await.insert_all(normalised.media); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 583 | events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 584 | .send(Event::TimelineReset { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 585 | timeline_id: id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 586 | items: normalised.items, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 587 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 588 | .await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 589 | Ok(tokio::spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 590 | while let Some(diffs) = stream.next().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 591 | let normalised = normalise_timeline_diffs(diffs, current_user_id.as_deref(), &scope); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 592 | media.lock().await.insert_all(normalised.media); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 593 | if events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 594 | .send(Event::TimelineChanged { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 595 | timeline_id: id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 596 | diffs: normalised.diffs, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 597 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 598 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 599 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 600 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 601 | tracing::debug!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 602 | ?id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 603 | "timeline watcher stopped because the event receiver closed" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 604 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 605 | return; |
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 | tracing::debug!(?id, "timeline watcher stopped"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 609 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 610 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 611 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 612 | impl Drop for TimelineSession { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 613 | fn drop(&mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 614 | self.watcher.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 615 | self.pagination_worker.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 616 | self.pagination_result_task.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 617 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 618 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 619 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 620 | impl Engine { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 621 | pub async fn from_client( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 622 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 623 | config: EngineConfig, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 624 | ) -> Result<(Self, ClientHandle, EventStream), EngineError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 625 | if client.user_id().is_none() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 626 | return Err(EngineError::Unauthenticated); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 627 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 628 | if config.command_capacity == 0 |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 629 | || config.event_capacity == 0 |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 630 | || config.max_media_sources == 0 |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 631 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 632 | return Err(EngineError::ZeroCapacity); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 633 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 634 | let session_changes = client.subscribe_to_session_changes(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 635 | let sync = Arc::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 636 | SyncService::builder(client.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 637 | .with_offline_mode() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 638 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 639 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 640 | .map_err(EngineError::SyncService)?, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 641 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 642 | let (handle, port, events) = transport_pair( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 643 | client.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 644 | sync.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 645 | config.command_capacity, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 646 | config.event_capacity, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 647 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 648 | client.send_queue().enable_upload_progress(true); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 649 | let (room_actor_event_sender, room_actor_events) = mpsc::unbounded_channel(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 650 | let (notification_commands, notification_command_receiver) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 651 | mpsc::channel(config.command_capacity); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 652 | let (encryption_commands, encryption_command_receiver) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 653 | mpsc::channel(config.command_capacity); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 654 | let (room_commands, room_command_receiver) = mpsc::channel(config.command_capacity); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 655 | Ok(( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 656 | Self { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 657 | client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 658 | sync, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 659 | port, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 660 | rooms: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 661 | next_room_generation: 0, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 662 | room_actor_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 663 | room_actor_event_sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 664 | upload_watchers: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 665 | tasks: TaskRegistry::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 666 | session_changes: Some(session_changes), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 667 | initial_history_limit: config.initial_history_limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 668 | room_command_capacity: config.command_capacity, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 669 | max_media_bytes: config.max_media_bytes, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 670 | media: Arc::new(Mutex::new(MediaRegistry::new(config.max_media_sources))), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 671 | notification_commands, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 672 | notification_command_receiver: Some(notification_command_receiver), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 673 | encryption_commands, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 674 | encryption_command_receiver: Some(encryption_command_receiver), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 675 | verification: Arc::new(Mutex::new(VerificationController::default())), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 676 | room_commands, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 677 | room_command_receiver: Some(room_command_receiver), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 678 | global_account_gates: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 679 | room_account_gates: HashMap::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 680 | media_flights: Arc::new(Mutex::new(HashMap::new())), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 681 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 682 | handle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 683 | events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 684 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 685 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 686 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 687 | pub async fn run(mut self) -> Result<(), EngineError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 688 | let sender = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 689 | let user_id = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 690 | .client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 691 | .user_id() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 692 | .expect("authentication was checked during construction") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 693 | .to_owned(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 694 | if sender.send(Event::Authenticated { user_id }).await.is_err() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 695 | self.shutdown().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 696 | return Err(EngineError::EventReceiverClosed); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 697 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 698 | self.port |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 699 | .connection_sender() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 700 | .send_replace(ConnectionState::Connecting); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 701 | if sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 702 | .send(Event::ConnectionChanged(ConnectionState::Connecting)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 703 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 704 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 705 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 706 | self.shutdown().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 707 | return Err(EngineError::EventReceiverClosed); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 708 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 709 | self.start_watchers(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 710 | self.start_domain_workers(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 711 | self.sync.start().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 712 | let mut terminal_error = None; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 713 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 714 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 715 | let activity = tokio::select! { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 716 | biased; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 717 | _ = sender.closed() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 718 | terminal_error = Some(EngineError::EventReceiverClosed); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 719 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 720 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 721 | request = self.port.recv() => EngineActivity::Request(request.map(Box::new)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 722 | completed = self.tasks.join_next(), if !self.tasks.is_empty() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 723 | EngineActivity::Task(completed) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 724 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 725 | event = self.room_actor_events.recv() => EngineActivity::RoomActor(event), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 726 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 727 | match activity { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 728 | EngineActivity::Request(Some(request)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 729 | let Some(envelope) = self.dispatch_request(*request).await else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 730 | continue; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 731 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 732 | let id = envelope.id; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 733 | let command = envelope.command; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 734 | if matches!(command, Command::Lifecycle(LifecycleCommand::Shutdown)) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 735 | let _ = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 736 | emit_command_result(&sender, id, CommandKind::Lifecycle, Ok(())).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 737 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 738 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 739 | let kind = command.kind(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 740 | tracing::debug!(?id, ?kind, "dispatching command"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 741 | let result = self.execute(id, command).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 742 | if Self::emit_sync_result(&sender, id, kind, result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 743 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 744 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 745 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 746 | terminal_error = Some(EngineError::EventReceiverClosed); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 747 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 748 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 749 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 750 | EngineActivity::Request(None) => break, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 751 | EngineActivity::Task(Some((context, result))) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 752 | if let Err(error) = result |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 753 | && error.is_panic() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 754 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 755 | match context { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 756 | TaskContext::Command { id, kind } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 757 | if sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 758 | .send(Event::CommandFailed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 759 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 760 | command: kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 761 | kind: CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 762 | message: "command task panicked".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 763 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 764 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 765 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 766 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 767 | terminal_error = Some(EngineError::EventReceiverClosed); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 768 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 769 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 770 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 771 | context => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 772 | terminal_error = Some(EngineError::TaskPanicked { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 773 | task: context.label(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 774 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 775 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 776 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 777 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 778 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 779 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 780 | EngineActivity::Task(None) => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 781 | EngineActivity::RoomActor(Some(event)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 782 | if let Err(error) = self.handle_room_actor_event(event).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 783 | terminal_error = Some(error); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 784 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 785 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 786 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 787 | EngineActivity::RoomActor(None) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 788 | terminal_error = Some(EngineError::TaskPanicked { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 789 | task: "room actor event channel".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 790 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 791 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 792 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 793 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 794 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 795 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 796 | if let Some(error) = terminal_error.as_ref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 797 | let _ = sender.send(Event::Failed(error.failure())).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 798 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 799 | self.shutdown().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 800 | let _ = sender.send(Event::Stopped).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 801 | if let Some(error) = terminal_error { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 802 | Err(error) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 803 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 804 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 805 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 806 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 807 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 808 | async fn emit_sync_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 809 | sender: &EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 810 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 811 | kind: CommandKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 812 | result: Result<bool, CommandError>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 813 | ) -> Result<(), SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 814 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 815 | Ok(true) => Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 816 | Ok(false) => emit_command_result(sender, id, kind, Ok(())).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 817 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 818 | tracing::warn!(?id, ?kind, error = ?error, "command failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 819 | emit_command_result(sender, id, kind, Err(error)).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 820 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 821 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 822 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 823 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 824 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 825 | fn spawn_logout(&mut self, id: CommandId) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 826 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 827 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 828 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 829 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 830 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 831 | kind: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 832 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 833 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 834 | match client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 835 | .logout() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 836 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 837 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 838 | .map_err(|source| CommandError::from_sdk("logout failed", source)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 839 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 840 | Ok(()) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 841 | let _ = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 842 | emit_command_result(&events, id, CommandKind::Account, Ok(())).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 843 | let _ = events.send(Event::LoggedOut).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 844 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 845 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 846 | let _ = emit_command_result(&events, id, CommandKind::Account, Err(error)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 847 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 848 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 849 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 850 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 851 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 852 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 853 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 854 | async fn dispatch_request(&mut self, request: DriverRequest) -> Option<CommandEnvelope> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 855 | match request { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 856 | DriverRequest::Command(command) => Some(command), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 857 | DriverRequest::DeleteDevices { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 858 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 859 | device_ids, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 860 | auth, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 861 | reply, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 862 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 863 | self.spawn_delete_devices(id, device_ids, auth, Some(reply)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 864 | None |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 865 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 866 | DriverRequest::RoomResult { id, command, reply } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 867 | if let Err(error) = self.spawn_room_command_with_reply(id, command, Some(reply)) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 868 | tracing::warn!(?id, error = ?error, "room result request failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 869 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 870 | None |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 871 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 872 | DriverRequest::SetGlobalAccountData { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 873 | event_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 874 | content, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 875 | reply, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 876 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 877 | self.spawn_global_account_data_write(event_type, content, reply); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 878 | None |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 879 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 880 | DriverRequest::SetRoomAccountData { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 881 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 882 | event_type, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 883 | content, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 884 | reply, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 885 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 886 | self.spawn_room_account_data_write(room_id, event_type, content, reply); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 887 | None |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 888 | } |
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 | fn spawn_mark_read(&mut self, id: CommandId, timeline_id: TimelineId, event_id: OwnedEventId) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 893 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 894 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 895 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 896 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 897 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 898 | kind: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 899 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 900 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 901 | let result = mark_read(&client, &timeline_id, event_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 902 | let _ = emit_command_result(&events, id, CommandKind::Room, result).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 903 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 904 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 905 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 906 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 907 | fn spawn_typing(&mut self, id: CommandId, room_id: OwnedRoomId, typing: bool) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 908 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 909 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 910 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 911 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 912 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 913 | kind: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 914 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 915 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 916 | let result = set_typing(&client, &room_id, typing).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 917 | let _ = emit_command_result(&events, id, CommandKind::Room, result).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 918 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 919 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 920 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 921 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 922 | fn spawn_cancel_send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 923 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 924 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 925 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 926 | transaction_id: matrix_sdk::ruma::OwnedTransactionId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 927 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 928 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 929 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 930 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 931 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 932 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 933 | kind: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 934 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 935 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 936 | let result = cancel_send(&client, &room_id, &transaction_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 937 | let _ = emit_command_result(&events, id, CommandKind::Room, result).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 938 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 939 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 940 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 941 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 942 | fn spawn_retry_send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 943 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 944 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 945 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 946 | transaction_id: matrix_sdk::ruma::OwnedTransactionId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 947 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 948 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 949 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 950 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 951 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 952 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 953 | kind: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 954 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 955 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 956 | let result = retry_send(&client, &room_id, &transaction_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 957 | let _ = emit_command_result(&events, id, CommandKind::Room, result).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 958 | }, |
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 | fn spawn_delete_devices( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 963 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 964 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 965 | device_ids: Vec<OwnedDeviceId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 966 | auth: Option<InteractiveAuth>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 967 | reply: Option<oneshot::Sender<Result<InteractiveAuthOutcome, CommandResultError>>>, |
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::Account, |
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 auth = match auth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 978 | Some(auth) => match auth.into_sdk( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 979 | client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 980 | .user_id() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 981 | .expect("authentication was checked during construction"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 982 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 983 | Ok(auth) => Some(auth), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 984 | Err(message) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 985 | let failure = CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 986 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 987 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 988 | kind: CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 989 | message: format!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 990 | "interactive authentication data is invalid, {message}" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 991 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 992 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 993 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 994 | let _ = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 995 | reply.send(Err(CommandResultError::Failed(failure.clone()))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 996 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 997 | let _ = events.send(Event::CommandFailed(failure)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 998 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 999 | return; |
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 | None => None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1003 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1004 | let result = client.delete_devices(&device_ids, auth).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1005 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1006 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1007 | Ok(_) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1008 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1009 | let _ = reply.send(Ok(InteractiveAuthOutcome::Completed)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1010 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1011 | let _ = emit_command_result(&events, id, CommandKind::Account, Ok(())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1012 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1013 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1014 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1015 | Err(source) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1016 | if let Some(info) = source.as_uiaa_response() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1017 | let challenge = InteractiveAuthChallenge::from(info); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1018 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1019 | let _ = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1020 | reply.send(Ok(InteractiveAuthOutcome::Challenge(challenge))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1021 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1022 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1023 | .send(Event::InteractiveAuthRequired { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1024 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1025 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1026 | challenge: challenge.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1027 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1028 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1029 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1030 | .send(Event::CommandFailed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1031 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1032 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1033 | kind: CommandErrorKind::InteractiveAuthRequired, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1034 | message: "interactive authentication is required" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1035 | .to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1036 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1037 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1038 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1039 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1040 | let error = CommandError::from_http("deleting devices failed", source); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1041 | let failure = CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1042 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1043 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1044 | kind: error.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1045 | message: error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1046 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1047 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1048 | let _ = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1049 | reply.send(Err(CommandResultError::Failed(failure.clone()))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1050 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1051 | let _ = events.send(Event::CommandFailed(failure)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1052 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1053 | } |
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 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1057 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1058 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1059 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1060 | fn spawn_rename_device( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1061 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1062 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1063 | device_id: OwnedDeviceId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1064 | display_name: String, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1065 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1066 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1067 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1068 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1069 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1070 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1071 | kind: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1072 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1073 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1074 | match client.rename_device(&device_id, &display_name).await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1075 | Ok(_) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1076 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1077 | .send(Event::CommandSucceeded { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1078 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1079 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1080 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1081 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1082 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1083 | Err(source) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1084 | let error = CommandError::from_http("renaming device failed", source); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1085 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1086 | .send(Event::CommandFailed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1087 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1088 | command: CommandKind::Account, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1089 | kind: error.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1090 | message: error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1091 | })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1092 | .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 | fn spawn_global_account_data_write( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1100 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1101 | event_type: GlobalAccountDataEventType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1102 | content: Raw<AnyGlobalAccountDataEventContent>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1103 | reply: oneshot::Sender<Result<(), ClientQueryError>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1104 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1105 | let (done, next) = oneshot::channel(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1106 | let previous = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1107 | .global_account_gates |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1108 | .insert(event_type.to_string(), next); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1109 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1110 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1111 | TaskContext::Watcher("global account data writer"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1112 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1113 | if let Some(previous) = previous { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1114 | let _ = previous.await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1115 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1116 | let result = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1117 | .account() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1118 | .set_account_data_raw(event_type, content) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1119 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1120 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1121 | .map_err(ClientQueryError::Sdk); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1122 | let _ = reply.send(result); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1123 | let _ = done.send(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1124 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1125 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1126 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1127 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1128 | fn spawn_room_account_data_write( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1129 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1130 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1131 | event_type: RoomAccountDataEventType, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1132 | content: Raw<matrix_sdk::ruma::events::AnyRoomAccountDataEventContent>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1133 | reply: oneshot::Sender<Result<(), ClientQueryError>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1134 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1135 | let (done, next) = oneshot::channel(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1136 | let previous = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1137 | .room_account_gates |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1138 | .insert((room_id.clone(), event_type.to_string()), next); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1139 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1140 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1141 | TaskContext::Watcher("room account data writer"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1142 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1143 | if let Some(previous) = previous { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1144 | let _ = previous.await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1145 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1146 | let result = match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1147 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1148 | .set_account_data_raw(event_type, content) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1149 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1150 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1151 | .map_err(ClientQueryError::Sdk), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1152 | None => Err(ClientQueryError::RoomUnknown(room_id)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1153 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1154 | let _ = reply.send(result); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1155 | let _ = done.send(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1156 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1157 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1158 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1159 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1160 | fn start_domain_workers(&mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1161 | let mut notification_commands = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1162 | .notification_command_receiver |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1163 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1164 | .expect("notification worker has not started"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1165 | let notification_client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1166 | let notification_events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1167 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1168 | TaskContext::Watcher("notification command worker"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1169 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1170 | while let Some(CommandJob { id, command }) = notification_commands.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1171 | let result = AssertUnwindSafe(execute_notification_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1172 | ¬ification_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1173 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1174 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1175 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1176 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1177 | .unwrap_or_else(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1178 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1179 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1180 | "notification command panicked", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1181 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1182 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1183 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1184 | ¬ification_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1185 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1186 | CommandKind::Notification, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1187 | result, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1188 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1189 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1190 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1191 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1192 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1193 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1194 | let mut encryption_commands = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1195 | .encryption_command_receiver |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1196 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1197 | .expect("encryption worker has not started"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1198 | let encryption_client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1199 | let encryption_events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1200 | let verification = self.verification.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1201 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1202 | TaskContext::Watcher("encryption command worker"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1203 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1204 | while let Some(work) = encryption_commands.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1205 | match work { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1206 | EncryptionWork::Command(CommandJob { id, command }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1207 | if matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1208 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1209 | EncryptionCommand::Recover(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1210 | | EncryptionCommand::RecoverKeyBackup(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1211 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1212 | let result = AssertUnwindSafe(execute_encryption_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1213 | &encryption_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1214 | &encryption_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1215 | &mut VerificationController::default(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1216 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1217 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1218 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1219 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1220 | .unwrap_or_else(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1221 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1222 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1223 | "encryption command panicked", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1224 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1225 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1226 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1227 | &encryption_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1228 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1229 | CommandKind::Encryption, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1230 | result, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1231 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1232 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1233 | continue; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1234 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1235 | let is_key_backup = matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1236 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1237 | EncryptionCommand::CreateKeyBackup |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1238 | | EncryptionCommand::EnableKeyBackup |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1239 | | EncryptionCommand::DisableKeyBackup { .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1240 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1241 | let result = if is_key_backup { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1242 | AssertUnwindSafe(execute_key_backup_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1243 | &encryption_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1244 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1245 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1246 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1247 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1248 | .unwrap_or_else(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1249 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1250 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1251 | "encryption command panicked", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1252 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1253 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1254 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1255 | let mut verification = verification.lock().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1256 | let result = AssertUnwindSafe(execute_encryption_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1257 | &encryption_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1258 | &encryption_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1259 | &mut verification, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1260 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1261 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1262 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1263 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1264 | .unwrap_or_else(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1265 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1266 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1267 | "encryption command panicked", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1268 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1269 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1270 | drop(verification); |
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 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1274 | &encryption_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1275 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1276 | CommandKind::Encryption, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1277 | result, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1278 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1279 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1280 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1281 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1282 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1283 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1284 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1285 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1286 | let room_commands = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1287 | .room_command_receiver |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1288 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1289 | .expect("room command worker has not started"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1290 | let room_client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1291 | let room_events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1292 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1293 | TaskContext::Watcher("room command worker"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1294 | run_room_command_worker( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1295 | room_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1296 | room_events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1297 | room_commands, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1298 | self.room_command_capacity, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1299 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1300 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1301 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1302 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1303 | fn start_watchers(&mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1304 | let mut session_changes = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1305 | .session_changes |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1306 | .take() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1307 | .expect("session watcher has not started"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1308 | let session_sender = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1309 | self.tasks |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1310 | .spawn(TaskContext::Watcher("session change watcher"), async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1311 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1312 | let event = match session_changes.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1313 | Ok(SessionChange::TokensRefreshed) => Event::SessionTokensChanged, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1314 | Ok(SessionChange::UnknownToken(_)) => Event::SessionAccessRejected, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1315 | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1316 | Event::SessionTokensChanged |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1317 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1318 | Err(tokio::sync::broadcast::error::RecvError::Closed) => break, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1319 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1320 | if session_sender.send(event).await.is_err() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1321 | break; |
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 | let mut sync_states = self.sync.state(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1327 | let state_sender = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1328 | let connection_sender = self.port.connection_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1329 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1330 | TaskContext::Watcher("connection state watcher"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1331 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1332 | while let Some(state) = sync_states.next().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1333 | let state = match state { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1334 | SyncState::Idle => ConnectionState::Idle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1335 | SyncState::Running => ConnectionState::Online, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1336 | SyncState::Offline => ConnectionState::Offline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1337 | SyncState::Terminated => ConnectionState::Stopped, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1338 | SyncState::Error(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1339 | tracing::warn!(error = %error, "sync service reported an error"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1340 | ConnectionState::Offline |
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 | connection_sender.send_replace(state); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1344 | if state_sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1345 | .send(Event::ConnectionChanged(state)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1346 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1347 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1348 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1349 | tracing::debug!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1350 | "connection watcher stopped because the event receiver closed" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1351 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1352 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1353 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1354 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1355 | tracing::debug!("connection watcher stopped because the sync state stream ended"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1356 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1357 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1358 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1359 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1360 | let encryption_sender = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1361 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1362 | TaskContext::Watcher("encryption state watcher"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1363 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1364 | let Some(device_id) = client.device_id().map(ToOwned::to_owned) else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1365 | tracing::warn!("encryption watcher stopped because the client has no device id"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1366 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1367 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1368 | let recovery = client.encryption().recovery(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1369 | let backups = client.encryption().backups(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1370 | let mut recovery_stream = recovery.state_stream(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1371 | let mut backup_stream = backups.state_stream(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1372 | let mut verification_stream = client.encryption().verification_state(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1373 | let mut recovery_state = recovery.state(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1374 | let mut backup_state = backups.state(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1375 | let mut verification_state = verification_stream.get(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1376 | if encryption_sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1377 | .send(Event::EncryptionChanged(encryption_state( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1378 | device_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1379 | recovery_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1380 | verification_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1381 | backup_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1382 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1383 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1384 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1385 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1386 | tracing::debug!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1387 | "encryption watcher stopped because the event receiver closed" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1388 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1389 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1390 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1391 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1392 | tokio::select! { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1393 | update = recovery_stream.next() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1394 | let Some(update) = update else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1395 | tracing::debug!("encryption watcher stopped because the recovery stream ended"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1396 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1397 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1398 | recovery_state = update; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1399 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1400 | update = verification_stream.next() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1401 | let Some(update) = update else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1402 | tracing::debug!("encryption watcher stopped because the verification stream ended"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1403 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1404 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1405 | verification_state = update; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1406 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1407 | update = backup_stream.next() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1408 | let Some(update) = update else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1409 | tracing::debug!("backup watcher stopped because the backup state stream ended"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1410 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1411 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1412 | backup_state = update.unwrap_or_else(|_| backups.state()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1413 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1414 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1415 | if encryption_sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1416 | .send(Event::EncryptionChanged(encryption_state( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1417 | device_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1418 | recovery_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1419 | verification_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1420 | backup_state, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1421 | ))) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1422 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1423 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1424 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1425 | tracing::debug!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1426 | "encryption watcher stopped because the event receiver closed" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1427 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1428 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1429 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1430 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1431 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1432 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1433 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1434 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1435 | let room_sender = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1436 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1437 | TaskContext::Watcher("room list watcher"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1438 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1439 | let (_, mut room_stream) = client.rooms_stream(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1440 | let mut info_stream = client.room_info_notable_update_receiver(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1441 | if room_sender.send(Event::RoomsChanged).await.is_err() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1442 | tracing::debug!("room watcher stopped because the event receiver closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1443 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1444 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1445 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1446 | tokio::select! { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1447 | update = room_stream.next() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1448 | if update.is_none() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1449 | tracing::debug!("room watcher stopped because the room stream ended"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1450 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1451 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1452 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1453 | update = info_stream.recv() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1454 | if update.is_err() && info_stream.is_closed() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1455 | tracing::debug!("room watcher stopped because the room info stream ended"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1456 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1457 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1458 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1459 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1460 | if room_sender.send(Event::RoomsChanged).await.is_err() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1461 | tracing::debug!("room watcher stopped because the event receiver closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1462 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1463 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1464 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1465 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1466 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1467 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1468 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1469 | let notification_sender = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1470 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1471 | TaskContext::Watcher("notification settings watcher"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1472 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1473 | let settings = client.notification_settings().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1474 | let mut changes = settings.subscribe_to_changes(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1475 | while let Ok(()) | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1476 | changes.recv().await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1477 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1478 | if notification_sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1479 | .send(Event::NotificationSettingsChanged) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1480 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1481 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1482 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1483 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1484 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1485 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1486 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1487 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1488 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1489 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1490 | async fn execute(&mut self, id: CommandId, command: Command) -> Result<bool, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1491 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1492 | Command::Account(AccountCommand::Logout) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1493 | self.spawn_logout(id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1494 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1495 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1496 | Command::Account(AccountCommand::RenameDevice { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1497 | device_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1498 | display_name, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1499 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1500 | self.spawn_rename_device(id, device_id, display_name); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1501 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1502 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1503 | Command::Account(AccountCommand::DeleteDevices { device_ids, auth }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1504 | self.spawn_delete_devices(id, device_ids, auth, None); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1505 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1506 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1507 | Command::Room(RoomCommand::Open(room_id)) => self.open_room(id, room_id).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1508 | Command::Room(RoomCommand::Close(room_id)) => self.close_room(id, &room_id).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1509 | Command::Room(RoomCommand::MarkRead { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1510 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1511 | event_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1512 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1513 | self.spawn_mark_read(id, timeline_id, event_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1514 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1515 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1516 | Command::Room(RoomCommand::SetTyping { room_id, typing }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1517 | self.spawn_typing(id, room_id, typing); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1518 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1519 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1520 | Command::Room(RoomCommand::Join { room, via }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1521 | self.spawn_room_command(id, RoomCommand::Join { room, via }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1522 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1523 | Command::Room(RoomCommand::Leave(room_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1524 | self.spawn_room_command(id, RoomCommand::Leave(room_id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1525 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1526 | Command::Room(RoomCommand::Forget(room_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1527 | self.spawn_room_command(id, RoomCommand::Forget(room_id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1528 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1529 | Command::Room(RoomCommand::AcceptInvite(room_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1530 | self.spawn_room_command(id, RoomCommand::AcceptInvite(room_id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1531 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1532 | Command::Room(RoomCommand::RejectInvite(room_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1533 | self.spawn_room_command(id, RoomCommand::RejectInvite(room_id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1534 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1535 | Command::Room(command @ RoomCommand::InviteUser { .. }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1536 | | Command::Room(command @ RoomCommand::KickUser { .. }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1537 | | Command::Room(command @ RoomCommand::BanUser { .. }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1538 | | Command::Room(command @ RoomCommand::UnbanUser { .. }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1539 | | Command::Room(command @ RoomCommand::SetPowerLevels { .. }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1540 | | Command::Room(command @ RoomCommand::SetUserPowerLevel { .. }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1541 | self.spawn_room_command(id, command) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1542 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1543 | Command::Room(RoomCommand::Create(options)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1544 | self.spawn_room_command(id, RoomCommand::Create(options)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1545 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1546 | Command::Room(RoomCommand::CreateDirect(user_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1547 | self.spawn_room_command(id, RoomCommand::CreateDirect(user_id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1548 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1549 | Command::Room(RoomCommand::CancelSend { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1550 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1551 | transaction_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1552 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1553 | self.spawn_cancel_send(id, room_id, transaction_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1554 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1555 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1556 | Command::Room(RoomCommand::RetrySend { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1557 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1558 | transaction_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1559 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1560 | self.spawn_retry_send(id, room_id, transaction_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1561 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1562 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1563 | Command::Timeline(command) => self.route_timeline(id, command), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1564 | Command::Media(MediaCommand::Fetch { id: media_id, size }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1565 | self.spawn_media_fetch(id, media_id, size); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1566 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1567 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1568 | Command::Media(MediaCommand::Upload { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1569 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1570 | filename, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1571 | mimetype, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1572 | bytes, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1573 | caption, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1574 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1575 | let mut upload = AttachmentUpload::new(timeline_id, filename, mimetype, bytes); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1576 | upload.caption = caption; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1577 | self.spawn_media_upload(id, upload).await.map(|()| true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1578 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1579 | Command::Media(MediaCommand::UploadAttachment(upload)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1580 | self.spawn_media_upload(id, upload).await.map(|()| true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1581 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1582 | Command::Encryption(command) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1583 | self.encryption_commands |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1584 | .try_send(EncryptionWork::Command(CommandJob { id, command })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1585 | .map_err(|error| domain_queue_error(error, "encryption"))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1586 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1587 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1588 | Command::Notification(command) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1589 | self.notification_commands |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1590 | .try_send(CommandJob { id, command }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1591 | .map_err(|error| domain_queue_error(error, "notification"))?; |
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::Lifecycle(LifecycleCommand::Shutdown) => Ok(false), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1595 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1596 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1597 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1598 | async fn open_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1599 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1600 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1601 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1602 | ) -> Result<bool, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1603 | match self.rooms.get_mut(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1604 | Some(RoomActorState::Opening { sender, .. }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1605 | send_room_actor_request(sender, RoomActorRequest::Opened { id })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1606 | return Ok(true); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1607 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1608 | Some(RoomActorState::Open { .. }) => return Ok(false), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1609 | Some(RoomActorState::Closing { pending, .. }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1610 | if pending.len() >= self.room_command_capacity { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1611 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1612 | CommandErrorKind::RoomBusy, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1613 | "room lifecycle queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1614 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1615 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1616 | pending.push_back(DeferredRoomLifecycle::Open(id)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1617 | return Ok(true); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1618 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1619 | None => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1620 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1621 | self.start_room_actor(room_id, vec![id]).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1622 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1623 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1624 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1625 | async fn start_room_actor( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1626 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1627 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1628 | open_ids: Vec<CommandId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1629 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1630 | let room = self.client.get_room(&room_id).ok_or_else(|| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1631 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1632 | CommandErrorKind::RoomUnknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1633 | "room is not available in the local store", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1634 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1635 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1636 | self.next_room_generation = self.next_room_generation.wrapping_add(1); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1637 | let generation = self.next_room_generation; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1638 | let (sender, receiver) = mpsc::channel(self.room_command_capacity); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1639 | let actor = RoomActor { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1640 | room_id: room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1641 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1642 | room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1643 | receiver, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1644 | events: self.port.event_sender(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1645 | engine_events: self.room_actor_event_sender.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1646 | media: self.media.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1647 | initial_history_limit: self.initial_history_limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1648 | open_ids, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1649 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1650 | let current_user_id = self.client.user_id().map(ToOwned::to_owned); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1651 | self.rooms.insert( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1652 | room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1653 | RoomActorState::Opening { generation, sender }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1654 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1655 | let actor_room_id = room_id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1656 | let engine_events = self.room_actor_event_sender.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1657 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1658 | TaskContext::RoomActor { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1659 | room_id: room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1660 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1661 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1662 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1663 | actor.run(current_user_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1664 | let _ = engine_events.send(RoomActorEvent::Exited { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1665 | room_id: actor_room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1666 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1667 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1668 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1669 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1670 | self.update_room_subscriptions().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1671 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1672 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1673 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1674 | fn spawn_room_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1675 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1676 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1677 | command: RoomCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1678 | ) -> Result<bool, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1679 | self.spawn_room_command_with_reply(id, command, None)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1680 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1681 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1682 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1683 | fn spawn_room_command_with_reply( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1684 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1685 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1686 | command: RoomCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1687 | reply: Option<oneshot::Sender<Result<OwnedRoomId, CommandResultError>>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1688 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1689 | let job = RoomJob { id, command, reply }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1690 | self.room_commands.try_send(job).map_err(|error| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1691 | let (job, kind, message) = match error { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1692 | mpsc::error::TrySendError::Full(job) => ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1693 | job, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1694 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1695 | "room command queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1696 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1697 | mpsc::error::TrySendError::Closed(job) => ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1698 | job, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1699 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1700 | "room command worker has stopped", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1701 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1702 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1703 | let error = CommandError::new(kind, message); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1704 | if let Some(reply) = job.reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1705 | let _ = reply.send(Err(CommandResultError::Failed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1706 | id: job.id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1707 | command: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1708 | kind: error.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1709 | message: error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1710 | }))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1711 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1712 | error |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1713 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1714 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1715 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1716 | fn route_timeline( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1717 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1718 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1719 | command: TimelineCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1720 | ) -> Result<bool, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1721 | let room_id = command.room_id(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1722 | let sender = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1723 | .rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1724 | .get(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1725 | .and_then(RoomActorState::sender) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1726 | .ok_or_else(|| CommandError::new(CommandErrorKind::RoomNotOpen, "room is not open"))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1727 | send_room_actor_request(sender, RoomActorRequest::Execute { id, command })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1728 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1729 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1730 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1731 | async fn close_room(&mut self, id: CommandId, room_id: &RoomId) -> Result<bool, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1732 | let Some(state) = self.rooms.get_mut(room_id) else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1733 | return Ok(false); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1734 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1735 | if let RoomActorState::Closing { pending, .. } = state { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1736 | if pending.len() >= self.room_command_capacity { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1737 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1738 | CommandErrorKind::RoomBusy, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1739 | "room lifecycle queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1740 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1741 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1742 | pending.push_back(DeferredRoomLifecycle::Close(id)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1743 | return Ok(true); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1744 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1745 | let Some(sender) = state.sender().cloned() else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1746 | return Ok(false); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1747 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1748 | let generation = state.generation(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1749 | send_room_actor_request(&sender, RoomActorRequest::Close { id })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1750 | self.rooms.insert( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1751 | room_id.to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1752 | RoomActorState::Closing { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1753 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1754 | pending: VecDeque::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1755 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1756 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1757 | self.update_room_subscriptions().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1758 | Ok(true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1759 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1760 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1761 | async fn handle_room_actor_event(&mut self, event: RoomActorEvent) -> Result<(), EngineError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1762 | match event { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1763 | RoomActorEvent::Ready { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1764 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1765 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1766 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1767 | let stale = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1768 | .rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1769 | .get(&room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1770 | .is_none_or(|state| state.generation() != generation); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1771 | if stale { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1772 | return Ok(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1773 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1774 | let Some(state) = self.rooms.remove(&room_id) else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1775 | return Ok(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1776 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1777 | match state { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1778 | RoomActorState::Opening { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1779 | generation: current, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1780 | sender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1781 | } if current == generation => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1782 | self.rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1783 | .insert(room_id, RoomActorState::Open { generation, sender }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1784 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1785 | state => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1786 | self.rooms.insert(room_id, state); |
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 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1790 | RoomActorEvent::Exited { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1791 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1792 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1793 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1794 | if self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1795 | .rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1796 | .get(&room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1797 | .is_none_or(|state| state.generation() != generation) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1798 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1799 | return Ok(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1800 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1801 | let state = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1802 | .rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1803 | .remove(&room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1804 | .expect("room generation was checked"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1805 | let pending = match state { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1806 | RoomActorState::Closing { pending, .. } => pending, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1807 | RoomActorState::Opening { .. } | RoomActorState::Open { .. } => VecDeque::new(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1808 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1809 | for operation in pending { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1810 | let (id, result) = match operation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1811 | DeferredRoomLifecycle::Open(id) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1812 | (id, self.open_room(id, room_id.clone()).await) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1813 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1814 | DeferredRoomLifecycle::Close(id) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1815 | (id, self.close_room(id, &room_id).await) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1816 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1817 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1818 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1819 | Ok(true) => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1820 | Ok(false) => emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1821 | &self.port.event_sender(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1822 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1823 | CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1824 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1825 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1826 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1827 | .map_err(|_| EngineError::EventReceiverClosed)?, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1828 | Err(error) => emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1829 | &self.port.event_sender(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1830 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1831 | CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1832 | Err(error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1833 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1834 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1835 | .map_err(|_| EngineError::EventReceiverClosed)?, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1836 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1837 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1838 | self.update_room_subscriptions().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1839 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1840 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1841 | Ok(()) |
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 | async fn update_room_subscriptions(&self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1845 | let room_ids = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1846 | .rooms |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1847 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1848 | .filter(|(_, state)| !matches!(state, RoomActorState::Closing { .. })) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1849 | .map(|(room_id, _)| room_id.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1850 | .collect::<Vec<_>>(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1851 | let room_refs = room_ids.iter().map(AsRef::as_ref).collect::<Vec<_>>(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1852 | self.sync |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1853 | .room_list_service() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1854 | .subscribe_to_rooms(&room_refs) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1855 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1856 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1857 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1858 | fn spawn_media_fetch(&mut self, command_id: CommandId, media_id: MediaId, size: MediaSize) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1859 | let client = self.client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1860 | let media = self.media.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1861 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1862 | let max_media_bytes = self.max_media_bytes; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1863 | let flights = self.media_flights.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1864 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1865 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1866 | id: command_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1867 | kind: CommandKind::Media, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1868 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1869 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1870 | let leader = { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1871 | let mut flights = flights.lock().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1872 | let waiters = flights.entry((media_id.clone(), size)).or_default(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1873 | waiters.push(command_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1874 | waiters.len() == 1 |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1875 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1876 | if !leader { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1877 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1878 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1879 | let result = fetch_media( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1880 | client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1881 | media, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1882 | events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1883 | max_media_bytes, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1884 | media_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1885 | size, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1886 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1887 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1888 | let waiters = { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1889 | let mut flights = flights.lock().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1890 | flights.remove(&(media_id, size)).unwrap_or_default() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1891 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1892 | let result = match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1893 | Ok(()) => Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1894 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1895 | let kind = error.kind; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1896 | let message = error.to_string(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1897 | Err((kind, message)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1898 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1899 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1900 | for waiter in waiters { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1901 | let waiter_result = match &result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1902 | Ok(()) => Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1903 | Err((kind, message)) => Err(CommandError::new(*kind, message.clone())), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1904 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1905 | if emit_command_result(&events, waiter, CommandKind::Media, waiter_result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1906 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1907 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1908 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1909 | tracing::debug!(?waiter, "media fetch result was not delivered"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1910 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1911 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1912 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1913 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1914 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1915 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1916 | async fn spawn_media_upload( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1917 | &mut self, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1918 | command_id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1919 | upload: AttachmentUpload, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1920 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1921 | let room_id = upload.timeline_id.room_id().clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1922 | let room = self.client.get_room(&room_id).ok_or_else(|| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1923 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1924 | CommandErrorKind::RoomUnknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1925 | "room is not available in the local store", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1926 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1927 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1928 | if self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1929 | .upload_watchers |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1930 | .get(&room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1931 | .is_some_and(|(watcher, _)| watcher.is_finished()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1932 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1933 | self.upload_watchers.remove(&room_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1934 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1935 | let ready = if let Some((_, ready)) = self.upload_watchers.get(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1936 | ready.clone() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1937 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1938 | let (ready_sender, ready) = watch::channel(None); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1939 | let watched_room_id = room_id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1940 | let progress_room_id = room_id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1941 | let queued_room = room.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1942 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1943 | let watcher = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1944 | self.tasks |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1945 | .spawn(TaskContext::Watcher("media upload watcher"), async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1946 | let (_, mut updates) = match queued_room.send_queue().subscribe().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1947 | Ok(subscription) => subscription, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1948 | Err(source) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1949 | tracing::warn!(room_id = %progress_room_id, error = %source, "reading media send queue failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1950 | ready_sender.send_replace(Some(Err(CommandErrorKind::Store))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1951 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1952 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1953 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1954 | ready_sender.send_replace(Some(Ok(()))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1955 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1956 | match updates.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1957 | Ok(RoomSendQueueUpdate::MediaUpload { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1958 | related_to, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1959 | file, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1960 | progress, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1961 | .. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1962 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1963 | if events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1964 | .send(Event::MediaUploadProgress { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1965 | room_id: progress_room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1966 | transaction_id: related_to, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1967 | uploaded_bytes: progress.current as u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1968 | total_bytes: progress.total as u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1969 | complete: file.is_some() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1970 | && progress.current == progress.total, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1971 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1972 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1973 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1974 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1975 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1976 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1977 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1978 | Ok(RoomSendQueueUpdate::SendError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1979 | transaction_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1980 | error, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1981 | is_recoverable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1982 | }) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1983 | let is_attachment = queued_room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1984 | .send_queue() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1985 | .subscribe() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1986 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1987 | .is_ok_and(|(echoes, _)| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1988 | echoes.iter().any(|echo| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1989 | echo.transaction_id == transaction_id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1990 | && is_attachment_echo(&echo.content) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1991 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1992 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1993 | if is_attachment |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1994 | && events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1995 | .send(Event::MediaUploadFailed { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1996 | room_id: progress_room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1997 | transaction_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1998 | kind: classify_sdk_error(&error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 1999 | recoverable: is_recoverable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2000 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2001 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2002 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2003 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2004 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2005 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2006 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2007 | Ok(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2008 | | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2009 | Err(tokio::sync::broadcast::error::RecvError::Closed) => break, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2010 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2011 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2012 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2013 | self.upload_watchers |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2014 | .insert(watched_room_id, (watcher, ready.clone())); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2015 | ready |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2016 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2017 | let max_media_bytes = self.max_media_bytes; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2018 | let events = self.port.event_sender(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2019 | self.tasks.spawn( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2020 | TaskContext::Command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2021 | id: command_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2022 | kind: CommandKind::Media, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2023 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2024 | async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2025 | let mut ready = ready; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2026 | let result = async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2027 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2028 | if let Some(result) = *ready.borrow_and_update() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2029 | result.map_err(|kind| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2030 | CommandError::new(kind, "reading media send queue failed") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2031 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2032 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2033 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2034 | ready.changed().await.map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2035 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2036 | CommandErrorKind::Store, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2037 | "media send queue watcher stopped", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2038 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2039 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2040 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2041 | queue_media_upload(room, upload, max_media_bytes).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2042 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2043 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2044 | if emit_command_result(&events, command_id, CommandKind::Media, result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2045 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2046 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2047 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2048 | tracing::debug!(?command_id, "media upload result was not delivered"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2049 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2050 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2051 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2052 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2053 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2054 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2055 | async fn shutdown(&mut self) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2056 | self.tasks.shutdown().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2057 | self.verification.lock().await.stop().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2058 | self.rooms.clear(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2059 | self.upload_watchers.clear(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2060 | self.media.lock().await.entries.clear(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2061 | self.sync.stop().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2062 | self.port |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2063 | .connection_sender() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2064 | .send_replace(ConnectionState::Stopped); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2065 | let _ = self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2066 | .port |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2067 | .send(Event::ConnectionChanged(ConnectionState::Stopped)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2068 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2069 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2070 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2071 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2072 | async fn mark_read( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2073 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2074 | timeline_id: &TimelineId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2075 | event_id: OwnedEventId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2076 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2077 | let room = client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2078 | .get_room(timeline_id.room_id()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2079 | .ok_or_else(room_unknown)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2080 | let receipt_thread = match timeline_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2081 | TimelineId::Room(_) => ReceiptThread::Main, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2082 | TimelineId::Thread { root_event_id, .. } => ReceiptThread::Thread(root_event_id.clone()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2083 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2084 | room.send_single_receipt(ReceiptType::Read, receipt_thread, event_id.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2085 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2086 | .map_err(|source| CommandError::from_sdk("sending read receipt failed", source))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2087 | if matches!(timeline_id, TimelineId::Room(_)) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2088 | room.send_single_receipt(ReceiptType::FullyRead, ReceiptThread::Unthreaded, event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2089 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2090 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2091 | CommandError::from_sdk("sending fully read receipt failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2092 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2093 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2094 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2095 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2096 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2097 | async fn set_typing(client: &Client, room_id: &RoomId, typing: bool) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2098 | let room = client.get_room(room_id).ok_or_else(room_unknown)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2099 | room.typing_notice(typing) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2100 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2101 | .map_err(|source| CommandError::from_sdk("typing update failed", source)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2102 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2103 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2104 | fn is_attachment_echo(content: &LocalEchoContent) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2105 | let LocalEchoContent::Event { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2106 | serialized_event, .. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2107 | } = content |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2108 | else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2109 | return false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2110 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2111 | matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2112 | serialized_event.deserialize(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2113 | Ok(AnyMessageLikeEventContent::RoomMessage(content)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2114 | if matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2115 | content.msgtype, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2116 | MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2117 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2118 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2119 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2120 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2121 | async fn send_handle( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2122 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2123 | room_id: &RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2124 | transaction_id: &matrix_sdk::ruma::TransactionId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2125 | ) -> Result<SendHandle, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2126 | let room = client.get_room(room_id).ok_or_else(room_unknown)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2127 | let (echoes, _) = room.send_queue().subscribe().await.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2128 | CommandError::with_source(CommandErrorKind::Store, "reading send queue failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2129 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2130 | echoes |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2131 | .into_iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2132 | .find_map(|echo| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2133 | if echo.transaction_id.as_str() != transaction_id.as_str() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2134 | return None; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2135 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2136 | match echo.content { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2137 | LocalEchoContent::Event { send_handle, .. } => Some(send_handle), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2138 | _ => None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2139 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2140 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2141 | .ok_or_else(|| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2142 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2143 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2144 | "send is no longer pending or is not a message", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2145 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2146 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2147 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2148 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2149 | async fn cancel_send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2150 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2151 | room_id: &RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2152 | transaction_id: &matrix_sdk::ruma::TransactionId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2153 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2154 | let handle = send_handle(client, room_id, transaction_id).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2155 | match handle.abort().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2156 | Ok(true) => Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2157 | Ok(false) => Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2158 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2159 | "send could not be cancelled because it has already been sent", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2160 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2161 | Err(source) => Err(CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2162 | CommandErrorKind::Store, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2163 | "cancelling send failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2164 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2165 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2166 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2167 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2168 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2169 | async fn retry_send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2170 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2171 | room_id: &RoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2172 | transaction_id: &matrix_sdk::ruma::TransactionId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2173 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2174 | let handle = send_handle(client, room_id, transaction_id).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2175 | handle.unwedge().await.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2176 | CommandError::with_source(CommandErrorKind::Store, "retrying send failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2177 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2178 | client.send_queue().set_enabled(true).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2179 | Ok(()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2180 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2181 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2182 | fn send_room_actor_request( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2183 | sender: &mpsc::Sender<RoomActorRequest>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2184 | request: RoomActorRequest, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2185 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2186 | sender.try_send(request).map_err(|error| match error { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2187 | mpsc::error::TrySendError::Full(_) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2188 | CommandError::new(CommandErrorKind::RoomBusy, "room command queue is full") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2189 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2190 | mpsc::error::TrySendError::Closed(_) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2191 | CommandError::new(CommandErrorKind::RoomNotOpen, "room is not open") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2192 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2193 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2194 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2195 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2196 | fn domain_queue_error<T>(error: mpsc::error::TrySendError<T>, domain: &str) -> CommandError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2197 | match error { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2198 | mpsc::error::TrySendError::Full(_) => CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2199 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2200 | format!("{domain} command queue is full"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2201 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2202 | mpsc::error::TrySendError::Closed(_) => CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2203 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2204 | format!("{domain} command worker has stopped"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2205 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2206 | } |
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 | async fn run_room_command_worker( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2210 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2211 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2212 | mut commands: mpsc::Receiver<RoomJob>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2213 | queue_limit: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2214 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2215 | let mut queues = HashMap::<RoomMutationKey, VecDeque<RoomJob>>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2216 | let mut running = HashSet::<RoomMutationKey>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2217 | let mut tasks = JoinSet::<RoomJobCompletion>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2218 | let mut receiving = true; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2219 | let mut queued = 0_usize; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2220 | while receiving || !tasks.is_empty() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2221 | tokio::select! { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2222 | biased; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2223 | completed = tasks.join_next(), if !tasks.is_empty() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2224 | let completion = match completed { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2225 | Some(Ok(completion)) => completion, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2226 | Some(Err(error)) => panic!("room command task failed: {error}"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2227 | None => continue, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2228 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2229 | let key = completion.key.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2230 | emit_room_job_result(&events, completion).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2231 | let next = queues.get_mut(&key).and_then(VecDeque::pop_front); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2232 | if next.is_some() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2233 | queued -= 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2234 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2235 | if queues.get(&key).is_some_and(VecDeque::is_empty) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2236 | queues.remove(&key); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2237 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2238 | if let Some(job) = next { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2239 | spawn_room_job(&mut tasks, client.clone(), key, job); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2240 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2241 | running.remove(&key); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2242 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2243 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2244 | job = commands.recv(), if receiving => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2245 | match job { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2246 | Some(job) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2247 | let key = room_mutation_key(job.id, &job.command); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2248 | if running.insert(key.clone()) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2249 | spawn_room_job(&mut tasks, client.clone(), key, job); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2250 | } else if queued < queue_limit { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2251 | queues.entry(key).or_default().push_back(job); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2252 | queued += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2253 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2254 | let failure = CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2255 | id: job.id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2256 | command: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2257 | kind: CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2258 | message: "room command queue is full".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2259 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2260 | if let Some(reply) = job.reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2261 | let _ = reply.send(Err(CommandResultError::Failed(failure))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2262 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2263 | let _ = events.send(Event::CommandFailed(failure)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2264 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2265 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2266 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2267 | None => receiving = false, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2268 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2269 | } |
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 | fn spawn_room_job( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2275 | tasks: &mut JoinSet<RoomJobCompletion>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2276 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2277 | key: RoomMutationKey, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2278 | job: RoomJob, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2279 | ) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2280 | tasks.spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2281 | let RoomJob { id, command, reply } = job; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2282 | let result = AssertUnwindSafe(execute_room_command(&client, command)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2283 | .catch_unwind() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2284 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2285 | .unwrap_or_else(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2286 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2287 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2288 | "room command panicked", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2289 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2290 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2291 | RoomJobCompletion { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2292 | key, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2293 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2294 | reply, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2295 | result, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2296 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2297 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2298 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2299 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2300 | fn room_mutation_key(id: CommandId, command: &RoomCommand) -> RoomMutationKey { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2301 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2302 | RoomCommand::Join { room, .. } if room.is_room_id() => RoomMutationKey::Room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2303 | OwnedRoomId::try_from(room.clone()).expect("room ID variant was checked"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2304 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2305 | RoomCommand::Join { room, .. } => RoomMutationKey::Target(room.to_string()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2306 | RoomCommand::Leave(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2307 | | RoomCommand::Forget(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2308 | | RoomCommand::AcceptInvite(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2309 | | RoomCommand::RejectInvite(room_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2310 | | RoomCommand::InviteUser { room_id, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2311 | | RoomCommand::KickUser { room_id, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2312 | | RoomCommand::BanUser { room_id, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2313 | | RoomCommand::UnbanUser { room_id, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2314 | | RoomCommand::SetPowerLevels { room_id, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2315 | | RoomCommand::SetUserPowerLevel { room_id, .. } => RoomMutationKey::Room(room_id.clone()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2316 | RoomCommand::CreateDirect(user_id) => RoomMutationKey::Direct(user_id.clone()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2317 | _ => RoomMutationKey::Independent(id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2318 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2319 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2320 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2321 | async fn emit_room_job_result(events: &EventSender, completion: RoomJobCompletion) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2322 | let RoomJobCompletion { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2323 | id, reply, result, .. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2324 | } = completion; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2325 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2326 | Ok(Some(room_id)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2327 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2328 | let _ = reply.send(Ok(room_id.clone())); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2329 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2330 | let _ = emit_command_result_with_room(events, id, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2331 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2332 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2333 | Ok(None) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2334 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2335 | let _ = reply.send(Err(CommandResultError::Failed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2336 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2337 | command: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2338 | kind: CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2339 | message: "room operation did not return a room ID".to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2340 | }))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2341 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2342 | let _ = emit_command_result(events, id, CommandKind::Room, Ok(())).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2343 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2344 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2345 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2346 | let failure = CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2347 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2348 | command: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2349 | kind: error.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2350 | message: error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2351 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2352 | if let Some(reply) = reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2353 | let _ = reply.send(Err(CommandResultError::Failed(failure))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2354 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2355 | let _ = events.send(Event::CommandFailed(failure)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2356 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2357 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2358 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2359 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2360 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2361 | async fn execute_room_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2362 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2363 | command: RoomCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2364 | ) -> Result<Option<OwnedRoomId>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2365 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2366 | RoomCommand::Join { room, via } => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2367 | .join_room_by_id_or_alias(&room, &via) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2368 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2369 | .map(|room| Some(room.room_id().to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2370 | .map_err(|source| CommandError::from_sdk("joining room failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2371 | RoomCommand::Leave(room_id) => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2372 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2373 | .leave() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2374 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2375 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2376 | .map_err(|source| CommandError::from_sdk("leaving room failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2377 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2378 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2379 | RoomCommand::Forget(room_id) => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2380 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2381 | .forget() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2382 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2383 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2384 | .map_err(|source| CommandError::from_sdk("forgetting room failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2385 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2386 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2387 | RoomCommand::AcceptInvite(room_id) => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2388 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2389 | .join() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2390 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2391 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2392 | .map_err(|source| CommandError::from_sdk("accepting invite failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2393 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2394 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2395 | RoomCommand::RejectInvite(room_id) => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2396 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2397 | .leave() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2398 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2399 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2400 | .map_err(|source| CommandError::from_sdk("rejecting invite failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2401 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2402 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2403 | RoomCommand::InviteUser { room_id, user_id } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2404 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2405 | .invite_user_by_id(&user_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2406 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2407 | .map(|()| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2408 | .map_err(|source| CommandError::from_sdk("inviting user failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2409 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2410 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2411 | RoomCommand::KickUser { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2412 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2413 | user_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2414 | reason, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2415 | } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2416 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2417 | .kick_user(&user_id, reason.as_deref()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2418 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2419 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2420 | .map_err(|source| CommandError::from_sdk("kicking user failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2421 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2422 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2423 | RoomCommand::BanUser { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2424 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2425 | user_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2426 | reason, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2427 | } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2428 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2429 | .ban_user(&user_id, reason.as_deref()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2430 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2431 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2432 | .map_err(|source| CommandError::from_sdk("banning user failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2433 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2434 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2435 | RoomCommand::UnbanUser { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2436 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2437 | user_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2438 | reason, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2439 | } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2440 | Some(room) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2441 | .unban_user(&user_id, reason.as_deref()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2442 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2443 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2444 | .map_err(|source| CommandError::from_sdk("unbanning user failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2445 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2446 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2447 | RoomCommand::SetPowerLevels { room_id, changes } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2448 | Some(room) => apply_power_level_changes(&room, changes) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2449 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2450 | .map(|_| None), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2451 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2452 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2453 | RoomCommand::SetUserPowerLevel { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2454 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2455 | user_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2456 | level, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2457 | } => match client.get_room(&room_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2458 | Some(room) => match matrix_sdk::ruma::Int::try_from(level) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2459 | Ok(level) => room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2460 | .update_power_levels(vec![(&user_id, level)]) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2461 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2462 | .map(|_| None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2463 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2464 | CommandError::from_sdk("updating user power level failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2465 | }), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2466 | Err(_) => Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2467 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2468 | "power level is outside the Matrix integer range", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2469 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2470 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2471 | None => Err(room_unknown()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2472 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2473 | RoomCommand::Create(options) => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2474 | .create_room(create_room_request(options)) |
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("creating room failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2478 | RoomCommand::CreateDirect(user_id) => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2479 | .create_dm(&user_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2480 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2481 | .map(|room| Some(room.room_id().to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2482 | .map_err(|source| CommandError::from_sdk("creating direct room failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2483 | _ => Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2484 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2485 | "unsupported room command", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2486 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2487 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2488 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2489 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2490 | fn room_unknown() -> CommandError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2491 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2492 | CommandErrorKind::RoomUnknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2493 | "room is not available in the local store", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2494 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2495 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2496 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2497 | const fn sdk_notification_mode( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2498 | mode: RoomNotificationMode, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2499 | ) -> matrix_sdk::notification_settings::RoomNotificationMode { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2500 | match mode { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2501 | RoomNotificationMode::AllMessages => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2502 | matrix_sdk::notification_settings::RoomNotificationMode::AllMessages |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2503 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2504 | RoomNotificationMode::MentionsAndKeywordsOnly => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2505 | matrix_sdk::notification_settings::RoomNotificationMode::MentionsAndKeywordsOnly |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2506 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2507 | RoomNotificationMode::Mute => matrix_sdk::notification_settings::RoomNotificationMode::Mute, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2508 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2509 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2510 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2511 | async fn execute_notification_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2512 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2513 | command: NotificationCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2514 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2515 | use matrix_sdk::notification_settings::{IsEncrypted, IsOneToOne}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2516 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2517 | let settings = client.notification_settings().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2518 | let result = match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2519 | NotificationCommand::SetRoomMode { room_id, mode } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2520 | settings |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2521 | .set_room_notification_mode(&room_id, sdk_notification_mode(mode)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2522 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2523 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2524 | NotificationCommand::ResetRoomMode(room_id) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2525 | settings.delete_user_defined_room_rules(&room_id).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2526 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2527 | NotificationCommand::SetDefaultMode { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2528 | encrypted, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2529 | one_to_one, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2530 | mode, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2531 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2532 | settings |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2533 | .set_default_room_notification_mode( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2534 | IsEncrypted::from(encrypted), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2535 | IsOneToOne::from(one_to_one), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2536 | sdk_notification_mode(mode), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2537 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2538 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2539 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2540 | NotificationCommand::AddKeyword(keyword) => settings.add_keyword(keyword).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2541 | NotificationCommand::RemoveKeyword(keyword) => settings.remove_keyword(&keyword).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2542 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2543 | result.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2544 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2545 | CommandErrorKind::Server, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2546 | "updating notification settings failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2547 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2548 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2549 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2550 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2551 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2552 | async fn execute_encryption_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2553 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2554 | events: &EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2555 | verification: &mut VerificationController, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2556 | command: EncryptionCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2557 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2558 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2559 | EncryptionCommand::Recover(key) => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2560 | .encryption() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2561 | .recovery() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2562 | .recover(&key) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2563 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2564 | .map_err(|source| CommandError::from_recovery("account recovery failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2565 | EncryptionCommand::StartVerification(target) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2566 | verification.start(client, events.clone(), target).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2567 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2568 | EncryptionCommand::Verify(action) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2569 | verification.apply(client, events.clone(), action).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2570 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2571 | command => execute_key_backup_command(client, command).await, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2572 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2573 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2574 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2575 | async fn execute_key_backup_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2576 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2577 | command: EncryptionCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2578 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2579 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2580 | EncryptionCommand::CreateKeyBackup => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2581 | .encryption() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2582 | .backups() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2583 | .create() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2584 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2585 | .map_err(|source| CommandError::from_sdk("creating key backup failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2586 | EncryptionCommand::EnableKeyBackup => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2587 | .encryption() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2588 | .recovery() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2589 | .enable_backup() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2590 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2591 | .map_err(|source| CommandError::from_recovery("enabling key backup failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2592 | EncryptionCommand::DisableKeyBackup { delete_from_server } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2593 | let backups = client.encryption().backups(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2594 | if delete_from_server { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2595 | backups.disable_and_delete().await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2596 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2597 | backups.disable().await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2598 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2599 | .map_err(|source| CommandError::from_sdk("disabling key backup failed", source)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2600 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2601 | EncryptionCommand::RecoverKeyBackup(key) => client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2602 | .encryption() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2603 | .recovery() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2604 | .recover_and_fix_backup(&key) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2605 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2606 | .map_err(|source| CommandError::from_recovery("recovering key backup failed", source)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2607 | _ => unreachable!("only key backup commands are spawned here"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2608 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2609 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2610 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2611 | async fn apply_power_level_changes( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2612 | room: &Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2613 | changes: PowerLevelChanges, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2614 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2615 | use matrix_sdk::ruma::events::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2616 | MessageLikeEventType, StateEventType, room::power_levels::RoomPowerLevelsEventContent, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2617 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2618 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2619 | let mut levels = room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2620 | .power_levels() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2621 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2622 | .map_err(|source| CommandError::from_sdk("reading power levels failed", source.into()))?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2623 | if let Some(value) = changes.ban { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2624 | levels.ban = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2625 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2626 | if let Some(value) = changes.invite { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2627 | levels.invite = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2628 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2629 | if let Some(value) = changes.kick { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2630 | levels.kick = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2631 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2632 | if let Some(value) = changes.redact { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2633 | levels.redact = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2634 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2635 | if let Some(value) = changes.events_default { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2636 | levels.events_default = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2637 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2638 | if let Some(value) = changes.state_default { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2639 | levels.state_default = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2640 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2641 | if let Some(value) = changes.users_default { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2642 | levels.users_default = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2643 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2644 | if let Some(value) = changes.room_notification { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2645 | levels.notifications.room = matrix_power_level(value)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2646 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2647 | for (event_type, value) in [ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2648 | (StateEventType::RoomName.into(), changes.room_name), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2649 | (StateEventType::RoomAvatar.into(), changes.room_avatar), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2650 | (StateEventType::RoomTopic.into(), changes.room_topic), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2651 | (StateEventType::SpaceChild.into(), changes.space_child), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2652 | (MessageLikeEventType::Beacon.into(), changes.beacon), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2653 | (StateEventType::BeaconInfo.into(), changes.beacon_info), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2654 | ] { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2655 | if let Some(value) = value { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2656 | levels.events.insert(event_type, matrix_power_level(value)?); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2657 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2658 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2659 | let content = RoomPowerLevelsEventContent::try_from(levels).map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2660 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2661 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2662 | "normalizing power levels failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2663 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2664 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2665 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2666 | room.send_state_event(content) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2667 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2668 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2669 | .map_err(|source| CommandError::from_sdk("updating power levels failed", source)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2670 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2671 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2672 | fn matrix_power_level(value: i64) -> Result<matrix_sdk::ruma::Int, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2673 | value.try_into().map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2674 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2675 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2676 | "power level is outside the Matrix integer range", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2677 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2678 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2679 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2680 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2681 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2682 | fn create_room_request( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2683 | options: CreateRoomOptions, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2684 | ) -> matrix_sdk::ruma::api::client::room::create_room::v3::Request { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2685 | use matrix_sdk::ruma::api::client::room::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2686 | Visibility as SdkVisibility, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2687 | create_room::v3::{Request, RoomPreset as SdkPreset}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2688 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2689 | let mut request = Request::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2690 | request.name = options.name; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2691 | request.topic = options.topic; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2692 | request.room_alias_name = options.alias_localpart; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2693 | request.invite = options.invite; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2694 | request.is_direct = options.direct; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2695 | request.visibility = match options.visibility { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2696 | RoomVisibility::Private => SdkVisibility::Private, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2697 | RoomVisibility::Public => SdkVisibility::Public, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2698 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2699 | request.preset = options.preset.map(|preset| match preset { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2700 | RoomPreset::PrivateChat => SdkPreset::PrivateChat, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2701 | RoomPreset::TrustedPrivateChat => SdkPreset::TrustedPrivateChat, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2702 | RoomPreset::PublicChat => SdkPreset::PublicChat, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2703 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2704 | request |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2705 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2706 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2707 | async fn queue_media_upload( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2708 | room: Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2709 | upload: AttachmentUpload, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2710 | max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2711 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2712 | let (upload, mime, mut config) = prepare_upload(upload, max_media_bytes)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2713 | if let TimelineId::Thread { root_event_id, .. } = &upload.timeline_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2714 | config.reply = Some(SdkReply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2715 | event_id: root_event_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2716 | enforce_thread: EnforceThread::Threaded(ReplyWithinThread::No), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2717 | add_mentions: AddMentions::No, |
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 | room.send_queue() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2721 | .send_attachment(upload.filename, mime, upload.bytes, config) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2722 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2723 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2724 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2725 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2726 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2727 | "media upload could not be queued", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2728 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2729 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2730 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2731 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2732 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2733 | async fn fetch_media( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2734 | client: Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2735 | registry: Arc<Mutex<MediaRegistry>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2736 | events: EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2737 | max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2738 | media_id: MediaId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2739 | size: MediaSize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2740 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2741 | let media = registry |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2742 | .lock() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2743 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2744 | .entries |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2745 | .get(&media_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2746 | .cloned() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2747 | .ok_or_else(|| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2748 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2749 | CommandErrorKind::MediaUnknown, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2750 | "media is not registered in a retained timeline", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2751 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2752 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2753 | let (source, format, mimetype, declared_size) = match size { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2754 | MediaSize::Full => ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2755 | media.full.source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2756 | MediaFormat::File, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2757 | media.full.mimetype, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2758 | media.full.declared_size, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2759 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2760 | MediaSize::Thumbnail => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2761 | if let Some(thumbnail) = media.thumbnail { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2762 | ( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2763 | thumbnail.source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2764 | MediaFormat::File, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2765 | thumbnail.mimetype, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2766 | thumbnail.declared_size, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2767 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2768 | } else if matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2769 | media.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2770 | crate::MediaKind::Image | crate::MediaKind::Video |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2771 | ) && matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2772 | &media.full.source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2773 | matrix_sdk::ruma::events::room::MediaSource::Plain(_) |
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 | media.full.source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2777 | MediaFormat::Thumbnail(MediaThumbnailSettings::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2778 | 800_u32.into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2779 | 800_u32.into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2780 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2781 | media.full.mimetype, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2782 | None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2783 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2784 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2785 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2786 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2787 | "the requested media size is unavailable", |
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 | if declared_size.is_some_and(|size| size > max_media_bytes as u64) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2793 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2794 | CommandErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2795 | "media exceeds the configured byte limit", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2796 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2797 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2798 | let request = MediaRequestParameters { source, format }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2799 | if let Some(cached) = cached_media(&client, &request).await? { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2800 | if cached.len() > max_media_bytes { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2801 | remove_oversize_media(&client, &request).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2802 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2803 | CommandErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2804 | "cached media exceeds the configured byte limit", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2805 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2806 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2807 | let bytes = maybe_decrypt_media(cached, &request, max_media_bytes)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2808 | events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2809 | .send(Event::MediaReady { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2810 | media_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2811 | size, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2812 | bytes: Arc::from(bytes), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2813 | mimetype: mimetype.unwrap_or_else(|| "application/octet-stream".into()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2814 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2815 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2816 | .map_err(send_error)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2817 | return Ok(()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2818 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2819 | let bytes = download_bounded(&client, &request, max_media_bytes).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2820 | cache_media(&client, &request, &bytes).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2821 | let bytes = maybe_decrypt_media(bytes, &request, max_media_bytes)?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2822 | events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2823 | .send(Event::MediaReady { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2824 | media_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2825 | size, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2826 | bytes: Arc::from(bytes), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2827 | mimetype: mimetype.unwrap_or_else(|| "application/octet-stream".into()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2828 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2829 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2830 | .map_err(send_error) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2831 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2832 | async fn cached_media( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2833 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2834 | request: &MediaRequestParameters, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2835 | ) -> Result<Option<Vec<u8>>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2836 | client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2837 | .media_store() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2838 | .lock() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2839 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2840 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2841 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2842 | CommandErrorKind::Store, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2843 | "reading media cache failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2844 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2845 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2846 | })? |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2847 | .get_media_content(request) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2848 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2849 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2850 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2851 | CommandErrorKind::Store, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2852 | "reading media cache failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2853 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2854 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2855 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2856 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2857 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2858 | async fn remove_oversize_media(client: &Client, request: &MediaRequestParameters) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2859 | if let Ok(store) = client.media_store().lock().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2860 | let _ = store.remove_media_content(request).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2861 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2862 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2863 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2864 | async fn cache_media(client: &Client, request: &MediaRequestParameters, bytes: &[u8]) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2865 | if let Ok(store) = client.media_store().lock().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2866 | let _ = store |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2867 | .add_media_content( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2868 | request, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2869 | bytes.to_vec(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2870 | matrix_sdk::media::store::IgnoreMediaRetentionPolicy::No, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2871 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2872 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2873 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2874 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2875 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2876 | fn maybe_decrypt_media( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2877 | bytes: Vec<u8>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2878 | request: &MediaRequestParameters, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2879 | max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2880 | ) -> Result<Vec<u8>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2881 | let matrix_sdk::ruma::events::room::MediaSource::Encrypted(file) = &request.source else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2882 | return Ok(bytes); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2883 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2884 | let info: MediaEncryptionInfo = (**file).clone().into(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2885 | let mut cursor = std::io::Cursor::new(bytes); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2886 | let mut decryptor = AttachmentDecryptor::new(&mut cursor, info).map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2887 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2888 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2889 | "media decryption failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2890 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2891 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2892 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2893 | let mut decrypted = Vec::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2894 | let mut chunk = [0_u8; 8192]; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2895 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2896 | match decryptor.read(&mut chunk) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2897 | Ok(0) => break, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2898 | Ok(read) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2899 | if read > max_media_bytes.saturating_sub(decrypted.len()) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2900 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2901 | CommandErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2902 | "decrypted media exceeds the configured byte limit", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2903 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2904 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2905 | decrypted.extend_from_slice(&chunk[..read]); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2906 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2907 | Err(source) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2908 | return Err(CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2909 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2910 | "media decryption failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2911 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2912 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2913 | } |
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 | Ok(decrypted) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2917 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2918 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2919 | async fn download_bounded( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2920 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2921 | request: &MediaRequestParameters, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2922 | max_media_bytes: usize, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2923 | ) -> Result<Vec<u8>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2924 | if is_local_media(&request.source) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2925 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2926 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2927 | "local media is not in the media cache", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2928 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2929 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2930 | let http_request = media_http_request(client, request).await?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2931 | let http_client = client.http_client().clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2932 | let request = http_client |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2933 | .execute(request_from_http(http_request)?) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2934 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2935 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2936 | CommandError::with_source(CommandErrorKind::Network, "media download failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2937 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2938 | let status = request.status(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2939 | if status == matrix_sdk::reqwest::StatusCode::UNAUTHORIZED { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2940 | client.refresh_access_token().await.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2941 | CommandError::from_refresh_token("refreshing media access token failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2942 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2943 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2944 | CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2945 | "media download was not authorized", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2946 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2947 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2948 | if !status.is_success() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2949 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2950 | CommandErrorKind::MediaUnavailable, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2951 | format!("media download failed with status {status}"), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2952 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2953 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2954 | if request |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2955 | .content_length() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2956 | .is_some_and(|length| length > max_media_bytes as u64) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2957 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2958 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2959 | CommandErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2960 | "media exceeds the configured byte limit", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2961 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2962 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2963 | let mut stream = request.bytes_stream(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2964 | let mut bytes = Vec::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2965 | while let Some(chunk) = stream.next().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2966 | let chunk = chunk.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2967 | CommandError::with_source(CommandErrorKind::Network, "media download failed", source) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2968 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2969 | if chunk.len() > max_media_bytes.saturating_sub(bytes.len()) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2970 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2971 | CommandErrorKind::MediaTooLarge, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2972 | "downloaded media exceeds the configured byte limit", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2973 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2974 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2975 | bytes.extend_from_slice(&chunk); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2976 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2977 | Ok(bytes) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2978 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2979 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2980 | fn is_local_media(source: &matrix_sdk::ruma::events::room::MediaSource) -> bool { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2981 | let uri = match source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2982 | matrix_sdk::ruma::events::room::MediaSource::Plain(uri) => uri, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2983 | matrix_sdk::ruma::events::room::MediaSource::Encrypted(file) => &file.url, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2984 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2985 | let Ok(server) = uri.server_name() else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2986 | return false; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2987 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2988 | server.as_str() == "send-queue.localhost" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2989 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2990 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2991 | async fn media_http_request( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2992 | client: &Client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2993 | request: &MediaRequestParameters, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2994 | ) -> Result<http::Request<Vec<u8>>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2995 | use matrix_sdk::ruma::api::client::{authenticated_media, media}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2996 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2997 | let versions = client.supported_versions().await.map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2998 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 2999 | CommandErrorKind::Network, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3000 | "reading supported server versions failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3001 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3002 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3003 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3004 | let use_auth = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3005 | authenticated_media::get_content::v1::Request::PATH_BUILDER.is_supported(&versions); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3006 | let base = client.homeserver().to_string(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3007 | let token = client.access_token(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3008 | let build = |request: http::Request<Vec<u8>>| -> Result<http::Request<Vec<u8>>, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3009 | let mut request = request; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3010 | if let Some(token) = token.as_deref() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3011 | request.headers_mut().insert( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3012 | http::header::AUTHORIZATION, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3013 | format!("Bearer {token}").parse().map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3014 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3015 | CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3016 | "stored access token is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3017 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3018 | })?, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3019 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3020 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3021 | Ok(request) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3022 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3023 | match &request.source { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3024 | matrix_sdk::ruma::events::room::MediaSource::Encrypted(file) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3025 | if use_auth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3026 | let endpoint = authenticated_media::get_content::v1::Request::from_uri(&file.url) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3027 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3028 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3029 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3030 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3031 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3032 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3033 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3034 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3035 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3036 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3037 | SendAccessToken::IfRequired(token.as_deref().unwrap_or("")), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3038 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3039 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3040 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3041 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3042 | CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3043 | "media request could not be authorized", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3044 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3045 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3046 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3047 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3048 | #[allow(deprecated)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3049 | let endpoint = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3050 | media::get_content::v3::Request::from_url(&file.url).map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3051 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3052 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3053 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3054 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3055 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3056 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3057 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3058 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3059 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3060 | SendAccessToken::None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3061 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3062 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3063 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3064 | CommandError::new(CommandErrorKind::Other, "media request is invalid") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3065 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3066 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3067 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3068 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3069 | matrix_sdk::ruma::events::room::MediaSource::Plain(uri) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3070 | if let MediaFormat::Thumbnail(settings) = &request.format { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3071 | if use_auth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3072 | let mut endpoint = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3073 | authenticated_media::get_content_thumbnail::v1::Request::from_uri( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3074 | uri, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3075 | settings.width, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3076 | settings.height, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3077 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3078 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3079 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3080 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3081 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3082 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3083 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3084 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3085 | endpoint.method = Some(settings.method.clone()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3086 | endpoint.animated = Some(settings.animated); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3087 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3088 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3089 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3090 | SendAccessToken::IfRequired(token.as_deref().unwrap_or("")), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3091 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3092 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3093 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3094 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3095 | CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3096 | "media request could not be authorized", |
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 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3100 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3101 | #[allow(deprecated)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3102 | let endpoint = { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3103 | let mut endpoint = media::get_content_thumbnail::v3::Request::from_url( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3104 | uri, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3105 | settings.width, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3106 | settings.height, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3107 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3108 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3109 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3110 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3111 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3112 | source, |
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 | endpoint.method = Some(settings.method.clone()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3116 | endpoint.animated = Some(settings.animated); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3117 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3118 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3119 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3120 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3121 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3122 | SendAccessToken::None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3123 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3124 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3125 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3126 | CommandError::new(CommandErrorKind::Other, "media request is invalid") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3127 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3128 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3129 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3130 | } else if use_auth { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3131 | let endpoint = authenticated_media::get_content::v1::Request::from_uri(uri) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3132 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3133 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3134 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3135 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3136 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3137 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3138 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3139 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3140 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3141 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3142 | SendAccessToken::IfRequired(token.as_deref().unwrap_or("")), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3143 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3144 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3145 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3146 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3147 | CommandErrorKind::Unauthenticated, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3148 | "media request could not be authorized", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3149 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3150 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3151 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3152 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3153 | #[allow(deprecated)] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3154 | let endpoint = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3155 | media::get_content::v3::Request::from_url(uri).map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3156 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3157 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3158 | "media URI is invalid", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3159 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3160 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3161 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3162 | endpoint |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3163 | .try_into_http_request::<Vec<u8>>( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3164 | &base, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3165 | SendAccessToken::None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3166 | Cow::Borrowed(&versions), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3167 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3168 | .map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3169 | CommandError::new(CommandErrorKind::Other, "media request is invalid") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3170 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3171 | .and_then(build) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3172 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3173 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3174 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3175 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3176 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3177 | fn request_from_http( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3178 | request: http::Request<Vec<u8>>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3179 | ) -> Result<matrix_sdk::reqwest::Request, CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3180 | let (parts, _) = request.into_parts(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3181 | let mut builder = matrix_sdk::reqwest::Client::new().get(parts.uri.to_string()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3182 | for (name, value) in parts.headers.iter() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3183 | builder = builder.header(name.as_str(), value.as_bytes()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3184 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3185 | builder |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3186 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3187 | .map_err(|_| CommandError::new(CommandErrorKind::Network, "media request is invalid")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3188 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3189 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3190 | impl RoomActor { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3191 | async fn run(mut self, current_user_id: Option<ruma::OwnedUserId>) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3192 | let timeline = match self |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3193 | .room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3194 | .timeline_builder() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3195 | .with_focus(TimelineFocus::Live { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3196 | hide_threaded_events: true, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3197 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3198 | .track_read_marker_and_receipts(TimelineReadReceiptTracking::AllEvents) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3199 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3200 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3201 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3202 | Ok(timeline) => timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3203 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3204 | tracing::warn!(error = %error, room_id = %self.room_id, "room actor could not build its timeline"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3205 | let message = format!("room timeline build failed: {error}"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3206 | for id in self.open_ids.drain(..) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3207 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3208 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3209 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3210 | CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3211 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3212 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3213 | message.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3214 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3215 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3216 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3217 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3218 | self.receiver.close(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3219 | while let Some(request) = self.receiver.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3220 | let (id, command) = match request { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3221 | RoomActorRequest::Opened { id } | RoomActorRequest::Close { id } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3222 | (id, CommandKind::Room) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3223 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3224 | RoomActorRequest::Execute { id, .. } => (id, CommandKind::Timeline), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3225 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3226 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3227 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3228 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3229 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3230 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3231 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3232 | "room timeline build failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3233 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3234 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3235 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3236 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3237 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3238 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3239 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3240 | let room_timeline_id = TimelineId::Room(self.room_id.clone()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3241 | let mut room_timeline = match TimelineSession::start( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3242 | room_timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3243 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3244 | self.events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3245 | self.media.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3246 | current_user_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3247 | self.initial_history_limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3248 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3249 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3250 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3251 | Ok(timeline) => timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3252 | Err(_) => return, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3253 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3254 | let _ = self.engine_events.send(RoomActorEvent::Ready { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3255 | room_id: self.room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3256 | generation: self.generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3257 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3258 | for id in self.open_ids.drain(..) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3259 | if emit_command_result(&self.events, id, CommandKind::Room, Ok(())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3260 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3261 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3262 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3263 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3264 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3265 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3266 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3267 | let (typing_guard, mut typing_rx) = self.room.subscribe_to_typing_notifications(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3268 | let typing_sender = self.events.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3269 | let typing_room_id = self.room_id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3270 | let mut room_tasks = JoinSet::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3271 | room_tasks.spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3272 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3273 | match typing_rx.recv().await { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3274 | Ok(users) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3275 | let state = TypingState { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3276 | room_id: typing_room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3277 | users, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3278 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3279 | if typing_sender |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3280 | .send(Event::TypingChanged(state)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3281 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3282 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3283 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3284 | tracing::debug!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3285 | room_id = %typing_room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3286 | "typing watcher stopped because the event receiver closed" |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3287 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3288 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3289 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3290 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3291 | Err(tokio::sync::broadcast::error::RecvError::Lagged(skipped)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3292 | tracing::warn!(room_id = %typing_room_id, skipped, "typing watcher lagged"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3293 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3294 | Err(tokio::sync::broadcast::error::RecvError::Closed) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3295 | tracing::debug!(room_id = %typing_room_id, "typing watcher stopped"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3296 | break; |
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 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3302 | enum QueuedThreadCommand { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3303 | Open { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3304 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3305 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3306 | Execute { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3307 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3308 | command: TimelineCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3309 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3310 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3311 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3312 | struct OpeningThread { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3313 | generation: u64, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3314 | abort: AbortHandle, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3315 | task_id: TaskId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3316 | queued: VecDeque<QueuedThreadCommand>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3317 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3318 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3319 | let mut thread_timelines = HashMap::<OwnedEventId, TimelineSession>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3320 | let mut opening_threads = HashMap::<OwnedEventId, OpeningThread>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3321 | let mut thread_task_ids = HashMap::<TaskId, OwnedEventId>::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3322 | let mut thread_open_tasks = JoinSet::new(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3323 | let mut next_thread_generation = 0_u64; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3324 | let mut close_id = None; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3325 | let thread_queue_cap = self.receiver.max_capacity(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3326 | let mut queued_thread_count = 0; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3327 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3328 | 'actor: loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3329 | tokio::select! { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3330 | request = self.receiver.recv() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3331 | let Some(request) = request else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3332 | tracing::debug!(room_id = %self.room_id, "room actor stopped because its request channel closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3333 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3334 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3335 | match request { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3336 | RoomActorRequest::Opened { id } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3337 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3338 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3339 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3340 | CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3341 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3342 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3343 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3344 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3345 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3346 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3347 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3348 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3349 | RoomActorRequest::Execute { id, command } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3350 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3351 | TimelineCommand::OpenThread { root_event_id, .. } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3352 | if thread_timelines.contains_key(&root_event_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3353 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3354 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3355 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3356 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3357 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3358 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3359 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3360 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3361 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3362 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3363 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3364 | } else if let Some(opening) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3365 | opening_threads.get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3366 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3367 | if queued_thread_count >= thread_queue_cap { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3368 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3369 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3370 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3371 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3372 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3373 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3374 | "thread command queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3375 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3376 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3377 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3378 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3379 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3380 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3381 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3382 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3383 | opening |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3384 | .queued |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3385 | .push_back(QueuedThreadCommand::Open { id }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3386 | queued_thread_count += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3387 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3388 | } else if queued_thread_count >= thread_queue_cap { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3389 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3390 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3391 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3392 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3393 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3394 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3395 | "thread command queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3396 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3397 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3398 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3399 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3400 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3401 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3402 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3403 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3404 | let generation = next_thread_generation; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3405 | next_thread_generation = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3406 | next_thread_generation.wrapping_add(1); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3407 | let task_root = root_event_id.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3408 | let room = self.room.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3409 | let abort = thread_open_tasks.spawn(async move { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3410 | let result = room |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3411 | .timeline_builder() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3412 | .with_focus(TimelineFocus::Thread { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3413 | root_event_id: root_event_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3414 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3415 | .track_read_marker_and_receipts( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3416 | TimelineReadReceiptTracking::AllEvents, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3417 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3418 | .build() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3419 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3420 | (root_event_id, generation, result) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3421 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3422 | let task_id = abort.id(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3423 | thread_task_ids.insert(task_id, task_root.clone()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3424 | opening_threads.insert( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3425 | task_root, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3426 | OpeningThread { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3427 | generation, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3428 | abort, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3429 | task_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3430 | queued: VecDeque::from([ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3431 | QueuedThreadCommand::Open { id }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3432 | ]), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3433 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3434 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3435 | queued_thread_count += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3436 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3437 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3438 | TimelineCommand::CloseThread { root_event_id, .. } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3439 | if let Some(timeline) = thread_timelines.remove(&root_event_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3440 | timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3441 | .close( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3442 | CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3443 | "thread closed while pagination was in progress", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3444 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3445 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3446 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3447 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3448 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3449 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3450 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3451 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3452 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3453 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3454 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3455 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3456 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3457 | } else if let Some(opening) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3458 | opening_threads.remove(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3459 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3460 | thread_task_ids.remove(&opening.task_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3461 | queued_thread_count -= opening.queued.len(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3462 | opening.abort.abort(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3463 | for queued in opening.queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3464 | let queued_id = match queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3465 | QueuedThreadCommand::Open { id } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3466 | QueuedThreadCommand::Execute { id, .. } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3467 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3468 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3469 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3470 | queued_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3471 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3472 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3473 | CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3474 | "thread was closed before it finished opening", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3475 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3476 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3477 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3478 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3479 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3480 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3481 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3482 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3483 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3484 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3485 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3486 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3487 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3488 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3489 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3490 | } else if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3491 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3492 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3493 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3494 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3495 | CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3496 | "thread timeline is not open", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3497 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3498 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3499 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3500 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3501 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3502 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3503 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3504 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3505 | TimelineCommand::Paginate { timeline_id, limit } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3506 | match timeline_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3507 | TimelineId::Room(_) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3508 | room_timeline.paginate(limit, Some(id)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3509 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3510 | TimelineId::Thread { room_id, root_event_id } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3511 | if let Some(timeline) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3512 | thread_timelines.get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3513 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3514 | timeline.paginate(limit, Some(id)).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3515 | } else if let Some(opening) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3516 | opening_threads.get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3517 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3518 | if queued_thread_count >= thread_queue_cap { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3519 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3520 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3521 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3522 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3523 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3524 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3525 | "thread command queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3526 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3527 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3528 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3529 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3530 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3531 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3532 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3533 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3534 | opening.queued.push_back( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3535 | QueuedThreadCommand::Execute { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3536 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3537 | command: |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3538 | TimelineCommand::Paginate { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3539 | timeline_id: |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3540 | TimelineId::Thread { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3541 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3542 | root_event_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3543 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3544 | limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3545 | }, |
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 | queued_thread_count += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3549 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3550 | } else if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3551 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3552 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3553 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3554 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3555 | CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3556 | "timeline is not open", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3557 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3558 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3559 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3560 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3561 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3562 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3563 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3564 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3565 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3566 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3567 | command => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3568 | let thread_target = match command |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3569 | .timeline_id() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3570 | .expect("non-lifecycle timeline command has a timeline id") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3571 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3572 | TimelineId::Room(_) => None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3573 | TimelineId::Thread { root_event_id, .. } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3574 | Some(root_event_id.clone()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3575 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3576 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3577 | match thread_target { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3578 | None => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3579 | let result = execute_timeline_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3580 | &mut room_timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3581 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3582 | None, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3583 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3584 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3585 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3586 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3587 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3588 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3589 | result, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3590 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3591 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3592 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3593 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3594 | tracing::debug!(room_id = %self.room_id, "room actor stopped because the event receiver closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3595 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3596 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3597 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3598 | Some(root_event_id) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3599 | if let Some(timeline) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3600 | thread_timelines.get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3601 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3602 | let thread_root = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3603 | timeline.id.thread_root().cloned(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3604 | let result = execute_timeline_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3605 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3606 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3607 | thread_root.as_ref(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3608 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3609 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3610 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3611 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3612 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3613 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3614 | result, |
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 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3618 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3619 | tracing::debug!(room_id = %self.room_id, "room actor stopped because the event receiver closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3620 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3621 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3622 | } else if let Some(opening) = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3623 | opening_threads.get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3624 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3625 | if queued_thread_count >= thread_queue_cap { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3626 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3627 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3628 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3629 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3630 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3631 | CommandErrorKind::QueueFull, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3632 | "thread command queue is full", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3633 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3634 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3635 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3636 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3637 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3638 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3639 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3640 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3641 | opening.queued.push_back( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3642 | QueuedThreadCommand::Execute { id, command }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3643 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3644 | queued_thread_count += 1; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3645 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3646 | } else if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3647 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3648 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3649 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3650 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3651 | CommandErrorKind::TimelineNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3652 | "timeline is not open", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3653 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3654 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3655 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3656 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3657 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3658 | tracing::debug!(room_id = %self.room_id, "room actor stopped because the event receiver closed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3659 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3660 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3661 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3662 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3663 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3664 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3665 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3666 | RoomActorRequest::Close { id } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3667 | close_id = Some(id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3668 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3669 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3670 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3671 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3672 | completed = thread_open_tasks.join_next(), if !thread_open_tasks.is_empty() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3673 | match completed { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3674 | Some(Ok((root_event_id, generation, result))) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3675 | if opening_threads |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3676 | .get(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3677 | .is_none_or(|opening| opening.generation != generation) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3678 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3679 | continue; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3680 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3681 | let opening = opening_threads |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3682 | .remove(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3683 | .expect("thread generation was checked"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3684 | thread_task_ids.remove(&opening.task_id); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3685 | queued_thread_count -= opening.queued.len(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3686 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3687 | Ok(timeline) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3688 | let timeline_id = TimelineId::thread( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3689 | self.room_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3690 | root_event_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3691 | ); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3692 | match TimelineSession::start( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3693 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3694 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3695 | self.events.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3696 | self.media.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3697 | current_user_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3698 | self.initial_history_limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3699 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3700 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3701 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3702 | Ok(session) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3703 | thread_timelines |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3704 | .insert(root_event_id.clone(), session); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3705 | let timeline = thread_timelines |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3706 | .get_mut(&root_event_id) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3707 | .expect("thread session was inserted"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3708 | for queued in opening.queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3709 | match queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3710 | QueuedThreadCommand::Open { id } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3711 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3712 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3713 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3714 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3715 | Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3716 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3717 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3718 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3719 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3720 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3721 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3722 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3723 | QueuedThreadCommand::Execute { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3724 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3725 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3726 | } => match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3727 | TimelineCommand::Paginate { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3728 | limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3729 | .. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3730 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3731 | timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3732 | .paginate(limit, Some(id)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3733 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3734 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3735 | command => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3736 | let thread_root = timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3737 | .id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3738 | .thread_root() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3739 | .cloned(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3740 | let result = |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3741 | execute_timeline_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3742 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3743 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3744 | thread_root.as_ref(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3745 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3746 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3747 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3748 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3749 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3750 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3751 | result, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3752 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3753 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3754 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3755 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3756 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3757 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3758 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3759 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3760 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3761 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3762 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3763 | Err(_) => break 'actor, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3764 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3765 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3766 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3767 | tracing::warn!(error = %error, room_id = %self.room_id, %root_event_id, "thread timeline build failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3768 | let message = format!("thread timeline build failed: {error}"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3769 | for queued in opening.queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3770 | let queued_id = match queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3771 | QueuedThreadCommand::Open { id } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3772 | QueuedThreadCommand::Execute { id, .. } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3773 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3774 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3775 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3776 | queued_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3777 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3778 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3779 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3780 | message.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3781 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3782 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3783 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3784 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3785 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3786 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3787 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3788 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3789 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3790 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3791 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3792 | Some(Err(error)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3793 | if let Some(root_event_id) = thread_task_ids.remove(&error.id()) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3794 | tracing::warn!(error = %error, room_id = %self.room_id, %root_event_id, "thread open task failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3795 | if let Some(opening) = opening_threads.remove(&root_event_id) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3796 | queued_thread_count -= opening.queued.len(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3797 | for queued in opening.queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3798 | let queued_id = match queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3799 | QueuedThreadCommand::Open { id } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3800 | QueuedThreadCommand::Execute { id, .. } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3801 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3802 | if emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3803 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3804 | queued_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3805 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3806 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3807 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3808 | "thread timeline build task failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3809 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3810 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3811 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3812 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3813 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3814 | break 'actor; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3815 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3816 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3817 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3818 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3819 | tracing::debug!(error = %error, room_id = %self.room_id, "thread open task was aborted"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3820 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3821 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3822 | None => {} |
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 | completed = room_tasks.join_next(), if !room_tasks.is_empty() => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3826 | match completed { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3827 | Some(Err(error)) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3828 | tracing::warn!(error = %error, room_id = %self.room_id, "room actor task failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3829 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3830 | Some(Ok(())) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3831 | tracing::warn!(room_id = %self.room_id, "room actor task stopped unexpectedly"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3832 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3833 | None => {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3834 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3835 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3836 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3837 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3838 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3839 | drop(typing_guard); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3840 | room_tasks.abort_all(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3841 | while room_tasks.join_next().await.is_some() {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3842 | thread_task_ids.clear(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3843 | let pending_opens = opening_threads |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3844 | .drain() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3845 | .flat_map(|(_, opening)| opening.queued) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3846 | .collect::<Vec<_>>(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3847 | for queued in pending_opens { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3848 | let queued_id = match queued { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3849 | QueuedThreadCommand::Open { id } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3850 | QueuedThreadCommand::Execute { id, .. } => id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3851 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3852 | let _ = emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3853 | &self.events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3854 | queued_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3855 | CommandKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3856 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3857 | CommandErrorKind::RoomNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3858 | "room closed while thread timeline was opening", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3859 | )), |
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 | thread_open_tasks.abort_all(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3864 | while thread_open_tasks.join_next().await.is_some() {} |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3865 | for (_, timeline) in thread_timelines { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3866 | timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3867 | .close( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3868 | CommandErrorKind::RoomNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3869 | "room closed while thread pagination was in progress", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3870 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3871 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3872 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3873 | room_timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3874 | .close( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3875 | CommandErrorKind::RoomNotOpen, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3876 | "room closed while pagination was in progress", |
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 | if let Some(id) = close_id |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3880 | && emit_command_result(&self.events, id, CommandKind::Room, Ok(())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3881 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3882 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3883 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3884 | tracing::debug!(room_id = %self.room_id, "room close acknowledgement was not delivered"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3885 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3886 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3887 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3888 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3889 | async fn execute_timeline_command( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3890 | session: &mut TimelineSession, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3891 | command: TimelineCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3892 | thread_root: Option<&OwnedEventId>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3893 | ) -> Result<(), CommandError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3894 | let timeline = &session.timeline; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3895 | match command { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3896 | TimelineCommand::OpenThread { .. } | TimelineCommand::CloseThread { .. } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3897 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3898 | CommandErrorKind::Other, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3899 | "thread lifecycle command was misrouted", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3900 | )) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3901 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3902 | TimelineCommand::Resync { .. } => session.resync().await.map_err(|_| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3903 | CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3904 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3905 | "timeline resync could not emit a reset", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3906 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3907 | }), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3908 | TimelineCommand::Paginate { .. } => Ok(()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3909 | TimelineCommand::Send { content, .. } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3910 | let body = match &content { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3911 | MessageContent::Text { content, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3912 | | MessageContent::Emote { content, .. } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3913 | | MessageContent::Reply { content, .. } => &content.body, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3914 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3915 | if body.trim().is_empty() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3916 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3917 | CommandErrorKind::EmptyBody, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3918 | "message body is empty", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3919 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3920 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3921 | match content { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3922 | MessageContent::Text { content, mentions } => timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3923 | .send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3924 | message_content(content, mentions, false) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3925 | .with_relation(None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3926 | .into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3927 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3928 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3929 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3930 | .map_err(timeline_error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3931 | MessageContent::Emote { content, mentions } => timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3932 | .send( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3933 | message_content(content, mentions, true) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3934 | .with_relation(None) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3935 | .into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3936 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3937 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3938 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3939 | .map_err(timeline_error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3940 | MessageContent::Reply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3941 | event_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3942 | content, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3943 | mentions, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3944 | notify_replied_to, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3945 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3946 | let content = timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3947 | .room() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3948 | .make_reply_event( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3949 | message_content(content, mentions, false), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3950 | SdkReply { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3951 | event_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3952 | enforce_thread: if thread_root.is_some() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3953 | EnforceThread::Threaded(ReplyWithinThread::Yes) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3954 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3955 | EnforceThread::MaybeThreaded |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3956 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3957 | add_mentions: if notify_replied_to { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3958 | AddMentions::Yes |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3959 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3960 | AddMentions::No |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3961 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3962 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3963 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3964 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3965 | .map_err(|source| { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3966 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3967 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3968 | "reply operation failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3969 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3970 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3971 | })?; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3972 | if let Some(thread_root) = thread_root |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3973 | && !matches!( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3974 | content.relates_to.as_ref(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3975 | Some(Relation::Thread(thread)) if &thread.event_id == thread_root |
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 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3979 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3980 | "reply target does not belong to the selected thread", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3981 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3982 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3983 | timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3984 | .send(content.into()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3985 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3986 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3987 | .map_err(timeline_error) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3988 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3989 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3990 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3991 | TimelineCommand::Edit { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3992 | event_id, content, .. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3993 | } => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3994 | let (content, mentions, emote) = match content { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3995 | MessageEditContent::Text { content, mentions } => (content, mentions, false), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3996 | MessageEditContent::Emote { content, mentions } => (content, mentions, true), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3997 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3998 | if content.body.trim().is_empty() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3999 | return Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4000 | CommandErrorKind::EmptyBody, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4001 | "message body is empty", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4002 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4003 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4004 | timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4005 | .edit( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4006 | &TimelineEventItemId::EventId(event_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4007 | EditedContent::RoomMessage(message_content(content, mentions, emote)), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4008 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4009 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4010 | .map_err(timeline_error) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4011 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4012 | TimelineCommand::Redact { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4013 | event_id, reason, .. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4014 | } => timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4015 | .redact(&TimelineEventItemId::EventId(event_id), reason.as_deref()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4016 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4017 | .map_err(timeline_error), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4018 | TimelineCommand::ToggleReaction { event_id, key, .. } => timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4019 | .toggle_reaction(&TimelineEventItemId::EventId(event_id), &key) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4020 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4021 | .map(|_| ()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4022 | .map_err(timeline_error), |
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 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4026 | fn message_content( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4027 | content: RichText, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4028 | mentions: MessageMentions, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4029 | emote: bool, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4030 | ) -> RoomMessageEventContentWithoutRelation { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4031 | let content = match content.formatted_body { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4032 | Some(formatted_body) if emote => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4033 | RoomMessageEventContentWithoutRelation::emote_html(content.body, formatted_body) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4034 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4035 | Some(formatted_body) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4036 | RoomMessageEventContentWithoutRelation::text_html(content.body, formatted_body) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4037 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4038 | None if emote => RoomMessageEventContentWithoutRelation::emote_plain(content.body), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4039 | None => RoomMessageEventContentWithoutRelation::text_plain(content.body), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4040 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4041 | let content = content.add_mentions(Mentions::with_user_ids(mentions.user_ids)); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4042 | if mentions.room { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4043 | content.add_mentions(Mentions::with_room_mention()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4044 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4045 | content |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4046 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4047 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4048 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4049 | async fn run_pagination(request: PaginationRequest) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4050 | let PaginationRequest { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4051 | timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4052 | events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4053 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4054 | reached_start, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4055 | limit, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4056 | command_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4057 | results, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4058 | } = request; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4059 | // One worker per timeline preserves pagination order without accumulating waiting tasks. |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4060 | if reached_start.load(Ordering::Acquire) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4061 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4062 | .send(Event::HistoryChanged { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4063 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4064 | state: HistoryState::StartReached, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4065 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4066 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4067 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4068 | let _ = results.send((id, Ok(()))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4069 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4070 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4071 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4072 | if events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4073 | .send(Event::HistoryChanged { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4074 | timeline_id: timeline_id.clone(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4075 | state: HistoryState::Loading, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4076 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4077 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4078 | .is_err() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4079 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4080 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4081 | let _ = results.send(( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4082 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4083 | Err(CommandError::new( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4084 | CommandErrorKind::EventReceiverClosed, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4085 | "event receiver has closed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4086 | )), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4087 | )); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4088 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4089 | return; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4090 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4091 | let result = timeline |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4092 | .paginate_backwards(limit.max(1)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4093 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4094 | .map_err(timeline_error); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4095 | match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4096 | Ok(at_start) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4097 | reached_start.store(at_start, Ordering::Release); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4098 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4099 | .send(Event::HistoryChanged { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4100 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4101 | state: if at_start { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4102 | HistoryState::StartReached |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4103 | } else { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4104 | HistoryState::Idle |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4105 | }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4106 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4107 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4108 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4109 | let _ = results.send((id, Ok(()))); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4110 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4111 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4112 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4113 | let _ = events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4114 | .send(Event::HistoryChanged { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4115 | timeline_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4116 | state: HistoryState::Failed(error.to_string()), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4117 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4118 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4119 | if let Some(id) = command_id { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4120 | let _ = results.send((id, Err(error))); |
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 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4124 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4125 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4126 | async fn emit_command_result_with_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4127 | events: &EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4128 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4129 | room_id: OwnedRoomId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4130 | ) -> Result<(), SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4131 | events |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4132 | .send(Event::CommandSucceededWithRoom { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4133 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4134 | command: CommandKind::Room, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4135 | room_id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4136 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4137 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4138 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4139 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4140 | async fn emit_command_result( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4141 | events: &EventSender, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4142 | id: CommandId, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4143 | command: CommandKind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4144 | result: Result<(), CommandError>, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4145 | ) -> Result<(), SendEventError> { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4146 | let event = match result { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4147 | Ok(()) => Event::CommandSucceeded { id, command }, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4148 | Err(error) => { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4149 | tracing::warn!(?id, ?command, error = ?error, "room actor command failed"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4150 | Event::CommandFailed(CommandFailure { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4151 | id, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4152 | command, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4153 | kind: error.kind, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4154 | message: error.to_string(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4155 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4156 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4157 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4158 | events.send(event).await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4159 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4160 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4161 | fn send_error(source: SendEventError) -> CommandError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4162 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4163 | CommandErrorKind::EventReceiverClosed, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4164 | "event receiver has closed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4165 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4166 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4167 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4168 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4169 | fn timeline_error(source: matrix_sdk_ui::timeline::Error) -> CommandError { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4170 | CommandError::with_source( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4171 | CommandErrorKind::Timeline, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4172 | "timeline operation failed", |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4173 | source, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4174 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4175 | } |