Commit 5b585cd970fd
tarball1 file changed+62-3
@@ -4,10 +4,14 @@ use futures_util::FutureExt;
44
use matrix_sdk::encryption::verification::VerificationRequest;55
use matrix_sdk::{66
Client,7
- notification_settings::{IsEncrypted, IsOneToOne, RoomNotificationMode as SdkMode},7
+ notification_settings::{8
+ IsEncrypted, IsOneToOne, NotificationSettings, RoomNotificationMode as SdkMode,9
+ },810
ruma::{911
OwnedDeviceId, OwnedEventId, OwnedMxcUri, RoomId, TransactionId,10
- api::client::receipt::create_receipt::v3::ReceiptType, events::receipt::ReceiptThread,12
+ api::client::{push::set_pushrule_actions, receipt::create_receipt::v3::ReceiptType},13
+ events::receipt::ReceiptThread,14
+ push::{Action, HighlightTweakValue, RuleKind, SoundTweakValue, Tweak},1115
},1216
send_queue::{LocalEchoContent, SendHandle},1317
};@@ -134,7 +138,12 @@ async fn notification(client: &Client, command: NotificationCommand) -> Result<(
134138
)135139
.await136140
}137
- NotificationCommand::AddKeyword(keyword) => settings.add_keyword(keyword).await,141
+ NotificationCommand::AddKeyword(keyword) => {142
+ match settings.add_keyword(keyword.clone()).await {143
+ Ok(()) => return highlight_keyword(client, &settings, &keyword).await,144
+ Err(error) => Err(error),145
+ }146
+ }138147
NotificationCommand::RemoveKeyword(keyword) => settings.remove_keyword(&keyword).await,139148
}140149
.map_err(|source| {@@ -146,6 +155,56 @@ async fn notification(client: &Client, command: NotificationCommand) -> Result<(
146155
})147156
}148157
158
+/// sdk keyword rules carry only notify and a sound, while Element's loud keywords also159
+/// highlight, which is what lets the inbox keep them as mentions160
+async fn highlight_keyword(161
+ client: &Client,162
+ settings: &NotificationSettings,163
+ keyword: &str,164
+) -> Result<(), Error> {165
+ let ruleset = settings.ruleset().await;166
+ for rule in ruleset167
+ .content168
+ .iter()169
+ .filter(|rule| !rule.default && rule.pattern == keyword)170
+ .filter(|rule| !rule.actions.iter().any(Action::is_highlight))171
+ {172
+ client173
+ .send(set_pushrule_actions::v3::Request::new(174
+ RuleKind::Content,175
+ rule.rule_id.clone(),176
+ keyword_actions(&rule.actions),177
+ ))178
+ .await179
+ .map_err(|source| {180
+ Error::from_sdk("highlighting a notification keyword failed", source.into())181
+ })?;182
+ }183
+ Ok(())184
+}185
+186
+fn keyword_actions(existing: &[Action]) -> Vec<Action> {187
+ let mut actions: Vec<_> = existing188
+ .iter()189
+ .filter(|action| !matches!(action, Action::SetTweak(Tweak::Highlight(_))))190
+ .cloned()191
+ .collect();192
+ if !actions193
+ .iter()194
+ .any(|action| matches!(action, Action::Notify))195
+ {196
+ actions.insert(0, Action::Notify);197
+ }198
+ if !actions199
+ .iter()200
+ .any(|action| matches!(action, Action::SetTweak(Tweak::Sound(_))))201
+ {202
+ actions.push(Action::SetTweak(Tweak::Sound(SoundTweakValue::Default)));203
+ }204
+ actions.push(Action::SetTweak(Tweak::Highlight(HighlightTweakValue::Yes)));205
+ actions206
+}207
+149208
const fn sdk_mode(mode: RoomNotificationMode) -> SdkMode {150209
match mode {151210
RoomNotificationMode::AllMessages => SdkMode::AllMessages,