Backend: Use Url type in BackendData.server_url

This commit is contained in:
Alejandro Domínguez 2018-12-30 00:25:04 +01:00 committed by Daniel Garcia Moreno
parent 48ffd7bd6e
commit 09f504471c
5 changed files with 15 additions and 15 deletions

View file

@ -7,6 +7,7 @@ use crate::globals;
use crate::backend::types::BKResponse;
use crate::backend::types::Backend;
use crate::error::Error;
use std::str::Split;
use std::thread;
use crate::util::cache_path;
@ -25,7 +26,6 @@ pub fn protocols(bk: &Backend) -> Result<(), Error> {
.append_pair("access_token", &tk);
let tx = bk.tx.clone();
let s = bk.data.lock().unwrap().server_url.clone();
get!(
&url,
move |r: JsonValue| {
@ -33,7 +33,11 @@ pub fn protocols(bk: &Backend) -> Result<(), Error> {
protocols.push(Protocol {
id: String::new(),
desc: String::from(s.split('/').last().unwrap_or_default()),
desc: baseu
.path_segments()
.and_then(Split::last)
.map(Into::into)
.unwrap_or_default(),
});
if let Some(prs) = r.as_object() {

View file

@ -34,7 +34,8 @@ impl Backend {
let data = BackendData {
user_id: String::from("Guest"),
access_token: String::new(),
server_url: String::from("https://matrix.org"),
server_url: Url::parse("https://matrix.org")
.expect("Wrong server_url value in BackendData"),
scalar_token: None,
scalar_url: String::from("https://scalar.vector.im"),
sticker_widget: None,
@ -53,8 +54,7 @@ impl Backend {
}
fn get_base_url(&self) -> Result<Url, Error> {
let s = self.data.lock().unwrap().server_url.clone();
let url = Url::parse(&s)?;
let url = self.data.lock().unwrap().server_url.clone();
Ok(url)
}

View file

@ -15,7 +15,7 @@ pub fn guest(bk: &Backend, server: &str) -> Result<(), Error> {
let url = Url::parse(server)
.unwrap()
.join("/_matrix/client/r0/register?kind=guest")?;
bk.data.lock().unwrap().server_url = String::from(server);
bk.data.lock().unwrap().server_url = Url::parse(server)?;
let data = bk.data.clone();
let tx = bk.tx.clone();
@ -67,8 +67,7 @@ fn build_login_attrs(user: &str, password: &str) -> Result<JsonValue, Error> {
}
pub fn login(bk: &Backend, user: &str, password: &str, server: &str) -> Result<(), Error> {
let s = String::from(server);
bk.data.lock().unwrap().server_url = s;
bk.data.lock().unwrap().server_url = Url::parse(server)?;
let url = bk.url("login", vec![])?;
let attrs = build_login_attrs(user, password)?;
@ -100,8 +99,7 @@ pub fn login(bk: &Backend, user: &str, password: &str, server: &str) -> Result<(
}
pub fn set_token(bk: &Backend, token: String, uid: String, server: &str) -> Result<(), Error> {
let s = String::from(server);
bk.data.lock().unwrap().server_url = s;
bk.data.lock().unwrap().server_url = Url::parse(server)?;
bk.data.lock().unwrap().access_token = token.clone();
bk.data.lock().unwrap().user_id = uid.clone();
bk.data.lock().unwrap().since = None;
@ -131,8 +129,7 @@ pub fn logout(bk: &Backend) -> Result<(), Error> {
}
pub fn register(bk: &Backend, user: &str, password: &str, server: &str) -> Result<(), Error> {
let s = String::from(server);
bk.data.lock().unwrap().server_url = s;
bk.data.lock().unwrap().server_url = Url::parse(server)?;
let url = bk.url("register", vec![("kind", String::from("user"))])?;
let attrs = json!({

View file

@ -166,8 +166,7 @@ pub fn purchase(bk: &Backend, group: &StickerGroup) -> Result<(), Error> {
}
fn get_base_url(data: &Arc<Mutex<BackendData>>) -> Result<Url, Error> {
let s = data.lock().unwrap().server_url.clone();
let url = Url::parse(&s)?;
let url = data.lock().unwrap().server_url.clone();
Ok(url)
}

View file

@ -186,7 +186,7 @@ pub enum RoomType {
pub struct BackendData {
pub user_id: String,
pub access_token: String,
pub server_url: String,
pub server_url: Url,
pub scalar_token: Option<String>,
pub scalar_url: String,
pub sticker_widget: Option<String>,