diff --git a/src/matrix-rocketchat/api/matrix/mod.rs b/src/matrix-rocketchat/api/matrix/mod.rs index 25b7899..bc64eae 100644 --- a/src/matrix-rocketchat/api/matrix/mod.rs +++ b/src/matrix-rocketchat/api/matrix/mod.rs @@ -103,7 +103,7 @@ impl MatrixApi { let params = HashMap::new(); debug!(logger, "Querying homeserver {} for API versions", url); - let (body, status_code) = RestApi::call_matrix(&GetSupportedVersionsEndpoint::method(), &url, "", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(GetSupportedVersionsEndpoint::method(), &url, "", ¶ms)?; if !status_code.is_success() { let matrix_error_resp: MatrixErrorResponse = serde_json::from_str(&body).chain_err(|| { ErrorKind::InvalidJSON(format!( diff --git a/src/matrix-rocketchat/api/matrix/r0.rs b/src/matrix-rocketchat/api/matrix/r0.rs index 6df5fb9..6fd8410 100644 --- a/src/matrix-rocketchat/api/matrix/r0.rs +++ b/src/matrix-rocketchat/api/matrix/r0.rs @@ -83,7 +83,7 @@ impl super::MatrixApi for MatrixApi { let mut params = self.params_hash(); params.insert("user_id", &user_id); - let (body, status_code) = RestApi::call_matrix(&CreateRoomEndpoint::method(), &endpoint, payload, ¶ms)?; + let (body, status_code) = RestApi::call_matrix(CreateRoomEndpoint::method(), &endpoint, payload, ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -103,7 +103,7 @@ impl super::MatrixApi for MatrixApi { let endpoint = self.base_url.clone() + &format!("/_matrix/client/r0/directory/room/{}", &encoded_room_alias); let params = self.params_hash(); - let (body, status_code) = RestApi::call_matrix(&DeleteAliasEndpoint::method(), &endpoint, "{}", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(DeleteAliasEndpoint::method(), &endpoint, "{}", ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -118,7 +118,7 @@ impl super::MatrixApi for MatrixApi { let mut params = self.params_hash(); params.insert("user_id", &user_id); - let (body, status_code) = RestApi::call_matrix(&ForgetRoomEndpoint::method(), &endpoint, "{}", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(ForgetRoomEndpoint::method(), &endpoint, "{}", ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -130,7 +130,7 @@ impl super::MatrixApi for MatrixApi { let endpoint = self.base_url.clone() + &GetContentEndpoint::request_path(path_params); let params = self.params_hash(); - let mut resp = RestApi::get_matrix_file(&GetContentEndpoint::method(), &endpoint, "", ¶ms)?; + let mut resp = RestApi::get_matrix_file(GetContentEndpoint::method(), &endpoint, "", ¶ms)?; if !resp.status().is_success() { let mut body = String::new(); resp.read_to_string(&mut body).chain_err(|| ErrorKind::ApiCallFailed(endpoint.clone()))?; @@ -148,7 +148,7 @@ impl super::MatrixApi for MatrixApi { let endpoint = self.base_url.clone() + &GetDisplayNameEndpoint::request_path(path_params); let params = self.params_hash(); - let (body, status_code) = RestApi::call_matrix(&GetDisplayNameEndpoint::method(), &endpoint, "", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(GetDisplayNameEndpoint::method(), &endpoint, "", ¶ms)?; if status_code == StatusCode::NOT_FOUND { return Ok(None); } @@ -173,7 +173,7 @@ impl super::MatrixApi for MatrixApi { let mut params = self.params_hash(); params.insert("user_id", &user_id); - let (body, status_code) = RestApi::call_matrix(&SyncEventsEndpoint::method(), &endpoint, "", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(SyncEventsEndpoint::method(), &endpoint, "", ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); @@ -196,7 +196,7 @@ impl super::MatrixApi for MatrixApi { let endpoint = self.base_url.clone() + &format!("/_matrix/client/r0/directory/room/{}", &encoded_room_alias); let params = self.params_hash(); - let (body, status_code) = RestApi::call_matrix(&GetAliasEndpoint::method(), &endpoint, "{}", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(GetAliasEndpoint::method(), &endpoint, "{}", ¶ms)?; if status_code == StatusCode::NOT_FOUND { return Ok(None); } @@ -219,7 +219,7 @@ impl super::MatrixApi for MatrixApi { let mut params = self.params_hash(); params.insert("user_id", &user_id); - let (body, status_code) = RestApi::call_matrix(&GetStateEvents::method(), &endpoint, "", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(GetStateEvents::method(), &endpoint, "", ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); @@ -248,7 +248,7 @@ impl super::MatrixApi for MatrixApi { let endpoint = self.base_url.clone() + &GetStateEventsForEmptyKeyEndpoint::request_path(path_params); let params = self.params_hash(); - let (body, status_code) = RestApi::call_matrix(&GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?; if status_code == StatusCode::NOT_FOUND { return Ok(None); } @@ -278,7 +278,7 @@ impl super::MatrixApi for MatrixApi { let endpoint = self.base_url.clone() + &GetStateEventsForEmptyKeyEndpoint::request_path(path_params); let params = self.params_hash(); - let (body, status_code) = RestApi::call_matrix(&GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -305,7 +305,7 @@ impl super::MatrixApi for MatrixApi { params.insert("user_id", &user_id); } - let (body, status_code) = RestApi::call_matrix(&GetMemberEventsEndpoint::method(), &endpoint, "{}", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(GetMemberEventsEndpoint::method(), &endpoint, "{}", ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -323,7 +323,7 @@ impl super::MatrixApi for MatrixApi { let endpoint = self.base_url.clone() + &GetStateEventsForEmptyKeyEndpoint::request_path(path_params); let params = self.params_hash(); - let (body, status_code) = RestApi::call_matrix(&GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?; if status_code == StatusCode::NOT_FOUND { return Ok(None); } @@ -351,7 +351,7 @@ impl super::MatrixApi for MatrixApi { let body_params = invite_user::BodyParams { user_id: receiver_user_id.clone() }; let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("invite"))?; - let (body, status_code) = RestApi::call_matrix(&InviteUserEndpoint::method(), &endpoint, payload, ¶ms)?; + let (body, status_code) = RestApi::call_matrix(InviteUserEndpoint::method(), &endpoint, payload, ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -365,7 +365,7 @@ impl super::MatrixApi for MatrixApi { let endpoint = self.base_url.clone() + &GetStateEventsForEmptyKeyEndpoint::request_path(path_params); let params = self.params_hash(); - let (_, status_code) = RestApi::call_matrix(&GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?; + let (_, status_code) = RestApi::call_matrix(GetStateEventsForEmptyKeyEndpoint::method(), &endpoint, "{}", ¶ms)?; Ok(status_code != StatusCode::FORBIDDEN) } @@ -377,7 +377,7 @@ impl super::MatrixApi for MatrixApi { let mut params = self.params_hash(); params.insert("user_id", &user_id); - let (body, status_code) = RestApi::call_matrix(&JoinRoomByIdEndpoint::method(), &endpoint, "{}", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(JoinRoomByIdEndpoint::method(), &endpoint, "{}", ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -393,7 +393,7 @@ impl super::MatrixApi for MatrixApi { let mut params = self.params_hash(); params.insert("user_id", &user_id); - let (body, status_code) = RestApi::call_matrix(&LeaveRoomEndpoint::method(), &endpoint, "{}", ¶ms)?; + let (body, status_code) = RestApi::call_matrix(LeaveRoomEndpoint::method(), &endpoint, "{}", ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -414,7 +414,7 @@ impl super::MatrixApi for MatrixApi { let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("canonical room alias"))?; let (body, status_code) = - RestApi::call_matrix(&SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?; + RestApi::call_matrix(SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -434,7 +434,7 @@ impl super::MatrixApi for MatrixApi { }; let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("account"))?; - let (body, status_code) = RestApi::call_matrix(&RegisterEndpoint::method(), &endpoint, payload, ¶ms)?; + let (body, status_code) = RestApi::call_matrix(RegisterEndpoint::method(), &endpoint, payload, ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -460,7 +460,7 @@ impl super::MatrixApi for MatrixApi { let mut params = self.params_hash(); params.insert("user_id", &user_id); - let (body, status_code) = RestApi::call_matrix(&SendMessageEventEndpoint::method(), &endpoint, payload, ¶ms)?; + let (body, status_code) = RestApi::call_matrix(SendMessageEventEndpoint::method(), &endpoint, payload, ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); @@ -487,7 +487,7 @@ impl super::MatrixApi for MatrixApi { let mut params = self.params_hash(); params.insert("user_id", &user_id); - let (body, status_code) = RestApi::call_matrix(&SendMessageEventEndpoint::method(), &endpoint, payload, ¶ms)?; + let (body, status_code) = RestApi::call_matrix(SendMessageEventEndpoint::method(), &endpoint, payload, ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); @@ -515,7 +515,7 @@ impl super::MatrixApi for MatrixApi { let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("power levels"))?; let (body, status_code) = - RestApi::call_matrix(&SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?; + RestApi::call_matrix(SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -531,7 +531,7 @@ impl super::MatrixApi for MatrixApi { let body_params = set_display_name::BodyParams { displayname: Some(name) }; let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("set display name"))?; - let (body, status_code) = RestApi::call_matrix(&SetDisplayNameEndpoint::method(), &endpoint, payload, ¶ms)?; + let (body, status_code) = RestApi::call_matrix(SetDisplayNameEndpoint::method(), &endpoint, payload, ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -547,7 +547,7 @@ impl super::MatrixApi for MatrixApi { let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("room name"))?; let (body, status_code) = - RestApi::call_matrix(&SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?; + RestApi::call_matrix(SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } @@ -563,7 +563,7 @@ impl super::MatrixApi for MatrixApi { let payload = serde_json::to_string(&body_params).chain_err(|| body_params_error!("room topic"))?; let (body, status_code) = - RestApi::call_matrix(&SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?; + RestApi::call_matrix(SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms)?; if !status_code.is_success() { return Err(build_error(&endpoint, &body, &status_code)); } diff --git a/src/matrix-rocketchat/api/rest_api.rs b/src/matrix-rocketchat/api/rest_api.rs index aa05790..d44bb88 100644 --- a/src/matrix-rocketchat/api/rest_api.rs +++ b/src/matrix-rocketchat/api/rest_api.rs @@ -24,12 +24,12 @@ pub struct RestApi {} impl RestApi { /// Call a matrix REST API endpoint pub fn call_matrix<'a, T: Into>( - method: &RumaHttpMethod, + method: RumaHttpMethod, url: &str, payload: T, params: &HashMap<&str, &'a str>, ) -> Result<(String, StatusCode)> { - let method = match *method { + let method = match method { RumaHttpMethod::Delete => Method::DELETE, RumaHttpMethod::Get => Method::GET, RumaHttpMethod::Post => Method::POST, @@ -42,12 +42,12 @@ impl RestApi { /// Get a file that was uploaded to a Matrix homeserver pub fn get_matrix_file<'a, T: Into>( - method: &RumaHttpMethod, + method: RumaHttpMethod, url: &str, payload: T, params: &HashMap<&str, &'a str>, ) -> Result { - let method = match *method { + let method = match method { RumaHttpMethod::Delete => Method::DELETE, RumaHttpMethod::Get => Method::GET, RumaHttpMethod::Post => Method::POST, diff --git a/src/matrix-rocketchat/api/rocketchat/v1.rs b/src/matrix-rocketchat/api/rocketchat/v1.rs index 0606276..4e3a78c 100644 --- a/src/matrix-rocketchat/api/rocketchat/v1.rs +++ b/src/matrix-rocketchat/api/rocketchat/v1.rs @@ -543,7 +543,7 @@ impl super::RocketchatApi for RocketchatApi { if !resp.status().is_success() { let mut body = String::new(); resp.read_to_string(&mut body).chain_err(|| ErrorKind::ApiCallFailed(attachment.title_link.clone()))?; - return Err(build_error(&get_file_endpoint.url(), &body, &resp.status())); + return Err(build_error(&get_file_endpoint.url(), &body, resp.status())); } let mut buffer = Vec::new(); @@ -595,7 +595,7 @@ impl super::RocketchatApi for RocketchatApi { let (body, status_code) = RestApi::call_rocketchat(&channels_list_endpoint)?; if !status_code.is_success() { - return Err(build_error(&channels_list_endpoint.url(), &body, &status_code)); + return Err(build_error(&channels_list_endpoint.url(), &body, status_code)); } let channels_list_response: ChannelsListResponse = serde_json::from_str(&body).chain_err(|| { @@ -648,7 +648,7 @@ impl super::RocketchatApi for RocketchatApi { let (body, status_code) = RestApi::call_rocketchat(&message_endpoint)?; if !status_code.is_success() { - return Err(build_error(&message_endpoint.url(), &body, &status_code)); + return Err(build_error(&message_endpoint.url(), &body, status_code)); } let message_response: MessageResponse = serde_json::from_str(&body).chain_err(|| { @@ -699,7 +699,7 @@ impl super::RocketchatApi for RocketchatApi { let (body, status_code) = RestApi::call_rocketchat(&chat_post_message_endpoint)?; if !status_code.is_success() { - return Err(build_error(&chat_post_message_endpoint.url(), &body, &status_code)); + return Err(build_error(&chat_post_message_endpoint.url(), &body, status_code)); } Ok(()) @@ -718,7 +718,7 @@ impl super::RocketchatApi for RocketchatApi { let (body, status_code) = RestApi::call_rocketchat(&direct_messages_list_endpoint)?; if !status_code.is_success() { - return Err(build_error(&direct_messages_list_endpoint.url(), &body, &status_code)); + return Err(build_error(&direct_messages_list_endpoint.url(), &body, status_code)); } let direct_messages_list_response: DMListResponse = serde_json::from_str(&body).chain_err(|| { @@ -745,7 +745,7 @@ impl super::RocketchatApi for RocketchatApi { let (body, status_code) = RestApi::call_rocketchat(&channels_list_endpoint)?; if !status_code.is_success() { - return Err(build_error(&channels_list_endpoint.url(), &body, &status_code)); + return Err(build_error(&channels_list_endpoint.url(), &body, status_code)); } let channels_list_response: GroupsListResponse = serde_json::from_str(&body).chain_err(|| { @@ -789,7 +789,7 @@ impl super::RocketchatApi for RocketchatApi { let (body, status_code) = RestApi::call_rocketchat(&login_endpoint)?; if !status_code.is_success() { - return Err(build_error(&login_endpoint.url(), &body, &status_code)); + return Err(build_error(&login_endpoint.url(), &body, status_code)); } let login_response: LoginResponse = serde_json::from_str(&body).chain_err(|| { @@ -811,7 +811,7 @@ impl super::RocketchatApi for RocketchatApi { let (body, status_code) = RestApi::call_rocketchat(&me_endpoint)?; if !status_code.is_success() { - return Err(build_error(&me_endpoint.url(), &body, &status_code)); + return Err(build_error(&me_endpoint.url(), &body, status_code)); } let user: User = serde_json::from_str(&body).chain_err(|| { @@ -834,7 +834,7 @@ impl super::RocketchatApi for RocketchatApi { let (body, status_code) = RestApi::call_rocketchat(&post_file_message_endpoint)?; if !status_code.is_success() { - return Err(build_error(&post_file_message_endpoint.url(), &body, &status_code)); + return Err(build_error(&post_file_message_endpoint.url(), &body, status_code)); } Ok(()) @@ -855,7 +855,7 @@ impl super::RocketchatApi for RocketchatApi { let (body, status_code) = RestApi::call_rocketchat(&users_info_endpoint)?; if !status_code.is_success() { - return Err(build_error(&users_info_endpoint.url(), &body, &status_code)); + return Err(build_error(&users_info_endpoint.url(), &body, status_code)); } let users_info_response: UsersInfoResponse = serde_json::from_str(&body).chain_err(|| { @@ -889,7 +889,7 @@ fn get_channel_members(rocketchat_api: &RocketchatApi, room_id: &str, offset: i3 let (body, status_code) = RestApi::call_rocketchat(&channel_members_endpoint)?; if !status_code.is_success() { - return Err(build_error(&channel_members_endpoint.url(), &body, &status_code)); + return Err(build_error(&channel_members_endpoint.url(), &body, status_code)); } let channel_members_response: MembersResponse = serde_json::from_str(&body).chain_err(|| { @@ -915,7 +915,7 @@ fn get_group_members(rocketchat_api: &RocketchatApi, room_id: &str, offset: i32) let (body, status_code) = RestApi::call_rocketchat(&group_members_endpoint)?; if !status_code.is_success() { - return Err(build_error(&group_members_endpoint.url(), &body, &status_code)); + return Err(build_error(&group_members_endpoint.url(), &body, status_code)); } let group_members_response: MembersResponse = serde_json::from_str(&body).chain_err(|| { @@ -940,7 +940,7 @@ fn channels_list_joined(rocketchat_api: &RocketchatApi, offset: i32) -> Result Result Error { +fn build_error(endpoint: &str, body: &str, status_code: StatusCode) -> Error { let json_error_msg = format!( "Could not deserialize error from Rocket.Chat API endpoint {} with status code {}: `{}`", endpoint, status_code, body @@ -966,7 +966,7 @@ fn build_error(endpoint: &str, body: &str, status_code: &StatusCode) -> Error { } }; - if *status_code == StatusCode::UNAUTHORIZED { + if status_code == StatusCode::UNAUTHORIZED { return Error { error_chain: ErrorKind::RocketchatAuthenticationFailed(rocketchat_error_resp.message.unwrap_or_default()).into(), user_message: Some(t!(["errors", "authentication_failed"])), diff --git a/tests/matrix-rocketchat-test/helpers.rs b/tests/matrix-rocketchat-test/helpers.rs index fc3a526..3b41cad 100644 --- a/tests/matrix-rocketchat-test/helpers.rs +++ b/tests/matrix-rocketchat-test/helpers.rs @@ -292,5 +292,5 @@ pub fn add_room_alias_id(config: &Config, room_id: RoomId, room_alias_id: RoomAl body_params.insert("alias".to_string(), json!(room_alias)); let payload = serde_json::to_string(&body_params).unwrap(); - RestApi::call_matrix(&SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms).unwrap(); + RestApi::call_matrix(SendStateEventForEmptyKeyEndpoint::method(), &endpoint, payload, ¶ms).unwrap(); }