abstract account-data deserializations for serde_json::from_elim

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-02 07:57:18 +00:00 committed by strawberry
parent 48a767d52c
commit da34b43302
12 changed files with 133 additions and 210 deletions

View file

@ -5,14 +5,17 @@ use conduit::{
utils::{stream::TryIgnore, ReadyExt},
Err, Error, Result,
};
use database::{Deserialized, Map};
use database::{Deserialized, Handle, Map};
use futures::{StreamExt, TryFutureExt};
use ruma::{
events::{AnyGlobalAccountDataEvent, AnyRawAccountDataEvent, AnyRoomAccountDataEvent, RoomAccountDataEventType},
events::{
AnyGlobalAccountDataEvent, AnyRawAccountDataEvent, AnyRoomAccountDataEvent, GlobalAccountDataEventType,
RoomAccountDataEventType,
},
serde::Raw,
RoomId, UserId,
};
use serde_json::value::RawValue;
use serde::Deserialize;
use crate::{globals, Dep};
@ -97,18 +100,36 @@ pub async fn update(
Ok(())
}
/// Searches the account data for a specific kind.
/// Searches the room account data for a specific kind.
#[implement(Service)]
pub async fn get(
&self, room_id: Option<&RoomId>, user_id: &UserId, kind: RoomAccountDataEventType,
) -> Result<Box<RawValue>> {
let key = (room_id, user_id, kind.to_string());
pub async fn get_global<T>(&self, user_id: &UserId, kind: GlobalAccountDataEventType) -> Result<T>
where
T: for<'de> Deserialize<'de>,
{
self.get_raw(None, user_id, &kind.to_string())
.await
.deserialized()
}
/// Searches the global account data for a specific kind.
#[implement(Service)]
pub async fn get_room<T>(&self, room_id: &RoomId, user_id: &UserId, kind: RoomAccountDataEventType) -> Result<T>
where
T: for<'de> Deserialize<'de>,
{
self.get_raw(Some(room_id), user_id, &kind.to_string())
.await
.deserialized()
}
#[implement(Service)]
pub async fn get_raw(&self, room_id: Option<&RoomId>, user_id: &UserId, kind: &str) -> Result<Handle<'_>> {
let key = (room_id, user_id, kind.to_owned());
self.db
.roomusertype_roomuserdataid
.qry(&key)
.and_then(|roomuserdataid| self.db.roomuserdataid_accountdata.get(&roomuserdataid))
.await
.deserialized()
}
/// Returns all changes to the account data that happened after `since`.

View file

@ -143,9 +143,8 @@ async fn set_room_tag(&self, room_id: &RoomId, user_id: &UserId, tag: &str) -> R
let mut event = self
.services
.account_data
.get(Some(room_id), user_id, RoomAccountDataEventType::Tag)
.get_room(room_id, user_id, RoomAccountDataEventType::Tag)
.await
.and_then(|event| serde_json::from_str(event.get()).map_err(Into::into))
.unwrap_or_else(|_| TagEvent {
content: TagEventContent {
tags: BTreeMap::new(),

View file

@ -215,13 +215,12 @@ async fn db_lt_12(services: &Services) -> Result<()> {
},
};
let raw_rules_list = services
let mut account_data: PushRulesEvent = services
.account_data
.get(None, &user, GlobalAccountDataEventType::PushRules.to_string().into())
.get_global(&user, GlobalAccountDataEventType::PushRules)
.await
.expect("Username is invalid");
let mut account_data = serde_json::from_str::<PushRulesEvent>(raw_rules_list.get()).unwrap();
let rules_list = &mut account_data.content.global;
//content rule
@ -294,14 +293,12 @@ async fn db_lt_13(services: &Services) -> Result<()> {
},
};
let raw_rules_list = services
let mut account_data: PushRulesEvent = services
.account_data
.get(None, &user, GlobalAccountDataEventType::PushRules.to_string().into())
.get_global(&user, GlobalAccountDataEventType::PushRules)
.await
.expect("Username is invalid");
let mut account_data = serde_json::from_str::<PushRulesEvent>(raw_rules_list.get()).unwrap();
let user_default_rules = Ruleset::server_default(&user);
account_data
.content

View file

@ -146,12 +146,9 @@ impl Service {
if let Ok(tag_event) = self
.services
.account_data
.get(Some(&predecessor.room_id), user_id, RoomAccountDataEventType::Tag)
.get_room(&predecessor.room_id, user_id, RoomAccountDataEventType::Tag)
.await
.and_then(|event| {
serde_json::from_str(event.get())
.map_err(|e| err!(Database(warn!("Invalid account data event in db: {e:?}"))))
}) {
{
self.services
.account_data
.update(Some(room_id), user_id, RoomAccountDataEventType::Tag, &tag_event)
@ -163,12 +160,9 @@ impl Service {
if let Ok(mut direct_event) = self
.services
.account_data
.get(None, user_id, GlobalAccountDataEventType::Direct.to_string().into())
.get_global::<DirectEvent>(user_id, GlobalAccountDataEventType::Direct)
.await
.and_then(|event| {
serde_json::from_str::<DirectEvent>(event.get())
.map_err(|e| err!(Database(warn!("Invalid account data event in db: {e:?}"))))
}) {
{
let mut room_ids_updated = false;
for room_ids in direct_event.content.0.values_mut() {
if room_ids.iter().any(|r| r == &predecessor.room_id) {

View file

@ -407,9 +407,8 @@ impl Service {
let rules_for_user = self
.services
.account_data
.get(None, user, GlobalAccountDataEventType::PushRules.to_string().into())
.get_global(user, GlobalAccountDataEventType::PushRules)
.await
.and_then(|event| serde_json::from_str::<PushRulesEvent>(event.get()).map_err(Into::into))
.map_or_else(|_| Ruleset::server_default(user), |ev: PushRulesEvent| ev.content.global);
let mut highlight = false;

View file

@ -539,9 +539,8 @@ impl Service {
let rules_for_user = self
.services
.account_data
.get(None, userid, GlobalAccountDataEventType::PushRules.to_string().into())
.get_global(userid, GlobalAccountDataEventType::PushRules)
.await
.and_then(|event| serde_json::from_str::<PushRulesEvent>(event.get()).map_err(Into::into))
.map_or_else(
|_| push::Ruleset::server_default(userid),
|ev: PushRulesEvent| ev.content.global,

View file

@ -98,19 +98,9 @@ impl Service {
pub async fn user_is_ignored(&self, sender_user: &UserId, recipient_user: &UserId) -> bool {
self.services
.account_data
.get(
None,
recipient_user,
GlobalAccountDataEventType::IgnoredUserList
.to_string()
.into(),
)
.get_global(recipient_user, GlobalAccountDataEventType::IgnoredUserList)
.await
.and_then(|event| {
serde_json::from_str::<IgnoredUserListEvent>(event.get())
.map_err(|e| err!(Database(warn!("Invalid account data event in db: {e:?}"))))
})
.map_or(false, |ignored| {
.map_or(false, |ignored: IgnoredUserListEvent| {
ignored
.content
.ignored_users