31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | use std::{future::Future, panic::AssertUnwindSafe}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 3 | use futures_util::FutureExt; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 4 | use matrix_sdk::encryption::verification::VerificationRequest; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | use matrix_sdk::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | notification_settings::{IsEncrypted, IsOneToOne, RoomNotificationMode as SdkMode}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | ruma::{ |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 9 | OwnedDeviceId, OwnedEventId, OwnedMxcUri, RoomId, TransactionId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 10 | api::client::receipt::create_receipt::v3::ReceiptType, events::receipt::ReceiptThread, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | send_queue::{LocalEchoContent, SendHandle}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 13 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | use tokio::sync::mpsc; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 15 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | use crate::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 17 | EncryptionCommand, Error, ErrorKind, InteractiveAuth, InteractiveAuthChallenge, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | NotificationCommand, RoomNotificationMode, TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 19 | encryption::VerificationController, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | transport::{EventSender, Responder}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | pub(super) struct Job<T> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 24 | pub(super) command: T, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 25 | pub(super) reply: Responder, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | /// a panicking command answers with an error instead of taking its worker down with it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | pub(super) async fn guarded<T>( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | label: &'static str, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | work: impl Future<Output = Result<T, Error>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | ) -> Result<T, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | AssertUnwindSafe(work) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | .catch_unwind() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | .unwrap_or_else(|_| Err(Error::new(ErrorKind::Other, format!("{label} panicked")))) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | pub(super) async fn run_notifications( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | mut jobs: mpsc::Receiver<Job<NotificationCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | while let Some(Job { command, reply }) = jobs.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | reply.done(guarded("notification command", notification(&client, command)).await); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | pub(super) async fn run_encryption( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | mut jobs: mpsc::Receiver<Job<EncryptionCommand>>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 52 | mut incoming: mpsc::UnboundedReceiver<VerificationRequest>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | let mut verification = VerificationController::default(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 55 | loop { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 56 | tokio::select! { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 57 | job = jobs.recv() => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 58 | let Some(Job { command, reply }) = job else { return }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 59 | let work = encryption(&client, &events, &mut verification, command); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 60 | reply.done(guarded("encryption command", work).await); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 61 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 62 | Some(request) = incoming.recv() => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 63 | verification.adopt_incoming(events.clone(), request).await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 64 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 65 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | async fn encryption( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | events: &EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | verification: &mut VerificationController, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | command: EncryptionCommand, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | ) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | let encryption = client.encryption(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | EncryptionCommand::Recover(key) => encryption |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | .recovery() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | .recover(&key) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | .map_err(|source| Error::from_recovery("account recovery failed", source)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | EncryptionCommand::RecoverKeyBackup(key) => encryption |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | .recovery() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | .recover_and_fix_backup(&key) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | .map_err(|source| Error::from_recovery("recovering key backup failed", source)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | EncryptionCommand::CreateKeyBackup => encryption |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | .backups() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | .create() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | .map_err(|source| Error::from_sdk("creating key backup failed", source)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | EncryptionCommand::EnableKeyBackup => encryption |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | .recovery() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | .enable_backup() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | .map_err(|source| Error::from_recovery("enabling key backup failed", source)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | EncryptionCommand::DisableKeyBackup { delete_from_server } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | let backups = encryption.backups(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | if delete_from_server { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | backups.disable_and_delete().await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | } else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | backups.disable().await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | .map_err(|source| Error::from_sdk("disabling key backup failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | EncryptionCommand::StartVerification(target) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | verification.start(client, events.clone(), target).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | EncryptionCommand::Verify(action) => verification.apply(events.clone(), action).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | async fn notification(client: &Client, command: NotificationCommand) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | let settings = client.notification_settings().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | NotificationCommand::SetRoomMode { room_id, mode } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | settings |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | .set_room_notification_mode(&room_id, sdk_mode(mode)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | NotificationCommand::ResetRoomMode(room_id) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | settings.delete_user_defined_room_rules(&room_id).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | NotificationCommand::SetDefaultMode { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | encrypted, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | one_to_one, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | mode, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | settings |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | .set_default_room_notification_mode( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | IsEncrypted::from(encrypted), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | IsOneToOne::from(one_to_one), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | sdk_mode(mode), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | NotificationCommand::AddKeyword(keyword) => settings.add_keyword(keyword).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | NotificationCommand::RemoveKeyword(keyword) => settings.remove_keyword(&keyword).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | .map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | Error::with_source( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | ErrorKind::Server, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | "updating notification settings failed", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 144 | source, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 145 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 146 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 148 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 149 | const fn sdk_mode(mode: RoomNotificationMode) -> SdkMode { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 150 | match mode { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 151 | RoomNotificationMode::AllMessages => SdkMode::AllMessages, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 152 | RoomNotificationMode::MentionsAndKeywordsOnly => SdkMode::MentionsAndKeywordsOnly, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 153 | RoomNotificationMode::Mute => SdkMode::Mute, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | fn room(client: &Client, room_id: &RoomId) -> Result<matrix_sdk::Room, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 158 | client.get_room(room_id).ok_or_else(Error::room_unknown) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 161 | /// the closed-room path. without a timeline nothing stops the marker going backwards |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 162 | pub(super) async fn mark_read( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 163 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 164 | timeline_id: &TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 165 | event_id: OwnedEventId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 166 | ) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 167 | let room = room(client, timeline_id.room_id())?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 168 | let receipt_failed = |source| Error::from_sdk("sending read receipt failed", source); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 169 | let thread = match timeline_id.thread_root() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 170 | None => ReceiptThread::Main, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 171 | Some(root) => ReceiptThread::Thread(root.clone()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 172 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 173 | room.send_single_receipt(ReceiptType::Read, thread, event_id.clone()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 174 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | .map_err(receipt_failed)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 176 | if timeline_id.thread_root().is_none() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 177 | room.send_single_receipt(ReceiptType::FullyRead, ReceiptThread::Unthreaded, event_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 178 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 179 | .map_err(receipt_failed)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 180 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 181 | Ok(()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 182 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 183 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 184 | pub(super) async fn set_typing( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 185 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 186 | room_id: &RoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 187 | typing: bool, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 188 | ) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 189 | room(client, room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 190 | .typing_notice(typing) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 191 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 192 | .map_err(|source| Error::from_sdk("typing update failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 193 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | async fn send_handle( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 196 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 197 | room_id: &RoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 198 | transaction_id: &TransactionId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 199 | ) -> Result<SendHandle, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 200 | let (echoes, _) = room(client, room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 201 | .send_queue() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 202 | .subscribe() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 203 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 204 | .map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 205 | Error::with_source(ErrorKind::Store, "reading send queue failed", source) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 206 | })?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 207 | echoes |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 208 | .into_iter() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 209 | .find_map(|echo| match echo.content { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 210 | LocalEchoContent::Event { send_handle, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 211 | if echo.transaction_id.as_str() == transaction_id.as_str() => |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 212 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 213 | Some(send_handle) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 214 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | _ => None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 216 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 217 | .ok_or_else(|| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 218 | Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 219 | ErrorKind::Invalid, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 220 | "send is no longer pending or is not a message", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 221 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 222 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 223 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 224 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 225 | pub(super) async fn cancel_send( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | room_id: &RoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | transaction_id: &TransactionId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | ) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 230 | match send_handle(client, room_id, transaction_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 231 | .await? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | .abort() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 235 | Ok(true) => Ok(()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 236 | Ok(false) => Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 237 | ErrorKind::Invalid, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 238 | "send could not be cancelled because it has already been sent", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | Err(source) => Err(Error::with_source( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | ErrorKind::Store, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | "cancelling send failed", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | source, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 245 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 246 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 248 | pub(super) async fn retry_send( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 249 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 250 | room_id: &RoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 251 | transaction_id: &TransactionId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | ) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 253 | send_handle(client, room_id, transaction_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 254 | .await? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | .unwedge() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 257 | .map_err(|source| Error::with_source(ErrorKind::Store, "retrying send failed", source))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 258 | room(client, room_id)?.send_queue().set_enabled(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 259 | Ok(()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 261 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 262 | pub(super) async fn upload_avatar( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 263 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | mimetype: &str, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | bytes: Vec<u8>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | max_bytes: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 267 | ) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 268 | if bytes.len() > max_bytes { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 269 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 270 | ErrorKind::MediaTooLarge, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 271 | "avatar exceeds the configured byte limit", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 272 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 273 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 274 | let mimetype: mime::Mime = mimetype |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 275 | .parse() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 276 | .map_err(|_| Error::new(ErrorKind::Invalid, "avatar MIME type is invalid"))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 277 | if mimetype.type_() != mime::IMAGE { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 278 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 279 | ErrorKind::Invalid, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 280 | "an avatar has to be an image", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 281 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 282 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 283 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 284 | .account() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 285 | .upload_avatar(&mimetype, bytes) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 286 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 287 | .map(drop) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 288 | .map_err(|source| Error::from_sdk("uploading avatar failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 289 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 290 | |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 291 | pub(super) async fn upload_media( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 292 | client: &Client, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 293 | mimetype: &str, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 294 | bytes: Vec<u8>, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 295 | max_bytes: usize, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 296 | ) -> Result<OwnedMxcUri, Error> { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 297 | if bytes.len() > max_bytes { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 298 | return Err(Error::new( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 299 | ErrorKind::MediaTooLarge, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 300 | "media exceeds the configured byte limit", |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 301 | )); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 302 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 303 | let mimetype: mime::Mime = mimetype |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 304 | .parse() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 305 | .map_err(|_| Error::new(ErrorKind::Invalid, "media MIME type is invalid"))?; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 306 | client |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 307 | .media() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 308 | .upload(&mimetype, bytes, None) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 309 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 310 | .map(|response| response.content_uri) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 311 | .map_err(|source| Error::from_sdk("uploading media failed", source)) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 312 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 313 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | pub(super) async fn rename_device( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 315 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | device_id: &OwnedDeviceId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | display_name: &str, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | ) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | .rename_device(device_id, display_name) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | .map(drop) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 323 | .map_err(|source| Error::from_http("renaming device failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 326 | pub(super) async fn delete_devices( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | client: &Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | device_ids: &[OwnedDeviceId], |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | auth: Option<InteractiveAuth>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 330 | ) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 331 | let user_id = client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 332 | .user_id() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 333 | .ok_or_else(|| Error::new(ErrorKind::Unauthenticated, "the client is not logged in"))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 334 | let auth = auth |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 335 | .map(|auth| auth.into_sdk(user_id)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | .transpose() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | .map_err(|message| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 338 | Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 339 | ErrorKind::Invalid, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 340 | format!("interactive authentication data is invalid, {message}"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 341 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 342 | })?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | match client.delete_devices(device_ids, auth).await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | Ok(_) => Ok(()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 345 | Err(source) => match source.as_uiaa_response() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 346 | Some(info) => Err(Error::interactive_auth(InteractiveAuthChallenge::from( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 347 | info, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 348 | ))), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 349 | None => Err(Error::from_http("deleting devices failed", source)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 351 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 352 | } |