From: Dessalines Date: Wed, 1 Mar 2023 02:27:33 +0000 (-0500) Subject: Fixing broken websockets. (#2770) X-Git-Url: http://these/git/%7B%60/feeds/c/%24%7Bthis.state.communityName%7D.xml?a=commitdiff_plain;h=3844ac76c32c66a39074dc5ad3cb8b9610ef682c;p=lemmy.git Fixing broken websockets. (#2770) * Fixing broken websockets. * Use cloned() for serde::Value --- diff --git a/src/api_routes_websocket.rs b/src/api_routes_websocket.rs index d0309806..fdaba6ba 100644 --- a/src/api_routes_websocket.rs +++ b/src/api_routes_websocket.rs @@ -248,14 +248,18 @@ async fn parse_json_message( context: LemmyContext, ) -> Result { let json: Value = serde_json::from_str(&msg)?; - let data = &json + let data = json .get("data") - .ok_or_else(|| LemmyError::from_message("missing data"))? - .to_string(); - let op = &json + .cloned() + .ok_or_else(|| LemmyError::from_message("missing data"))?; + + let missing_op_err = || LemmyError::from_message("missing op"); + + let op = json .get("op") - .ok_or_else(|| LemmyError::from_message("missing op"))? - .to_string(); + .ok_or_else(missing_op_err)? + .as_str() + .ok_or_else(missing_op_err)?; // check if api call passes the rate limit, and generate future for later execution if let Ok(user_operation_crud) = UserOperationCrud::from_str(op) { @@ -299,7 +303,7 @@ pub async fn match_websocket_operation_crud( context: LemmyContext, id: ConnectionId, op: UserOperationCrud, - data: &str, + data: Value, ) -> result::Result { match op { // User ops @@ -392,13 +396,13 @@ async fn do_websocket_operation_crud<'a, 'b, Data>( context: LemmyContext, id: ConnectionId, op: UserOperationCrud, - data: &str, + data: Value, ) -> result::Result where Data: PerformCrud + SendActivity::Response>, for<'de> Data: Deserialize<'de>, { - let parsed_data: Data = serde_json::from_str(data)?; + let parsed_data: Data = serde_json::from_value(data)?; let res = parsed_data .perform(&web::Data::new(context.clone()), Some(id)) .await?; @@ -410,7 +414,7 @@ pub async fn match_websocket_operation_apub( context: LemmyContext, id: ConnectionId, op: UserOperationApub, - data: &str, + data: Value, ) -> result::Result { match op { UserOperationApub::GetPersonDetails => { @@ -436,13 +440,13 @@ async fn do_websocket_operation_apub<'a, 'b, Data>( context: LemmyContext, id: ConnectionId, op: UserOperationApub, - data: &str, + data: Value, ) -> result::Result where Data: PerformApub + SendActivity::Response>, for<'de> Data: Deserialize<'de>, { - let parsed_data: Data = serde_json::from_str(data)?; + let parsed_data: Data = serde_json::from_value(data)?; let res = parsed_data .perform(&web::Data::new(context.clone()), Some(id)) .await?; @@ -454,7 +458,7 @@ pub async fn match_websocket_operation( context: LemmyContext, id: ConnectionId, op: UserOperation, - data: &str, + data: Value, ) -> result::Result { match op { // User ops @@ -614,13 +618,13 @@ async fn do_websocket_operation<'a, 'b, Data>( context: LemmyContext, id: ConnectionId, op: UserOperation, - data: &str, + data: Value, ) -> result::Result where Data: Perform + SendActivity::Response>, for<'de> Data: Deserialize<'de>, { - let parsed_data: Data = serde_json::from_str(data)?; + let parsed_data: Data = serde_json::from_value(data)?; let res = parsed_data .perform(&web::Data::new(context.clone()), Some(id)) .await?;