Fixing broken websockets. (#2770)

* Fixing broken websockets.

* Use cloned() for serde::Value
add_deadpool_timeouts
Dessalines 2023-02-28 21:27:33 -05:00 committed by GitHub
parent f0e1627824
commit 3844ac76c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 15 deletions

View File

@ -248,14 +248,18 @@ async fn parse_json_message(
context: LemmyContext, context: LemmyContext,
) -> Result<String, LemmyError> { ) -> Result<String, LemmyError> {
let json: Value = serde_json::from_str(&msg)?; let json: Value = serde_json::from_str(&msg)?;
let data = &json let data = json
.get("data") .get("data")
.ok_or_else(|| LemmyError::from_message("missing data"))? .cloned()
.to_string(); .ok_or_else(|| LemmyError::from_message("missing data"))?;
let op = &json
let missing_op_err = || LemmyError::from_message("missing op");
let op = json
.get("op") .get("op")
.ok_or_else(|| LemmyError::from_message("missing op"))? .ok_or_else(missing_op_err)?
.to_string(); .as_str()
.ok_or_else(missing_op_err)?;
// check if api call passes the rate limit, and generate future for later execution // check if api call passes the rate limit, and generate future for later execution
if let Ok(user_operation_crud) = UserOperationCrud::from_str(op) { if let Ok(user_operation_crud) = UserOperationCrud::from_str(op) {
@ -299,7 +303,7 @@ pub async fn match_websocket_operation_crud(
context: LemmyContext, context: LemmyContext,
id: ConnectionId, id: ConnectionId,
op: UserOperationCrud, op: UserOperationCrud,
data: &str, data: Value,
) -> result::Result<String, LemmyError> { ) -> result::Result<String, LemmyError> {
match op { match op {
// User ops // User ops
@ -392,13 +396,13 @@ async fn do_websocket_operation_crud<'a, 'b, Data>(
context: LemmyContext, context: LemmyContext,
id: ConnectionId, id: ConnectionId,
op: UserOperationCrud, op: UserOperationCrud,
data: &str, data: Value,
) -> result::Result<String, LemmyError> ) -> result::Result<String, LemmyError>
where where
Data: PerformCrud + SendActivity<Response = <Data as PerformCrud>::Response>, Data: PerformCrud + SendActivity<Response = <Data as PerformCrud>::Response>,
for<'de> Data: Deserialize<'de>, 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 let res = parsed_data
.perform(&web::Data::new(context.clone()), Some(id)) .perform(&web::Data::new(context.clone()), Some(id))
.await?; .await?;
@ -410,7 +414,7 @@ pub async fn match_websocket_operation_apub(
context: LemmyContext, context: LemmyContext,
id: ConnectionId, id: ConnectionId,
op: UserOperationApub, op: UserOperationApub,
data: &str, data: Value,
) -> result::Result<String, LemmyError> { ) -> result::Result<String, LemmyError> {
match op { match op {
UserOperationApub::GetPersonDetails => { UserOperationApub::GetPersonDetails => {
@ -436,13 +440,13 @@ async fn do_websocket_operation_apub<'a, 'b, Data>(
context: LemmyContext, context: LemmyContext,
id: ConnectionId, id: ConnectionId,
op: UserOperationApub, op: UserOperationApub,
data: &str, data: Value,
) -> result::Result<String, LemmyError> ) -> result::Result<String, LemmyError>
where where
Data: PerformApub + SendActivity<Response = <Data as PerformApub>::Response>, Data: PerformApub + SendActivity<Response = <Data as PerformApub>::Response>,
for<'de> Data: Deserialize<'de>, 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 let res = parsed_data
.perform(&web::Data::new(context.clone()), Some(id)) .perform(&web::Data::new(context.clone()), Some(id))
.await?; .await?;
@ -454,7 +458,7 @@ pub async fn match_websocket_operation(
context: LemmyContext, context: LemmyContext,
id: ConnectionId, id: ConnectionId,
op: UserOperation, op: UserOperation,
data: &str, data: Value,
) -> result::Result<String, LemmyError> { ) -> result::Result<String, LemmyError> {
match op { match op {
// User ops // User ops
@ -614,13 +618,13 @@ async fn do_websocket_operation<'a, 'b, Data>(
context: LemmyContext, context: LemmyContext,
id: ConnectionId, id: ConnectionId,
op: UserOperation, op: UserOperation,
data: &str, data: Value,
) -> result::Result<String, LemmyError> ) -> result::Result<String, LemmyError>
where where
Data: Perform + SendActivity<Response = <Data as Perform>::Response>, Data: Perform + SendActivity<Response = <Data as Perform>::Response>,
for<'de> Data: Deserialize<'de>, 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 let res = parsed_data
.perform(&web::Data::new(context.clone()), Some(id)) .perform(&web::Data::new(context.clone()), Some(id))
.await?; .await?;