push: Add support for event_property_contains push condition

According to MSC3966
This commit is contained in:
Kévin Commaille 2023-04-07 12:53:59 +02:00 committed by Kévin Commaille
parent 2e7b67bb23
commit 2ccc0323f6
5 changed files with 104 additions and 1 deletions

View File

@ -21,6 +21,7 @@ Improvements:
- Add a convenience method to construct `RoomEncryptionEventContent` with the recommended defaults.
- `PushCondition::EventMatch` and `FlattenedJson` now use escaped dotted paths, according to MSC3873
- Add unstable support for `event_property_is` push condition according to MSC3758
- Add unstable support for `event_property_contains` push condition according to MSC3966
# 0.11.3

View File

@ -49,6 +49,7 @@ unstable-msc3932 = ["unstable-msc3931"]
unstable-msc3954 = ["unstable-msc1767"]
unstable-msc3955 = ["unstable-msc1767"]
unstable-msc3956 = ["unstable-msc1767"]
unstable-msc3966 = []
unstable-pdu = []
unstable-sanitize = ["dep:html5ever", "dep:phf"]
unstable-unspecified = []

View File

@ -119,6 +119,19 @@ pub enum PushCondition {
value: ScalarJsonValue,
},
/// Exact value match on a value in an array property of the event.
#[cfg(feature = "unstable-msc3966")]
EventPropertyContains {
/// The dot-separated path of the property of the event to match. See [MSC3873] for its
/// format.
///
/// [MSC3873]: https://github.com/matrix-org/matrix-spec-proposals/pull/3873
key: String,
/// The value to match against.
value: ScalarJsonValue,
},
#[doc(hidden)]
_Custom(_CustomPushCondition),
}
@ -191,6 +204,11 @@ impl PushCondition {
},
#[cfg(feature = "unstable-msc3758")]
Self::EventPropertyIs { key, value } => event.get(key).map_or(false, |v| v == value),
#[cfg(feature = "unstable-msc3966")]
Self::EventPropertyContains { key, value } => event
.get(key)
.and_then(FlattenedJsonValue::as_array)
.map_or(false, |a| a.contains(value)),
Self::_Custom(_) => false,
}
}
@ -851,4 +869,68 @@ mod tests {
};
assert!(null_match.applies(&event, &context));
}
#[cfg(feature = "unstable-msc3966")]
#[test]
fn event_property_contains_applies() {
use crate::push::condition::ScalarJsonValue;
let context = push_context();
let event_raw = serde_json::from_str::<Raw<JsonValue>>(
r#"{
"sender": "@worthy_whale:server.name",
"content": {
"org.fake.array": ["Boom!", false, 13, null]
}
}"#,
)
.unwrap();
let event = FlattenedJson::from_raw(&event_raw);
let wrong_key =
PushCondition::EventPropertyContains { key: "send".to_owned(), value: false.into() };
assert!(!wrong_key.applies(&event, &context));
let string_match = PushCondition::EventPropertyContains {
key: r"content.org\.fake\.array".to_owned(),
value: "Boom!".into(),
};
assert!(string_match.applies(&event, &context));
let string_no_match = PushCondition::EventPropertyContains {
key: r"content.org\.fake\.array".to_owned(),
value: "Boom".into(),
};
assert!(!string_no_match.applies(&event, &context));
let bool_match = PushCondition::EventPropertyContains {
key: r"content.org\.fake\.array".to_owned(),
value: false.into(),
};
assert!(bool_match.applies(&event, &context));
let bool_no_match = PushCondition::EventPropertyContains {
key: r"content.org\.fake\.array".to_owned(),
value: true.into(),
};
assert!(!bool_no_match.applies(&event, &context));
let int_match = PushCondition::EventPropertyContains {
key: r"content.org\.fake\.array".to_owned(),
value: int!(13).into(),
};
assert!(int_match.applies(&event, &context));
let int_no_match = PushCondition::EventPropertyContains {
key: r"content.org\.fake\.array".to_owned(),
value: int!(130).into(),
};
assert!(!int_no_match.applies(&event, &context));
let null_match = PushCondition::EventPropertyContains {
key: r"content.org\.fake\.array".to_owned(),
value: ScalarJsonValue::Null,
};
assert!(null_match.applies(&event, &context));
}
}

View File

@ -5,7 +5,7 @@ use crate::serde::from_raw_json_value;
#[cfg(feature = "unstable-msc3931")]
use super::RoomVersionFeature;
#[cfg(feature = "unstable-msc3758")]
#[cfg(any(feature = "unstable-msc3758", feature = "unstable-msc3966"))]
use super::ScalarJsonValue;
use super::{PushCondition, RoomMemberCountIs};
@ -47,6 +47,11 @@ impl<'de> Deserialize<'de> for PushCondition {
let helper: PushConditionSerDeHelper = from_raw_json_value(&json)?;
Ok(helper.into())
}
#[cfg(feature = "unstable-msc3966")]
"org.matrix.msc3966.exact_event_property_contains" => {
let helper: PushConditionSerDeHelper = from_raw_json_value(&json)?;
Ok(helper.into())
}
_ => from_raw_json_value(&json).map(Self::_Custom),
}
}
@ -104,6 +109,10 @@ enum PushConditionSerDeHelper {
#[cfg(feature = "unstable-msc3758")]
#[serde(rename = "com.beeper.msc3758.exact_event_match")]
EventPropertyIs { key: String, value: ScalarJsonValue },
#[cfg(feature = "unstable-msc3966")]
#[serde(rename = "org.matrix.msc3966.exact_event_property_contains")]
EventPropertyContains { key: String, value: ScalarJsonValue },
}
impl From<PushConditionSerDeHelper> for PushCondition {
@ -125,6 +134,10 @@ impl From<PushConditionSerDeHelper> for PushCondition {
PushConditionSerDeHelper::EventPropertyIs { key, value } => {
Self::EventPropertyIs { key, value }
}
#[cfg(feature = "unstable-msc3966")]
PushConditionSerDeHelper::EventPropertyContains { key, value } => {
Self::EventPropertyContains { key, value }
}
}
}
}
@ -142,6 +155,10 @@ impl From<PushCondition> for PushConditionSerDeHelper {
PushCondition::RoomVersionSupports { feature } => Self::RoomVersionSupports { feature },
#[cfg(feature = "unstable-msc3758")]
PushCondition::EventPropertyIs { key, value } => Self::EventPropertyIs { key, value },
#[cfg(feature = "unstable-msc3966")]
PushCondition::EventPropertyContains { key, value } => {
Self::EventPropertyContains { key, value }
}
PushCondition::_Custom(_) => unimplemented!(),
}
}

View File

@ -161,6 +161,7 @@ unstable-msc3932 = ["ruma-common/unstable-msc3932"]
unstable-msc3954 = ["ruma-common/unstable-msc3954"]
unstable-msc3955 = ["ruma-common/unstable-msc3955"]
unstable-msc3956 = ["ruma-common/unstable-msc3956"]
unstable-msc3966 = ["ruma-common/unstable-msc3966"]
unstable-pdu = ["ruma-common/unstable-pdu"]
unstable-sanitize = ["ruma-common/unstable-sanitize"]
unstable-unspecified = [
@ -203,6 +204,7 @@ __ci = [
"unstable-msc3954",
"unstable-msc3955",
"unstable-msc3956",
"unstable-msc3966",
]
[dependencies]