Skip to content

Commit 33af4fc

Browse files
committed
feat(push): add experimental support for MSC3768 (in-app-only notifications)
Signed-off-by: Johannes Marbach <[email protected]>
1 parent 92192c5 commit 33af4fc

File tree

12 files changed

+137
-47
lines changed

12 files changed

+137
-47
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/matrix-sdk-ffi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ crate-type = [
2626
[features]
2727
default = ["bundled-sqlite", "unstable-msc4274"]
2828
bundled-sqlite = ["matrix-sdk/bundled-sqlite"]
29+
unstable-msc3768 = ["matrix-sdk-ui/unstable-msc3768"]
2930
unstable-msc4274 = ["matrix-sdk-ui/unstable-msc4274"]
3031
# Required when targeting a Javascript environment, like Wasm in a browser.
3132
js = ["matrix-sdk-ui/js"]

bindings/matrix-sdk-ffi/src/notification_settings.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,13 @@ impl TryFrom<Tweak> for SdkTweak {
322322
#[derive(Clone, uniffi::Enum)]
323323
/// Enum representing the push notification actions for a rule.
324324
pub enum Action {
325-
/// Causes matching events to generate a notification.
325+
/// Causes matching events to generate a notification (both in-app and
326+
/// remote / push).
326327
Notify,
328+
/// Causes matching events to generate an in-app notification but no remote
329+
/// / push notification.
330+
#[cfg(feature = "unstable-msc3768")]
331+
NotifyInApp,
327332
/// Sets an entry in the 'tweaks' dictionary sent to the push gateway.
328333
SetTweak { value: Tweak },
329334
}
@@ -334,6 +339,8 @@ impl TryFrom<SdkAction> for Action {
334339
fn try_from(value: SdkAction) -> Result<Self, Self::Error> {
335340
Ok(match value {
336341
SdkAction::Notify => Self::Notify,
342+
#[cfg(feature = "unstable-msc3768")]
343+
SdkAction::NotifyInApp => Self::NotifyInApp,
337344
SdkAction::SetTweak(tweak) => Self::SetTweak {
338345
value: tweak.try_into().map_err(|e| format!("Failed to convert tweak: {e}"))?,
339346
},
@@ -348,6 +355,8 @@ impl TryFrom<Action> for SdkAction {
348355
fn try_from(value: Action) -> Result<Self, Self::Error> {
349356
Ok(match value {
350357
Action::Notify => Self::Notify,
358+
#[cfg(feature = "unstable-msc3768")]
359+
Action::NotifyInApp => Self::NotifyInApp,
351360
Action::SetTweak { value } => Self::SetTweak(
352361
value.try_into().map_err(|e| format!("Failed to convert tweak: {e}"))?,
353362
),
@@ -358,10 +367,14 @@ impl TryFrom<Action> for SdkAction {
358367
/// Enum representing the push notification modes for a room.
359368
#[derive(Clone, uniffi::Enum)]
360369
pub enum RoomNotificationMode {
361-
/// Receive notifications for all messages.
370+
/// Receive remote and in-app notifications for all messages.
362371
AllMessages,
363-
/// Receive notifications for mentions and keywords only.
372+
/// Receive remote and in-app notifications for mentions and keywords only.
364373
MentionsAndKeywordsOnly,
374+
/// Receive remote and in-app notifications for mentions and keywords and
375+
/// in-app notifications only for other room messages.
376+
#[cfg(feature = "unstable-msc3768")]
377+
MentionsAndKeywordsOnlyTheRestInApp,
365378
/// Do not receive any notifications.
366379
Mute,
367380
}
@@ -371,6 +384,10 @@ impl From<SdkRoomNotificationMode> for RoomNotificationMode {
371384
match value {
372385
SdkRoomNotificationMode::AllMessages => Self::AllMessages,
373386
SdkRoomNotificationMode::MentionsAndKeywordsOnly => Self::MentionsAndKeywordsOnly,
387+
#[cfg(feature = "unstable-msc3768")]
388+
SdkRoomNotificationMode::MentionsAndKeywordsOnlyTheRestInApp => {
389+
Self::MentionsAndKeywordsOnlyTheRestInApp
390+
}
374391
SdkRoomNotificationMode::Mute => Self::Mute,
375392
}
376393
}
@@ -381,6 +398,10 @@ impl From<RoomNotificationMode> for SdkRoomNotificationMode {
381398
match value {
382399
RoomNotificationMode::AllMessages => Self::AllMessages,
383400
RoomNotificationMode::MentionsAndKeywordsOnly => Self::MentionsAndKeywordsOnly,
401+
#[cfg(feature = "unstable-msc3768")]
402+
RoomNotificationMode::MentionsAndKeywordsOnlyTheRestInApp => {
403+
Self::MentionsAndKeywordsOnlyTheRestInApp
404+
}
384405
RoomNotificationMode::Mute => Self::Mute,
385406
}
386407
}

crates/matrix-sdk-base/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ qrcode = ["matrix-sdk-crypto?/qrcode"]
2828
automatic-room-key-forwarding = ["matrix-sdk-crypto?/automatic-room-key-forwarding"]
2929
experimental-send-custom-to-device = ["matrix-sdk-crypto?/experimental-send-custom-to-device"]
3030
uniffi = ["dep:uniffi", "matrix-sdk-crypto?/uniffi", "matrix-sdk-common/uniffi"]
31+
unstable-msc3768 = ["ruma/unstable-msc3768"]
3132

3233
# Private feature, see
3334
# https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823 for the gory

crates/matrix-sdk-base/src/notification_settings.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ use serde::{Deserialize, Serialize};
1919
/// Enum representing the push notification modes for a room.
2020
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
2121
pub enum RoomNotificationMode {
22-
/// Receive notifications for all messages.
22+
/// Receive remote and in-app notifications for all messages.
2323
AllMessages,
24-
/// Receive notifications for mentions and keywords only.
24+
/// Receive remote and in-app notifications for mentions and keywords only.
2525
MentionsAndKeywordsOnly,
26+
/// Receive remote and in-app notifications for mentions and keywords and
27+
/// in-app notifications only for other room messages.
28+
#[cfg(feature = "unstable-msc3768")]
29+
MentionsAndKeywordsOnlyTheRestInApp,
2630
/// Do not receive any notifications.
2731
Mute,
2832
}

crates/matrix-sdk-ui/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ rustls-tls = ["matrix-sdk/rustls-tls"]
1919
js = ["matrix-sdk/js"]
2020
uniffi = ["dep:uniffi", "matrix-sdk/uniffi", "matrix-sdk-base/uniffi"]
2121

22+
# Add support for in-app only notifications
23+
unstable-msc3768 = ["matrix-sdk/unstable-msc3768"]
24+
2225
# Add support for encrypted extensible events.
2326
unstable-msc3956 = ["ruma/unstable-msc3956"]
2427

@@ -58,6 +61,7 @@ tracing = { workspace = true, features = ["attributes"] }
5861
unicode-normalization.workspace = true
5962
uniffi = { workspace = true, optional = true }
6063

64+
cfg-if = "1.0.0"
6165
emojis = "0.6.4"
6266
unicode-segmentation = "1.12.0"
6367

crates/matrix-sdk-ui/src/notification_client.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::{
1818
time::Duration,
1919
};
2020

21+
use cfg_if::cfg_if;
2122
use futures_util::{StreamExt as _, pin_mut};
2223
use matrix_sdk::{
2324
Client, ClientBuildError, SlidingSyncList, SlidingSyncMode, room::Room, sleep::sleep,
@@ -677,7 +678,7 @@ impl NotificationClient {
677678

678679
let should_notify = push_actions
679680
.as_ref()
680-
.is_some_and(|actions| actions.iter().any(|a| a.should_notify()));
681+
.is_some_and(|actions| actions.iter().any(should_action_notify_remote));
681682

682683
if !should_notify {
683684
// The event has been filtered out by the user's push rules.
@@ -749,7 +750,7 @@ impl NotificationClient {
749750
}
750751

751752
if let Some(actions) = timeline_event.push_actions()
752-
&& !actions.iter().any(|a| a.should_notify())
753+
&& !actions.iter().any(should_action_notify_remote)
753754
{
754755
return Ok(NotificationStatus::EventFilteredOut);
755756
}
@@ -771,6 +772,17 @@ impl NotificationClient {
771772
}
772773
}
773774

775+
fn should_action_notify_remote(action: &Action) -> bool {
776+
cfg_if! {
777+
if #[cfg(feature = "unstable-msc3768")] {
778+
action.should_notify_remote()
779+
} else {
780+
// Before MSC3768 only combined remote/local notifications existed
781+
action.should_notify()
782+
}
783+
}
784+
}
785+
774786
fn is_event_encrypted(event_type: TimelineEventType) -> bool {
775787
let is_still_encrypted = matches!(event_type, TimelineEventType::RoomEncrypted);
776788

crates/matrix-sdk/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ experimental-widgets = ["dep:uuid", "experimental-send-custom-to-device"]
5959

6060
docsrs = ["e2e-encryption", "sqlite", "indexeddb", "sso-login", "qrcode"]
6161

62+
# Add support for in-app only notifications
63+
unstable-msc3768 = ["matrix-sdk-base/unstable-msc3768"]
64+
6265
# Add support for inline media galleries via msgtypes
6366
unstable-msc4274 = ["ruma/unstable-msc4274", "matrix-sdk-base/unstable-msc4274"]
6467

crates/matrix-sdk/src/notification_settings/command.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use crate::NotificationSettingsError;
1414
#[derive(Clone, Debug)]
1515
pub(crate) enum Command {
1616
/// Set a new `Room` push rule
17-
SetRoomPushRule { room_id: OwnedRoomId, notify: bool },
17+
SetRoomPushRule { room_id: OwnedRoomId, notify: Notify },
1818
/// Set a new `Override` push rule matching a `RoomId`
19-
SetOverridePushRule { rule_id: String, room_id: OwnedRoomId, notify: bool },
19+
SetOverridePushRule { rule_id: String, room_id: OwnedRoomId, notify: Notify },
2020
/// Set a new push rule for a keyword.
2121
SetKeywordPushRule { keyword: String },
2222
/// Set whether a push rule is enabled
@@ -29,21 +29,13 @@ pub(crate) enum Command {
2929
SetCustomPushRule { rule: NewPushRule },
3030
}
3131

32-
fn get_notify_actions(notify: bool) -> Vec<Action> {
33-
if notify {
34-
vec![Action::Notify, Action::SetTweak(Tweak::Sound("default".into()))]
35-
} else {
36-
vec![]
37-
}
38-
}
39-
4032
impl Command {
4133
/// Tries to create a push rule corresponding to this command
4234
pub(crate) fn to_push_rule(&self) -> Result<NewPushRule, NotificationSettingsError> {
4335
match self {
4436
Self::SetRoomPushRule { room_id, notify } => {
4537
// `Room` push rule for this `room_id`
46-
let new_rule = NewSimplePushRule::new(room_id.clone(), get_notify_actions(*notify));
38+
let new_rule = NewSimplePushRule::new(room_id.clone(), notify.get_actions());
4739
Ok(NewPushRule::Room(new_rule))
4840
}
4941

@@ -55,7 +47,7 @@ impl Command {
5547
key: "room_id".to_owned(),
5648
pattern: room_id.to_string(),
5749
}],
58-
get_notify_actions(*notify),
50+
notify.get_actions(),
5951
);
6052
Ok(NewPushRule::Override(new_rule))
6153
}
@@ -65,7 +57,7 @@ impl Command {
6557
let new_rule = NewPatternedPushRule::new(
6658
keyword.clone(),
6759
keyword.clone(),
68-
get_notify_actions(true),
60+
Notify::All.get_actions(),
6961
);
7062
Ok(NewPushRule::Content(new_rule))
7163
}
@@ -80,3 +72,28 @@ impl Command {
8072
}
8173
}
8274
}
75+
76+
/// Enum describing if and how to deliver a notification.
77+
#[derive(Clone, Debug, Eq, PartialEq)]
78+
pub(crate) enum Notify {
79+
/// Generate a notification both in-app and remote / push.
80+
All,
81+
82+
/// Only generate an in-app notification but no remote / push notification.
83+
#[cfg(feature = "unstable-msc3768")]
84+
InAppOnly,
85+
86+
/// Don't notify at all.
87+
None,
88+
}
89+
90+
impl Notify {
91+
fn get_actions(&self) -> Vec<Action> {
92+
match self {
93+
Self::All => vec![Action::Notify, Action::SetTweak(Tweak::Sound("default".into()))],
94+
#[cfg(feature = "unstable-msc3768")]
95+
Self::InAppOnly => vec![Action::NotifyInApp],
96+
Self::None => vec![],
97+
}
98+
}
99+
}

crates/matrix-sdk/src/notification_settings/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub use matrix_sdk_base::notification_settings::RoomNotificationMode;
4141

4242
use crate::{
4343
config::RequestConfig, error::NotificationSettingsError, event_handler::EventHandlerDropGuard,
44-
Client, Result,
44+
notification_settings::command::Notify, Client, Result,
4545
};
4646

4747
/// Whether or not a room is encrypted
@@ -320,15 +320,20 @@ impl NotificationSettings {
320320
let (new_rule_kind, notify) = match mode {
321321
RoomNotificationMode::AllMessages => {
322322
// insert a `Room` rule which notifies
323-
(RuleKind::Room, true)
323+
(RuleKind::Room, Notify::All)
324324
}
325325
RoomNotificationMode::MentionsAndKeywordsOnly => {
326326
// insert a `Room` rule which doesn't notify
327-
(RuleKind::Room, false)
327+
(RuleKind::Room, Notify::None)
328+
}
329+
#[cfg(feature = "unstable-msc3768")]
330+
RoomNotificationMode::MentionsAndKeywordsOnlyTheRestInApp => {
331+
// insert a `Room` rule which notifies in-app only
332+
(RuleKind::Room, Notify::InAppOnly)
328333
}
329334
RoomNotificationMode::Mute => {
330335
// insert an `Override` rule which doesn't notify
331-
(RuleKind::Override, false)
336+
(RuleKind::Override, Notify::None)
332337
}
333338
};
334339

@@ -923,6 +928,8 @@ mod tests {
923928
let new_modes = [
924929
RoomNotificationMode::AllMessages,
925930
RoomNotificationMode::MentionsAndKeywordsOnly,
931+
#[cfg(feature = "unstable-msc3768")]
932+
RoomNotificationMode::MentionsAndKeywordsOnlyTheRestInApp,
926933
RoomNotificationMode::Mute,
927934
];
928935
for new_mode in new_modes {

0 commit comments

Comments
 (0)