mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-16 15:21:24 +01:00
add type-safe accessors to account_data service
This commit is contained in:
parent
b82458a460
commit
88ad596e8d
12 changed files with 202 additions and 179 deletions
|
|
@ -11,9 +11,7 @@ use ruma::{
|
|||
error::ErrorKind,
|
||||
uiaa::{AuthFlow, AuthType, UiaaInfo},
|
||||
},
|
||||
events::{
|
||||
room::message::RoomMessageEventContent, GlobalAccountDataEventType,
|
||||
},
|
||||
events::room::message::RoomMessageEventContent,
|
||||
push,
|
||||
serde::Raw,
|
||||
UserId,
|
||||
|
|
@ -238,13 +236,9 @@ pub(crate) async fn register_route(
|
|||
// Initial account data
|
||||
services().account_data.update_global(
|
||||
&user_id,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(
|
||||
&ruma::events::push_rules::PushRulesEventContent {
|
||||
global: push::Ruleset::server_default(&user_id),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
&Raw::new(&ruma::events::push_rules::PushRulesEventContent {
|
||||
global: push::Ruleset::server_default(&user_id),
|
||||
})
|
||||
.expect("constructed event should be valid"),
|
||||
)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ pub(crate) async fn set_global_account_data_route(
|
|||
) -> Result<Ra<set_global_account_data::v3::Response>> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
services().account_data.update_global(
|
||||
services().account_data.update_global_any(
|
||||
sender_user,
|
||||
&body.event_type,
|
||||
&body.data,
|
||||
|
|
@ -33,7 +33,7 @@ pub(crate) async fn set_room_account_data_route(
|
|||
) -> Result<Ra<set_room_account_data::v3::Response>> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
services().account_data.update_room(
|
||||
services().account_data.update_room_any(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&body.event_type,
|
||||
|
|
@ -53,7 +53,7 @@ pub(crate) async fn get_global_account_data_route(
|
|||
|
||||
let account_data = services()
|
||||
.account_data
|
||||
.get_global(sender_user, &body.event_type)?
|
||||
.get_global_any(sender_user, &body.event_type)?
|
||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||
|
||||
Ok(Ra(get_global_account_data::v3::Response {
|
||||
|
|
@ -71,7 +71,7 @@ pub(crate) async fn get_room_account_data_route(
|
|||
|
||||
let account_data = services()
|
||||
.account_data
|
||||
.get_room(&body.room_id, sender_user, &body.event_type)?
|
||||
.get_room_any(&body.room_id, sender_user, &body.event_type)?
|
||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||
|
||||
Ok(Ra(get_room_account_data::v3::Response {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use ruma::{
|
|||
set_pushrule_actions, set_pushrule_enabled,
|
||||
},
|
||||
},
|
||||
events::{push_rules::PushRulesEventContent, GlobalAccountDataEventType},
|
||||
events::push_rules::PushRulesEventContent,
|
||||
push::{AnyPushRuleRef, InsertPushRuleError, RemovePushRuleError},
|
||||
serde::Raw,
|
||||
};
|
||||
|
|
@ -24,16 +24,15 @@ pub(crate) async fn get_pushrules_all_route(
|
|||
|
||||
let event = services()
|
||||
.account_data
|
||||
.get_global(sender_user, &GlobalAccountDataEventType::PushRules)?
|
||||
.get_global::<PushRulesEventContent>(sender_user)?
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::NotFound,
|
||||
"PushRules event not found.",
|
||||
))?;
|
||||
|
||||
let account_data =
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
let account_data = event.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
Ok(Ra(get_pushrules_all::v3::Response {
|
||||
global: account_data.global,
|
||||
|
|
@ -50,16 +49,15 @@ pub(crate) async fn get_pushrule_route(
|
|||
|
||||
let event = services()
|
||||
.account_data
|
||||
.get_global(sender_user, &GlobalAccountDataEventType::PushRules)?
|
||||
.get_global::<PushRulesEventContent>(sender_user)?
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::NotFound,
|
||||
"PushRules event not found.",
|
||||
))?;
|
||||
|
||||
let account_data =
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
let account_data = event.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
let rule = account_data
|
||||
.global
|
||||
|
|
@ -86,16 +84,15 @@ pub(crate) async fn set_pushrule_route(
|
|||
|
||||
let event = services()
|
||||
.account_data
|
||||
.get_global(sender_user, &GlobalAccountDataEventType::PushRules)?
|
||||
.get_global::<PushRulesEventContent>(sender_user)?
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::NotFound,
|
||||
"PushRules event not found.",
|
||||
))?;
|
||||
|
||||
let mut account_data =
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
let mut account_data = event.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
if let Err(error) = account_data.global.insert(
|
||||
body.rule.clone(),
|
||||
|
|
@ -135,8 +132,7 @@ pub(crate) async fn set_pushrule_route(
|
|||
|
||||
services().account_data.update_global(
|
||||
sender_user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&account_data.into())
|
||||
&Raw::new(&account_data)
|
||||
.expect("json event serialization should always succeed"),
|
||||
)?;
|
||||
|
||||
|
|
@ -153,16 +149,15 @@ pub(crate) async fn get_pushrule_actions_route(
|
|||
|
||||
let event = services()
|
||||
.account_data
|
||||
.get_global(sender_user, &GlobalAccountDataEventType::PushRules)?
|
||||
.get_global::<PushRulesEventContent>(sender_user)?
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::NotFound,
|
||||
"PushRules event not found.",
|
||||
))?;
|
||||
|
||||
let account_data =
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
let account_data = event.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
let global = account_data.global;
|
||||
let actions = global
|
||||
|
|
@ -188,16 +183,15 @@ pub(crate) async fn set_pushrule_actions_route(
|
|||
|
||||
let event = services()
|
||||
.account_data
|
||||
.get_global(sender_user, &GlobalAccountDataEventType::PushRules)?
|
||||
.get_global::<PushRulesEventContent>(sender_user)?
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::NotFound,
|
||||
"PushRules event not found.",
|
||||
))?;
|
||||
|
||||
let mut account_data =
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
let mut account_data = event.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
if account_data
|
||||
.global
|
||||
|
|
@ -212,8 +206,7 @@ pub(crate) async fn set_pushrule_actions_route(
|
|||
|
||||
services().account_data.update_global(
|
||||
sender_user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&account_data.into())
|
||||
&Raw::new(&account_data)
|
||||
.expect("json event serialization should always suceed"),
|
||||
)?;
|
||||
|
||||
|
|
@ -230,16 +223,15 @@ pub(crate) async fn get_pushrule_enabled_route(
|
|||
|
||||
let event = services()
|
||||
.account_data
|
||||
.get_global(sender_user, &GlobalAccountDataEventType::PushRules)?
|
||||
.get_global::<PushRulesEventContent>(sender_user)?
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::NotFound,
|
||||
"PushRules event not found.",
|
||||
))?;
|
||||
|
||||
let account_data =
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
let account_data = event.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
let global = account_data.global;
|
||||
let enabled = global
|
||||
|
|
@ -265,16 +257,15 @@ pub(crate) async fn set_pushrule_enabled_route(
|
|||
|
||||
let event = services()
|
||||
.account_data
|
||||
.get_global(sender_user, &GlobalAccountDataEventType::PushRules)?
|
||||
.get_global::<PushRulesEventContent>(sender_user)?
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::NotFound,
|
||||
"PushRules event not found.",
|
||||
))?;
|
||||
|
||||
let mut account_data =
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
let mut account_data = event.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
if account_data
|
||||
.global
|
||||
|
|
@ -289,8 +280,7 @@ pub(crate) async fn set_pushrule_enabled_route(
|
|||
|
||||
services().account_data.update_global(
|
||||
sender_user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&account_data.into())
|
||||
&Raw::new(&account_data)
|
||||
.expect("json event serialization should always succeed"),
|
||||
)?;
|
||||
|
||||
|
|
@ -307,16 +297,15 @@ pub(crate) async fn delete_pushrule_route(
|
|||
|
||||
let event = services()
|
||||
.account_data
|
||||
.get_global(sender_user, &GlobalAccountDataEventType::PushRules)?
|
||||
.get_global::<PushRulesEventContent>(sender_user)?
|
||||
.ok_or(Error::BadRequest(
|
||||
ErrorKind::NotFound,
|
||||
"PushRules event not found.",
|
||||
))?;
|
||||
|
||||
let mut account_data =
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
let mut account_data = event.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
if let Err(error) =
|
||||
account_data.global.remove(body.kind.clone(), &body.rule_id)
|
||||
|
|
@ -337,8 +326,7 @@ pub(crate) async fn delete_pushrule_route(
|
|||
|
||||
services().account_data.update_global(
|
||||
sender_user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&account_data.into())
|
||||
&Raw::new(&account_data)
|
||||
.expect("json event serialization should always suceed"),
|
||||
)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -4,10 +4,7 @@ use ruma::{
|
|||
api::client::{
|
||||
error::ErrorKind, read_marker::set_read_marker, receipt::create_receipt,
|
||||
},
|
||||
events::{
|
||||
receipt::{ReceiptThread, ReceiptType},
|
||||
RoomAccountDataEventType,
|
||||
},
|
||||
events::receipt::{ReceiptThread, ReceiptType},
|
||||
serde::Raw,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
};
|
||||
|
|
@ -36,8 +33,7 @@ pub(crate) async fn set_read_marker_route(
|
|||
services().account_data.update_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&RoomAccountDataEventType::FullyRead,
|
||||
&Raw::new(&fully_read_event.into())
|
||||
&Raw::new(&fully_read_event)
|
||||
.expect("json event serialization should always suceed"),
|
||||
)?;
|
||||
}
|
||||
|
|
@ -131,8 +127,7 @@ pub(crate) async fn create_receipt_route(
|
|||
services().account_data.update_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&RoomAccountDataEventType::FullyRead,
|
||||
&Raw::new(&fully_read_event.into())
|
||||
&Raw::new(&fully_read_event)
|
||||
.expect("json event serialization should always succeed"),
|
||||
)?;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use std::collections::BTreeMap;
|
|||
|
||||
use ruma::{
|
||||
api::client::tag::{create_tag, delete_tag, get_tags},
|
||||
events::{tag::TagEventContent, RoomAccountDataEventType},
|
||||
events::tag::TagEventContent,
|
||||
serde::Raw,
|
||||
};
|
||||
|
||||
|
|
@ -18,11 +18,9 @@ pub(crate) async fn update_tag_route(
|
|||
) -> Result<Ra<create_tag::v3::Response>> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let event = services().account_data.get_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&RoomAccountDataEventType::Tag,
|
||||
)?;
|
||||
let event = services()
|
||||
.account_data
|
||||
.get_room::<TagEventContent>(&body.room_id, sender_user)?;
|
||||
|
||||
let mut tags_event = event.map_or_else(
|
||||
|| {
|
||||
|
|
@ -31,7 +29,7 @@ pub(crate) async fn update_tag_route(
|
|||
})
|
||||
},
|
||||
|e| {
|
||||
e.deserialize_as::<TagEventContent>().map_err(|_| {
|
||||
e.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})
|
||||
},
|
||||
|
|
@ -42,8 +40,7 @@ pub(crate) async fn update_tag_route(
|
|||
services().account_data.update_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&RoomAccountDataEventType::Tag,
|
||||
&Raw::new(&tags_event.into())
|
||||
&Raw::new(&tags_event)
|
||||
.expect("json event serialization should always suceed"),
|
||||
)?;
|
||||
|
||||
|
|
@ -60,11 +57,9 @@ pub(crate) async fn delete_tag_route(
|
|||
) -> Result<Ra<delete_tag::v3::Response>> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let event = services().account_data.get_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&RoomAccountDataEventType::Tag,
|
||||
)?;
|
||||
let event = services()
|
||||
.account_data
|
||||
.get_room::<TagEventContent>(&body.room_id, sender_user)?;
|
||||
|
||||
let mut tags_event = event.map_or_else(
|
||||
|| {
|
||||
|
|
@ -73,7 +68,7 @@ pub(crate) async fn delete_tag_route(
|
|||
})
|
||||
},
|
||||
|e| {
|
||||
e.deserialize_as::<TagEventContent>().map_err(|_| {
|
||||
e.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})
|
||||
},
|
||||
|
|
@ -84,8 +79,7 @@ pub(crate) async fn delete_tag_route(
|
|||
services().account_data.update_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&RoomAccountDataEventType::Tag,
|
||||
&Raw::new(&tags_event.into())
|
||||
&Raw::new(&tags_event)
|
||||
.expect("json value serialization should always succeed"),
|
||||
)?;
|
||||
|
||||
|
|
@ -102,11 +96,9 @@ pub(crate) async fn get_tags_route(
|
|||
) -> Result<Ra<get_tags::v3::Response>> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let event = services().account_data.get_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&RoomAccountDataEventType::Tag,
|
||||
)?;
|
||||
let event = services()
|
||||
.account_data
|
||||
.get_room::<TagEventContent>(&body.room_id, sender_user)?;
|
||||
|
||||
let tags_event = event.map_or_else(
|
||||
|| {
|
||||
|
|
@ -115,7 +107,7 @@ pub(crate) async fn get_tags_route(
|
|||
})
|
||||
},
|
||||
|e| {
|
||||
e.deserialize_as::<TagEventContent>().map_err(|_| {
|
||||
e.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})
|
||||
},
|
||||
|
|
|
|||
|
|
@ -7,9 +7,7 @@ use std::{
|
|||
};
|
||||
|
||||
use ruma::{
|
||||
events::{push_rules::PushRulesEventContent, GlobalAccountDataEventType},
|
||||
push::Ruleset,
|
||||
serde::Raw,
|
||||
events::push_rules::PushRulesEventContent, push::Ruleset, serde::Raw,
|
||||
EventId, OwnedRoomId, RoomId, UserId,
|
||||
};
|
||||
use tracing::{debug, error, info, info_span, warn, Instrument};
|
||||
|
|
@ -860,16 +858,12 @@ impl KeyValueDatabase {
|
|||
|
||||
let raw_rules_list = services()
|
||||
.account_data
|
||||
.get_global(
|
||||
&user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
)
|
||||
.get_global::<PushRulesEventContent>(&user)
|
||||
.unwrap()
|
||||
.expect("Username is invalid");
|
||||
|
||||
let mut account_data = raw_rules_list
|
||||
.deserialize_as::<PushRulesEventContent>()
|
||||
.unwrap();
|
||||
let mut account_data =
|
||||
raw_rules_list.deserialize().unwrap();
|
||||
let rules_list = &mut account_data.global;
|
||||
|
||||
//content rule
|
||||
|
|
@ -925,8 +919,7 @@ impl KeyValueDatabase {
|
|||
|
||||
services().account_data.update_global(
|
||||
&user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&account_data.into())
|
||||
&Raw::new(&account_data)
|
||||
.expect("json serialization should always succeed"),
|
||||
)?;
|
||||
}
|
||||
|
|
@ -954,16 +947,12 @@ impl KeyValueDatabase {
|
|||
|
||||
let raw_rules_list = services()
|
||||
.account_data
|
||||
.get_global(
|
||||
&user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
)
|
||||
.get_global::<PushRulesEventContent>(&user)
|
||||
.unwrap()
|
||||
.expect("Username is invalid");
|
||||
|
||||
let mut account_data = raw_rules_list
|
||||
.deserialize_as::<PushRulesEventContent>()
|
||||
.unwrap();
|
||||
let mut account_data =
|
||||
raw_rules_list.deserialize().unwrap();
|
||||
|
||||
let user_default_rules = Ruleset::server_default(&user);
|
||||
account_data
|
||||
|
|
@ -972,8 +961,7 @@ impl KeyValueDatabase {
|
|||
|
||||
services().account_data.update_global(
|
||||
&user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&account_data.into())
|
||||
&Raw::new(&account_data)
|
||||
.expect("json serialization should always succeed"),
|
||||
)?;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@ use ruma::{
|
|||
events::{
|
||||
AnyGlobalAccountDataEvent, AnyGlobalAccountDataEventContent,
|
||||
AnyRoomAccountDataEvent, AnyRoomAccountDataEventContent,
|
||||
GlobalAccountDataEventType, RoomAccountDataEventType,
|
||||
GlobalAccountDataEventContent, GlobalAccountDataEventType,
|
||||
RoomAccountDataEventContent, RoomAccountDataEventType,
|
||||
StaticEventContent,
|
||||
},
|
||||
serde::Raw,
|
||||
RoomId, UserId,
|
||||
|
|
@ -101,9 +103,29 @@ impl Service {
|
|||
}
|
||||
|
||||
/// Places one event in the global account data of the user and removes the
|
||||
/// previous entry.
|
||||
/// previous entry, with a static event type.
|
||||
#[tracing::instrument(skip(self, user_id, content))]
|
||||
pub(crate) fn update_global(
|
||||
pub(crate) fn update_global<T>(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
content: &Raw<T>,
|
||||
) -> Result<()>
|
||||
where
|
||||
T: GlobalAccountDataEventContent + StaticEventContent,
|
||||
{
|
||||
let event_type = T::TYPE.into();
|
||||
let content = content.cast_ref::<AnyGlobalAccountDataEventContent>();
|
||||
let event = raw_global_event_from_parts(&event_type, content);
|
||||
self.db.update(None, user_id, &event_type.to_string(), event.json())
|
||||
}
|
||||
|
||||
/// Places one event in the global account data of the user and removes the
|
||||
/// previous entry, with a dynamic event type.
|
||||
///
|
||||
/// If the event type is known statically, [`Service::update_global`] should
|
||||
/// be perferred for better type-safety.
|
||||
#[tracing::instrument(skip(self, user_id, content))]
|
||||
pub(crate) fn update_global_any(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
event_type: &GlobalAccountDataEventType,
|
||||
|
|
@ -114,9 +136,35 @@ impl Service {
|
|||
}
|
||||
|
||||
/// Places one event in the room account data of the user and removes the
|
||||
/// previous entry for that room.
|
||||
/// previous entry for that room, with a static event type.
|
||||
#[tracing::instrument(skip(self, room_id, user_id, content))]
|
||||
pub(crate) fn update_room(
|
||||
pub(crate) fn update_room<T>(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
user_id: &UserId,
|
||||
content: &Raw<T>,
|
||||
) -> Result<()>
|
||||
where
|
||||
T: RoomAccountDataEventContent + StaticEventContent,
|
||||
{
|
||||
let event_type = T::TYPE.into();
|
||||
let content = content.cast_ref::<AnyRoomAccountDataEventContent>();
|
||||
let event = raw_room_event_from_parts(&event_type, content);
|
||||
self.db.update(
|
||||
Some(room_id),
|
||||
user_id,
|
||||
&event_type.to_string(),
|
||||
event.json(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Places one event in the room account data of the user and removes the
|
||||
/// previous entry for that room, with a dynamic event type.
|
||||
///
|
||||
/// If the event type is known statically, [`Service::update_room`] should
|
||||
/// be perferred for better type-safety.
|
||||
#[tracing::instrument(skip(self, room_id, user_id, content))]
|
||||
pub(crate) fn update_room_any(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
user_id: &UserId,
|
||||
|
|
@ -132,9 +180,31 @@ impl Service {
|
|||
)
|
||||
}
|
||||
|
||||
/// Searches the global account data for a specific kind.
|
||||
/// Searches the global account data for a specific static event type.
|
||||
#[tracing::instrument(skip(self, user_id))]
|
||||
pub(crate) fn get_global<T>(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
) -> Result<Option<Raw<T>>>
|
||||
where
|
||||
T: GlobalAccountDataEventContent + StaticEventContent,
|
||||
{
|
||||
let Some(event) = self.db.get(None, user_id, T::TYPE)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let event = Raw::<AnyGlobalAccountDataEvent>::from_json(event);
|
||||
let (_, content) = raw_global_event_to_parts(&event).map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
Ok(Some(content.cast::<T>()))
|
||||
}
|
||||
|
||||
/// Searches the global account data for a specific dynamic event type.
|
||||
///
|
||||
/// If the event type is known statically, [`Service::get_global`] should
|
||||
/// be perferred for better type-safety.
|
||||
#[tracing::instrument(skip(self, user_id, event_type))]
|
||||
pub(crate) fn get_global(
|
||||
pub(crate) fn get_global_any(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
event_type: &GlobalAccountDataEventType,
|
||||
|
|
@ -151,9 +221,32 @@ impl Service {
|
|||
Ok(Some(content))
|
||||
}
|
||||
|
||||
/// Searches the room account data for a specific kind.
|
||||
/// Searches the room account data for a specific static event type.
|
||||
#[tracing::instrument(skip(self, room_id, user_id))]
|
||||
pub(crate) fn get_room<T>(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
user_id: &UserId,
|
||||
) -> Result<Option<Raw<T>>>
|
||||
where
|
||||
T: RoomAccountDataEventContent + StaticEventContent,
|
||||
{
|
||||
let Some(event) = self.db.get(Some(room_id), user_id, T::TYPE)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let event = Raw::<AnyRoomAccountDataEvent>::from_json(event);
|
||||
let (_, content) = raw_room_event_to_parts(&event).map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
Ok(Some(content.cast::<T>()))
|
||||
}
|
||||
|
||||
/// Searches the room account data for a specific dynamic event type.
|
||||
///
|
||||
/// If the event type is known statically, [`Service::get_room`] should
|
||||
/// be perferred for better type-safety.
|
||||
#[tracing::instrument(skip(self, room_id, user_id, event_type))]
|
||||
pub(crate) fn get_room(
|
||||
pub(crate) fn get_room_any(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
user_id: &UserId,
|
||||
|
|
|
|||
|
|
@ -773,15 +773,9 @@ impl Service {
|
|||
// Initial account data
|
||||
services().account_data.update_global(
|
||||
&user_id,
|
||||
&ruma::events::GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(
|
||||
&PushRulesEventContent {
|
||||
global: ruma::push::Ruleset::server_default(
|
||||
&user_id,
|
||||
),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
&Raw::new(&PushRulesEventContent {
|
||||
global: ruma::push::Ruleset::server_default(&user_id),
|
||||
})
|
||||
.expect("json serialization should always succeed"),
|
||||
)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ use ruma::{
|
|||
api::federation::discovery::ServerSigningKeys,
|
||||
events::{
|
||||
push_rules::PushRulesEventContent,
|
||||
room::message::RoomMessageEventContent, GlobalAccountDataEventType,
|
||||
room::message::RoomMessageEventContent,
|
||||
},
|
||||
push::Ruleset,
|
||||
serde::{Base64, Raw},
|
||||
|
|
@ -493,13 +493,9 @@ impl Service {
|
|||
|
||||
services().account_data.update_global(
|
||||
admin_bot,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(
|
||||
&PushRulesEventContent {
|
||||
global: ruleset,
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
&Raw::new(&PushRulesEventContent {
|
||||
global: ruleset,
|
||||
})
|
||||
.expect("json serialization should always succeed"),
|
||||
)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ use std::{
|
|||
|
||||
use ruma::{
|
||||
events::{
|
||||
direct::DirectEventContent,
|
||||
ignored_user_list::IgnoredUserListEventContent,
|
||||
room::member::MembershipState, AnyGlobalAccountDataEventContent,
|
||||
AnyStrippedStateEvent, AnySyncStateEvent, GlobalAccountDataEventType,
|
||||
RoomAccountDataEventType,
|
||||
room::member::MembershipState, tag::TagEventContent,
|
||||
AnyStrippedStateEvent, AnySyncStateEvent,
|
||||
},
|
||||
serde::Raw,
|
||||
OwnedRoomId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId,
|
||||
|
|
@ -96,14 +96,9 @@ impl Service {
|
|||
// We want to know if the sender is ignored by the receiver
|
||||
let is_ignored = services()
|
||||
.account_data
|
||||
// Ignored users are in global account data
|
||||
.get_global(
|
||||
// Receiver
|
||||
user_id,
|
||||
&GlobalAccountDataEventType::IgnoredUserList,
|
||||
)?
|
||||
.get_global::<IgnoredUserListEventContent>(user_id)?
|
||||
.map(|event| {
|
||||
event.deserialize_as::<IgnoredUserListEventContent>()
|
||||
event.deserialize()
|
||||
.map_err(|error| {
|
||||
warn!(
|
||||
%error,
|
||||
|
|
@ -192,20 +187,15 @@ impl Service {
|
|||
from_room_id: &RoomId,
|
||||
to_room_id: &RoomId,
|
||||
) -> Result<()> {
|
||||
let Some(event) = services().account_data.get_room(
|
||||
from_room_id,
|
||||
user_id,
|
||||
&RoomAccountDataEventType::Tag,
|
||||
)?
|
||||
let Some(event) = services()
|
||||
.account_data
|
||||
.get_room::<TagEventContent>(from_room_id, user_id)?
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
if let Err(error) = services().account_data.update_room(
|
||||
to_room_id,
|
||||
user_id,
|
||||
&RoomAccountDataEventType::Tag,
|
||||
&event,
|
||||
) {
|
||||
if let Err(error) =
|
||||
services().account_data.update_room(to_room_id, user_id, &event)
|
||||
{
|
||||
warn!(%error, "error writing m.tag account data to upgraded room");
|
||||
}
|
||||
|
||||
|
|
@ -223,7 +213,7 @@ impl Service {
|
|||
) -> Result<()> {
|
||||
let Some(event_content) = services()
|
||||
.account_data
|
||||
.get_global(user_id, &GlobalAccountDataEventType::Direct)?
|
||||
.get_global::<DirectEventContent>(user_id)?
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
|
|
@ -270,10 +260,9 @@ impl Service {
|
|||
if event_updated {
|
||||
if let Err(error) = services().account_data.update_global(
|
||||
user_id,
|
||||
&GlobalAccountDataEventType::Direct,
|
||||
&Raw::new(&event_content)
|
||||
.expect("json serialization should always succeed")
|
||||
.cast::<AnyGlobalAccountDataEventContent>(),
|
||||
.cast::<DirectEventContent>(),
|
||||
) {
|
||||
warn!(%error, "error writing m.direct account data event after upgrading room");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ use ruma::{
|
|||
power_levels::RoomPowerLevelsEventContent,
|
||||
redaction::RoomRedactionEventContent,
|
||||
},
|
||||
GlobalAccountDataEventType, StateEventType, TimelineEventType,
|
||||
StateEventType, TimelineEventType,
|
||||
},
|
||||
push::{Action, Ruleset, Tweak},
|
||||
state_res::{self, Event},
|
||||
|
|
@ -417,15 +417,11 @@ impl Service {
|
|||
|
||||
let rules_for_user = services()
|
||||
.account_data
|
||||
.get_global(user, &GlobalAccountDataEventType::PushRules)?
|
||||
.get_global::<PushRulesEventContent>(user)?
|
||||
.map(|event| {
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(
|
||||
|_| {
|
||||
Error::bad_database(
|
||||
"Invalid push rules event in db.",
|
||||
)
|
||||
},
|
||||
)
|
||||
event.deserialize().map_err(|_| {
|
||||
Error::bad_database("Invalid push rules event in db.")
|
||||
})
|
||||
})
|
||||
.transpose()?
|
||||
.map_or_else(|| Ruleset::server_default(user), |ev| ev.global);
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ use ruma::{
|
|||
device_id,
|
||||
events::{
|
||||
push_rules::PushRulesEventContent, receipt::ReceiptType,
|
||||
AnySyncEphemeralRoomEvent, GlobalAccountDataEventType,
|
||||
AnySyncEphemeralRoomEvent,
|
||||
},
|
||||
push,
|
||||
serde::Raw,
|
||||
|
|
@ -857,11 +857,9 @@ async fn handle_push_event(
|
|||
|
||||
let rules_for_user = services()
|
||||
.account_data
|
||||
.get_global(userid, &GlobalAccountDataEventType::PushRules)
|
||||
.get_global::<PushRulesEventContent>(userid)
|
||||
.unwrap_or_default()
|
||||
.and_then(|event| {
|
||||
event.deserialize_as::<PushRulesEventContent>().ok()
|
||||
})
|
||||
.and_then(|event| event.deserialize().ok())
|
||||
.map_or_else(
|
||||
|| push::Ruleset::server_default(userid),
|
||||
|ev| ev.global,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue