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