implement `GET /_matrix/client/v3/pushrules/global/`

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-11-15 21:40:13 -05:00
parent ead9d66797
commit 6319384072
No known key found for this signature in database
2 changed files with 69 additions and 1 deletions

View File

@ -5,7 +5,7 @@ use ruma::{
error::ErrorKind,
push::{
delete_pushrule, get_pushers, get_pushrule, get_pushrule_actions, get_pushrule_enabled, get_pushrules_all,
set_pusher, set_pushrule, set_pushrule_actions, set_pushrule_enabled,
get_pushrules_global_scope, set_pusher, set_pushrule, set_pushrule_actions, set_pushrule_enabled,
},
},
events::{
@ -67,6 +67,73 @@ pub(crate) async fn get_pushrules_all_route(
})
}
/// # `GET /_matrix/client/r0/pushrules/global/`
///
/// Retrieves the push rules event for this user.
///
/// This appears to be the exact same as `GET /_matrix/client/r0/pushrules/`.
pub(crate) async fn get_pushrules_global_route(
State(services): State<crate::State>, body: Ruma<get_pushrules_global_scope::v3::Request>,
) -> Result<get_pushrules_global_scope::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let Some(content_value) = services
.account_data
.get_global::<CanonicalJsonObject>(sender_user, GlobalAccountDataEventType::PushRules)
.await
.ok()
.and_then(|event| event.get("content").cloned())
.filter(CanonicalJsonValue::is_object)
else {
// user somehow has non-existent push rule event. recreate it and return server
// default silently
services
.account_data
.update(
None,
sender_user,
GlobalAccountDataEventType::PushRules.to_string().into(),
&serde_json::to_value(PushRulesEvent {
content: PushRulesEventContent {
global: Ruleset::server_default(sender_user),
},
})
.expect("to json always works"),
)
.await?;
return Ok(get_pushrules_global_scope::v3::Response {
global: Ruleset::server_default(sender_user),
});
};
let account_data_content = serde_json::from_value::<PushRulesEventContent>(content_value.into())
.map_err(|e| err!(Database(warn!("Invalid push rules account data event in database: {e}"))))?;
let mut global_ruleset = account_data_content.global;
// remove old deprecated mentions push rules as per MSC4210
#[allow(deprecated)]
{
use ruma::push::RuleKind::*;
global_ruleset
.remove(Override, PredefinedOverrideRuleId::ContainsDisplayName)
.ok();
global_ruleset
.remove(Override, PredefinedOverrideRuleId::RoomNotif)
.ok();
global_ruleset
.remove(Content, PredefinedContentRuleId::ContainsUserName)
.ok();
};
Ok(get_pushrules_global_scope::v3::Response {
global: global_ruleset,
})
}
/// # `GET /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}`
///
/// Retrieves a single specified push rule for this user.

View File

@ -45,6 +45,7 @@ pub fn build(router: Router<State>, server: &Server) -> Router<State> {
.ruma_route(&client::check_registration_token_validity)
.ruma_route(&client::get_capabilities_route)
.ruma_route(&client::get_pushrules_all_route)
.ruma_route(&client::get_pushrules_global_route)
.ruma_route(&client::set_pushrule_route)
.ruma_route(&client::get_pushrule_route)
.ruma_route(&client::set_pushrule_enabled_route)