mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-18 16:21:24 +01:00
separate account_data service methods for room vs global events
Previously we were mashing everything together as RoomAccountDataEvent, even the global events. This technically worked, because of the hidden custom fields on the ruma event types, but it's confusing and easy to mess up. Separate methods with appropriate types are preferable.
This commit is contained in:
parent
6897f0ba34
commit
66210bc32d
16 changed files with 349 additions and 361 deletions
|
|
@ -7,12 +7,13 @@ use ruma::{
|
|||
error::ErrorKind,
|
||||
},
|
||||
events::{
|
||||
AnyGlobalAccountDataEventContent, AnyRoomAccountDataEventContent,
|
||||
AnyGlobalAccountDataEvent, AnyGlobalAccountDataEventContent,
|
||||
AnyRoomAccountDataEvent, AnyRoomAccountDataEventContent,
|
||||
},
|
||||
serde::Raw,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{json, value::RawValue as RawJsonValue};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::{services, Ar, Error, Ra, Result};
|
||||
|
||||
|
|
@ -24,21 +25,17 @@ 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 data: serde_json::Value = serde_json::from_str(body.data.json().get())
|
||||
.map_err(|_| {
|
||||
Error::BadRequest(ErrorKind::BadJson, "Data is invalid.")
|
||||
})?;
|
||||
let event = Raw::new(&json!({
|
||||
"type": &body.event_type,
|
||||
"content": &body.data,
|
||||
}))
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?
|
||||
.cast::<AnyGlobalAccountDataEvent>();
|
||||
|
||||
let event_type = body.event_type.to_string();
|
||||
|
||||
services().account_data.update(
|
||||
None,
|
||||
services().account_data.update_global(
|
||||
sender_user,
|
||||
event_type.clone().into(),
|
||||
&json!({
|
||||
"type": event_type,
|
||||
"content": data,
|
||||
}),
|
||||
&body.event_type,
|
||||
&event,
|
||||
)?;
|
||||
|
||||
Ok(Ra(set_global_account_data::v3::Response {}))
|
||||
|
|
@ -52,21 +49,18 @@ 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 data: serde_json::Value = serde_json::from_str(body.data.json().get())
|
||||
.map_err(|_| {
|
||||
Error::BadRequest(ErrorKind::BadJson, "Data is invalid.")
|
||||
})?;
|
||||
let event = Raw::new(&json!({
|
||||
"type": &body.event_type,
|
||||
"content": &body.data,
|
||||
}))
|
||||
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?
|
||||
.cast::<AnyRoomAccountDataEvent>();
|
||||
|
||||
let event_type = body.event_type.to_string();
|
||||
|
||||
services().account_data.update(
|
||||
Some(&body.room_id),
|
||||
services().account_data.update_room(
|
||||
&body.room_id,
|
||||
sender_user,
|
||||
event_type.clone().into(),
|
||||
&json!({
|
||||
"type": event_type,
|
||||
"content": data,
|
||||
}),
|
||||
&body.event_type,
|
||||
&event,
|
||||
)?;
|
||||
|
||||
Ok(Ra(set_room_account_data::v3::Response {}))
|
||||
|
|
@ -80,17 +74,15 @@ 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: Box<RawJsonValue> = services()
|
||||
let event = services()
|
||||
.account_data
|
||||
.get(None, sender_user, body.event_type.to_string().into())?
|
||||
.get_global(sender_user, &body.event_type)?
|
||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||
|
||||
let account_data =
|
||||
serde_json::from_str::<ExtractGlobalEventContent>(event.get())
|
||||
.map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?
|
||||
.content;
|
||||
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,
|
||||
|
|
@ -105,17 +97,15 @@ 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: Box<RawJsonValue> = services()
|
||||
let event = services()
|
||||
.account_data
|
||||
.get(Some(&body.room_id), sender_user, body.event_type.clone())?
|
||||
.get_room(&body.room_id, sender_user, &body.event_type)?
|
||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||
|
||||
let account_data =
|
||||
serde_json::from_str::<ExtractRoomEventContent>(event.get())
|
||||
.map_err(|_| {
|
||||
Error::bad_database("Invalid account data event in db.")
|
||||
})?
|
||||
.content;
|
||||
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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue