Skip to content

Commit

Permalink
fix: style to match cargo 1.72.0 (#278)
Browse files Browse the repository at this point in the history
* Update README.md

Signed-off-by: Agustina Aldasoro <[email protected]>

* fix style

* fix style

---------

Signed-off-by: Agustina Aldasoro <[email protected]>
  • Loading branch information
agusaldasoro authored Sep 5, 2023
1 parent 29c9625 commit 2a93606
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 72 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/components/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Redis {
None => false,
Some(mut conn) => {
let result: RedisResult<String> = cmd("PING").query_async(&mut conn).await;
matches!(result, Ok(_))
result.is_ok()
}
}
}
Expand Down
177 changes: 122 additions & 55 deletions src/ws/service/friendships_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,21 @@ impl FriendshipsServiceServer<SocialContext, RPCFriendshipsServiceError> 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);
};
Expand Down Expand Up @@ -124,22 +132,32 @@ impl FriendshipsServiceServer<SocialContext, RPCFriendshipsServiceError> 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();
Expand Down Expand Up @@ -210,27 +228,43 @@ impl FriendshipsServiceServer<SocialContext, RPCFriendshipsServiceError> 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);
};
return Ok(friendships_generator);
};

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);
};
Expand All @@ -246,13 +280,21 @@ impl FriendshipsServiceServer<SocialContext, RPCFriendshipsServiceError> 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);
};
Expand Down Expand Up @@ -288,23 +330,36 @@ impl FriendshipsServiceServer<SocialContext, RPCFriendshipsServiceError> 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();
Expand Down Expand Up @@ -400,11 +455,19 @@ impl FriendshipsServiceServer<SocialContext, RPCFriendshipsServiceError> 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
Expand Down Expand Up @@ -460,15 +523,19 @@ impl FriendshipsServiceServer<SocialContext, RPCFriendshipsServiceError> 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(
Expand Down
24 changes: 12 additions & 12 deletions src/ws/service/mapper/payload.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::{domain::error::CommonError, friendships::UpdateFriendshipPayload};

pub fn get_synapse_token(request: UpdateFriendshipPayload) -> Result<String, CommonError> {
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())
}

0 comments on commit 2a93606

Please sign in to comment.