use event content in account_data service api instead of full events

This eliminates the possibility of passing an event that has a
mismatching type, reducing the space of possible invalid states.
This commit is contained in:
Olivia Lee 2025-03-23 14:45:33 -07:00
parent 66210bc32d
commit b82458a460
No known key found for this signature in database
GPG key ID: 54D568A15B9CD1F9
15 changed files with 277 additions and 240 deletions

View file

@ -7,10 +7,7 @@ use std::{
};
use ruma::{
events::{
push_rules::PushRulesEvent, AnyGlobalAccountDataEvent,
GlobalAccountDataEventType,
},
events::{push_rules::PushRulesEventContent, GlobalAccountDataEventType},
push::Ruleset,
serde::Raw,
EventId, OwnedRoomId, RoomId, UserId,
@ -871,9 +868,9 @@ impl KeyValueDatabase {
.expect("Username is invalid");
let mut account_data = raw_rules_list
.deserialize_as::<PushRulesEvent>()
.deserialize_as::<PushRulesEventContent>()
.unwrap();
let rules_list = &mut account_data.content.global;
let rules_list = &mut account_data.global;
//content rule
{
@ -929,9 +926,8 @@ impl KeyValueDatabase {
services().account_data.update_global(
&user,
&GlobalAccountDataEventType::PushRules,
&Raw::new(&account_data)
.expect("json serialization should always succeed")
.cast::<AnyGlobalAccountDataEvent>(),
&Raw::new(&account_data.into())
.expect("json serialization should always succeed"),
)?;
}
Ok(())
@ -966,21 +962,19 @@ impl KeyValueDatabase {
.expect("Username is invalid");
let mut account_data = raw_rules_list
.deserialize_as::<PushRulesEvent>()
.deserialize_as::<PushRulesEventContent>()
.unwrap();
let user_default_rules = Ruleset::server_default(&user);
account_data
.content
.global
.update_with_server_default(user_default_rules);
services().account_data.update_global(
&user,
&GlobalAccountDataEventType::PushRules,
&Raw::new(&account_data)
.expect("json serialization should always succeed")
.cast::<AnyGlobalAccountDataEvent>(),
&Raw::new(&account_data.into())
.expect("json serialization should always succeed"),
)?;
}
Ok(())