From 2a9360681b05a14cb453d3ac90827867d69c9395 Mon Sep 17 00:00:00 2001 From: Agustina Aldasoro Date: Tue, 5 Sep 2023 16:09:37 -0300 Subject: [PATCH] fix: style to match cargo 1.72.0 (#278) * Update README.md Signed-off-by: Agustina Aldasoro * fix style * fix style --------- Signed-off-by: Agustina Aldasoro --- README.md | 8 +- src/components/redis.rs | 2 +- src/ws/service/friendships_service.rs | 177 ++++++++++++++++++-------- src/ws/service/mapper/payload.rs | 24 ++-- 4 files changed, 139 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index cdd40741..a539ce99 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,9 @@ There are two recommended editors/IDEs at the moment: ### Building the server -The main of this project will run an HTTP Server and a Websocket Server. +This project will run an HTTP Server and a WebSocket Server. -The Websocket server implements the protocol definition defined in https://github.com/decentraland/protocol/blob/main/proto/decentraland/social/friendships/friendships.proto which is automatically downloaded from GitHub during the build time. If a build fails, it could be related to that. +The WebSocket server implements the protocol definition defined in https://github.com/decentraland/protocol/blob/main/proto/decentraland/social/friendships/friendships.proto which is automatically downloaded from GitHub during the build time. If a build fails, it could be related to that. ### Requirements @@ -80,7 +80,7 @@ make test ### Database & Migrations -Migrations or pending migrations run when the server starts up programatically with the [sqlx](https://github.com/launchbadge/sqlx) API. +Migrations or pending migrations run when the server starts programmatically using the [sqlx](https://github.com/launchbadge/sqlx) API. In order to create a new migration, you have to run: @@ -90,7 +90,7 @@ make migration name={YOUR_MIGRATION_NAME} This command will create the migration SQL files (up and down) with the given name -#### Enter in the db +#### Enter in the DB ``` docker exec -ti social_service_db psql -U postgres -d social_service diff --git a/src/components/redis.rs b/src/components/redis.rs index c5fcbf62..9028e9c9 100644 --- a/src/components/redis.rs +++ b/src/components/redis.rs @@ -59,7 +59,7 @@ impl Redis { None => false, Some(mut conn) => { let result: RedisResult = cmd("PING").query_async(&mut conn).await; - matches!(result, Ok(_)) + result.is_ok() } } } diff --git a/src/ws/service/friendships_service.rs b/src/ws/service/friendships_service.rs index ca121d5c..2151dc48 100644 --- a/src/ws/service/friendships_service.rs +++ b/src/ws/service/friendships_service.rs @@ -87,13 +87,21 @@ impl FriendshipsServiceServer for MyF let Some(repos) = context.server_context.db.db_repos.clone() else { log::error!("[RPC] Get friends > Db repositories > `repos` is None."); - let error = InternalServerError{ message: "An error occurred while getting the friendships".to_owned() }; - metrics.record_procedure_call_and_duration_and_out_size( Some(error.clone().into()), Procedure::GetFriends, start_time, error.encoded_len()); + let error = InternalServerError { + message: "An error occurred while getting the friendships".to_owned(), + }; + metrics.record_procedure_call_and_duration_and_out_size( + Some(error.clone().into()), + Procedure::GetFriends, + start_time, + error.encoded_len(), + ); let result = friendships_yielder - .r#yield(UsersResponse::from_response(users_response::Response::InternalServerError( - error))) - .await; + .r#yield(UsersResponse::from_response( + users_response::Response::InternalServerError(error), + )) + .await; if let Err(err) = result { log::error!("[RPC] There was an error yielding the error to the friendships generator: {:?}", err); }; @@ -124,22 +132,32 @@ impl FriendshipsServiceServer for MyF let Ok(mut friendship) = repos .friendships .get_user_friends_stream(&user_id.social_id, true) - .await else { - log::error!( + .await + else { + log::error!( "[RPC] Get friends > Get user friends stream > Error: There was an error accessing to the friendships repository." ); - let error = InternalServerError{ message: "An error occurred while sending the response to the stream".to_owned() }; - metrics.record_procedure_call_and_duration_and_out_size(Some(error.clone().into()), Procedure::GetFriends, start_time, error.encoded_len()); + let error = InternalServerError { + message: "An error occurred while sending the response to the stream" + .to_owned(), + }; + metrics.record_procedure_call_and_duration_and_out_size( + Some(error.clone().into()), + Procedure::GetFriends, + start_time, + error.encoded_len(), + ); - let result = friendships_yielder - .r#yield(UsersResponse::from_response(users_response::Response::InternalServerError( - error))) - .await; - if let Err(err) = result { - log::error!("[RPC] There was an error yielding the error to the friendships generator: {:?}", err); - }; - return Ok(friendships_generator); + let result = friendships_yielder + .r#yield(UsersResponse::from_response( + users_response::Response::InternalServerError(error), + )) + .await; + if let Err(err) = result { + log::error!("[RPC] There was an error yielding the error to the friendships generator: {:?}", err); }; + return Ok(friendships_generator); + }; let metrics_clone = metrics.clone(); tokio::spawn(async move { let mut users = Users::default(); @@ -210,13 +228,21 @@ impl FriendshipsServiceServer for MyF let (friendships_generator, friendships_yielder) = Generator::create(); let Some(other_user) = request.user.clone() else { - let error = BadRequestError{ message: "`user` was not provided".to_owned() }; - metrics.record_procedure_call_and_duration_and_out_size(Some(error.clone().into()), Procedure::GetMutualFriends, start_time, error.encoded_len()); + let error = BadRequestError { + message: "`user` was not provided".to_owned(), + }; + metrics.record_procedure_call_and_duration_and_out_size( + Some(error.clone().into()), + Procedure::GetMutualFriends, + start_time, + error.encoded_len(), + ); let result = friendships_yielder - .r#yield(UsersResponse::from_response(users_response::Response::BadRequestError( - error))) - .await; + .r#yield(UsersResponse::from_response( + users_response::Response::BadRequestError(error), + )) + .await; if let Err(err) = result { log::error!("[RPC] There was an error yielding the error to the mutual friendships generator: {:?}", err); }; @@ -224,13 +250,21 @@ impl FriendshipsServiceServer for MyF }; let Some(auth_token) = request.clone().auth_token.take() else { - let error = UnauthorizedError{ message: "`auth_token` was not provided".to_owned() }; - metrics.record_procedure_call_and_duration_and_out_size(Some(error.clone().into()), Procedure::GetMutualFriends, start_time, error.encoded_len()); + let error = UnauthorizedError { + message: "`auth_token` was not provided".to_owned(), + }; + metrics.record_procedure_call_and_duration_and_out_size( + Some(error.clone().into()), + Procedure::GetMutualFriends, + start_time, + error.encoded_len(), + ); let result = friendships_yielder - .r#yield(UsersResponse::from_response(users_response::Response::UnauthorizedError( - error))) - .await; + .r#yield(UsersResponse::from_response( + users_response::Response::UnauthorizedError(error), + )) + .await; if let Err(err) = result { log::error!("[RPC] There was an error yielding the error to the mutual friendships generator: {:?}", err); }; @@ -246,13 +280,21 @@ impl FriendshipsServiceServer for MyF let Some(repos) = context.server_context.db.db_repos.clone() else { log::error!("[RPC] Get mutual friends > Db repositories > `repos` is None."); - let error = InternalServerError{ message: "An error occurred while getting the mutual friendships".to_owned() }; - metrics.record_procedure_call_and_duration_and_out_size( Some(error.clone().into()), Procedure::GetMutualFriends, start_time, error.encoded_len()); + let error = InternalServerError { + message: "An error occurred while getting the mutual friendships".to_owned(), + }; + metrics.record_procedure_call_and_duration_and_out_size( + Some(error.clone().into()), + Procedure::GetMutualFriends, + start_time, + error.encoded_len(), + ); let result = friendships_yielder - .r#yield(UsersResponse::from_response(users_response::Response::InternalServerError( - error))) - .await; + .r#yield(UsersResponse::from_response( + users_response::Response::InternalServerError(error), + )) + .await; if let Err(err) = result { log::error!("[RPC] There was an error yielding the error to the mutual friendships generator: {:?}", err); }; @@ -288,23 +330,36 @@ impl FriendshipsServiceServer for MyF let Ok(mut friendship) = repos .friendships .clone() - .get_mutual_friends_stream(user_id.social_id.clone().to_string(), other_user.address.clone().to_string()) - .await else { - log::error!( + .get_mutual_friends_stream( + user_id.social_id.clone().to_string(), + other_user.address.clone().to_string(), + ) + .await + else { + log::error!( "[RPC] Get mutual friends > Get user friends stream > Error: There was an error accessing to the friendships repository." ); - let error = InternalServerError{ message: "An error occurred while sending the response to the stream".to_owned() }; - metrics.record_procedure_call_and_duration_and_out_size(Some(error.clone().into()), Procedure::GetMutualFriends, start_time, error.encoded_len()); + let error = InternalServerError { + message: "An error occurred while sending the response to the stream" + .to_owned(), + }; + metrics.record_procedure_call_and_duration_and_out_size( + Some(error.clone().into()), + Procedure::GetMutualFriends, + start_time, + error.encoded_len(), + ); - let result = friendships_yielder - .r#yield(UsersResponse::from_response(users_response::Response::InternalServerError( - error))) - .await; - if let Err(err) = result { - log::error!("[RPC] There was an error yielding the error to the mutual friendships generator: {:?}", err); - }; - return Ok(friendships_generator); + let result = friendships_yielder + .r#yield(UsersResponse::from_response( + users_response::Response::InternalServerError(error), + )) + .await; + if let Err(err) = result { + log::error!("[RPC] There was an error yielding the error to the mutual friendships generator: {:?}", err); }; + return Ok(friendships_generator); + }; let metrics_clone = metrics.clone(); tokio::spawn(async move { let mut users: Users = Users::default(); @@ -400,11 +455,19 @@ impl FriendshipsServiceServer for MyF let Some(repos) = context.server_context.db.db_repos.clone() else { log::error!("[RPC] Get request events > Db repositories > `repos` is None."); - let error = InternalServerError { message: "".to_owned() }; - metrics.record_procedure_call_and_duration_and_out_size(Some(error.clone().into()), Procedure::GetRequestEvents, start_time, error.encoded_len()); + let error = InternalServerError { + message: "".to_owned(), + }; + metrics.record_procedure_call_and_duration_and_out_size( + Some(error.clone().into()), + Procedure::GetRequestEvents, + start_time, + error.encoded_len(), + ); return Ok(RequestEventsResponse::from_response( - request_events_response::Response::InternalServerError(error))); + request_events_response::Response::InternalServerError(error), + )); }; let requests = repos @@ -460,15 +523,19 @@ impl FriendshipsServiceServer for MyF metrics.record_in_procedure_call_size(Procedure::UpdateFriendshipEvent, &request); let Some(auth_token) = request.clone().auth_token.take() else { - let error = UnauthorizedError{ message: "`auth_token` was not provided".to_owned() }; - metrics.record_procedure_call_and_duration_and_out_size(Some(error.clone().into()), Procedure::UpdateFriendshipEvent, start_time, error.encoded_len()); + let error = UnauthorizedError { + message: "`auth_token` was not provided".to_owned(), + }; + metrics.record_procedure_call_and_duration_and_out_size( + Some(error.clone().into()), + Procedure::UpdateFriendshipEvent, + start_time, + error.encoded_len(), + ); return Ok(UpdateFriendshipResponse::from_response( - update_friendship_response::Response::UnauthorizedError( - error - ) - ) - ); + update_friendship_response::Response::UnauthorizedError(error), + )); }; let request_user_id = get_user_id_from_request( diff --git a/src/ws/service/mapper/payload.rs b/src/ws/service/mapper/payload.rs index c2f1d56b..682671f0 100644 --- a/src/ws/service/mapper/payload.rs +++ b/src/ws/service/mapper/payload.rs @@ -1,18 +1,18 @@ use crate::{domain::error::CommonError, friendships::UpdateFriendshipPayload}; pub fn get_synapse_token(request: UpdateFriendshipPayload) -> Result { - let Some(auth_token) = request - .auth_token - .as_ref() else { - log::error!("[RPC] Handle friendship update > `auth_token` is missing."); - return Err(CommonError::Unauthorized("`auth_token` is missing".to_owned())); - }; + let Some(auth_token) = request.auth_token.as_ref() else { + log::error!("[RPC] Handle friendship update > `auth_token` is missing."); + return Err(CommonError::Unauthorized( + "`auth_token` is missing".to_owned(), + )); + }; - let Some(synapse_token) = auth_token - .synapse_token - .as_ref() else { - log::error!("[RPC] Handle friendship update > `synapse_token` is missing."); - return Err(CommonError::Unauthorized("`synapse_token` is missing".to_owned())) - }; + let Some(synapse_token) = auth_token.synapse_token.as_ref() else { + log::error!("[RPC] Handle friendship update > `synapse_token` is missing."); + return Err(CommonError::Unauthorized( + "`synapse_token` is missing".to_owned(), + )); + }; Ok(synapse_token.to_string()) }