[fractal] Set user avatar through matrix-sdk
- From: Alexandre Franke <afranke src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] Set user avatar through matrix-sdk
- Date: Tue, 1 Dec 2020 20:22:00 +0000 (UTC)
commit 3413dca059fee2e8531afeab7b1a06aa1a3a41b5
Author: Alejandro DomÃnguez <adomu net-c com>
Date: Wed Sep 9 22:27:34 2020 +0200
Set user avatar through matrix-sdk
fractal-gtk/src/actions/account_settings.rs | 14 +++---
fractal-gtk/src/backend/user.rs | 56 +++++++++-------------
fractal-matrix-api/src/meson.build | 3 --
fractal-matrix-api/src/r0.rs | 1 -
fractal-matrix-api/src/r0/media.rs | 1 -
fractal-matrix-api/src/r0/media/create_content.rs | 33 -------------
fractal-matrix-api/src/r0/profile.rs | 1 -
.../src/r0/profile/set_avatar_url.rs | 31 ------------
8 files changed, 28 insertions(+), 112 deletions(-)
---
diff --git a/fractal-gtk/src/actions/account_settings.rs b/fractal-gtk/src/actions/account_settings.rs
index 76362c32..be7c3bfa 100644
--- a/fractal-gtk/src/actions/account_settings.rs
+++ b/fractal-gtk/src/actions/account_settings.rs
@@ -5,9 +5,8 @@ use gio::SimpleAction;
use gio::SimpleActionGroup;
use glib::clone;
use std::sync::{Arc, Mutex};
-use std::thread;
-use crate::app::App;
+use crate::app::{App, RUNTIME};
use crate::appop::AppOp;
use crate::widgets::FileDialog::open;
@@ -24,18 +23,17 @@ pub fn new(window: >k::Window, op: Arc<Mutex<AppOp>>) -> gio::SimpleActionGrou
actions.add_action(&change_avatar);
change_avatar.connect_activate(clone!(@weak window => move |a, _| {
- let login_data = unwrap_or_unit_return!(op.lock().unwrap().login_data.clone());
- let server_url = login_data.session_client.homeserver().clone();
- let access_token = login_data.access_token;
- let uid = login_data.uid;
+ let (session_client, uid) = unwrap_or_unit_return!(
+ op.lock().unwrap().login_data.as_ref().map(|ld| (ld.session_client.clone(), ld.uid.clone()))
+ );
let filter = gtk::FileFilter::new();
filter.add_mime_type("image/*");
filter.set_name(Some(i18n("Images").as_str()));
if let Some(path) = open(&window, i18n("Select a new avatar").as_str(), &[filter]) {
a.change_state(&ButtonState::Insensitive.into());
- thread::spawn(move || {
- match user::set_user_avatar(server_url, access_token, uid, path) {
+ RUNTIME.spawn(async move {
+ match user::set_user_avatar(session_client, &uid, path).await {
Ok(path) => {
APPOP!(show_new_avatar, (path));
}
diff --git a/fractal-gtk/src/backend/user.rs b/fractal-gtk/src/backend/user.rs
index 05c77af1..c2972efc 100644
--- a/fractal-gtk/src/backend/user.rs
+++ b/fractal-gtk/src/backend/user.rs
@@ -2,7 +2,6 @@ use fractal_api::identifiers::UserId;
use fractal_api::reqwest::Error as ReqwestError;
use fractal_api::url::{ParseError as UrlError, Url};
use fractal_api::{Client as MatrixClient, Error as MatrixError};
-use std::fs;
use std::io::Error as IoError;
use super::MediaError;
@@ -14,9 +13,11 @@ use log::error;
use std::convert::TryInto;
use std::path::PathBuf;
+use super::room::AttachedFileError;
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::profile::set_avatar_url::Request as SetAvatarUrlRequest;
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;
use fractal_api::identity::r0::association::msisdn::submit_token::Body as SubmitPhoneTokenBody;
@@ -48,12 +49,6 @@ use fractal_api::r0::contact::request_verification_token_msisdn::request as requ
use fractal_api::r0::contact::request_verification_token_msisdn::Body as PhoneTokenBody;
use fractal_api::r0::contact::request_verification_token_msisdn::Parameters as PhoneTokenParameters;
use fractal_api::r0::contact::request_verification_token_msisdn::Response as PhoneTokenResponse;
-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::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;
use fractal_api::r0::profile::set_display_name::request as set_display_name;
use fractal_api::r0::profile::set_display_name::Body as SetDisplayNameBody;
use fractal_api::r0::profile::set_display_name::Parameters as SetDisplayNameParameters;
@@ -489,46 +484,39 @@ pub fn account_destruction(
#[derive(Debug)]
pub enum SetUserAvatarError {
Io(IoError),
- Reqwest(ReqwestError),
+ Matrix(MatrixError),
+ ParseUrl(UrlError),
}
-impl From<IoError> for SetUserAvatarError {
- fn from(err: IoError) -> Self {
- Self::Io(err)
+impl From<MatrixError> for SetUserAvatarError {
+ fn from(err: MatrixError) -> Self {
+ Self::Matrix(err)
}
}
-impl From<ReqwestError> for SetUserAvatarError {
- fn from(err: ReqwestError) -> Self {
- Self::Reqwest(err)
+impl From<AttachedFileError> for SetUserAvatarError {
+ fn from(err: AttachedFileError) -> Self {
+ match err {
+ AttachedFileError::Io(err) => Self::Io(err),
+ AttachedFileError::Matrix(err) => Self::Matrix(err),
+ AttachedFileError::ParseUrl(err) => Self::ParseUrl(err),
+ }
}
}
impl HandleError for SetUserAvatarError {}
-pub fn set_user_avatar(
- base: Url,
- access_token: AccessToken,
- uid: UserId,
+pub async fn set_user_avatar(
+ session_client: MatrixClient,
+ user_id: &UserId,
avatar: PathBuf,
) -> Result<PathBuf, SetUserAvatarError> {
- let params_upload = CreateContentParameters {
- access_token: access_token.clone(),
- filename: None,
- };
+ let avatar_url = super::room::upload_file(session_client.clone(), &avatar)
+ .await?
+ .content_uri;
- let contents = fs::read(&avatar)?;
- let request = create_content(base.clone(), ¶ms_upload, contents)?;
- let upload_response: CreateContentResponse =
- HTTP_CLIENT.get_client().execute(request)?.json()?;
-
- let params_avatar = SetAvatarUrlParameters { access_token };
- let body = SetAvatarUrlBody {
- avatar_url: Some(upload_response.content_uri),
- };
-
- let request = set_avatar_url(base, ¶ms_avatar, &body, &uid)?;
- HTTP_CLIENT.get_client().execute(request)?;
+ let request = SetAvatarUrlRequest::new(user_id, Some(&avatar_url));
+ session_client.send(request).await?;
Ok(avatar)
}
diff --git a/fractal-matrix-api/src/meson.build b/fractal-matrix-api/src/meson.build
index ea08e81d..05fa5cbf 100644
--- a/fractal-matrix-api/src/meson.build
+++ b/fractal-matrix-api/src/meson.build
@@ -13,8 +13,6 @@ api_sources = files(
'r0/contact/get_identifiers.rs',
'r0/contact/request_verification_token_email.rs',
'r0/contact/request_verification_token_msisdn.rs',
- 'r0/media/create_content.rs',
- 'r0/profile/set_avatar_url.rs',
'r0/profile/set_display_name.rs',
'r0/server/domain_info.rs',
'r0/sync/sync_events.rs',
@@ -22,7 +20,6 @@ api_sources = files(
'r0/account.rs',
'r0/contact.rs',
'r0/filter.rs',
- 'r0/media.rs',
'r0/profile.rs',
'r0/server.rs',
'r0/sync.rs',
diff --git a/fractal-matrix-api/src/r0.rs b/fractal-matrix-api/src/r0.rs
index 81a8282d..9784f349 100644
--- a/fractal-matrix-api/src/r0.rs
+++ b/fractal-matrix-api/src/r0.rs
@@ -1,7 +1,6 @@
pub mod account;
pub mod contact;
pub mod filter;
-pub mod media;
pub mod profile;
pub mod server;
pub mod sync;
diff --git a/fractal-matrix-api/src/r0/profile.rs b/fractal-matrix-api/src/r0/profile.rs
index 580e8270..5adf0aaf 100644
--- a/fractal-matrix-api/src/r0/profile.rs
+++ b/fractal-matrix-api/src/r0/profile.rs
@@ -1,2 +1 @@
-pub mod set_avatar_url;
pub mod set_display_name;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]