From e5735c81edba1b7c8e492c7e5cc878937f83e4f3 Mon Sep 17 00:00:00 2001 From: strawberry Date: Tue, 30 Apr 2024 21:33:02 -0400 Subject: [PATCH] dedupe half of account/room data config.rs code Signed-off-by: strawberry --- src/api/client_server/config.rs | 61 ++++++++++++++++----------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/src/api/client_server/config.rs b/src/api/client_server/config.rs index c67c9311..603c192a 100644 --- a/src/api/client_server/config.rs +++ b/src/api/client_server/config.rs @@ -5,6 +5,7 @@ use ruma::{ }, events::{AnyGlobalAccountDataEventContent, AnyRoomAccountDataEventContent}, serde::Raw, + OwnedUserId, RoomId, }; use serde::Deserialize; 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( body: Ruma, ) -> Result { - 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_type = body.event_type.to_string(); - - services().account_data.update( - None, - sender_user, - event_type.clone().into(), - &json!({ - "type": event_type, - "content": data, - }), - )?; + set_account_data(None, &body.sender_user, &body.event_type.to_string(), body.data.json())?; 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( body: Ruma, ) -> Result { - 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_type = body.event_type.to_string(); - - services().account_data.update( + set_account_data( Some(&body.room_id), - sender_user, - event_type.clone().into(), - &json!({ - "type": event_type, - "content": data, - }), + &body.sender_user, + &body.event_type.to_string(), + body.data.json(), )?; Ok(set_room_account_data::v3::Response {}) @@ -74,7 +50,7 @@ pub(crate) async fn get_global_account_data_route( let event: Box = services() .account_data .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::(event.get()) .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 = services() .account_data .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::(event.get()) .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, 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)] struct ExtractRoomEventContent { content: Raw,