From e79bf6b8c0407e0d2e2485d5448bf4358922f633 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 21 Sep 2018 22:59:53 +0300 Subject: [PATCH] matrix-api/util.rs: Replace build_url macros with functions --- fractal-matrix-api/src/backend/mod.rs | 4 +- fractal-matrix-api/src/backend/room.rs | 8 ++-- fractal-matrix-api/src/backend/stickers.rs | 6 +-- fractal-matrix-api/src/backend/user.rs | 4 +- fractal-matrix-api/src/util.rs | 49 +++++++++------------- 5 files changed, 31 insertions(+), 40 deletions(-) diff --git a/fractal-matrix-api/src/backend/mod.rs b/fractal-matrix-api/src/backend/mod.rs index 923a0400..ab573b44 100644 --- a/fractal-matrix-api/src/backend/mod.rs +++ b/fractal-matrix-api/src/backend/mod.rs @@ -6,7 +6,7 @@ use std::sync::mpsc::channel; use std::sync::mpsc::RecvError; use std::collections::HashMap; -use util::build_url; +use util::{build_url, client_url}; use error::Error; @@ -66,7 +66,7 @@ impl Backend { let mut params2 = params.to_vec(); params2.push(("access_token", tk.clone())); - client_url!(&base, path, params2) + client_url(&base, path, params2) } pub fn run(mut self) -> Sender { diff --git a/fractal-matrix-api/src/backend/room.rs b/fractal-matrix-api/src/backend/room.rs index 56372d20..56ce4891 100644 --- a/fractal-matrix-api/src/backend/room.rs +++ b/fractal-matrix-api/src/backend/room.rs @@ -14,7 +14,7 @@ use error::Error; use util::json_q; use util::dw_media; use util::get_initial_room_messages; -use util::build_url; +use util::{build_url, client_url, media_url}; use util::put_media; use util; use util::cache_path; @@ -144,7 +144,7 @@ pub fn get_room_messages(bk: &Backend, roomid: String) -> Result<(), Error> { } fn parse_context(tx: Sender, tk: String, baseu: Url, roomid: String, eid: String, limit: i32) -> Result<(), Error> { - let url = client_url!(&baseu, &format!("rooms/{}/context/{}", roomid, eid), + let url = client_url(&baseu, &format!("rooms/{}/context/{}", roomid, eid), vec![("limit", format!("{}", limit)), ("access_token", tk.clone())])?; get!(&url, @@ -341,7 +341,7 @@ pub fn set_room_avatar(bk: &Backend, roomid: String, avatar: String) -> Result<( let baseu = bk.get_base_url()?; let tk = bk.data.lock().unwrap().access_token.clone(); let params = vec![("access_token", tk.clone())]; - let mediaurl = media_url!(&baseu, "upload", params)?; + let mediaurl = media_url(&baseu, "upload", params)?; let roomurl = bk.url(&format!("rooms/{}/state/m.room.avatar", roomid), vec![])?; let mut file = File::open(&avatar)?; @@ -388,7 +388,7 @@ pub fn attach_file(bk: &Backend, msg: Message) -> Result<(), Error> { let baseu = bk.get_base_url()?; let tk = bk.data.lock().unwrap().access_token.clone(); let params = vec![("access_token", tk.clone())]; - let mediaurl = media_url!(&baseu, "upload", params)?; + let mediaurl = media_url(&baseu, "upload", params)?; let mut m = msg.clone(); let tx = bk.tx.clone(); diff --git a/fractal-matrix-api/src/backend/stickers.rs b/fractal-matrix-api/src/backend/stickers.rs index 9fd254f0..59fb4be7 100644 --- a/fractal-matrix-api/src/backend/stickers.rs +++ b/fractal-matrix-api/src/backend/stickers.rs @@ -6,7 +6,7 @@ use self::chrono::prelude::*; use std::thread; use util::json_q; -use util::build_url; +use util::{scalar_url, build_url, client_url}; use url::Url; use std::sync::{Arc, Mutex}; use backend::BackendData; @@ -170,7 +170,7 @@ fn url(data: Arc>, path: &str, params: Vec<(&str, String)>) - let mut params2 = params.to_vec(); params2.push(("access_token", tk.clone())); - client_url!(&base, path, params2) + client_url(&base, path, params2) } fn get_scalar_token(data: Arc>) -> Result { @@ -204,6 +204,6 @@ fn vurl(data: Arc>, path: &str, params: Vec<(&str, String)>) let mut params2 = params.to_vec(); params2.push(("scalar_token", tk)); - scalar_url!(&base, path, params2) + scalar_url(&base, path, params2) } diff --git a/fractal-matrix-api/src/backend/user.rs b/fractal-matrix-api/src/backend/user.rs index 482e45f5..23198b1a 100644 --- a/fractal-matrix-api/src/backend/user.rs +++ b/fractal-matrix-api/src/backend/user.rs @@ -11,7 +11,7 @@ use std::sync::{Arc, Mutex}; use std::sync::mpsc::Sender; use error::Error; use util::json_q; -use util::build_url; +use util::{build_url, media_url}; use util::put_media; use util::get_user_avatar; use util::get_user_avatar_img; @@ -391,7 +391,7 @@ pub fn set_user_avatar(bk: &Backend, avatar: String) -> Result<(), Error> { let id = bk.data.lock().unwrap().user_id.clone(); let tk = bk.data.lock().unwrap().access_token.clone(); let params = vec![("access_token", tk.clone())]; - let mediaurl = media_url!(&baseu, "upload", params)?; + let mediaurl = media_url(&baseu, "upload", params)?; let url = bk.url(&format!("profile/{}/avatar_url", id), vec![])?; let mut file = File::open(&avatar)?; diff --git a/fractal-matrix-api/src/util.rs b/fractal-matrix-api/src/util.rs index 399df1b5..8bb215b1 100644 --- a/fractal-matrix-api/src/util.rs +++ b/fractal-matrix-api/src/util.rs @@ -81,27 +81,6 @@ macro_rules! clone { ); } -#[macro_export] -macro_rules! client_url { - ($b: expr, $path: expr, $params: expr) => ( - build_url($b, &format!("/_matrix/client/r0/{}", $path), $params) - ) -} - -#[macro_export] -macro_rules! scalar_url { - ($b: expr, $path: expr, $params: expr) => ( - build_url($b, &format!("api/{}", $path), $params) - ) -} - -#[macro_export] -macro_rules! media_url { - ($b: expr, $path: expr, $params: expr) => ( - build_url($b, &format!("/_matrix/media/r0/{}", $path), $params) - ) -} - #[macro_export] macro_rules! derror { ($from: path, $to: path) => { @@ -459,7 +438,7 @@ pub fn get_prev_batch_from(baseu: &Url, tk: String, roomid: String, evid: String ]; let path = format!("rooms/{}/context/{}", roomid, evid); - let url = client_url!(baseu, &path, params)?; + let url = client_url(baseu, &path, params)?; let r = json_q("get", &url, &json!(null), globals::TIMEOUT)?; let prev_batch = r["start"].to_string().trim_matches('"').to_string(); @@ -491,7 +470,7 @@ pub fn get_room_media_list(baseu: &Url, }; let path = format!("rooms/{}/messages", roomid); - let url = client_url!(baseu, &path, params)?; + let url = client_url(baseu, &path, params)?; let r = json_q("get", &url, &json!(null), globals::TIMEOUT)?; let array = r["chunk"].as_array(); @@ -557,7 +536,7 @@ pub fn resolve_media_url( path = format!("download/{}/{}", server, media); } - media_url!(base, &path, params) + media_url(base, &path, params) } pub fn dw_media(base: &Url, @@ -584,7 +563,7 @@ pub fn dw_media(base: &Url, path = format!("download/{}/{}", server, media); } - let url = media_url!(base, &path, params)?; + let url = media_url(base, &path, params)?; let fname = match dest { None if thumb => { cache_dir_path("thumbs", &media)? } @@ -665,7 +644,7 @@ pub fn json_q(method: &str, url: &Url, attrs: &JsonValue, timeout: u64) -> Resul } pub fn get_user_avatar(baseu: &Url, userid: &str) -> Result<(String, String), Error> { - let url = client_url!(baseu, &format!("profile/{}", userid), vec![])?; + let url = client_url(baseu, &format!("profile/{}", userid), vec![])?; let attrs = json!(null); match json_q("get", &url, &attrs, globals::TIMEOUT) { @@ -690,7 +669,7 @@ pub fn get_user_avatar(baseu: &Url, userid: &str) -> Result<(String, String), Er } pub fn get_room_st(base: &Url, tk: &str, roomid: &str) -> Result { - let url = client_url!(base, &format!("rooms/{}/state", roomid), vec![("access_token", String::from(tk))])?; + let url = client_url(base, &format!("rooms/{}/state", roomid), vec![("access_token", String::from(tk))])?; let attrs = json!(null); let st = json_q("get", &url, &attrs, globals::TIMEOUT)?; @@ -823,7 +802,7 @@ pub fn get_initial_room_messages(baseu: &Url, }; let path = format!("rooms/{}/messages", roomid); - let url = client_url!(baseu, &path, params)?; + let url = client_url(baseu, &path, params)?; let r = json_q("get", &url, &json!(null), globals::TIMEOUT)?; nend = String::from(r["end"].as_str().unwrap_or("")); @@ -873,7 +852,7 @@ pub fn fill_room_gap(baseu: &Url, params.push(("to", to.clone())); let path = format!("rooms/{}/messages", roomid); - let url = client_url!(baseu, &path, params)?; + let url = client_url(baseu, &path, params)?; let r = json_q("get", &url, &json!(null), globals::TIMEOUT)?; nend = String::from(r["end"].as_str().unwrap_or("")); @@ -913,6 +892,18 @@ pub fn build_url(base: &Url, path: &str, params: Vec<(&str, String)>) -> Result< Ok(url) } +pub fn client_url(base: &Url, path: &str, params: Vec<(&str, String)>) -> Result { + build_url(base, &format!("/_matrix/client/r0/{}", path), params) +} + +pub fn scalar_url(base: &Url, path: &str, params: Vec<(&str, String)>) -> Result { + build_url(base, &format!("api/{}", path), params) +} + +pub fn media_url(base: &Url, path: &str, params: Vec<(&str, String)>) -> Result { + build_url(base, &format!("/_matrix/media/r0/{}", path), params) +} + pub fn cache_path(name: &str) -> Result { let mut path = match glib::get_user_cache_dir() { Some(path) => path,