Remove user_id from Backend

This commit is contained in:
Alejandro Domínguez 2019-12-08 22:43:38 +01:00
parent cb3fec53a2
commit 219b77803f
17 changed files with 226 additions and 213 deletions

View file

@ -21,6 +21,7 @@ pub fn new(
backend: &Sender<BKCommand>,
server_url: Url,
access_token: AccessToken,
uid: String,
) -> gio::SimpleActionGroup {
let actions = SimpleActionGroup::new();
// TODO create two stats loading interaction and connect it to the avatar box
@ -42,6 +43,7 @@ pub fn new(
let _ = backend.send(BKCommand::SetUserAvatar(
server_url.clone(),
access_token.clone(),
uid.clone(),
file.to_string(),
));
} else {

View file

@ -28,8 +28,7 @@ pub fn backend_loop(rx: Receiver<BKResponse>) {
APPOP!(bk_logout);
}
BKResponse::Name(Ok(username)) => {
let u = Some(username);
APPOP!(set_username, (u));
APPOP!(set_username, (username));
}
BKResponse::GetThreePID(Ok(list)) => {
let l = Some(list);

View file

@ -82,6 +82,7 @@ impl App {
&backend,
login_data.server_url,
login_data.access_token,
login_data.uid,
);
let container = self
.ui

View file

@ -28,8 +28,9 @@ impl App {
if let (Some(active_room), Some(login_data)) = (&op.active_room, &op.login_data) {
let server = login_data.server_url.clone();
let access_token = login_data.access_token.clone();
let uid = login_data.uid.clone();
op.backend
.send(BKCommand::ChangeLanguage(access_token, server, lang_code, active_room.clone()))
.send(BKCommand::ChangeLanguage(access_token, server, uid, active_room.clone(), lang_code))
.unwrap();
}
}

View file

@ -573,6 +573,7 @@ impl AppOp {
.send(BKCommand::SetUserName(
login_data.server_url,
login_data.access_token,
login_data.uid,
username,
))
.unwrap();

View file

@ -138,9 +138,7 @@ impl AppOp {
// Only the access token should be used.
if let Ok((username, password, server, id_url)) = self.get_pass() {
if let Ok((Some(access_token), uid)) = self.get_token() {
self.backend
.send(BKCommand::SetUserID(uid.clone()))
.unwrap();
self.backend.send(BKCommand::SetUserID).unwrap();
self.bk_login(uid, access_token, self.device_id.clone(), server, id_url);
} else {
self.connect(username, password, server, id_url);

View file

@ -137,6 +137,7 @@ impl AppOp {
bk.send(BKCommand::AddToFav(
login_data.server_url.clone(),
login_data.access_token.clone(),
login_data.uid.clone(),
room.id.clone(),
tofav,
))
@ -707,6 +708,7 @@ impl AppOp {
.send(BKCommand::SendTyping(
login_data.server_url,
login_data.access_token,
login_data.uid,
active_room.clone(),
))
.unwrap();

View file

@ -25,6 +25,7 @@ impl AppOp {
.send(BKCommand::DirectChat(
login_data.server_url,
login_data.access_token,
login_data.uid,
user.0.clone(),
internal_id.clone(),
))

View file

@ -25,6 +25,7 @@ impl AppOp {
.send(BKCommand::Sync(
login_data.server_url,
login_data.access_token,
login_data.uid,
since,
initial,
))

View file

@ -15,10 +15,13 @@ impl AppOp {
pub fn get_username(&self) {
let login_data = unwrap_or_unit_return!(self.login_data.clone());
self.backend
.send(BKCommand::GetUsername(login_data.server_url.clone()))
.send(BKCommand::GetUsername(
login_data.server_url.clone(),
login_data.uid.clone(),
))
.unwrap();
self.backend
.send(BKCommand::GetAvatar(login_data.server_url))
.send(BKCommand::GetAvatar(login_data.server_url, login_data.uid))
.unwrap();
}

View file

@ -37,7 +37,6 @@ pub use self::types::RoomType;
impl Backend {
pub fn new(tx: Sender<BKResponse>) -> Backend {
let data = BackendData {
user_id: String::from("Guest"),
scalar_token: None,
scalar_url: Url::parse("https://scalar.vector.im")
.expect("Wrong scalar_url value in BackendData"),
@ -97,12 +96,22 @@ impl Backend {
register::register(self, user, passwd, server, id_url)
}
Ok(BKCommand::Guest(server, id_url)) => register::guest(self, server, id_url),
Ok(BKCommand::SetUserID(uid)) => register::set_uid(self, uid),
Ok(BKCommand::SetUserID) => register::set_uid(self),
// User module
Ok(BKCommand::GetUsername(server)) => user::get_username(self, server),
Ok(BKCommand::SetUserName(server, access_token, name)) => {
user::set_username(self, server, access_token, name)
Ok(BKCommand::GetUsername(server, uid)) => {
thread::spawn(move || {
let query = user::get_username(server, uid);
tx.send(BKResponse::Name(query))
.expect_log("Connection closed");
});
}
Ok(BKCommand::SetUserName(server, access_token, uid, username)) => {
thread::spawn(move || {
let query = user::set_username(server, access_token, uid, username);
tx.send(BKResponse::SetUserName(query))
.expect_log("Connection closed");
});
}
Ok(BKCommand::GetThreePID(server, access_token)) => {
thread::spawn(move || {
@ -175,9 +184,19 @@ impl Backend {
.expect_log("Connection closed");
});
}
Ok(BKCommand::GetAvatar(server)) => user::get_avatar(self, server),
Ok(BKCommand::SetUserAvatar(server, access_token, file)) => {
user::set_user_avatar(self, server, access_token, file)
Ok(BKCommand::GetAvatar(server, uid)) => {
thread::spawn(move || {
let query = user::get_avatar(server, uid);
tx.send(BKResponse::Avatar(query))
.expect_log("Connection closed");
});
}
Ok(BKCommand::SetUserAvatar(server, access_token, uid, file)) => {
thread::spawn(move || {
let query = user::set_user_avatar(server, access_token, uid, file);
tx.send(BKResponse::SetUserAvatar(query))
.expect_log("Connection closed");
});
}
Ok(BKCommand::GetAvatarAsync(server, member, ctx)) => {
user::get_avatar_async(self, server, member, ctx)
@ -200,11 +219,11 @@ impl Backend {
}
// Sync module
Ok(BKCommand::Sync(server, access_token, since, initial)) => {
sync::sync(self, server, access_token, since, initial)
Ok(BKCommand::Sync(server, access_token, uid, since, initial)) => {
sync::sync(self, server, access_token, uid, since, initial)
}
Ok(BKCommand::SyncForced(server, access_token)) => {
sync::force_sync(self, server, access_token)
Ok(BKCommand::SyncForced(server, access_token, uid)) => {
sync::force_sync(self, server, access_token, uid)
}
// Room module
@ -231,8 +250,8 @@ impl Backend {
let r = room::redact_msg(self, server, access_token, &msg);
bkerror2!(r, tx, BKResponse::SentMsgRedaction);
}
Ok(BKCommand::SendTyping(server, access_token, room)) => {
let r = room::send_typing(self, server, access_token, room);
Ok(BKCommand::SendTyping(server, access_token, uid, room)) => {
let r = room::send_typing(self, server, access_token, uid, room);
bkerror!(r, tx, BKResponse::SendTypingError);
}
Ok(BKCommand::SetRoom(server, access_token, id)) => {
@ -285,15 +304,16 @@ impl Backend {
.expect_log("Connection closed");
}
}
Ok(BKCommand::DirectChat(server, access_token, user, internalid)) => {
let r = room::direct_chat(self, server, access_token, user, internalid.clone());
Ok(BKCommand::DirectChat(server, access_token, uid, user, internalid)) => {
let r =
room::direct_chat(self, server, access_token, uid, user, internalid.clone());
if let Err(e) = r {
tx.send(BKResponse::NewRoom(Err(e), internalid))
.expect_log("Connection closed");
}
}
Ok(BKCommand::AddToFav(server, access_token, roomid, tofav)) => {
let r = room::add_to_fav(self, server, access_token, roomid, tofav);
Ok(BKCommand::AddToFav(server, access_token, uid, roomid, tofav)) => {
let r = room::add_to_fav(self, server, access_token, uid, roomid, tofav);
bkerror2!(r, tx, BKResponse::AddedToFav);
}
Ok(BKCommand::AcceptInv(server, access_token, roomid)) => {
@ -308,8 +328,8 @@ impl Backend {
let r = room::invite(self, server, access_token, room, userid);
bkerror!(r, tx, BKResponse::InviteError);
}
Ok(BKCommand::ChangeLanguage(access_token, server, lang, room)) => {
let r = room::set_language(self, access_token, server, &room, &lang);
Ok(BKCommand::ChangeLanguage(access_token, server, uid, room, lang)) => {
let r = room::set_language(self, access_token, server, uid, room, lang);
bkerror2!(r, tx, BKResponse::ChangeLanguage);
}
@ -380,16 +400,16 @@ impl Backend {
}
// Stickers module
Ok(BKCommand::ListStickers(access_token)) => {
let r = stickers::list(self, access_token);
Ok(BKCommand::ListStickers(access_token, uid)) => {
let r = stickers::list(self, access_token, uid);
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, group)) => {
let r = stickers::purchase(self, access_token, group);
Ok(BKCommand::PurchaseSticker(access_token, uid, group)) => {
let r = stickers::purchase(self, access_token, uid, group);
bkerror2!(r, tx, BKResponse::Stickers);
}

View file

@ -53,7 +53,6 @@ pub fn guest(bk: &Backend, server: Url, id_url: Url) {
let dev = response.device_id;
if let Some(tk) = response.access_token {
data.lock().unwrap().user_id = uid.clone();
data.lock().unwrap().since = None;
tx.send(BKResponse::Token(uid, tk, dev, server, id_url)) // TODO: Use UserId and DeviceId
.expect_log("Connection closed");
@ -112,7 +111,6 @@ pub fn login(bk: &Backend, user: String, password: String, server: Url, id_url:
let dev = response.device_id;
if let (Some(tk), false) = (response.access_token, uid.is_empty()) {
data.lock().unwrap().user_id = uid.clone();
data.lock().unwrap().since = None;
tx.send(BKResponse::Token(uid, tk, dev, server, id_url)) // TODO: Use UserId and DeviceId
.expect_log("Connection closed");
@ -129,8 +127,7 @@ pub fn login(bk: &Backend, user: String, password: String, server: Url, id_url:
});
}
pub fn set_uid(bk: &Backend, uid: String) {
bk.data.lock().unwrap().user_id = uid.clone();
pub fn set_uid(bk: &Backend) {
bk.data.lock().unwrap().since = None;
}
@ -152,7 +149,6 @@ pub fn logout(bk: &Backend, server: Url, access_token: AccessToken) {
.and(Ok(()));
if query.is_ok() {
data.lock().unwrap().user_id = Default::default();
data.lock().unwrap().since = None;
}
@ -189,7 +185,6 @@ pub fn register(bk: &Backend, user: String, password: String, server: Url, id_ur
let dev = response.device_id;
if let Some(tk) = response.access_token {
data.lock().unwrap().user_id = uid.clone();
data.lock().unwrap().since = None;
tx.send(BKResponse::Token(uid, tk, dev, server, id_url)) // TODO: Use UserId
.expect_log("Connection closed");

View file

@ -375,9 +375,9 @@ pub fn send_typing(
bk: &Backend,
base: Url,
access_token: AccessToken,
userid: String,
roomid: String,
) -> Result<(), Error> {
let userid = bk.data.lock().unwrap().user_id.clone();
let url = bk.url(
base,
&access_token,
@ -828,6 +828,7 @@ pub fn direct_chat(
bk: &Backend,
base: Url,
access_token: AccessToken,
userid: String,
user: Member,
internal_id: String,
) -> Result<(), Error> {
@ -846,7 +847,6 @@ pub fn direct_chat(
}
});
let userid = bk.data.lock().unwrap().user_id.clone();
let direct_url = bk.url(
base,
&access_token,
@ -882,10 +882,10 @@ pub fn add_to_fav(
bk: &Backend,
base: Url,
access_token: AccessToken,
userid: String,
roomid: String,
tofav: bool,
) -> Result<(), Error> {
let userid = bk.data.lock().unwrap().user_id.clone();
let url = bk.url(
base,
&access_token,
@ -960,10 +960,10 @@ pub fn set_language(
bk: &Backend,
access_token: AccessToken,
server: Url,
roomid: &str,
language_code: &str,
userid: String,
roomid: String,
input_language: String,
) -> Result<(), Error> {
let userid = bk.data.lock().unwrap().user_id.clone();
let url = bk.url(
server,
&access_token,
@ -974,10 +974,9 @@ pub fn set_language(
),
vec![],
)?;
let body = json!(Language {
input_language: language_code.to_string(),
});
let body = json!(Language { input_language });
// FIXME: Manage errors in the AppOp loop
put!(url, &body, |_| {}, |err| {
error!(
"Matrix failed to set room language with error code: {:?}",

View file

@ -20,49 +20,50 @@ 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) -> Result<(), Error> {
let widget = bk.data.lock().unwrap().sticker_widget.clone();
if widget.is_none() {
pub fn list(bk: &Backend, access_token: AccessToken, uid: 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 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);
}
tx.send(BKResponse::Stickers(Ok(stickers)))
.expect_log("Connection closed");
},
|err| {
tx.send(BKResponse::Stickers(Err(err)))
.expect_log("Connection closed");
}
);
Ok(())
} else {
get_sticker_widget_id(
bk,
access_token.clone(),
BKCommand::ListStickers(access_token),
uid.clone(),
BKCommand::ListStickers(access_token, uid),
)?;
return Ok(());
Ok(())
}
let widget_id = widget.unwrap();
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, "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);
}
tx.send(BKResponse::Stickers(Ok(stickers)))
.expect_log("Connection closed");
},
|err| {
tx.send(BKResponse::Stickers(Err(err)))
.expect_log("Connection closed");
}
);
Ok(())
}
pub fn get_sticker_widget_id(
bk: &Backend,
access_token: AccessToken,
uid: String,
then: BKCommand,
) -> Result<(), Error> {
let data = json!({
@ -72,7 +73,7 @@ pub fn get_sticker_widget_id(
let d = bk.data.clone();
let itx = bk.internal_tx.clone();
let url = vurl(&d, &access_token, "widgets/request", vec![]).unwrap();
let url = vurl(&d, &access_token, uid, "widgets/request", vec![]).unwrap();
post!(
url,
&data,
@ -161,48 +162,59 @@ pub fn send(
Ok(())
}
pub fn purchase(bk: &Backend, access_token: AccessToken, group: StickerGroup) -> Result<(), Error> {
let widget = bk.data.lock().unwrap().sticker_widget.clone();
if widget.is_none() {
pub fn purchase(
bk: &Backend,
access_token: AccessToken,
uid: String,
group: StickerGroup,
) -> Result<(), Error> {
if let Some(widget_id) = bk.data.lock().unwrap().sticker_widget.clone() {
let asset = group.asset.clone();
let data = vec![
("asset_type", asset.clone()),
("widget_id", widget_id.clone()),
("widget_type", "m.stickerpicker".to_string()),
];
let url = vurl(
&bk.data,
&access_token,
uid.clone(),
"widgets/purchase_asset",
data,
)?;
let tx = bk.tx.clone();
let itx = bk.internal_tx.clone();
get!(
url,
|_| if let Some(t) = itx {
t.send(BKCommand::ListStickers(access_token, uid))
.expect_log("Connection closed");
},
|err| {
tx.send(BKResponse::Stickers(Err(err)))
.expect_log("Connection closed");
}
);
Ok(())
} else {
get_sticker_widget_id(
bk,
access_token.clone(),
BKCommand::PurchaseSticker(access_token, group.clone()),
uid.clone(),
BKCommand::PurchaseSticker(access_token, uid, group.clone()),
)?;
return Ok(());
Ok(())
}
let widget_id = widget.unwrap();
let asset = group.asset.clone();
let data = vec![
("asset_type", asset.clone()),
("widget_id", widget_id.clone()),
("widget_type", "m.stickerpicker".to_string()),
];
let url = vurl(&bk.data, &access_token, "widgets/purchase_asset", data)?;
let tx = bk.tx.clone();
let itx = bk.internal_tx.clone();
get!(
url,
|_| if let Some(t) = itx {
t.send(BKCommand::ListStickers(access_token))
.expect_log("Connection closed");
},
|err| {
tx.send(BKResponse::Stickers(Err(err)))
.expect_log("Connection closed");
}
);
Ok(())
}
fn get_scalar_token(
data: &Arc<Mutex<BackendData>>,
access_token: &AccessToken,
uid: String,
) -> Result<String, Error> {
let base = data.lock().unwrap().scalar_url.clone();
let uid = data.lock().unwrap().user_id.clone();
let params = &[("access_token", access_token.to_string())];
let path = &format!("user/{}/openid/request_token", uid);
@ -226,6 +238,7 @@ fn get_scalar_token(
fn vurl(
data: &Arc<Mutex<BackendData>>,
access_token: &AccessToken,
uid: String,
path: &str,
mut params: Vec<(&str, String)>,
) -> Result<Url, Error> {
@ -235,7 +248,7 @@ fn vurl(
.unwrap()
.scalar_token
.clone()
.unwrap_or(get_scalar_token(data, access_token)?);
.unwrap_or(get_scalar_token(data, access_token, uid)?);
params.push(("scalar_token", tk));

View file

@ -34,12 +34,12 @@ pub fn sync(
bk: &Backend,
base: Url,
access_token: AccessToken,
userid: String,
new_since: Option<String>,
initial: bool,
) {
let tx = bk.tx.clone();
let data = bk.data.clone();
let userid = bk.data.lock().unwrap().user_id.clone();
let since = bk
.data
@ -271,7 +271,7 @@ pub fn sync(
});
}
pub fn force_sync(bk: &Backend, base: Url, access_token: AccessToken) {
pub fn force_sync(bk: &Backend, base: Url, access_token: AccessToken, user_id: String) {
bk.data.lock().unwrap().since = None;
sync(bk, base, access_token, None, true)
sync(bk, base, access_token, user_id, None, true)
}

View file

@ -21,14 +21,14 @@ use url::Url;
#[derive(Debug)]
pub enum BKCommand {
Login(String, String, Url, Url),
SetUserID(String),
SetUserID,
Logout(Url, AccessToken),
#[allow(dead_code)]
Register(String, String, Url, Url),
#[allow(dead_code)]
Guest(Url, Url),
GetUsername(Url),
SetUserName(Url, AccessToken, String),
GetUsername(Url, String),
SetUserName(Url, AccessToken, String, String),
GetThreePID(Url, AccessToken),
GetTokenEmail(Url, AccessToken, Url, String, String),
GetTokenPhone(Url, AccessToken, Url, String, String),
@ -37,10 +37,10 @@ pub enum BKCommand {
DeleteThreePID(Url, AccessToken, Medium, String),
ChangePassword(Url, AccessToken, String, String, String),
AccountDestruction(Url, AccessToken, String, String),
GetAvatar(Url),
SetUserAvatar(Url, AccessToken, String),
Sync(Url, AccessToken, Option<String>, bool),
SyncForced(Url, AccessToken),
GetAvatar(Url, String),
SetUserAvatar(Url, AccessToken, String, String),
Sync(Url, AccessToken, String, Option<String>, bool),
SyncForced(Url, AccessToken, String),
GetRoomMembers(Url, AccessToken, String),
GetRoomMessages(Url, AccessToken, String, String),
GetRoomMessagesFromMsg(Url, AccessToken, String, Message),
@ -64,7 +64,7 @@ pub enum BKCommand {
GetUserNameAsync(Url, String, Sender<String>),
SendMsg(Url, AccessToken, Message),
SendMsgRedaction(Url, AccessToken, Message),
SendTyping(Url, AccessToken, String),
SendTyping(Url, AccessToken, String, String),
SetRoom(Url, AccessToken, String),
ShutDown,
DirectoryProtocols(Url, AccessToken),
@ -77,16 +77,16 @@ pub enum BKCommand {
SetRoomAvatar(Url, AccessToken, String, String),
AttachFile(Url, AccessToken, Message),
NewRoom(Url, AccessToken, String, RoomType, String),
DirectChat(Url, AccessToken, Member, String),
AddToFav(Url, AccessToken, String, bool),
DirectChat(Url, AccessToken, String, Member, String),
AddToFav(Url, AccessToken, String, String, bool),
AcceptInv(Url, AccessToken, String),
RejectInv(Url, AccessToken, String),
UserSearch(Url, AccessToken, String),
Invite(Url, AccessToken, String, String),
ListStickers(AccessToken),
ListStickers(AccessToken, String),
SendSticker(Url, AccessToken, String, Sticker),
PurchaseSticker(AccessToken, StickerGroup),
ChangeLanguage(AccessToken, Url, String, String),
PurchaseSticker(AccessToken, String, StickerGroup),
ChangeLanguage(AccessToken, Url, String, String, String),
}
#[derive(Debug)]
@ -94,7 +94,7 @@ pub enum BKResponse {
ShutDown,
Token(String, AccessToken, Option<String>, Url, Url),
Logout(Result<(), Error>),
Name(Result<String, Error>),
Name(Result<Option<String>, Error>),
SetUserName(Result<String, Error>),
GetThreePID(Result<Vec<ThirdPartyIdentifier>, Error>),
GetTokenEmail(Result<(String, String), Error>),
@ -156,7 +156,6 @@ pub enum RoomType {
}
pub struct BackendData {
pub user_id: String,
pub scalar_token: Option<String>,
pub scalar_url: Url,
pub sticker_widget: Option<String>,

View file

@ -1,7 +1,6 @@
use std::fs;
use url::Url;
use crate::backend::types::BKResponse;
use crate::backend::types::Backend;
use crate::error::Error;
use crate::util::cache_dir_path;
@ -68,25 +67,17 @@ use crate::r0::Medium;
use crate::r0::ThreePIDCredentials;
use crate::types::Member;
pub fn get_username(bk: &Backend, base: Url) {
let tx = bk.tx.clone();
let uid = bk.data.lock().unwrap().user_id.clone();
thread::spawn(move || {
let query = get_display_name(base, &encode_uid(&uid))
.map_err(Into::into)
.and_then(|request| {
HTTP_CLIENT
.get_client()?
.execute(request)?
.json::<GetDisplayNameResponse>()
.map_err(Into::into)
})
.map(|response| response.displayname.unwrap_or(uid));
tx.send(BKResponse::Name(query))
.expect_log("Connection closed");
});
pub fn get_username(base: Url, uid: String) -> Result<Option<String>, Error> {
get_display_name(base, &encode_uid(&uid))
.map_err(Into::into)
.and_then(|request| {
HTTP_CLIENT
.get_client()?
.execute(request)?
.json::<GetDisplayNameResponse>()
.map_err(Into::into)
})
.map(|response| response.displayname)
}
// FIXME: This function manages errors *really* wrong and isn't more async
@ -106,29 +97,26 @@ pub fn get_username_async(base: Url, uid: String) -> String {
.unwrap_or(uid)
}
pub fn set_username(bk: &Backend, base: Url, access_token: AccessToken, name: String) {
let tx = bk.tx.clone();
let uid = bk.data.lock().unwrap().user_id.clone();
pub fn set_username(
base: Url,
access_token: AccessToken,
uid: String,
username: String,
) -> Result<String, Error> {
let params = SetDisplayNameParameters { access_token };
let body = SetDisplayNameBody {
displayname: Some(name.clone()),
displayname: Some(username.clone()),
};
thread::spawn(move || {
let query = set_display_name(base, &params, &body, &encode_uid(&uid))
.map_err(Into::into)
.and_then(|request| {
HTTP_CLIENT
.get_client()?
.execute(request)
.map_err(Into::into)
})
.and(Ok(name));
tx.send(BKResponse::SetUserName(query))
.expect_log("Connection closed");
});
set_display_name(base, &params, &body, &encode_uid(&uid))
.map_err(Into::into)
.and_then(|request| {
HTTP_CLIENT
.get_client()?
.execute(request)
.map_err(Into::into)
})
.and(Ok(username))
}
pub fn get_threepid(
@ -352,15 +340,8 @@ pub fn account_destruction(
.and(Ok(()))
}
pub fn get_avatar(bk: &Backend, base: Url) {
let userid = bk.data.lock().unwrap().user_id.clone();
let tx = bk.tx.clone();
thread::spawn(move || {
let query = get_user_avatar(&base, &userid).map(|(_, fname)| fname);
tx.send(BKResponse::Avatar(query))
.expect_log("Connection closed");
});
pub fn get_avatar(base: Url, userid: String) -> Result<String, Error> {
get_user_avatar(&base, &userid).map(|(_, fname)| fname)
}
pub fn get_avatar_async(bk: &Backend, base: Url, member: Option<Member>, tx: Sender<String>) {
@ -377,51 +358,48 @@ pub fn get_avatar_async(bk: &Backend, base: Url, member: Option<Member>, tx: Sen
}
}
pub fn set_user_avatar(bk: &Backend, base: Url, access_token: AccessToken, avatar: String) {
let tx = bk.tx.clone();
let id = bk.data.lock().unwrap().user_id.clone();
pub fn set_user_avatar(
base: Url,
access_token: AccessToken,
id: String,
avatar: String,
) -> Result<String, Error> {
let params_upload = CreateContentParameters {
access_token: access_token.clone(),
filename: None,
};
thread::spawn(move || {
let query = fs::read(&avatar)
.map_err(Into::into)
.and_then(|contents| {
let (mime, _) = gio::content_type_guess(None, &contents);
let mime_value = HeaderValue::from_str(&mime).or(Err(Error::BackendError))?;
let upload_response =
create_content(base.clone(), &params_upload, contents, Some(mime_value))
.map_err::<Error, _>(Into::into)
.and_then(|request| {
HTTP_CLIENT
.get_client()?
.execute(request)?
.json::<CreateContentResponse>()
.map_err(Into::into)
})?;
let params_avatar = SetAvatarUrlParameters { access_token };
let body = SetAvatarUrlBody {
avatar_url: Some(upload_response.content_uri),
};
set_avatar_url(base, &params_avatar, &body, &encode_uid(&id))
.map_err(Into::into)
fs::read(&avatar)
.map_err(Into::into)
.and_then(|contents| {
let (mime, _) = gio::content_type_guess(None, &contents);
let mime_value = HeaderValue::from_str(&mime).or(Err(Error::BackendError))?;
let upload_response =
create_content(base.clone(), &params_upload, contents, Some(mime_value))
.map_err::<Error, _>(Into::into)
.and_then(|request| {
HTTP_CLIENT
.get_client()?
.execute(request)
.execute(request)?
.json::<CreateContentResponse>()
.map_err(Into::into)
})
})
.and(Ok(avatar));
})?;
tx.send(BKResponse::SetUserAvatar(query))
.expect_log("Connection closed");
});
let params_avatar = SetAvatarUrlParameters { access_token };
let body = SetAvatarUrlBody {
avatar_url: Some(upload_response.content_uri),
};
set_avatar_url(base, &params_avatar, &body, &encode_uid(&id))
.map_err(Into::into)
.and_then(|request| {
HTTP_CLIENT
.get_client()?
.execute(request)
.map_err(Into::into)
})
})
.and(Ok(avatar))
}
pub fn get_user_info_async(