[fractal] Fix account details loading



commit 7c694015dd809a1d708fcc3d87dfe7af7cab7ece
Author: Alejandro Domínguez <adomu net-c com>
Date:   Mon Jul 6 16:16:20 2020 +0200

    Fix account details loading
    
    Applies the changes proposed by Link Dupont. Fix #592

 fractal-gtk/src/appop/message.rs                      | 17 +++++++++++------
 fractal-gtk/src/appop/user.rs                         |  2 +-
 fractal-gtk/src/backend/user.rs                       | 16 ++++++++++++----
 fractal-gtk/src/cache/mod.rs                          |  7 +++++--
 fractal-gtk/src/widgets/message.rs                    |  7 ++++++-
 fractal-gtk/src/widgets/room_history.rs               | 13 +++++++++++--
 fractal-matrix-api/src/r0/profile/get_display_name.rs | 12 +++++++++---
 7 files changed, 55 insertions(+), 19 deletions(-)
---
diff --git a/fractal-gtk/src/appop/message.rs b/fractal-gtk/src/appop/message.rs
index 52d7adb6..a1d1365e 100644
--- a/fractal-gtk/src/appop/message.rs
+++ b/fractal-gtk/src/appop/message.rs
@@ -76,11 +76,12 @@ impl AppOp {
         let login_data = self.login_data.clone()?;
         let messages = self.history.as_ref()?.get_listbox();
         if let Some(ui_msg) = self.create_new_room_message(&msg) {
-            let mb = widgets::MessageBox::new(login_data.server_url).tmpwidget(
-                self.thread_pool.clone(),
-                self.user_info_cache.clone(),
-                &ui_msg,
-            );
+            let mb = widgets::MessageBox::new(login_data.server_url, login_data.access_token)
+                .tmpwidget(
+                    self.thread_pool.clone(),
+                    self.user_info_cache.clone(),
+                    &ui_msg,
+                );
             let m = mb.get_listbox_row();
             messages.add(m);
 
@@ -114,7 +115,11 @@ impl AppOp {
         let mut widgets = vec![];
         for t in self.msg_queue.iter().rev().filter(|m| m.msg.room == r.id) {
             if let Some(ui_msg) = self.create_new_room_message(&t.msg) {
-                let mb = widgets::MessageBox::new(login_data.server_url.clone()).tmpwidget(
+                let mb = widgets::MessageBox::new(
+                    login_data.server_url.clone(),
+                    login_data.access_token.clone(),
+                )
+                .tmpwidget(
                     self.thread_pool.clone(),
                     self.user_info_cache.clone(),
                     &ui_msg,
diff --git a/fractal-gtk/src/appop/user.rs b/fractal-gtk/src/appop/user.rs
index 588a1182..33bef69e 100644
--- a/fractal-gtk/src/appop/user.rs
+++ b/fractal-gtk/src/appop/user.rs
@@ -21,7 +21,7 @@ impl AppOp {
         let login_data = unwrap_or_unit_return!(self.login_data.clone());
 
         thread::spawn(clone!(@strong login_data => move || {
-            match user::get_username(login_data.server_url, login_data.uid) {
+            match user::get_username(login_data.server_url, login_data.access_token, login_data.uid) {
                 Ok(username) => {
                     APPOP!(set_username, (username));
                 }
diff --git a/fractal-gtk/src/backend/user.rs b/fractal-gtk/src/backend/user.rs
index 51f450bd..f10c7bbf 100644
--- a/fractal-gtk/src/backend/user.rs
+++ b/fractal-gtk/src/backend/user.rs
@@ -52,6 +52,7 @@ 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::get_profile::request as get_profile;
 use fractal_api::r0::profile::get_profile::Response as GetProfileResponse;
@@ -89,8 +90,13 @@ impl<T: Into<Error>> From<T> for NameError {
 
 impl HandleError for NameError {}
 
-pub fn get_username(base: Url, uid: UserId) -> Result<Option<String>, NameError> {
-    let request = get_display_name(base, &uid)?;
+pub fn get_username(
+    base: Url,
+    access_token: AccessToken,
+    uid: UserId,
+) -> Result<Option<String>, NameError> {
+    let params = GetDisplayNameParameters { access_token };
+    let request = get_display_name(base, &params, &uid)?;
     let response: GetDisplayNameResponse = HTTP_CLIENT.get_client()?.execute(request)?.json()?;
 
     Ok(response.displayname)
@@ -98,8 +104,10 @@ pub fn get_username(base: Url, uid: UserId) -> Result<Option<String>, NameError>
 
 // 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, uid: UserId) -> String {
-    get_display_name(base, &uid)
+pub fn get_username_async(base: Url, access_token: AccessToken, uid: UserId) -> String {
+    let params = GetDisplayNameParameters { access_token };
+
+    get_display_name(base, &params, &uid)
         .map_err::<Error, _>(Into::into)
         .and_then(|request| {
             HTTP_CLIENT
diff --git a/fractal-gtk/src/cache/mod.rs b/fractal-gtk/src/cache/mod.rs
index 77ccb9f1..f17ef22c 100644
--- a/fractal-gtk/src/cache/mod.rs
+++ b/fractal-gtk/src/cache/mod.rs
@@ -1,6 +1,7 @@
 use crate::backend::user;
 use crate::backend::ThreadPool;
 use crate::util::ResultExpectLog;
+use fractal_api::r0::AccessToken;
 use fractal_api::url::Url;
 use glib::source::Continue;
 use gtk::LabelExt;
@@ -165,13 +166,14 @@ 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,
     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, uid);
+        let query = user::get_username_async(server_url, access_token, uid);
         ctx.send(query).expect_log("Connection closed");
     });
     gtk::timeout_add(50, move || match rx.try_recv() {
@@ -193,6 +195,7 @@ pub fn download_to_cache_username(
  * 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,
     uid: UserId,
     text: &str,
     label: gtk::Label,
@@ -200,7 +203,7 @@ pub fn download_to_cache_username_emote(
 ) {
     let (ctx, rx): (Sender<String>, Receiver<String>) = channel();
     thread::spawn(move || {
-        let query = user::get_username_async(server_url, uid);
+        let query = user::get_username_async(server_url, access_token, uid);
         ctx.send(query).expect_log("Connection closed");
     });
     let text = text.to_string();
diff --git a/fractal-gtk/src/widgets/message.rs b/fractal-gtk/src/widgets/message.rs
index 80d7683f..562327f2 100644
--- a/fractal-gtk/src/widgets/message.rs
+++ b/fractal-gtk/src/widgets/message.rs
@@ -5,6 +5,7 @@ use crate::backend::ThreadPool;
 use crate::cache::CacheMap;
 use chrono::prelude::*;
 use fractal_api::identifiers::UserId;
+use fractal_api::r0::AccessToken;
 use fractal_api::url::Url;
 use glib::clone;
 use gtk::{prelude::*, ButtonExt, ContainerExt, LabelExt, Overlay, WidgetExt};
@@ -29,6 +30,7 @@ use crate::widgets::{AudioPlayerWidget, PlayerExt, VideoPlayerWidget};
 /* A message row in the room history */
 #[derive(Clone, Debug)]
 pub struct MessageBox {
+    access_token: AccessToken,
     server_url: Url,
     username: gtk::Label,
     pub username_event_box: gtk::EventBox,
@@ -41,7 +43,7 @@ pub struct MessageBox {
 }
 
 impl MessageBox {
-    pub fn new(server_url: Url) -> MessageBox {
+    pub fn new(server_url: Url, access_token: AccessToken) -> MessageBox {
         let username = gtk::Label::new(None);
         let eb = gtk::EventBox::new();
         let eventbox = gtk::EventBox::new();
@@ -53,6 +55,7 @@ impl MessageBox {
         gesture.set_touch_only(true);
 
         MessageBox {
+            access_token,
             server_url,
             username,
             username_event_box: eb,
@@ -291,6 +294,7 @@ impl MessageBox {
         );
         download_to_cache_username(
             self.server_url.clone(),
+            self.access_token.clone(),
             uid,
             self.username.clone(),
             Some(data),
@@ -664,6 +668,7 @@ impl MessageBox {
 
         download_to_cache_username_emote(
             self.server_url.clone(),
+            self.access_token.clone(),
             msg.sender.clone(),
             &markup,
             msg_label.clone(),
diff --git a/fractal-gtk/src/widgets/room_history.rs b/fractal-gtk/src/widgets/room_history.rs
index 69df6d48..bcfd5f4a 100644
--- a/fractal-gtk/src/widgets/room_history.rs
+++ b/fractal-gtk/src/widgets/room_history.rs
@@ -20,6 +20,7 @@ use crate::globals;
 use crate::widgets;
 use crate::widgets::{PlayerExt, VideoPlayerWidget};
 use fractal_api::identifiers::{RoomId, UserId};
+use fractal_api::r0::AccessToken;
 use fractal_api::url::Url;
 use gio::ActionMapExt;
 use gio::SimpleActionGroup;
@@ -259,6 +260,7 @@ impl Element {
 pub struct RoomHistory {
     /* Contains a list of msg ids to keep track of the displayed messages */
     rows: Rc<RefCell<List>>,
+    access_token: AccessToken,
     server_url: Url,
     source_id: Rc<RefCell<Option<source::SourceId>>>,
     queue: Rc<RefCell<VecDeque<MessageContent>>>,
@@ -284,9 +286,11 @@ impl RoomHistory {
 
         /* Add the action groupe to the room_history */
         listbox.insert_action_group("message", Some(&actions));
+        let login_data = op.login_data.clone()?;
         let mut rh = RoomHistory {
             rows: Rc::new(RefCell::new(List::new(scroll, listbox))),
-            server_url: op.login_data.clone()?.server_url,
+            access_token: login_data.access_token,
+            server_url: login_data.server_url,
             source_id: Rc::new(RefCell::new(None)),
             queue: Rc::new(RefCell::new(VecDeque::new())),
             edit_buffer: Rc::new(RefCell::new(VecDeque::new())),
@@ -450,6 +454,7 @@ impl RoomHistory {
             /* Lazy load initial messages */
             let source_id = self.source_id.clone();
             let server_url = self.server_url.clone();
+            let access_token = self.access_token.clone();
             *self.source_id.borrow_mut() = Some(gtk::idle_add(move || {
                 let mut data = queue.borrow_mut();
                 let mut edits = edit_buffer.borrow_mut();
@@ -510,6 +515,7 @@ impl RoomHistory {
                         item.clone(),
                         has_header,
                         server_url.clone(),
+                        access_token.clone(),
                         &rows,
                     ));
                     rows.borrow_mut().add_top(Element::Message(item));
@@ -585,6 +591,7 @@ impl RoomHistory {
             item.clone(),
             has_header,
             self.server_url.clone(),
+            self.access_token.clone(),
             &self.rows,
         );
         item.widget = Some(b);
@@ -622,6 +629,7 @@ impl RoomHistory {
             item.clone(),
             msg_widget.header,
             self.server_url.clone(),
+            self.access_token.clone(),
             &self.rows,
         ));
         rows.replace_item(i, msg_widget.get_listbox_row(), Element::Message(item));
@@ -733,11 +741,12 @@ fn create_row(
     row: MessageContent,
     has_header: bool,
     server_url: Url,
+    access_token: AccessToken,
     rows: &Rc<RefCell<List>>,
 ) -> widgets::MessageBox {
     /* we need to create a message with the username, so that we don't have to pass
      * all information to the widget creating each row */
-    let mut mb = widgets::MessageBox::new(server_url);
+    let mut mb = widgets::MessageBox::new(server_url, access_token);
     mb.create(
         thread_pool,
         user_info_cache,
diff --git a/fractal-matrix-api/src/r0/profile/get_display_name.rs 
b/fractal-matrix-api/src/r0/profile/get_display_name.rs
index b4716e03..1e7171af 100644
--- a/fractal-matrix-api/src/r0/profile/get_display_name.rs
+++ b/fractal-matrix-api/src/r0/profile/get_display_name.rs
@@ -1,16 +1,22 @@
+use crate::r0::AccessToken;
 use reqwest::blocking::Client;
 use reqwest::blocking::Request;
 use reqwest::Error;
 use ruma_identifiers::UserId;
-use serde::Deserialize;
+use serde::{Deserialize, Serialize};
 use url::Url;
 
+#[derive(Clone, Debug, Serialize)]
+pub struct Parameters {
+    pub access_token: AccessToken,
+}
+
 #[derive(Clone, Debug, Deserialize)]
 pub struct Response {
     pub displayname: Option<String>,
 }
 
-pub fn request(base: Url, user_id: &UserId) -> Result<Request, Error> {
+pub fn request(base: Url, params: &Parameters, user_id: &UserId) -> Result<Request, Error> {
     let url = base
         .join(&format!(
             "_matrix/client/r0/profile/{}/displayname",
@@ -18,5 +24,5 @@ pub fn request(base: Url, user_id: &UserId) -> Result<Request, Error> {
         ))
         .expect("Malformed URL in get_display_name");
 
-    Client::new().get(url).build()
+    Client::new().get(url).query(params).build()
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]