diff --git a/Cargo.toml b/Cargo.toml index a9b1181bc..6926aa125 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -398,6 +398,7 @@ features = [ "unstable-msc4484", "unstable-msc4540", "unstable-msc4560", + "unstable-msc4563", "unstable-extensible-events", ] diff --git a/conduwuit-example.toml b/conduwuit-example.toml index a822d79ac..637a5f705 100644 --- a/conduwuit-example.toml +++ b/conduwuit-example.toml @@ -2808,6 +2808,18 @@ # env: CONTINUWUITY_EXPERIMENTS__MSC4560 #msc4560 = false +# MSC4563: `direct_message` push rule +# +# Creates a new kind of push rule that creates a notification if a message +# is received in a room that is in the user's direct message list. This +# more accurately identifies direct messages, rather than relying on a "DM +# room" only consisting of two members. +# +# https://github.com/matrix-org/matrix-spec-proposals/pull/4563 +# +# env: CONTINUWUITY_EXPERIMENTS__MSC4563 +#msc4563 = false + #[global.experiments.msc2815] # MSC2815: Proposal to allow room moderators to view redacted event content # diff --git a/src/core/config/experiments.rs b/src/core/config/experiments.rs index 86be61339..f2fabc0aa 100644 --- a/src/core/config/experiments.rs +++ b/src/core/config/experiments.rs @@ -146,6 +146,19 @@ pub struct Experiments { /// default: false #[serde(default)] pub msc4560: SimpleExperiment, + + /// MSC4563: `direct_message` push rule + /// + /// Creates a new kind of push rule that creates a notification if a message + /// is received in a room that is in the user's direct message list. This + /// more accurately identifies direct messages, rather than relying on a "DM + /// room" only consisting of two members. + /// + /// https://github.com/matrix-org/matrix-spec-proposals/pull/4563 + /// + /// default: false + #[serde(default)] + pub msc4563: SimpleExperiment, } /// Represents a "simple" experiment, which is either disabled or enabled. diff --git a/src/service/pusher/mod.rs b/src/service/pusher/mod.rs index a7af1364c..03642bac2 100644 --- a/src/service/pusher/mod.rs +++ b/src/service/pusher/mod.rs @@ -2,6 +2,7 @@ mod webpush; use std::{fmt::Debug, mem, net::IpAddr, sync::Arc}; +use assign::assign; use bytes::BytesMut; use conduwuit::{config::experiments::ToggledExperiment, utils::response::LimitReadExt}; use conduwuit_core::{ @@ -23,7 +24,10 @@ use ruma::{ v1::{Device, Notification, NotificationCounts, NotificationPriority}, }, }, - events::{AnySyncTimelineEvent, TimelineEventType, room::power_levels::RoomPowerLevels}, + events::{ + AnySyncTimelineEvent, GlobalAccountDataEvent, GlobalAccountDataEventType, + TimelineEventType, direct::DirectEventContent, room::power_levels::RoomPowerLevels, + }, push::{ Action, HighlightTweakValue, PushConditionPowerLevelsCtx, PushConditionRoomCtx, PushFormat, Ruleset, Tweak, @@ -36,7 +40,7 @@ use url::Host; pub use self::webpush::ActivationOutcome; use self::webpush::Vapid; -use crate::{Dep, client, config, globals, rooms, sending, users}; +use crate::{Dep, account_data, client, config, globals, rooms, sending, users}; const PUSH_GATEWAY_SCHEMES: &[&str] = &["http", "https"]; const WEBPUSH_SCHEMES: &[&str] = &["https"]; @@ -57,6 +61,7 @@ pub struct Service { struct Services { server: Arc, globals: Dep, + account_data: Dep, config: Dep, client: Dep, state_accessor: Dep, @@ -86,6 +91,7 @@ impl crate::Service for Service { services: Services { server: args.server.clone(), globals: args.depend::("globals"), + account_data: args.depend::("account_data"), client: args.depend::("client"), config: args.depend::("config"), state_accessor: args @@ -488,11 +494,34 @@ impl Service { .await .unwrap_or_else(|_| user.localpart().to_owned()); - PushConditionRoomCtx::new( - room_id.to_owned(), - room_joined_count, - user.to_owned(), - user_display_name, + let is_direct = self.services.config.experiments.msc4563.is_enabled() + && self + .services + .account_data + .get_global::>( + user, + GlobalAccountDataEventType::Direct, + ) + .await + .ok() + .and_then(|direct| { + direct + .content + .values() + .flatten() + .any(|dm_room_id| dm_room_id == room_id) + .then_some(()) + }) + .is_some(); + + assign!( + PushConditionRoomCtx::new( + room_id.to_owned(), + room_joined_count, + user.to_owned(), + user_display_name, + ), + { is_direct } ) .with_power_levels(power_levels) }