Skip to content

Commit

Permalink
fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
agusaldasoro committed Sep 5, 2023
1 parent 88713db commit 4a1bc17
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 67 deletions.
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 4a1bc17

Please sign in to comment.