[fractal/get-profile-access-token] Add access token to get_profile requests
- From: Daniel Garcia Moreno <danigm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal/get-profile-access-token] Add access token to get_profile requests
- Date: Wed, 29 Jul 2020 16:24:21 +0000 (UTC)
commit d06fcd9b3dad5f8829527b2374d7c888f5b19245
Author: Daniel GarcĂa Moreno <dani danigm net>
Date: Wed Jul 29 18:23:06 2020 +0200
Add access token to get_profile requests
Some servers requires the authentication to retrieve the user profile so
we need to include the access_token in these requests.
Fix https://gitlab.gnome.org/GNOME/fractal/-/issues/522
fractal-gtk/src/appop/account.rs | 1 +
fractal-gtk/src/appop/notify.rs | 2 ++
fractal-gtk/src/appop/user.rs | 4 +++-
fractal-gtk/src/backend/sync.rs | 26 +++++++++++++++---------
fractal-gtk/src/backend/user.rs | 8 ++++++--
fractal-gtk/src/cache/mod.rs | 10 ++++++++-
fractal-gtk/src/model/room.rs | 5 ++++-
fractal-gtk/src/widgets/member.rs | 2 ++
fractal-gtk/src/widgets/message.rs | 1 +
fractal-matrix-api/src/r0/profile/get_profile.rs | 12 ++++++++---
10 files changed, 53 insertions(+), 18 deletions(-)
---
diff --git a/fractal-gtk/src/appop/account.rs b/fractal-gtk/src/appop/account.rs
index d311209e..a8cb9fb2 100644
--- a/fractal-gtk/src/appop/account.rs
+++ b/fractal-gtk/src/appop/account.rs
@@ -545,6 +545,7 @@ impl AppOp {
self.thread_pool.clone(),
self.user_info_cache.clone(),
login_data.server_url,
+ login_data.access_token,
login_data.uid,
data,
);
diff --git a/fractal-gtk/src/appop/notify.rs b/fractal-gtk/src/appop/notify.rs
index eed7b68b..1a5a9e64 100644
--- a/fractal-gtk/src/appop/notify.rs
+++ b/fractal-gtk/src/appop/notify.rs
@@ -45,6 +45,7 @@ impl AppOp {
pub fn notify(&self, app: gtk::Application, room_id: &RoomId, id: &EventId) -> Option<()> {
let server_url = self.login_data.clone()?.server_url;
+ let access_token = self.login_data.clone()?.access_token;
let msg = self.get_message_by_id(room_id, id)?;
let r = self.rooms.get(room_id)?;
let short_body = match &msg.mtype[..] {
@@ -68,6 +69,7 @@ impl AppOp {
self.thread_pool.clone(),
self.user_info_cache.clone(),
server_url,
+ access_token,
msg.sender,
tx,
);
diff --git a/fractal-gtk/src/appop/user.rs b/fractal-gtk/src/appop/user.rs
index a6c08d74..3789ed3b 100644
--- a/fractal-gtk/src/appop/user.rs
+++ b/fractal-gtk/src/appop/user.rs
@@ -32,7 +32,7 @@ impl AppOp {
}));
thread::spawn(clone!(@strong login_data => move || {
- match user::get_user_avatar(login_data.server_url, &login_data.uid) {
+ match user::get_user_avatar(login_data.server_url, login_data.access_token, &login_data.uid) {
Ok((_, path)) => {
APPOP!(set_avatar, (path));
}
@@ -91,6 +91,7 @@ impl AppOp {
self.thread_pool.clone(),
self.user_info_cache.clone(),
login_data.server_url.clone(),
+ login_data.access_token.clone(),
login_data.uid.clone(),
data,
);
@@ -116,6 +117,7 @@ impl AppOp {
self.thread_pool.clone(),
self.user_info_cache.clone(),
login_data.server_url.clone(),
+ login_data.access_token.clone(),
login_data.uid,
data,
);
diff --git a/fractal-gtk/src/backend/sync.rs b/fractal-gtk/src/backend/sync.rs
index 5b7db540..d02058b3 100644
--- a/fractal-gtk/src/backend/sync.rs
+++ b/fractal-gtk/src/backend/sync.rs
@@ -167,7 +167,7 @@ pub fn sync(
};
let params = SyncParameters {
- access_token,
+ access_token: access_token.clone(),
filter,
include_state: IncludeState::Changed {
since: since.clone().unwrap_or_default(),
@@ -192,13 +192,14 @@ pub fn sync(
match query {
Ok(response) => {
if since.is_none() {
- let rooms = Room::from_sync_response(&response, user_id, base)
- .map(|rooms| {
- let def = join_to_room
- .and_then(|jtr| rooms.iter().find(|x| x.id == jtr).cloned());
- (rooms, def)
- })
- .map_err(Into::into);
+ let rooms =
+ Room::from_sync_response(&response, user_id, access_token, base)
+ .map(|rooms| {
+ let def = join_to_room
+ .and_then(|jtr| rooms.iter().find(|x| x.id == jtr).cloned());
+ (rooms, def)
+ })
+ .map_err(Into::into);
let next_batch = response.next_batch;
@@ -207,8 +208,13 @@ pub fn sync(
let join = &response.rooms.join;
// New rooms
- let update_rooms =
- Room::from_sync_response(&response, user_id.clone(), base).map_err(Into::into);
+ let update_rooms = Room::from_sync_response(
+ &response,
+ user_id.clone(),
+ access_token,
+ base,
+ )
+ .map_err(Into::into);
// Message events
let room_messages = join
diff --git a/fractal-gtk/src/backend/user.rs b/fractal-gtk/src/backend/user.rs
index 0cbce396..c155c7f0 100644
--- a/fractal-gtk/src/backend/user.rs
+++ b/fractal-gtk/src/backend/user.rs
@@ -55,6 +55,7 @@ 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::Parameters as GetProfileParameters;
use fractal_api::r0::profile::get_profile::Response as GetProfileResponse;
use fractal_api::r0::profile::set_avatar_url::request as set_avatar_url;
use fractal_api::r0::profile::set_avatar_url::Body as SetAvatarUrlBody;
@@ -565,6 +566,7 @@ pub fn get_user_info_async(
thread_pool: ThreadPool,
user_info_cache: UserInfoCache,
baseu: Url,
+ access_token: AccessToken,
uid: UserId,
tx: Sender<UserInfo>,
) {
@@ -576,7 +578,7 @@ pub fn get_user_info_async(
}
thread_pool.run(move || {
- let info = get_user_avatar(baseu, &uid);
+ let info = get_user_avatar(baseu, access_token, &uid);
if let Ok(ref i0) = info {
user_info_cache.lock().unwrap().insert(uid, i0.clone());
@@ -658,9 +660,11 @@ impl HandleError for GetUserAvatarError {}
pub fn get_user_avatar(
base: Url,
+ access_token: AccessToken,
user_id: &UserId,
) -> Result<(String, PathBuf), GetUserAvatarError> {
- let request = get_profile(base.clone(), user_id)?;
+ let params = GetProfileParameters { access_token };
+ let request = get_profile(base.clone(), ¶ms, user_id)?;
let response: GetProfileResponse = HTTP_CLIENT.get_client().execute(request)?.json()?;
let name = response
diff --git a/fractal-gtk/src/cache/mod.rs b/fractal-gtk/src/cache/mod.rs
index 7f7b6939..708e112d 100644
--- a/fractal-gtk/src/cache/mod.rs
+++ b/fractal-gtk/src/cache/mod.rs
@@ -160,11 +160,19 @@ pub fn download_to_cache(
thread_pool: ThreadPool,
user_info_cache: UserInfoCache,
server_url: Url,
+ access_token: AccessToken,
uid: UserId,
data: Rc<RefCell<AvatarData>>,
) {
let (tx, rx) = channel::<(String, PathBuf)>();
- user::get_user_info_async(thread_pool, user_info_cache, server_url, uid, tx);
+ user::get_user_info_async(
+ thread_pool,
+ user_info_cache,
+ server_url,
+ access_token,
+ uid,
+ tx,
+ );
gtk::timeout_add(50, move || match rx.try_recv() {
Err(TryRecvError::Empty) => Continue(true),
diff --git a/fractal-gtk/src/model/room.rs b/fractal-gtk/src/model/room.rs
index d17cd19f..222f7364 100644
--- a/fractal-gtk/src/model/room.rs
+++ b/fractal-gtk/src/model/room.rs
@@ -8,6 +8,7 @@ use either::Either;
use fractal_api::identifiers::{Error as IdError, EventId, RoomId, UserId};
use fractal_api::r0::directory::post_public_rooms::Chunk as PublicRoomsChunk;
use fractal_api::r0::sync::sync_events::Response as SyncResponse;
+use fractal_api::r0::AccessToken;
use fractal_api::url::{ParseError as UrlError, Url};
use log::{debug, info};
use serde::{Deserialize, Serialize};
@@ -134,6 +135,7 @@ impl Room {
pub fn from_sync_response(
response: &SyncResponse,
user_id: UserId,
+ access_token: AccessToken,
baseu: Url,
) -> Result<Vec<Self>, IdError> {
// getting the list of direct rooms
@@ -208,7 +210,7 @@ impl Room {
if leave_id != user_id {
let kick_reason = &last_event["content"]["reason"];
if let Ok((kicker_alias, kicker_avatar)) =
- get_user_avatar(baseu.clone(), &leave_id)
+ get_user_avatar(baseu.clone(), access_token.clone(), &leave_id)
{
let kicker = Member {
alias: Some(kicker_alias),
@@ -248,6 +250,7 @@ impl Room {
.map_or(Ok(None), |ev| {
Ok(get_user_avatar(
baseu.clone(),
+ access_token.clone(),
&UserId::try_from(ev["sender"].as_str().unwrap_or_default())?,
)
.ok())
diff --git a/fractal-gtk/src/widgets/member.rs b/fractal-gtk/src/widgets/member.rs
index a767d319..9c6b8628 100644
--- a/fractal-gtk/src/widgets/member.rs
+++ b/fractal-gtk/src/widgets/member.rs
@@ -61,6 +61,7 @@ impl<'a> MemberBox<'a> {
self.op.thread_pool.clone(),
self.op.user_info_cache.clone(),
login_data.server_url,
+ login_data.access_token,
self.member.uid.clone(),
data,
);
@@ -105,6 +106,7 @@ impl<'a> MemberBox<'a> {
self.op.thread_pool.clone(),
self.op.user_info_cache.clone(),
login_data.server_url,
+ login_data.access_token,
self.member.uid.clone(),
data,
);
diff --git a/fractal-gtk/src/widgets/message.rs b/fractal-gtk/src/widgets/message.rs
index 9130ef0c..4f050eab 100644
--- a/fractal-gtk/src/widgets/message.rs
+++ b/fractal-gtk/src/widgets/message.rs
@@ -288,6 +288,7 @@ impl MessageBox {
thread_pool,
user_info_cache,
self.server_url.clone(),
+ self.access_token.clone(),
uid.clone(),
data.clone(),
);
diff --git a/fractal-matrix-api/src/r0/profile/get_profile.rs
b/fractal-matrix-api/src/r0/profile/get_profile.rs
index 203a6c6e..a59d99ee 100644
--- a/fractal-matrix-api/src/r0/profile/get_profile.rs
+++ b/fractal-matrix-api/src/r0/profile/get_profile.rs
@@ -1,20 +1,26 @@
+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 avatar_url: Option<String>,
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/{}", user_id))
.expect("Malformed URL in get_profile_avatar");
- 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]