mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-16 07:11:24 +01:00
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:
parent
66210bc32d
commit
b82458a460
15 changed files with 277 additions and 240 deletions
|
|
@ -12,8 +12,7 @@ use ruma::{
|
|||
uiaa::{AuthFlow, AuthType, UiaaInfo},
|
||||
},
|
||||
events::{
|
||||
room::message::RoomMessageEventContent, AnyGlobalAccountDataEvent,
|
||||
GlobalAccountDataEventType,
|
||||
room::message::RoomMessageEventContent, GlobalAccountDataEventType,
|
||||
},
|
||||
push,
|
||||
serde::Raw,
|
||||
|
|
@ -240,13 +239,13 @@ pub(crate) async fn register_route(
|
|||
services().account_data.update_global(
|
||||
&user_id,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&ruma::events::push_rules::PushRulesEvent {
|
||||
content: ruma::events::push_rules::PushRulesEventContent {
|
||||
&Raw::new(
|
||||
&ruma::events::push_rules::PushRulesEventContent {
|
||||
global: push::Ruleset::server_default(&user_id),
|
||||
},
|
||||
})
|
||||
.expect("constructed event should be valid")
|
||||
.cast::<AnyGlobalAccountDataEvent>(),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.expect("constructed event should be valid"),
|
||||
)?;
|
||||
|
||||
// Inhibit login does not work for guests
|
||||
|
|
|
|||
|
|
@ -1,19 +1,10 @@
|
|||
use ruma::{
|
||||
api::client::{
|
||||
config::{
|
||||
get_global_account_data, get_room_account_data,
|
||||
set_global_account_data, set_room_account_data,
|
||||
},
|
||||
error::ErrorKind,
|
||||
use ruma::api::client::{
|
||||
config::{
|
||||
get_global_account_data, get_room_account_data,
|
||||
set_global_account_data, set_room_account_data,
|
||||
},
|
||||
events::{
|
||||
AnyGlobalAccountDataEvent, AnyGlobalAccountDataEventContent,
|
||||
AnyRoomAccountDataEvent, AnyRoomAccountDataEventContent,
|
||||
},
|
||||
serde::Raw,
|
||||
error::ErrorKind,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{services, Ar, Error, Ra, Result};
|
||||
|
||||
|
|
@ -25,17 +16,10 @@ 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");
|
||||
|
||||
let event = Raw::new(&json!({
|
||||
"type": &body.event_type,
|
||||
"content": &body.data,
|
||||
}))
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?
|
||||
.cast::<AnyGlobalAccountDataEvent>();
|
||||
|
||||
services().account_data.update_global(
|
||||
sender_user,
|
||||
&body.event_type,
|
||||
&event,
|
||||
&body.data,
|
||||
)?;
|
||||
|
||||
Ok(Ra(set_global_account_data::v3::Response {}))
|
||||
|
|
@ -49,18 +33,11 @@ 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");
|
||||
|
||||
let event = Raw::new(&json!({
|
||||
"type": &body.event_type,
|
||||
"content": &body.data,
|
||||
}))
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?
|
||||
.cast::<AnyRoomAccountDataEvent>();
|
||||
|
||||
services().account_data.update_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&body.event_type,
|
||||
&event,
|
||||
&body.data,
|
||||
)?;
|
||||
|
||||
Ok(Ra(set_room_account_data::v3::Response {}))
|
||||
|
|
@ -74,16 +51,11 @@ pub(crate) async fn get_global_account_data_route(
|
|||
) -> Result<Ra<get_global_account_data::v3::Response>> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let event = services()
|
||||
let account_data = services()
|
||||
.account_data
|
||||
.get_global(sender_user, &body.event_type)?
|
||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||
|
||||
let account_data = event
|
||||
.deserialize_as::<ExtractGlobalEventContent>()
|
||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||
.content;
|
||||
|
||||
Ok(Ra(get_global_account_data::v3::Response {
|
||||
account_data,
|
||||
}))
|
||||
|
|
@ -97,27 +69,12 @@ pub(crate) async fn get_room_account_data_route(
|
|||
) -> Result<Ra<get_room_account_data::v3::Response>> {
|
||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
let event = services()
|
||||
let account_data = services()
|
||||
.account_data
|
||||
.get_room(&body.room_id, sender_user, &body.event_type)?
|
||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||
|
||||
let account_data = event
|
||||
.deserialize_as::<ExtractRoomEventContent>()
|
||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||
.content;
|
||||
|
||||
Ok(Ra(get_room_account_data::v3::Response {
|
||||
account_data,
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ExtractRoomEventContent {
|
||||
content: Raw<AnyRoomAccountDataEventContent>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ExtractGlobalEventContent {
|
||||
content: Raw<AnyGlobalAccountDataEventContent>,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,10 +7,7 @@ use ruma::{
|
|||
set_pushrule_actions, set_pushrule_enabled,
|
||||
},
|
||||
},
|
||||
events::{
|
||||
push_rules::PushRulesEvent, AnyGlobalAccountDataEvent,
|
||||
GlobalAccountDataEventType,
|
||||
},
|
||||
events::{push_rules::PushRulesEventContent, GlobalAccountDataEventType},
|
||||
push::{AnyPushRuleRef, InsertPushRuleError, RemovePushRuleError},
|
||||
serde::Raw,
|
||||
};
|
||||
|
|
@ -33,10 +30,10 @@ pub(crate) async fn get_pushrules_all_route(
|
|||
"PushRules event not found.",
|
||||
))?;
|
||||
|
||||
let account_data = event
|
||||
.deserialize_as::<PushRulesEvent>()
|
||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||
.content;
|
||||
let account_data =
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
Ok(Ra(get_pushrules_all::v3::Response {
|
||||
global: account_data.global,
|
||||
|
|
@ -59,10 +56,10 @@ pub(crate) async fn get_pushrule_route(
|
|||
"PushRules event not found.",
|
||||
))?;
|
||||
|
||||
let account_data = event
|
||||
.deserialize_as::<PushRulesEvent>()
|
||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||
.content;
|
||||
let account_data =
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
let rule = account_data
|
||||
.global
|
||||
|
|
@ -96,11 +93,11 @@ pub(crate) async fn set_pushrule_route(
|
|||
))?;
|
||||
|
||||
let mut account_data =
|
||||
event.deserialize_as::<PushRulesEvent>().map_err(|_| {
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
if let Err(error) = account_data.content.global.insert(
|
||||
if let Err(error) = account_data.global.insert(
|
||||
body.rule.clone(),
|
||||
body.after.as_deref(),
|
||||
body.before.as_deref(),
|
||||
|
|
@ -139,9 +136,8 @@ pub(crate) async fn set_pushrule_route(
|
|||
services().account_data.update_global(
|
||||
sender_user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&account_data)
|
||||
.expect("json event serialization should always succeed")
|
||||
.cast::<AnyGlobalAccountDataEvent>(),
|
||||
&Raw::new(&account_data.into())
|
||||
.expect("json event serialization should always succeed"),
|
||||
)?;
|
||||
|
||||
Ok(Ra(set_pushrule::v3::Response {}))
|
||||
|
|
@ -163,10 +159,10 @@ pub(crate) async fn get_pushrule_actions_route(
|
|||
"PushRules event not found.",
|
||||
))?;
|
||||
|
||||
let account_data = event
|
||||
.deserialize_as::<PushRulesEvent>()
|
||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||
.content;
|
||||
let account_data =
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
let global = account_data.global;
|
||||
let actions = global
|
||||
|
|
@ -199,12 +195,11 @@ pub(crate) async fn set_pushrule_actions_route(
|
|||
))?;
|
||||
|
||||
let mut account_data =
|
||||
event.deserialize_as::<PushRulesEvent>().map_err(|_| {
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
if account_data
|
||||
.content
|
||||
.global
|
||||
.set_actions(body.kind.clone(), &body.rule_id, body.actions.clone())
|
||||
.is_err()
|
||||
|
|
@ -218,9 +213,8 @@ pub(crate) async fn set_pushrule_actions_route(
|
|||
services().account_data.update_global(
|
||||
sender_user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&account_data)
|
||||
.expect("json event serialization should always suceed")
|
||||
.cast::<AnyGlobalAccountDataEvent>(),
|
||||
&Raw::new(&account_data.into())
|
||||
.expect("json event serialization should always suceed"),
|
||||
)?;
|
||||
|
||||
Ok(Ra(set_pushrule_actions::v3::Response {}))
|
||||
|
|
@ -243,11 +237,11 @@ pub(crate) async fn get_pushrule_enabled_route(
|
|||
))?;
|
||||
|
||||
let account_data =
|
||||
event.deserialize_as::<PushRulesEvent>().map_err(|_| {
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
let global = account_data.content.global;
|
||||
let global = account_data.global;
|
||||
let enabled = global
|
||||
.get(body.kind.clone(), &body.rule_id)
|
||||
.map(AnyPushRuleRef::enabled)
|
||||
|
|
@ -278,12 +272,11 @@ pub(crate) async fn set_pushrule_enabled_route(
|
|||
))?;
|
||||
|
||||
let mut account_data =
|
||||
event.deserialize_as::<PushRulesEvent>().map_err(|_| {
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
if account_data
|
||||
.content
|
||||
.global
|
||||
.set_enabled(body.kind.clone(), &body.rule_id, body.enabled)
|
||||
.is_err()
|
||||
|
|
@ -297,9 +290,8 @@ pub(crate) async fn set_pushrule_enabled_route(
|
|||
services().account_data.update_global(
|
||||
sender_user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&account_data)
|
||||
.expect("json event serialization should always succeed")
|
||||
.cast::<AnyGlobalAccountDataEvent>(),
|
||||
&Raw::new(&account_data.into())
|
||||
.expect("json event serialization should always succeed"),
|
||||
)?;
|
||||
|
||||
Ok(Ra(set_pushrule_enabled::v3::Response {}))
|
||||
|
|
@ -322,12 +314,12 @@ pub(crate) async fn delete_pushrule_route(
|
|||
))?;
|
||||
|
||||
let mut account_data =
|
||||
event.deserialize_as::<PushRulesEvent>().map_err(|_| {
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?;
|
||||
|
||||
if let Err(error) =
|
||||
account_data.content.global.remove(body.kind.clone(), &body.rule_id)
|
||||
account_data.global.remove(body.kind.clone(), &body.rule_id)
|
||||
{
|
||||
let err = match error {
|
||||
RemovePushRuleError::ServerDefault => Error::BadRequest(
|
||||
|
|
@ -346,9 +338,8 @@ pub(crate) async fn delete_pushrule_route(
|
|||
services().account_data.update_global(
|
||||
sender_user,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&account_data)
|
||||
.expect("json event serialization should always suceed")
|
||||
.cast::<AnyGlobalAccountDataEvent>(),
|
||||
&Raw::new(&account_data.into())
|
||||
.expect("json event serialization should always suceed"),
|
||||
)?;
|
||||
|
||||
Ok(Ra(delete_pushrule::v3::Response {}))
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use ruma::{
|
|||
},
|
||||
events::{
|
||||
receipt::{ReceiptThread, ReceiptType},
|
||||
AnyRoomAccountDataEvent, RoomAccountDataEventType,
|
||||
RoomAccountDataEventType,
|
||||
},
|
||||
serde::Raw,
|
||||
MilliSecondsSinceUnixEpoch,
|
||||
|
|
@ -29,18 +29,16 @@ pub(crate) async fn set_read_marker_route(
|
|||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
||||
|
||||
if let Some(fully_read) = &body.fully_read {
|
||||
let fully_read_event = ruma::events::fully_read::FullyReadEvent {
|
||||
content: ruma::events::fully_read::FullyReadEventContent {
|
||||
let fully_read_event =
|
||||
ruma::events::fully_read::FullyReadEventContent {
|
||||
event_id: fully_read.clone(),
|
||||
},
|
||||
};
|
||||
};
|
||||
services().account_data.update_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&RoomAccountDataEventType::FullyRead,
|
||||
&Raw::new(&fully_read_event)
|
||||
.expect("json event serialization should always suceed")
|
||||
.cast::<AnyRoomAccountDataEvent>(),
|
||||
&Raw::new(&fully_read_event.into())
|
||||
.expect("json event serialization should always suceed"),
|
||||
)?;
|
||||
}
|
||||
|
||||
|
|
@ -126,18 +124,16 @@ pub(crate) async fn create_receipt_route(
|
|||
|
||||
match body.receipt_type {
|
||||
create_receipt::v3::ReceiptType::FullyRead => {
|
||||
let fully_read_event = ruma::events::fully_read::FullyReadEvent {
|
||||
content: ruma::events::fully_read::FullyReadEventContent {
|
||||
let fully_read_event =
|
||||
ruma::events::fully_read::FullyReadEventContent {
|
||||
event_id: body.event_id.clone(),
|
||||
},
|
||||
};
|
||||
};
|
||||
services().account_data.update_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&RoomAccountDataEventType::FullyRead,
|
||||
&Raw::new(&fully_read_event)
|
||||
.expect("json event serialization should always succeed")
|
||||
.cast::<AnyRoomAccountDataEvent>(),
|
||||
&Raw::new(&fully_read_event.into())
|
||||
.expect("json event serialization should always succeed"),
|
||||
)?;
|
||||
}
|
||||
create_receipt::v3::ReceiptType::Read => {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,8 @@ use tracing::{debug, error};
|
|||
|
||||
use super::{load_timeline, share_encrypted_room};
|
||||
use crate::{
|
||||
service::rooms::timeline::PduCount, services, Ar, Error, Ra, Result,
|
||||
service::{account_data, rooms::timeline::PduCount},
|
||||
services, Ar, Error, Ra, Result,
|
||||
};
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
|
|
@ -645,7 +646,13 @@ pub(crate) async fn sync_events_v4_route(
|
|||
services()
|
||||
.account_data
|
||||
.global_changes_since(&sender_user, globalsince)?
|
||||
.into_values()
|
||||
.into_iter()
|
||||
.map(|(event_type, content)| {
|
||||
account_data::raw_global_event_from_parts(
|
||||
&event_type,
|
||||
&content,
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ use tracing::{debug, error, field};
|
|||
|
||||
use super::{load_timeline, share_encrypted_room};
|
||||
use crate::{
|
||||
service::{pdu::EventHash, rooms::timeline::PduCount},
|
||||
service::{account_data, pdu::EventHash, rooms::timeline::PduCount},
|
||||
services, utils, Ar, Error, PduEvent, Ra, Result,
|
||||
};
|
||||
|
||||
|
|
@ -235,7 +235,13 @@ pub(crate) async fn sync_events_route(
|
|||
events: services()
|
||||
.account_data
|
||||
.global_changes_since(ctx.sender_user, ctx.since)?
|
||||
.into_values()
|
||||
.into_iter()
|
||||
.map(|(event_type, content)| {
|
||||
account_data::raw_global_event_from_parts(
|
||||
&event_type,
|
||||
&content,
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
},
|
||||
device_lists: DeviceLists {
|
||||
|
|
@ -872,7 +878,13 @@ async fn load_joined_room(
|
|||
events: services()
|
||||
.account_data
|
||||
.room_changes_since(ctx.sender_user, room_id, ctx.since)?
|
||||
.into_values()
|
||||
.into_iter()
|
||||
.map(|(event_type, content)| {
|
||||
account_data::raw_room_event_from_parts(
|
||||
&event_type,
|
||||
&content,
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
},
|
||||
summary: RoomSummary {
|
||||
|
|
|
|||
|
|
@ -2,10 +2,7 @@ use std::collections::BTreeMap;
|
|||
|
||||
use ruma::{
|
||||
api::client::tag::{create_tag, delete_tag, get_tags},
|
||||
events::{
|
||||
tag::{TagEvent, TagEventContent},
|
||||
AnyRoomAccountDataEvent, RoomAccountDataEventType,
|
||||
},
|
||||
events::{tag::TagEventContent, RoomAccountDataEventType},
|
||||
serde::Raw,
|
||||
};
|
||||
|
||||
|
|
@ -29,31 +26,25 @@ pub(crate) async fn update_tag_route(
|
|||
|
||||
let mut tags_event = event.map_or_else(
|
||||
|| {
|
||||
Ok(TagEvent {
|
||||
content: TagEventContent {
|
||||
tags: BTreeMap::new(),
|
||||
},
|
||||
Ok(TagEventContent {
|
||||
tags: BTreeMap::new(),
|
||||
})
|
||||
},
|
||||
|e| {
|
||||
e.deserialize_as::<TagEvent>().map_err(|_| {
|
||||
e.deserialize_as::<TagEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})
|
||||
},
|
||||
)?;
|
||||
|
||||
tags_event
|
||||
.content
|
||||
.tags
|
||||
.insert(body.tag.clone().into(), body.tag_info.clone());
|
||||
tags_event.tags.insert(body.tag.clone().into(), body.tag_info.clone());
|
||||
|
||||
services().account_data.update_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&RoomAccountDataEventType::Tag,
|
||||
&Raw::new(&tags_event)
|
||||
.expect("json event serialization should always suceed")
|
||||
.cast::<AnyRoomAccountDataEvent>(),
|
||||
&Raw::new(&tags_event.into())
|
||||
.expect("json event serialization should always suceed"),
|
||||
)?;
|
||||
|
||||
Ok(Ra(create_tag::v3::Response {}))
|
||||
|
|
@ -77,28 +68,25 @@ pub(crate) async fn delete_tag_route(
|
|||
|
||||
let mut tags_event = event.map_or_else(
|
||||
|| {
|
||||
Ok(TagEvent {
|
||||
content: TagEventContent {
|
||||
tags: BTreeMap::new(),
|
||||
},
|
||||
Ok(TagEventContent {
|
||||
tags: BTreeMap::new(),
|
||||
})
|
||||
},
|
||||
|e| {
|
||||
e.deserialize_as::<TagEvent>().map_err(|_| {
|
||||
e.deserialize_as::<TagEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})
|
||||
},
|
||||
)?;
|
||||
|
||||
tags_event.content.tags.remove(&body.tag.clone().into());
|
||||
tags_event.tags.remove(&body.tag.clone().into());
|
||||
|
||||
services().account_data.update_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
&RoomAccountDataEventType::Tag,
|
||||
&Raw::new(&tags_event)
|
||||
.expect("json value serialization should always succeed")
|
||||
.cast::<AnyRoomAccountDataEvent>(),
|
||||
&Raw::new(&tags_event.into())
|
||||
.expect("json value serialization should always succeed"),
|
||||
)?;
|
||||
|
||||
Ok(Ra(delete_tag::v3::Response {}))
|
||||
|
|
@ -122,20 +110,18 @@ pub(crate) async fn get_tags_route(
|
|||
|
||||
let tags_event = event.map_or_else(
|
||||
|| {
|
||||
Ok(TagEvent {
|
||||
content: TagEventContent {
|
||||
tags: BTreeMap::new(),
|
||||
},
|
||||
Ok(TagEventContent {
|
||||
tags: BTreeMap::new(),
|
||||
})
|
||||
},
|
||||
|e| {
|
||||
e.deserialize_as::<TagEvent>().map_err(|_| {
|
||||
e.deserialize_as::<TagEventContent>().map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})
|
||||
},
|
||||
)?;
|
||||
|
||||
Ok(Ra(get_tags::v3::Response {
|
||||
tags: tags_event.content.tags,
|
||||
tags: tags_event.tags,
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(())
|
||||
|
|
|
|||
|
|
@ -2,14 +2,16 @@ use std::collections::HashMap;
|
|||
|
||||
use ruma::{
|
||||
events::{
|
||||
AnyGlobalAccountDataEvent, AnyRoomAccountDataEvent,
|
||||
AnyGlobalAccountDataEvent, AnyGlobalAccountDataEventContent,
|
||||
AnyRoomAccountDataEvent, AnyRoomAccountDataEventContent,
|
||||
GlobalAccountDataEventType, RoomAccountDataEventType,
|
||||
},
|
||||
serde::Raw,
|
||||
RoomId, UserId,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::Result;
|
||||
use crate::{Error, Result};
|
||||
|
||||
mod data;
|
||||
|
||||
|
|
@ -19,6 +21,78 @@ pub(crate) struct Service {
|
|||
pub(crate) db: &'static dyn Data,
|
||||
}
|
||||
|
||||
pub(crate) fn raw_global_event_to_parts(
|
||||
event: &Raw<AnyGlobalAccountDataEvent>,
|
||||
) -> serde_json::Result<(
|
||||
GlobalAccountDataEventType,
|
||||
Raw<AnyGlobalAccountDataEventContent>,
|
||||
)> {
|
||||
#[derive(Deserialize)]
|
||||
struct Parts {
|
||||
#[serde(rename = "type")]
|
||||
event_type: GlobalAccountDataEventType,
|
||||
content: Raw<AnyGlobalAccountDataEventContent>,
|
||||
}
|
||||
|
||||
let parts = event.deserialize_as::<Parts>()?;
|
||||
Ok((parts.event_type, parts.content))
|
||||
}
|
||||
|
||||
pub(crate) fn raw_global_event_from_parts(
|
||||
event_type: &GlobalAccountDataEventType,
|
||||
content: &Raw<AnyGlobalAccountDataEventContent>,
|
||||
) -> Raw<AnyGlobalAccountDataEvent> {
|
||||
#[derive(Serialize)]
|
||||
struct Parts<'a> {
|
||||
#[serde(rename = "type")]
|
||||
event_type: &'a GlobalAccountDataEventType,
|
||||
content: &'a Raw<AnyGlobalAccountDataEventContent>,
|
||||
}
|
||||
|
||||
Raw::new(&Parts {
|
||||
event_type,
|
||||
content,
|
||||
})
|
||||
.expect("json serialization should always succeed")
|
||||
.cast::<AnyGlobalAccountDataEvent>()
|
||||
}
|
||||
|
||||
pub(crate) fn raw_room_event_to_parts(
|
||||
event: &Raw<AnyRoomAccountDataEvent>,
|
||||
) -> serde_json::Result<(
|
||||
RoomAccountDataEventType,
|
||||
Raw<AnyRoomAccountDataEventContent>,
|
||||
)> {
|
||||
#[derive(Deserialize)]
|
||||
struct Parts {
|
||||
#[serde(rename = "type")]
|
||||
event_type: RoomAccountDataEventType,
|
||||
content: Raw<AnyRoomAccountDataEventContent>,
|
||||
}
|
||||
|
||||
let parts = event.deserialize_as::<Parts>()?;
|
||||
Ok((parts.event_type, parts.content))
|
||||
}
|
||||
|
||||
pub(crate) fn raw_room_event_from_parts(
|
||||
event_type: &RoomAccountDataEventType,
|
||||
content: &Raw<AnyRoomAccountDataEventContent>,
|
||||
) -> Raw<AnyRoomAccountDataEvent> {
|
||||
#[derive(Serialize)]
|
||||
struct Parts<'a> {
|
||||
#[serde(rename = "type")]
|
||||
event_type: &'a RoomAccountDataEventType,
|
||||
content: &'a Raw<AnyRoomAccountDataEventContent>,
|
||||
}
|
||||
|
||||
Raw::new(&Parts {
|
||||
event_type,
|
||||
content,
|
||||
})
|
||||
.expect("json serialization should always succeed")
|
||||
.cast::<AnyRoomAccountDataEvent>()
|
||||
}
|
||||
|
||||
impl Service {
|
||||
pub(crate) fn new(db: &'static dyn Data) -> Self {
|
||||
Self {
|
||||
|
|
@ -28,26 +102,28 @@ impl Service {
|
|||
|
||||
/// Places one event in the global account data of the user and removes the
|
||||
/// previous entry.
|
||||
#[tracing::instrument(skip(self, user_id, event))]
|
||||
#[tracing::instrument(skip(self, user_id, content))]
|
||||
pub(crate) fn update_global(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
event_type: &GlobalAccountDataEventType,
|
||||
event: &Raw<AnyGlobalAccountDataEvent>,
|
||||
content: &Raw<AnyGlobalAccountDataEventContent>,
|
||||
) -> Result<()> {
|
||||
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 room account data of the user and removes the
|
||||
/// previous entry for that room.
|
||||
#[tracing::instrument(skip(self, room_id, user_id, event))]
|
||||
#[tracing::instrument(skip(self, room_id, user_id, content))]
|
||||
pub(crate) fn update_room(
|
||||
&self,
|
||||
room_id: &RoomId,
|
||||
user_id: &UserId,
|
||||
event_type: &RoomAccountDataEventType,
|
||||
event: &Raw<AnyRoomAccountDataEvent>,
|
||||
content: &Raw<AnyRoomAccountDataEventContent>,
|
||||
) -> Result<()> {
|
||||
let event = raw_room_event_from_parts(event_type, content);
|
||||
self.db.update(
|
||||
Some(room_id),
|
||||
user_id,
|
||||
|
|
@ -62,11 +138,17 @@ impl Service {
|
|||
&self,
|
||||
user_id: &UserId,
|
||||
event_type: &GlobalAccountDataEventType,
|
||||
) -> Result<Option<Raw<AnyGlobalAccountDataEvent>>> {
|
||||
Ok(self
|
||||
.db
|
||||
.get(None, user_id, &event_type.to_string())?
|
||||
.map(Raw::from_json))
|
||||
) -> Result<Option<Raw<AnyGlobalAccountDataEventContent>>> {
|
||||
let Some(event) =
|
||||
self.db.get(None, user_id, &event_type.to_string())?
|
||||
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))
|
||||
}
|
||||
|
||||
/// Searches the room account data for a specific kind.
|
||||
|
|
@ -76,11 +158,17 @@ impl Service {
|
|||
room_id: &RoomId,
|
||||
user_id: &UserId,
|
||||
event_type: &RoomAccountDataEventType,
|
||||
) -> Result<Option<Raw<AnyRoomAccountDataEvent>>> {
|
||||
Ok(self
|
||||
.db
|
||||
.get(Some(room_id), user_id, &event_type.to_string())?
|
||||
.map(Raw::from_json))
|
||||
) -> Result<Option<Raw<AnyRoomAccountDataEventContent>>> {
|
||||
let Some(event) =
|
||||
self.db.get(Some(room_id), user_id, &event_type.to_string())?
|
||||
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))
|
||||
}
|
||||
|
||||
/// Returns all changes to global account data that happened after `since`.
|
||||
|
|
@ -93,16 +181,21 @@ impl Service {
|
|||
user_id: &UserId,
|
||||
since: u64,
|
||||
) -> Result<
|
||||
HashMap<GlobalAccountDataEventType, Raw<AnyGlobalAccountDataEvent>>,
|
||||
HashMap<
|
||||
GlobalAccountDataEventType,
|
||||
Raw<AnyGlobalAccountDataEventContent>,
|
||||
>,
|
||||
> {
|
||||
Ok(self
|
||||
.db
|
||||
self.db
|
||||
.changes_since(None, user_id, since)?
|
||||
.into_iter()
|
||||
.map(|(event_type, event)| {
|
||||
(event_type.into(), Raw::from_json(event))
|
||||
.into_values()
|
||||
.map(|event| {
|
||||
let event = Raw::<AnyGlobalAccountDataEvent>::from_json(event);
|
||||
raw_global_event_to_parts(&event).map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db")
|
||||
})
|
||||
})
|
||||
.collect())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Returns all changes to room account data that happened after `since`.
|
||||
|
|
@ -115,15 +208,18 @@ impl Service {
|
|||
user_id: &UserId,
|
||||
room_id: &RoomId,
|
||||
since: u64,
|
||||
) -> Result<HashMap<RoomAccountDataEventType, Raw<AnyRoomAccountDataEvent>>>
|
||||
{
|
||||
Ok(self
|
||||
.db
|
||||
) -> Result<
|
||||
HashMap<RoomAccountDataEventType, Raw<AnyRoomAccountDataEventContent>>,
|
||||
> {
|
||||
self.db
|
||||
.changes_since(Some(room_id), user_id, since)?
|
||||
.into_iter()
|
||||
.map(|(event_type, event)| {
|
||||
(event_type.into(), Raw::from_json(event))
|
||||
.into_values()
|
||||
.map(|event| {
|
||||
let event = Raw::<AnyRoomAccountDataEvent>::from_json(event);
|
||||
raw_room_event_to_parts(&event).map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db")
|
||||
})
|
||||
})
|
||||
.collect())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@ use crate::Result;
|
|||
/// distinguish between global and room events. Because there are no ruma types
|
||||
/// that cover both, we use strings for the event types and raw json values for
|
||||
/// the contents.
|
||||
//
|
||||
// TODO: once we have the ability to make db schema changes, we should consider
|
||||
// storing only the content in the db, rather than the whole event object.
|
||||
pub(crate) trait Data: Send + Sync {
|
||||
/// Places one event in the account data of the user and removes the
|
||||
/// previous entry.
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ use regex::Regex;
|
|||
use ruma::{
|
||||
api::appservice::Registration,
|
||||
events::{
|
||||
push_rules::{PushRulesEvent, PushRulesEventContent},
|
||||
push_rules::PushRulesEventContent,
|
||||
room::{
|
||||
canonical_alias::RoomCanonicalAliasEventContent,
|
||||
create::RoomCreateEventContent,
|
||||
|
|
@ -20,7 +20,7 @@ use ruma::{
|
|||
power_levels::RoomPowerLevelsEventContent,
|
||||
topic::RoomTopicEventContent,
|
||||
},
|
||||
AnyGlobalAccountDataEvent, TimelineEventType,
|
||||
TimelineEventType,
|
||||
},
|
||||
serde::Raw,
|
||||
signatures::verify_json,
|
||||
|
|
@ -774,15 +774,15 @@ impl Service {
|
|||
services().account_data.update_global(
|
||||
&user_id,
|
||||
&ruma::events::GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&PushRulesEvent {
|
||||
content: PushRulesEventContent {
|
||||
&Raw::new(
|
||||
&PushRulesEventContent {
|
||||
global: ruma::push::Ruleset::server_default(
|
||||
&user_id,
|
||||
),
|
||||
},
|
||||
})
|
||||
.expect("json serialization should always succeed")
|
||||
.cast::<AnyGlobalAccountDataEvent>(),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.expect("json serialization should always succeed"),
|
||||
)?;
|
||||
|
||||
// we dont add a device since we're not the user, just the
|
||||
|
|
|
|||
|
|
@ -18,8 +18,7 @@ use ruma::{
|
|||
api::federation::discovery::ServerSigningKeys,
|
||||
events::{
|
||||
push_rules::PushRulesEventContent,
|
||||
room::message::RoomMessageEventContent, AnyGlobalAccountDataEvent,
|
||||
GlobalAccountDataEvent, GlobalAccountDataEventType,
|
||||
room::message::RoomMessageEventContent, GlobalAccountDataEventType,
|
||||
},
|
||||
push::Ruleset,
|
||||
serde::{Base64, Raw},
|
||||
|
|
@ -495,13 +494,13 @@ impl Service {
|
|||
services().account_data.update_global(
|
||||
admin_bot,
|
||||
&GlobalAccountDataEventType::PushRules,
|
||||
&Raw::new(&GlobalAccountDataEvent {
|
||||
content: PushRulesEventContent {
|
||||
&Raw::new(
|
||||
&PushRulesEventContent {
|
||||
global: ruleset,
|
||||
},
|
||||
})
|
||||
.expect("json serialization should always succeed")
|
||||
.cast::<AnyGlobalAccountDataEvent>(),
|
||||
}
|
||||
.into(),
|
||||
)
|
||||
.expect("json serialization should always succeed"),
|
||||
)?;
|
||||
|
||||
res
|
||||
|
|
|
|||
|
|
@ -5,9 +5,10 @@ use std::{
|
|||
|
||||
use ruma::{
|
||||
events::{
|
||||
ignored_user_list::IgnoredUserListEvent, room::member::MembershipState,
|
||||
AnyGlobalAccountDataEvent, AnyStrippedStateEvent, AnySyncStateEvent,
|
||||
GlobalAccountDataEventType, RoomAccountDataEventType,
|
||||
ignored_user_list::IgnoredUserListEventContent,
|
||||
room::member::MembershipState, AnyGlobalAccountDataEventContent,
|
||||
AnyStrippedStateEvent, AnySyncStateEvent, GlobalAccountDataEventType,
|
||||
RoomAccountDataEventType,
|
||||
},
|
||||
serde::Raw,
|
||||
OwnedRoomId, OwnedServerName, OwnedUserId, RoomId, ServerName, UserId,
|
||||
|
|
@ -102,7 +103,7 @@ impl Service {
|
|||
&GlobalAccountDataEventType::IgnoredUserList,
|
||||
)?
|
||||
.map(|event| {
|
||||
event.deserialize_as::<IgnoredUserListEvent>()
|
||||
event.deserialize_as::<IgnoredUserListEventContent>()
|
||||
.map_err(|error| {
|
||||
warn!(
|
||||
%error,
|
||||
|
|
@ -114,7 +115,6 @@ impl Service {
|
|||
.transpose()?
|
||||
.is_some_and(|ignored| {
|
||||
ignored
|
||||
.content
|
||||
.ignored_users
|
||||
.iter()
|
||||
.any(|(user, _details)| user == sender)
|
||||
|
|
@ -221,14 +221,14 @@ impl Service {
|
|||
from_room_id: &RoomId,
|
||||
to_room_id: &RoomId,
|
||||
) -> Result<()> {
|
||||
let Some(event) = services()
|
||||
let Some(event_content) = services()
|
||||
.account_data
|
||||
.get_global(user_id, &GlobalAccountDataEventType::Direct)?
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let mut event = event
|
||||
let mut event_content = event_content
|
||||
.deserialize_as::<serde_json::Value>()
|
||||
.expect("RawValue -> Value should always succeed");
|
||||
|
||||
|
|
@ -240,14 +240,11 @@ impl Service {
|
|||
//
|
||||
// [1]: https://github.com/element-hq/element-web/issues/27630
|
||||
//
|
||||
// A valid m.direct event looks like this:
|
||||
// Valid m.direct event content looks like this:
|
||||
//
|
||||
// {
|
||||
// "type": "m.account_data",
|
||||
// "content": {
|
||||
// "@userid1": [ "!roomid1", "!roomid2" ],
|
||||
// "@userid2": [ "!roomid3" ],
|
||||
// }
|
||||
// "@userid1": [ "!roomid1", "!roomid2" ],
|
||||
// "@userid2": [ "!roomid3" ],
|
||||
// }
|
||||
//
|
||||
// We want to find userid keys where the value contains from_room_id,
|
||||
|
|
@ -257,10 +254,7 @@ impl Service {
|
|||
// parts.
|
||||
|
||||
let mut event_updated = false;
|
||||
let Some(direct_user_ids) = event.get_mut("content") else {
|
||||
return Ok(());
|
||||
};
|
||||
let Some(direct_user_ids) = direct_user_ids.as_object_mut() else {
|
||||
let Some(direct_user_ids) = event_content.as_object_mut() else {
|
||||
return Ok(());
|
||||
};
|
||||
for room_ids in direct_user_ids.values_mut() {
|
||||
|
|
@ -277,9 +271,9 @@ impl Service {
|
|||
if let Err(error) = services().account_data.update_global(
|
||||
user_id,
|
||||
&GlobalAccountDataEventType::Direct,
|
||||
&Raw::new(&event)
|
||||
&Raw::new(&event_content)
|
||||
.expect("json serialization should always succeed")
|
||||
.cast::<AnyGlobalAccountDataEvent>(),
|
||||
.cast::<AnyGlobalAccountDataEventContent>(),
|
||||
) {
|
||||
warn!(%error, "error writing m.direct account data event after upgrading room");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use ruma::{
|
|||
api::{client::error::ErrorKind, federation},
|
||||
canonical_json::to_canonical_value,
|
||||
events::{
|
||||
push_rules::PushRulesEvent,
|
||||
push_rules::PushRulesEventContent,
|
||||
room::{
|
||||
create::RoomCreateEventContent, encrypted::Relation,
|
||||
member::MembershipState, message::RoomMessageEventContent,
|
||||
|
|
@ -419,15 +419,16 @@ impl Service {
|
|||
.account_data
|
||||
.get_global(user, &GlobalAccountDataEventType::PushRules)?
|
||||
.map(|event| {
|
||||
event.deserialize_as::<PushRulesEvent>().map_err(|_| {
|
||||
Error::bad_database("Invalid push rules event in db.")
|
||||
})
|
||||
event.deserialize_as::<PushRulesEventContent>().map_err(
|
||||
|_| {
|
||||
Error::bad_database(
|
||||
"Invalid push rules event in db.",
|
||||
)
|
||||
},
|
||||
)
|
||||
})
|
||||
.transpose()?
|
||||
.map_or_else(
|
||||
|| Ruleset::server_default(user),
|
||||
|ev: PushRulesEvent| ev.content.global,
|
||||
);
|
||||
.map_or_else(|| Ruleset::server_default(user), |ev| ev.global);
|
||||
|
||||
let mut highlight = false;
|
||||
let mut notify = false;
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ use ruma::{
|
|||
},
|
||||
device_id,
|
||||
events::{
|
||||
push_rules::PushRulesEvent, receipt::ReceiptType,
|
||||
push_rules::PushRulesEventContent, receipt::ReceiptType,
|
||||
AnySyncEphemeralRoomEvent, GlobalAccountDataEventType,
|
||||
},
|
||||
push,
|
||||
|
|
@ -859,10 +859,12 @@ async fn handle_push_event(
|
|||
.account_data
|
||||
.get_global(userid, &GlobalAccountDataEventType::PushRules)
|
||||
.unwrap_or_default()
|
||||
.and_then(|event| event.deserialize_as::<PushRulesEvent>().ok())
|
||||
.and_then(|event| {
|
||||
event.deserialize_as::<PushRulesEventContent>().ok()
|
||||
})
|
||||
.map_or_else(
|
||||
|| push::Ruleset::server_default(userid),
|
||||
|ev: PushRulesEvent| ev.content.global,
|
||||
|ev| ev.global,
|
||||
);
|
||||
|
||||
let unread: UInt = services()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue