dedupe half of account/room data config.rs code
Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
parent
b17ccdadd2
commit
e5735c81ed
|
@ -5,6 +5,7 @@ use ruma::{
|
||||||
},
|
},
|
||||||
events::{AnyGlobalAccountDataEventContent, AnyRoomAccountDataEventContent},
|
events::{AnyGlobalAccountDataEventContent, AnyRoomAccountDataEventContent},
|
||||||
serde::Raw,
|
serde::Raw,
|
||||||
|
OwnedUserId, RoomId,
|
||||||
};
|
};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::{json, value::RawValue as RawJsonValue};
|
use serde_json::{json, value::RawValue as RawJsonValue};
|
||||||
|
@ -17,22 +18,7 @@ use crate::{services, Error, Result, Ruma};
|
||||||
pub(crate) async fn set_global_account_data_route(
|
pub(crate) async fn set_global_account_data_route(
|
||||||
body: Ruma<set_global_account_data::v3::Request>,
|
body: Ruma<set_global_account_data::v3::Request>,
|
||||||
) -> Result<set_global_account_data::v3::Response> {
|
) -> Result<set_global_account_data::v3::Response> {
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
set_account_data(None, &body.sender_user, &body.event_type.to_string(), body.data.json())?;
|
||||||
|
|
||||||
let data: serde_json::Value = serde_json::from_str(body.data.json().get())
|
|
||||||
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;
|
|
||||||
|
|
||||||
let event_type = body.event_type.to_string();
|
|
||||||
|
|
||||||
services().account_data.update(
|
|
||||||
None,
|
|
||||||
sender_user,
|
|
||||||
event_type.clone().into(),
|
|
||||||
&json!({
|
|
||||||
"type": event_type,
|
|
||||||
"content": data,
|
|
||||||
}),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(set_global_account_data::v3::Response {})
|
Ok(set_global_account_data::v3::Response {})
|
||||||
}
|
}
|
||||||
|
@ -43,21 +29,11 @@ pub(crate) async fn set_global_account_data_route(
|
||||||
pub(crate) async fn set_room_account_data_route(
|
pub(crate) async fn set_room_account_data_route(
|
||||||
body: Ruma<set_room_account_data::v3::Request>,
|
body: Ruma<set_room_account_data::v3::Request>,
|
||||||
) -> Result<set_room_account_data::v3::Response> {
|
) -> Result<set_room_account_data::v3::Response> {
|
||||||
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
set_account_data(
|
||||||
|
|
||||||
let data: serde_json::Value = serde_json::from_str(body.data.json().get())
|
|
||||||
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;
|
|
||||||
|
|
||||||
let event_type = body.event_type.to_string();
|
|
||||||
|
|
||||||
services().account_data.update(
|
|
||||||
Some(&body.room_id),
|
Some(&body.room_id),
|
||||||
sender_user,
|
&body.sender_user,
|
||||||
event_type.clone().into(),
|
&body.event_type.to_string(),
|
||||||
&json!({
|
body.data.json(),
|
||||||
"type": event_type,
|
|
||||||
"content": data,
|
|
||||||
}),
|
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(set_room_account_data::v3::Response {})
|
Ok(set_room_account_data::v3::Response {})
|
||||||
|
@ -74,7 +50,7 @@ pub(crate) async fn get_global_account_data_route(
|
||||||
let event: Box<RawJsonValue> = services()
|
let event: Box<RawJsonValue> = services()
|
||||||
.account_data
|
.account_data
|
||||||
.get(None, sender_user, body.event_type.to_string().into())?
|
.get(None, sender_user, body.event_type.to_string().into())?
|
||||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
.ok_or_else(|| Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||||
|
|
||||||
let account_data = serde_json::from_str::<ExtractGlobalEventContent>(event.get())
|
let account_data = serde_json::from_str::<ExtractGlobalEventContent>(event.get())
|
||||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||||
|
@ -96,7 +72,7 @@ pub(crate) async fn get_room_account_data_route(
|
||||||
let event: Box<RawJsonValue> = services()
|
let event: Box<RawJsonValue> = services()
|
||||||
.account_data
|
.account_data
|
||||||
.get(Some(&body.room_id), sender_user, body.event_type.clone())?
|
.get(Some(&body.room_id), sender_user, body.event_type.clone())?
|
||||||
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
.ok_or_else(|| Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
|
||||||
|
|
||||||
let account_data = serde_json::from_str::<ExtractRoomEventContent>(event.get())
|
let account_data = serde_json::from_str::<ExtractRoomEventContent>(event.get())
|
||||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||||
|
@ -107,6 +83,27 @@ pub(crate) async fn get_room_account_data_route(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_account_data(
|
||||||
|
room_id: Option<&RoomId>, sender_user: &Option<OwnedUserId>, event_type: &str, data: &RawJsonValue,
|
||||||
|
) -> Result<()> {
|
||||||
|
let sender_user = sender_user.as_ref().expect("user is authenticated");
|
||||||
|
|
||||||
|
let data: serde_json::Value =
|
||||||
|
serde_json::from_str(data.get()).map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?;
|
||||||
|
|
||||||
|
services().account_data.update(
|
||||||
|
room_id,
|
||||||
|
sender_user,
|
||||||
|
event_type.into(),
|
||||||
|
&json!({
|
||||||
|
"type": event_type,
|
||||||
|
"content": data,
|
||||||
|
}),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct ExtractRoomEventContent {
|
struct ExtractRoomEventContent {
|
||||||
content: Raw<AnyRoomAccountDataEventContent>,
|
content: Raw<AnyRoomAccountDataEventContent>,
|
||||||
|
|
Loading…
Reference in New Issue