matrix-api/util.rs: Replace build_url macros with functions
This commit is contained in:
parent
0d7526b8c7
commit
e79bf6b8c0
5 changed files with 31 additions and 40 deletions
|
@ -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<BKCommand> {
|
||||
|
|
|
@ -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<BKResponse>, 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();
|
||||
|
|
|
@ -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<Mutex<BackendData>>, 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<Mutex<BackendData>>) -> Result<String, Error> {
|
||||
|
@ -204,6 +204,6 @@ fn vurl(data: Arc<Mutex<BackendData>>, 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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)?;
|
||||
|
|
|
@ -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<JsonValue, Error> {
|
||||
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<Url, Error> {
|
||||
build_url(base, &format!("/_matrix/client/r0/{}", path), params)
|
||||
}
|
||||
|
||||
pub fn scalar_url(base: &Url, path: &str, params: Vec<(&str, String)>) -> Result<Url, Error> {
|
||||
build_url(base, &format!("api/{}", path), params)
|
||||
}
|
||||
|
||||
pub fn media_url(base: &Url, path: &str, params: Vec<(&str, String)>) -> Result<Url, Error> {
|
||||
build_url(base, &format!("/_matrix/media/r0/{}", path), params)
|
||||
}
|
||||
|
||||
pub fn cache_path(name: &str) -> Result<String, Error> {
|
||||
let mut path = match glib::get_user_cache_dir() {
|
||||
Some(path) => path,
|
||||
|
|
Loading…
Reference in a new issue