mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2026-02-04 15:51:23 +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.")
|
||||
})
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue