From f1a03ececde5280e9d84c6622d7c7a072f80d53f Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 18 Aug 2022 18:30:49 +0200 Subject: [PATCH] feat(sdk): Add public set_account_data[_raw] to Account --- crates/matrix-sdk/src/account.rs | 39 +++++++++++++++++++++++-- crates/matrix-sdk/src/encryption/mod.rs | 25 ++-------------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/crates/matrix-sdk/src/account.rs b/crates/matrix-sdk/src/account.rs index a40cf799d..979809df8 100644 --- a/crates/matrix-sdk/src/account.rs +++ b/crates/matrix-sdk/src/account.rs @@ -24,18 +24,23 @@ use ruma::{ add_3pid, change_password, deactivate, delete_3pid, get_3pids, request_3pid_management_token_via_email, request_3pid_management_token_via_msisdn, }, + config::set_global_account_data, profile::{ get_avatar_url, get_display_name, get_profile, set_avatar_url, set_display_name, }, uiaa::AuthData, }, assign, - events::room::MediaSource, + events::{ + room::MediaSource, AnyGlobalAccountDataEventContent, GlobalAccountDataEventContent, + GlobalAccountDataEventType, + }, + serde::Raw, thirdparty::Medium, ClientSecret, MxcUri, OwnedMxcUri, SessionId, UInt, }; -use crate::{config::RequestConfig, Client, Error, Result}; +use crate::{config::RequestConfig, Client, Error, HttpError, Result}; /// A high-level API to manage the client owner's account. /// @@ -623,4 +628,34 @@ impl Account { }); Ok(self.client.send(request, None).await?) } + + /// Set the given account data event. + pub async fn set_account_data( + &self, + content: T, + ) -> Result + where + T: GlobalAccountDataEventContent, + { + let own_user = + self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?; + + let request = set_global_account_data::v3::Request::new(own_user, &content)?; + + Ok(self.client.send(request, None).await?) + } + + /// Set the given raw account data event. + pub async fn set_account_data_raw( + &self, + event_type: GlobalAccountDataEventType, + content: Raw, + ) -> Result { + let own_user = + self.client.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?; + + let request = set_global_account_data::v3::Request::new_raw(event_type, own_user, content); + + Ok(self.client.send(request, None).await?) + } } diff --git a/crates/matrix-sdk/src/encryption/mod.rs b/crates/matrix-sdk/src/encryption/mod.rs index 7eb01a01e..8d9849bfb 100644 --- a/crates/matrix-sdk/src/encryption/mod.rs +++ b/crates/matrix-sdk/src/encryption/mod.rs @@ -33,10 +33,7 @@ use matrix_sdk_base::crypto::{ pub use matrix_sdk_base::crypto::{LocalTrust, MediaEncryptionInfo, RoomKeyImportResult}; use matrix_sdk_common::instant::Duration; #[cfg(feature = "e2e-encryption")] -use ruma::{ - api::client::config::set_global_account_data, events::GlobalAccountDataEventContent, - OwnedDeviceId, -}; +use ruma::OwnedDeviceId; use ruma::{ api::client::{ backup::add_backup_keys::v3::Response as KeysBackupResponse, @@ -61,7 +58,7 @@ use crate::{ identities::{Device, UserDevices}, verification::{SasVerification, Verification, VerificationRequest}, }, - error::{HttpError, HttpResult, RoomKeyImportError}, + error::{HttpResult, RoomKeyImportError}, room, Client, Error, Result, }; @@ -214,22 +211,6 @@ impl Client { }) } - #[cfg(feature = "e2e-encryption")] - async fn send_account_data( - &self, - content: T, - ) -> Result - where - T: GlobalAccountDataEventContent, - { - let own_user = - self.user_id().ok_or_else(|| Error::from(HttpError::AuthenticationRequired))?; - - let request = set_global_account_data::v3::Request::new(own_user, &content)?; - - Ok(self.send(request, None).await?) - } - #[cfg(feature = "e2e-encryption")] pub(crate) async fn create_dm_room( &self, @@ -268,7 +249,7 @@ impl Client { // TODO We should probably save the fact that we need to send this out // because otherwise we might end up in a state where we have a DM that // isn't marked as one. - self.send_account_data(content).await?; + self.account().set_account_data(content).await?; // If the room is already in our store, fetch it, otherwise wait for a // sync to be done which should put the room into our store.