[fractal] Backend: Remove scalar_url and scalar_token fields
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] Backend: Remove scalar_url and scalar_token fields
- Date: Mon, 6 Jan 2020 09:00:40 +0000 (UTC)
commit 4b6cce3eb516092031829e98c07f6f0bf483886e
Author: Alejandro DomÃnguez <adomu net-c com>
Date: Thu Dec 12 01:51:21 2019 +0100
Backend: Remove scalar_url and scalar_token fields
fractal-matrix-api/src/backend/mod.rs | 15 ++--
fractal-matrix-api/src/backend/stickers.rs | 110 ++++++++++++++++++-----------
fractal-matrix-api/src/backend/types.rs | 6 +-
3 files changed, 79 insertions(+), 52 deletions(-)
---
diff --git a/fractal-matrix-api/src/backend/mod.rs b/fractal-matrix-api/src/backend/mod.rs
index 6ce1a033..4450bd79 100644
--- a/fractal-matrix-api/src/backend/mod.rs
+++ b/fractal-matrix-api/src/backend/mod.rs
@@ -37,9 +37,9 @@ pub use self::types::RoomType;
impl Backend {
pub fn new(tx: Sender<BKResponse>) -> Backend {
let data = BackendData {
- scalar_token: None,
- scalar_url: Url::parse("https://scalar.vector.im")
- .expect("Wrong scalar_url value in BackendData"),
+ // scalar_token: None,
+ // scalar_url: Url::parse("https://scalar.vector.im")
+ // .expect("Wrong scalar_url value in BackendData"),
sticker_widget: None,
rooms_since: String::new(),
join_to_room: String::new(),
@@ -399,16 +399,17 @@ impl Backend {
}
// Stickers module
- Ok(BKCommand::ListStickers(access_token, uid)) => {
- let r = stickers::list(self, access_token, uid);
+ Ok(BKCommand::ListStickers(access_token, uid, scalar_url, scalar_token)) => {
+ let r = stickers::list(self, access_token, uid, scalar_url, scalar_token);
bkerror2!(r, tx, BKResponse::Stickers);
}
Ok(BKCommand::SendSticker(server, access_token, room, sticker)) => {
let r = stickers::send(self, server, access_token, room, sticker);
bkerror2!(r, tx, BKResponse::Stickers);
}
- Ok(BKCommand::PurchaseSticker(access_token, uid, group)) => {
- let r = stickers::purchase(self, access_token, uid, group);
+ Ok(BKCommand::PurchaseSticker(access_token, uid, group, scalar_url, scalar_token)) => {
+ let r =
+ stickers::purchase(self, access_token, uid, group, scalar_url, scalar_token);
bkerror2!(r, tx, BKResponse::Stickers);
}
diff --git a/fractal-matrix-api/src/backend/stickers.rs b/fractal-matrix-api/src/backend/stickers.rs
index 8ed5dd10..2f4472fd 100644
--- a/fractal-matrix-api/src/backend/stickers.rs
+++ b/fractal-matrix-api/src/backend/stickers.rs
@@ -1,11 +1,9 @@
use chrono::prelude::*;
use serde_json::json;
-use crate::backend::BackendData;
use crate::util::json_q;
use crate::util::ResultExpectLog;
use crate::util::{client_url, scalar_url};
-use std::sync::{Arc, Mutex};
use std::thread;
use url::Url;
@@ -20,24 +18,39 @@ use crate::types::StickerGroup;
use serde_json::Value as JsonValue;
/// Queries scalar.vector.im to list all the stickers
-pub fn list(bk: &Backend, access_token: AccessToken, uid: String) -> Result<(), Error> {
+pub fn list(
+ bk: &Backend,
+ access_token: AccessToken,
+ uid: String,
+ scalar_url: Url,
+ scalar_token: Option<String>,
+) -> Result<(), Error> {
if let Some(widget_id) = bk.data.lock().unwrap().sticker_widget.clone() {
let data = vec![
("widget_type", "m.stickerpicker".to_string()),
("widget_id", widget_id),
("filter_unpurchased", "true".to_string()),
];
- let url = vurl(&bk.data, &access_token, uid, "widgets/assets", data)?;
+ let url = vurl(
+ scalar_url,
+ scalar_token,
+ &access_token,
+ uid,
+ "widgets/assets",
+ data,
+ )?;
let tx = bk.tx.clone();
get!(
url,
|r: JsonValue| {
- let mut stickers = vec![];
- for sticker_group in r["assets"].as_array().unwrap_or(&vec![]).iter() {
- let group = StickerGroup::from_json(sticker_group);
- stickers.push(group);
- }
+ let stickers = r["assets"]
+ .as_array()
+ .iter()
+ .flat_map(|arr| arr.iter())
+ .map(StickerGroup::from_json)
+ .collect();
+
tx.send(BKResponse::Stickers(Ok(stickers)))
.expect_log("Connection closed");
},
@@ -46,18 +59,18 @@ pub fn list(bk: &Backend, access_token: AccessToken, uid: String) -> Result<(),
.expect_log("Connection closed");
}
);
-
- Ok(())
} else {
get_sticker_widget_id(
bk,
access_token.clone(),
uid.clone(),
- BKCommand::ListStickers(access_token, uid),
+ BKCommand::ListStickers(access_token, uid, scalar_url.clone(), scalar_token.clone()),
+ scalar_url,
+ scalar_token,
)?;
-
- Ok(())
}
+
+ Ok(())
}
pub fn get_sticker_widget_id(
@@ -65,6 +78,8 @@ pub fn get_sticker_widget_id(
access_token: AccessToken,
uid: String,
then: BKCommand,
+ scalar_url: Url,
+ scalar_token: Option<String>,
) -> Result<(), Error> {
let data = json!({
"data": {},
@@ -73,7 +88,15 @@ pub fn get_sticker_widget_id(
let d = bk.data.clone();
let itx = bk.internal_tx.clone();
- let url = vurl(&d, &access_token, uid, "widgets/request", vec![]).unwrap();
+ let url = vurl(
+ scalar_url,
+ scalar_token,
+ &access_token,
+ uid,
+ "widgets/request",
+ vec![],
+ )
+ .unwrap();
post!(
url,
&data,
@@ -167,6 +190,8 @@ pub fn purchase(
access_token: AccessToken,
uid: String,
group: StickerGroup,
+ scalar_url: Url,
+ scalar_token: Option<String>,
) -> Result<(), Error> {
if let Some(widget_id) = bk.data.lock().unwrap().sticker_widget.clone() {
let asset = group.asset.clone();
@@ -176,7 +201,8 @@ pub fn purchase(
("widget_type", "m.stickerpicker".to_string()),
];
let url = vurl(
- &bk.data,
+ scalar_url.clone(),
+ scalar_token.clone(),
&access_token,
uid.clone(),
"widgets/purchase_asset",
@@ -187,8 +213,13 @@ pub fn purchase(
get!(
url,
|_| if let Some(t) = itx {
- t.send(BKCommand::ListStickers(access_token, uid))
- .expect_log("Connection closed");
+ t.send(BKCommand::ListStickers(
+ access_token,
+ uid,
+ scalar_url,
+ scalar_token,
+ ))
+ .expect_log("Connection closed");
},
|err| {
tx.send(BKResponse::Stickers(Err(err)))
@@ -202,7 +233,15 @@ pub fn purchase(
bk,
access_token.clone(),
uid.clone(),
- BKCommand::PurchaseSticker(access_token, uid, group.clone()),
+ BKCommand::PurchaseSticker(
+ access_token,
+ uid,
+ group.clone(),
+ scalar_url.clone(),
+ scalar_token.clone(),
+ ),
+ scalar_url,
+ scalar_token,
)?;
Ok(())
@@ -210,47 +249,36 @@ pub fn purchase(
}
fn get_scalar_token(
- data: &Arc<Mutex<BackendData>>,
+ scalar_url: Url,
access_token: &AccessToken,
uid: String,
) -> Result<String, Error> {
- let base = data.lock().unwrap().scalar_url.clone();
-
let params = &[("access_token", access_token.to_string())];
let path = &format!("user/{}/openid/request_token", uid);
- let url = client_url(&base, path, params)?;
+ let url = client_url(&scalar_url, path, params)?;
let js = json_q("post", url, &json!({}))?;
- let vurl = base
+ let vurl = scalar_url
.join("/api/register")
.expect("Wrong URL in get_scalar_token()");
- let js = json_q("post", vurl, &js)?;
- match js["scalar_token"].as_str() {
- Some(st) => {
- data.lock().unwrap().scalar_token = Some(st.to_string());
- Ok(st.to_string())
- }
- None => Err(Error::BackendError),
- }
+ json_q("post", vurl, &js)?["scalar_token"]
+ .as_str()
+ .map(Into::into)
+ .ok_or(Error::BackendError)
}
fn vurl(
- data: &Arc<Mutex<BackendData>>,
+ s_url: Url,
+ scalar_token: Option<String>,
access_token: &AccessToken,
uid: String,
path: &str,
mut params: Vec<(&str, String)>,
) -> Result<Url, Error> {
- let base = data.lock().unwrap().scalar_url.clone();
- let tk = data
- .lock()
- .unwrap()
- .scalar_token
- .clone()
- .unwrap_or(get_scalar_token(data, access_token, uid)?);
+ let tk = scalar_token.unwrap_or(get_scalar_token(s_url.clone(), access_token, uid)?);
params.push(("scalar_token", tk));
- scalar_url(&base, path, ¶ms)
+ scalar_url(&s_url, path, ¶ms)
}
diff --git a/fractal-matrix-api/src/backend/types.rs b/fractal-matrix-api/src/backend/types.rs
index b772e29e..cd3a46ef 100644
--- a/fractal-matrix-api/src/backend/types.rs
+++ b/fractal-matrix-api/src/backend/types.rs
@@ -81,9 +81,9 @@ pub enum BKCommand {
RejectInv(Url, AccessToken, String),
UserSearch(Url, AccessToken, String),
Invite(Url, AccessToken, String, String),
- ListStickers(AccessToken, String),
+ ListStickers(AccessToken, String, Url, Option<String>),
SendSticker(Url, AccessToken, String, Sticker),
- PurchaseSticker(AccessToken, String, StickerGroup),
+ PurchaseSticker(AccessToken, String, StickerGroup, Url, Option<String>),
ChangeLanguage(AccessToken, Url, String, String, String),
}
@@ -154,8 +154,6 @@ pub enum RoomType {
}
pub struct BackendData {
- pub scalar_token: Option<String>,
- pub scalar_url: Url,
pub sticker_widget: Option<String>,
pub rooms_since: String,
pub join_to_room: String,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]