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