Skip to content

Commit

Permalink
Forward images from Rocket.Chat to Matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
exul committed Jan 2, 2018
1 parent 95da505 commit 2108141
Show file tree
Hide file tree
Showing 10 changed files with 1,055 additions and 207 deletions.
48 changes: 40 additions & 8 deletions src/matrix-rocketchat/api/rest_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::io::Read;

use url;
use reqwest::{Client, Method, StatusCode, Url};
use reqwest::{Client, Method, Response, StatusCode, Url};
use reqwest::header::Headers;
use ruma_client_api::Method as RumaHttpMethod;

Expand Down Expand Up @@ -32,7 +32,24 @@ impl RestApi {

/// Call a Rocket.Chat API endpoint
pub fn call_rocketchat(endpoint: &Endpoint) -> Result<(String, StatusCode)> {
RestApi::call(&endpoint.method(), &endpoint.url(), &endpoint.payload()?, &endpoint.query_params(), endpoint.headers())
RestApi::call(
&endpoint.method(),
&endpoint.url(),
&endpoint.payload()?,
&endpoint.query_params(),
endpoint.headers(),
)
}

/// Get a file that was uploaded to Rocket.Chat
pub fn get_rocketchat_file(endpoint: &Endpoint) -> Result<Response> {
RestApi::call_raw(
&endpoint.method(),
&endpoint.url(),
&endpoint.payload()?,
&endpoint.query_params(),
endpoint.headers(),
)
}

/// Call a REST API endpoint
Expand All @@ -43,6 +60,21 @@ impl RestApi {
params: &HashMap<&str, &'a str>,
headers: Option<Headers>,
) -> Result<(String, StatusCode)> {
let mut resp = RestApi::call_raw(method, url, payload, params, headers)?;
let mut body = String::new();
resp.read_to_string(&mut body)
.chain_err(|| ErrorKind::ApiCallFailed(url.to_owned()))?;

Ok((body, resp.status()))
}

fn call_raw<'a>(
method: &Method,
url: &str,
payload: &str,
params: &HashMap<&str, &'a str>,
headers: Option<Headers>,
) -> Result<Response> {
let client = Client::new();
let encoded_url = RestApi::encode_url(url.to_string(), params)?;

Expand All @@ -52,7 +84,9 @@ impl RestApi {
Method::Post => client.post(&encoded_url),
Method::Delete => client.delete(&encoded_url),
_ => {
return Err(Error::from(ErrorKind::UnsupportedHttpMethod(method.to_string())));
return Err(Error::from(ErrorKind::UnsupportedHttpMethod(
method.to_string(),
)));
}
};

Expand All @@ -62,12 +96,10 @@ impl RestApi {

req.body(payload.to_owned());

let mut resp = req.send().chain_err(|| ErrorKind::ApiCallFailed(url.to_owned()))?;
let mut body = String::new();

resp.read_to_string(&mut body).chain_err(|| ErrorKind::ApiCallFailed(url.to_owned()))?;
let resp = req.send()
.chain_err(|| ErrorKind::ApiCallFailed(url.to_owned()))?;

Ok((body, resp.status()))
Ok(resp)
}

fn encode_url(base: String, parameters: &HashMap<&str, &str>) -> Result<String> {
Expand Down
7 changes: 6 additions & 1 deletion src/matrix-rocketchat/api/rocketchat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub trait RocketchatApi {
fn current_username(&self) -> Result<String>;
/// List of direct messages the user is part of
fn direct_messages_list(&self) -> Result<Vec<Channel>>;
/// Get the url of an image that is attached to a message.
fn image_url(&self, message_id: &str) -> Result<Vec<String>>;
/// Login a user on the Rocket.Chat server
fn login(&self, username: &str, password: &str) -> Result<(String, String)>;
/// Post a chat message
Expand Down Expand Up @@ -123,7 +125,10 @@ impl RocketchatApi {
match serde_json::from_str(&body).chain_err(|| ErrorKind::NoRocketchatServer(url.clone())) {
Ok(rocketchat_info) => rocketchat_info,
Err(err) => {
bail_error!(err, t!(["errors", "no_rocketchat_server"]).with_vars(vec![("rocketchat_url", url)]));
bail_error!(
err,
t!(["errors", "no_rocketchat_server"]).with_vars(vec![("rocketchat_url", url)])
);
}
};

Expand Down

0 comments on commit 2108141

Please sign in to comment.