Skip to main content

tuwunel_api/client/push/
pushrules_rule_enabled.rs

1use axum::extract::State;
2use ruma::{
3	api::client::push::{get_pushrule_enabled, set_pushrule_enabled},
4	events::{GlobalAccountDataEventType, push_rules::PushRulesEvent},
5	push::{PredefinedContentRuleId, PredefinedOverrideRuleId},
6};
7use tuwunel_core::{Err, Result, err};
8
9use crate::Ruma;
10
11/// # `GET /_matrix/client/r0/pushrules/global/{kind}/{ruleId}/enabled`
12///
13/// Gets the enabled status of a single specified push rule for this user.
14pub(crate) async fn get_pushrule_enabled_route(
15	State(services): State<crate::State>,
16	body: Ruma<get_pushrule_enabled::v3::Request>,
17) -> Result<get_pushrule_enabled::v3::Response> {
18	let sender_user = body.sender_user();
19
20	// remove old deprecated mentions push rules as per MSC4210
21	#[expect(deprecated)]
22	if body.rule_id.as_str() == PredefinedContentRuleId::ContainsUserName.as_str()
23		|| body.rule_id.as_str() == PredefinedOverrideRuleId::ContainsDisplayName.as_str()
24		|| body.rule_id.as_str() == PredefinedOverrideRuleId::RoomNotif.as_str()
25	{
26		return Ok(get_pushrule_enabled::v3::Response { enabled: false });
27	}
28
29	let event: PushRulesEvent = services
30		.account_data
31		.get_global(sender_user, GlobalAccountDataEventType::PushRules)
32		.await
33		.map_err(|_| err!(Request(NotFound("PushRules event not found."))))?;
34
35	let enabled = event
36		.content
37		.global
38		.get(body.kind.clone(), &body.rule_id)
39		.map(ruma::push::AnyPushRuleRef::enabled)
40		.ok_or_else(|| err!(Request(NotFound("Push rule not found."))))?;
41
42	Ok(get_pushrule_enabled::v3::Response { enabled })
43}
44
45/// # `PUT /_matrix/client/r0/pushrules/global/{kind}/{ruleId}/enabled`
46///
47/// Sets the enabled status of a single specified push rule for this user.
48pub(crate) async fn set_pushrule_enabled_route(
49	State(services): State<crate::State>,
50	body: Ruma<set_pushrule_enabled::v3::Request>,
51) -> Result<set_pushrule_enabled::v3::Response> {
52	let sender_user = body.sender_user();
53
54	let mut account_data: PushRulesEvent = services
55		.account_data
56		.get_global(sender_user, GlobalAccountDataEventType::PushRules)
57		.await
58		.map_err(|_| err!(Request(NotFound("PushRules event not found."))))?;
59
60	if account_data
61		.content
62		.global
63		.set_enabled(body.kind.clone(), &body.rule_id, body.enabled)
64		.is_err()
65	{
66		return Err!(Request(NotFound("Push rule not found.")));
67	}
68
69	let ty = GlobalAccountDataEventType::PushRules;
70	services
71		.account_data
72		.update(None, sender_user, ty.to_string().into(), &serde_json::to_value(account_data)?)
73		.await?;
74
75	Ok(set_pushrule_enabled::v3::Response {})
76}