Skip to main content

tuwunel_service/account_data/
push_rules.rs

1use ruma::{
2	RoomId, UserId,
3	events::{GlobalAccountDataEventType, push_rules::PushRulesEvent},
4	push::{AnyPushRuleRef, NewPushRule, NewSimplePushRule, RuleKind},
5};
6use tuwunel_core::{Result, err, implement};
7
8#[implement(super::Service)]
9pub async fn copy_room_push_rule(
10	&self,
11	user_id: &UserId,
12	from_room: &RoomId,
13	to_room: &RoomId,
14) -> Result {
15	let Ok(mut account_data): Result<PushRulesEvent> = self
16		.get_global(user_id, GlobalAccountDataEventType::PushRules)
17		.await
18	else {
19		return Ok(());
20	};
21
22	let ruleset = &mut account_data.content.global;
23
24	let Some(AnyPushRuleRef::Room(rule)) = ruleset.get(RuleKind::Room, from_room) else {
25		return Ok(());
26	};
27
28	let actions = rule.actions.clone();
29	let rule = NewPushRule::Room(NewSimplePushRule::new(to_room.to_owned(), actions));
30
31	ruleset
32		.insert(rule, None, None)
33		.map_err(|e| err!(Request(InvalidParam("Failed to copy room push rule: {e}"))))?;
34
35	let ty = GlobalAccountDataEventType::PushRules;
36	let data = serde_json::to_value(account_data)?;
37
38	self.update(None, user_id, ty.to_string().into(), &data)
39		.await
40}