Skip to main content

tuwunel_api/client/push/
pushrules_rule_actions.rs

1use axum::extract::State;
2use ruma::api::client::push::{get_pushrule_actions, set_pushrule_actions};
3use tuwunel_core::{Err, Result, err};
4
5use crate::Ruma;
6
7/// # `GET /_matrix/client/r0/pushrules/global/{kind}/{ruleId}/actions`
8///
9/// Gets the actions of a single specified push rule for this user.
10pub(crate) async fn get_pushrule_actions_route(
11	State(services): State<crate::State>,
12	body: Ruma<get_pushrule_actions::v3::Request>,
13) -> Result<get_pushrule_actions::v3::Response> {
14	let sender_user = body.sender_user();
15
16	if super::is_deprecated_mention_rule(body.rule_id.as_str()) {
17		return Err!(Request(NotFound("Push rule not found.")));
18	}
19
20	let event = super::load_push_rules(&services, sender_user).await?;
21
22	let actions = event
23		.content
24		.global
25		.get(body.kind.clone(), &body.rule_id)
26		.map(|rule| rule.actions().to_owned())
27		.ok_or_else(|| err!(Request(NotFound("Push rule not found."))))?;
28
29	Ok(get_pushrule_actions::v3::Response { actions })
30}
31
32/// # `PUT /_matrix/client/r0/pushrules/global/{kind}/{ruleId}/actions`
33///
34/// Sets the actions of a single specified push rule for this user.
35pub(crate) async fn set_pushrule_actions_route(
36	State(services): State<crate::State>,
37	body: Ruma<set_pushrule_actions::v3::Request>,
38) -> Result<set_pushrule_actions::v3::Response> {
39	let sender_user = body.sender_user();
40	let mut account_data = super::load_push_rules(&services, sender_user).await?;
41
42	if account_data
43		.content
44		.global
45		.set_actions(body.kind.clone(), &body.rule_id, body.actions.clone().into())
46		.is_err()
47	{
48		return Err!(Request(NotFound("Push rule not found.")));
49	}
50
51	super::check_rule_size(&account_data.content.global, body.kind.clone(), &body.rule_id)?;
52
53	super::save_push_rules(&services, sender_user, &account_data).await?;
54
55	Ok(set_pushrule_actions::v3::Response {})
56}