[fractal] Get user name through matrix-sdk
- From: Alexandre Franke <afranke src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] Get user name through matrix-sdk
- Date: Tue, 1 Dec 2020 20:22:00 +0000 (UTC)
commit f756784c5e0796a79ebe2bc2209efbcfbaa2dd72
Author: Alejandro DomÃnguez <adomu net-c com>
Date: Sun Sep 6 20:36:50 2020 +0200
Get user name through matrix-sdk
fractal-gtk/src/appop/user.rs | 17 ++++---
fractal-gtk/src/backend/user.rs | 39 ++++------------
fractal-gtk/src/cache/mod.rs | 53 ++++++++--------------
fractal-gtk/src/widgets/message.rs | 20 ++++----
fractal-matrix-api/src/meson.build | 1 -
fractal-matrix-api/src/r0/profile.rs | 1 -
.../src/r0/profile/get_display_name.rs | 28 ------------
7 files changed, 46 insertions(+), 113 deletions(-)
---
diff --git a/fractal-gtk/src/appop/user.rs b/fractal-gtk/src/appop/user.rs
index 35fe4067..bb6c3c90 100644
--- a/fractal-gtk/src/appop/user.rs
+++ b/fractal-gtk/src/appop/user.rs
@@ -1,10 +1,8 @@
use gtk::prelude::*;
use crate::backend::{user, HandleError};
-use glib::clone;
use std::path::PathBuf;
-use std::thread;
use crate::app::{App, RUNTIME};
use crate::appop::AppOp;
@@ -18,10 +16,15 @@ use super::LoginData;
impl AppOp {
pub fn get_username(&self) {
- let login_data = unwrap_or_unit_return!(self.login_data.clone());
+ let (session_client, user_id) = unwrap_or_unit_return!(self
+ .login_data
+ .as_ref()
+ .map(|ld| (ld.session_client.clone(), ld.uid.clone())));
- thread::spawn(clone!(@strong login_data => move || {
- match user::get_username(login_data.session_client.homeserver().clone(),
login_data.access_token, login_data.uid) {
+ let s_client = session_client.clone();
+ let uid = user_id.clone();
+ RUNTIME.spawn(async move {
+ match user::get_username(s_client, &uid).await {
Ok(username) => {
APPOP!(set_username, (username));
}
@@ -29,10 +32,10 @@ impl AppOp {
err.handle_error();
}
}
- }));
+ });
RUNTIME.spawn(async move {
- match user::get_user_avatar(login_data.session_client, &login_data.uid).await {
+ match user::get_user_avatar(session_client, &user_id).await {
Ok((_, path)) => {
APPOP!(set_avatar, (path));
}
diff --git a/fractal-gtk/src/backend/user.rs b/fractal-gtk/src/backend/user.rs
index 41101f50..05c77af1 100644
--- a/fractal-gtk/src/backend/user.rs
+++ b/fractal-gtk/src/backend/user.rs
@@ -15,6 +15,7 @@ use std::convert::TryInto;
use std::path::PathBuf;
use crate::model::member::Member;
+use fractal_api::api::r0::profile::get_display_name::Request as GetDisplayNameRequest;
use fractal_api::api::r0::profile::get_profile::Request as GetProfileRequest;
use fractal_api::api::r0::user_directory::search_users::Request as UserDirectoryRequest;
use fractal_api::identity::r0::association::msisdn::submit_token::request as submit_phone_token_req;
@@ -50,9 +51,6 @@ use fractal_api::r0::contact::request_verification_token_msisdn::Response as Pho
use fractal_api::r0::media::create_content::request as create_content;
use fractal_api::r0::media::create_content::Parameters as CreateContentParameters;
use fractal_api::r0::media::create_content::Response as CreateContentResponse;
-use fractal_api::r0::profile::get_display_name::request as get_display_name;
-use fractal_api::r0::profile::get_display_name::Parameters as GetDisplayNameParameters;
-use fractal_api::r0::profile::get_display_name::Response as GetDisplayNameResponse;
use fractal_api::r0::profile::set_avatar_url::request as set_avatar_url;
use fractal_api::r0::profile::set_avatar_url::Body as SetAvatarUrlBody;
use fractal_api::r0::profile::set_avatar_url::Parameters as SetAvatarUrlParameters;
@@ -73,45 +71,26 @@ use crate::APPOP;
pub type UserInfo = (String, PathBuf);
#[derive(Debug)]
-pub struct NameError(ReqwestError);
+pub struct NameError(MatrixError);
-impl From<ReqwestError> for NameError {
- fn from(err: ReqwestError) -> Self {
+impl From<MatrixError> for NameError {
+ fn from(err: MatrixError) -> Self {
Self(err)
}
}
impl HandleError for NameError {}
-pub fn get_username(
- base: Url,
- access_token: AccessToken,
- uid: UserId,
+pub async fn get_username(
+ session_client: MatrixClient,
+ user_id: &UserId,
) -> Result<Option<String>, NameError> {
- let params = GetDisplayNameParameters { access_token };
- let request = get_display_name(base, ¶ms, &uid)?;
- let response: GetDisplayNameResponse = HTTP_CLIENT.get_client().execute(request)?.json()?;
+ let request = GetDisplayNameRequest::new(user_id);
+ let response = session_client.send(request).await?;
Ok(response.displayname)
}
-// FIXME: This function manages errors *really* wrong and isn't more async
-// than the normal function. It should be removed.
-pub fn get_username_async(base: Url, access_token: AccessToken, uid: UserId) -> String {
- let params = GetDisplayNameParameters { access_token };
-
- get_display_name(base, ¶ms, &uid)
- .and_then(|request| {
- HTTP_CLIENT
- .get_client()
- .execute(request)?
- .json::<GetDisplayNameResponse>()
- })
- .ok()
- .and_then(|response| response.displayname)
- .unwrap_or_else(|| uid.to_string())
-}
-
#[derive(Debug)]
pub struct SetUserNameError(ReqwestError);
diff --git a/fractal-gtk/src/cache/mod.rs b/fractal-gtk/src/cache/mod.rs
index c4044ce8..add6cbc5 100644
--- a/fractal-gtk/src/cache/mod.rs
+++ b/fractal-gtk/src/cache/mod.rs
@@ -2,14 +2,9 @@ use crate::app::RUNTIME;
use crate::appop::UserInfoCache;
use crate::backend::user;
use crate::util::cache_dir_path;
-use crate::util::ResultExpectLog;
-use fractal_api::r0::AccessToken;
-use fractal_api::url::Url;
use fractal_api::Client as MatrixClient;
-use glib::source::Continue;
use gtk::LabelExt;
use serde::{Deserialize, Serialize};
-use std::thread;
use crate::model::room::{Room, RoomList};
use anyhow::Error;
@@ -20,12 +15,6 @@ use std::time::{Duration, Instant};
use crate::globals;
-/* includes for avatar download */
-use std::sync::mpsc::channel;
-use std::sync::mpsc::Receiver;
-use std::sync::mpsc::Sender;
-use std::sync::mpsc::TryRecvError;
-
use crate::widgets::AvatarData;
use std::cell::RefCell;
use std::rc::Rc;
@@ -172,28 +161,25 @@ pub fn download_to_cache(
/* Get username based on the MXID, we should cache the username */
pub fn download_to_cache_username(
- server_url: Url,
- access_token: AccessToken,
+ session_client: MatrixClient,
uid: UserId,
label: gtk::Label,
avatar: Option<Rc<RefCell<AvatarData>>>,
) {
- let (ctx, rx): (Sender<String>, Receiver<String>) = channel();
- thread::spawn(move || {
- let query = user::get_username_async(server_url, access_token, uid);
- ctx.send(query).expect_log("Connection closed");
+ let response = RUNTIME.spawn(async move {
+ user::get_username(session_client, &uid)
+ .await
+ .ok()
+ .flatten()
+ .unwrap_or_default()
});
- glib::timeout_add_local(50, move || match rx.try_recv() {
- Err(TryRecvError::Empty) => Continue(true),
- Err(TryRecvError::Disconnected) => Continue(false),
- Ok(username) => {
+ glib::MainContext::default().spawn_local(async move {
+ if let Ok(username) = response.await {
label.set_text(&username);
if let Some(ref rc_data) = avatar {
let mut data = rc_data.borrow_mut();
data.redraw(Some(username));
}
-
- Continue(false)
}
});
}
@@ -201,30 +187,27 @@ pub fn download_to_cache_username(
/* Download username for a given MXID and update a emote message
* FIXME: We should cache this request and do it before we need to display the username in an emote*/
pub fn download_to_cache_username_emote(
- server_url: Url,
- access_token: AccessToken,
+ session_client: MatrixClient,
uid: UserId,
text: &str,
label: gtk::Label,
avatar: Option<Rc<RefCell<AvatarData>>>,
) {
- let (ctx, rx): (Sender<String>, Receiver<String>) = channel();
- thread::spawn(move || {
- let query = user::get_username_async(server_url, access_token, uid);
- ctx.send(query).expect_log("Connection closed");
+ let response = RUNTIME.spawn(async move {
+ user::get_username(session_client, &uid)
+ .await
+ .ok()
+ .flatten()
+ .unwrap_or_default()
});
let text = text.to_string();
- glib::timeout_add_local(50, move || match rx.try_recv() {
- Err(TryRecvError::Empty) => Continue(true),
- Err(TryRecvError::Disconnected) => Continue(false),
- Ok(username) => {
+ glib::MainContext::default().spawn_local(async move {
+ if let Ok(username) = response.await {
label.set_markup(&format!("<b>{}</b> {}", &username, text));
if let Some(ref rc_data) = avatar {
let mut data = rc_data.borrow_mut();
data.redraw(Some(username));
}
-
- Continue(false)
}
});
}
diff --git a/fractal-gtk/src/widgets/message.rs b/fractal-gtk/src/widgets/message.rs
index fcdd7175..bfa10555 100644
--- a/fractal-gtk/src/widgets/message.rs
+++ b/fractal-gtk/src/widgets/message.rs
@@ -237,7 +237,7 @@ impl MessageBox {
let body = match msg.mtype {
RowType::Sticker => self.build_room_msg_sticker(session_client, msg),
RowType::Image => self.build_room_msg_image(session_client, msg),
- RowType::Emote => self.build_room_msg_emote(msg),
+ RowType::Emote => self.build_room_msg_emote(session_client, msg),
RowType::Audio => self.build_room_audio_player(session_client, msg),
RowType::Video => self.build_room_video_player(session_client, msg),
RowType::File => self.build_room_msg_file(msg),
@@ -284,14 +284,13 @@ impl MessageBox {
self.username.set_text(&uid.to_string());
}
- download_to_cache(session_client, user_info_cache, uid.clone(), data.clone());
- download_to_cache_username(
- self.server_url.clone(),
- self.access_token.clone(),
- uid,
- self.username.clone(),
- Some(data),
+ download_to_cache(
+ session_client.clone(),
+ user_info_cache,
+ uid.clone(),
+ data.clone(),
);
+ download_to_cache_username(session_client, uid, self.username.clone(), Some(data));
avatar
}
@@ -655,7 +654,7 @@ impl MessageBox {
info
}
- fn build_room_msg_emote(&self, msg: &Message) -> gtk::Box {
+ fn build_room_msg_emote(&self, session_client: MatrixClient, msg: &Message) -> gtk::Box {
let bx = gtk::Box::new(gtk::Orientation::Horizontal, 0);
/* Use MXID till we have a alias */
let sname = msg
@@ -667,8 +666,7 @@ impl MessageBox {
let markup = markup_text(body);
download_to_cache_username_emote(
- self.server_url.clone(),
- self.access_token.clone(),
+ session_client,
msg.sender.clone(),
&markup,
msg_label.clone(),
diff --git a/fractal-matrix-api/src/meson.build b/fractal-matrix-api/src/meson.build
index 618a5df4..dff355d1 100644
--- a/fractal-matrix-api/src/meson.build
+++ b/fractal-matrix-api/src/meson.build
@@ -14,7 +14,6 @@ api_sources = files(
'r0/contact/request_verification_token_email.rs',
'r0/contact/request_verification_token_msisdn.rs',
'r0/media/create_content.rs',
- 'r0/profile/get_display_name.rs',
'r0/profile/set_avatar_url.rs',
'r0/profile/set_display_name.rs',
'r0/server/domain_info.rs',
diff --git a/fractal-matrix-api/src/r0/profile.rs b/fractal-matrix-api/src/r0/profile.rs
index 62d77463..580e8270 100644
--- a/fractal-matrix-api/src/r0/profile.rs
+++ b/fractal-matrix-api/src/r0/profile.rs
@@ -1,3 +1,2 @@
-pub mod get_display_name;
pub mod set_avatar_url;
pub mod set_display_name;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]