7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | // Copyright 2020 Damir Jelić |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | // Copyright 2020 The Matrix.org Foundation C.I.C. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | // Copyright 2022 Kévin Commaille |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | // you may not use this file except in compliance with the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | // You may obtain a copy of the License at |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | // Unless required by applicable law or agreed to in writing, software |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | // See the License for the specific language governing permissions and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | // limitations under the License. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | use futures_core::Stream; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | use futures_util::{StreamExt, stream}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | #[cfg(feature = "experimental-element-recent-emojis")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | use itertools::Itertools; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | #[cfg(feature = "experimental-element-recent-emojis")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | use js_int::uint; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | #[cfg(feature = "experimental-element-recent-emojis")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | use matrix_sdk_base::recent_emojis::RecentEmojisContent; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | use matrix_sdk_base::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | SendOutsideWasm, StateStoreDataKey, StateStoreDataValue, SyncOutsideWasm, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | media::{MediaFormat, MediaRequestParameters}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | store::StateStoreExt, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | use mime::Mime; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | #[cfg(feature = "experimental-element-recent-emojis")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | use ruma::api::client::config::set_global_account_data::v3::Request as UpdateGlobalAccountDataRequest; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | use ruma::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | ClientSecret, MxcUri, OwnedMxcUri, OwnedRoomId, OwnedUserId, RoomId, SessionId, UInt, UserId, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | api::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | Metadata, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | client::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | account::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | add_3pid, change_password, deactivate, delete_3pid, get_3pids, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | request_3pid_management_token_via_email, request_3pid_management_token_via_msisdn, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | request_openid_token, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | config::{get_global_account_data, set_global_account_data}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | profile::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | DisplayName, StaticProfileField, delete_profile_field, get_avatar_url, get_profile, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | get_profile_field, set_avatar_url, set_display_name, set_profile_field, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | uiaa::AuthData, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | error::ErrorKind, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | assign, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | events::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | AnyGlobalAccountDataEventContent, GlobalAccountDataEvent, GlobalAccountDataEventContent, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | GlobalAccountDataEventType, StaticEventContent, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | ignored_user_list::{IgnoredUser, IgnoredUserListEventContent}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | media_preview_config::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | InviteAvatars, MediaPreviewConfigEventContent, MediaPreviews, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | UnstableMediaPreviewConfigEventContent, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | push_rules::PushRulesEventContent, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | room::MediaSource, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | profile::{ProfileFieldName, ProfileFieldValue}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | push::Ruleset, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | serde::Raw, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | thirdparty::Medium, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | use serde::Deserialize; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | use tracing::error; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | use crate::{Client, Error, Result, config::RequestConfig}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | /// The maximum number of recent emojis that should be stored and loaded. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | #[cfg(feature = "experimental-element-recent-emojis")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | const MAX_RECENT_EMOJI_COUNT: usize = 100; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | /// A high-level API to manage the client owner's account. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 80 | /// All the methods on this struct send a request to the homeserver. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 81 | #[derive(Debug, Clone)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 82 | pub struct Account { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 83 | /// The underlying HTTP client. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 84 | client: Client, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 85 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 86 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 87 | impl Account { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 88 | /// The maximum number of visited room identifiers to keep in the state |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 89 | /// store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 90 | const VISITED_ROOMS_LIMIT: usize = 20; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 91 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 92 | pub(crate) fn new(client: Client) -> Self { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 93 | Self { client } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 94 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 95 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 96 | /// Get the display name of the account. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 97 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 98 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 99 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 100 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 101 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 102 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 103 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 104 | /// # let homeserver = Url::parse("http://example.com")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 105 | /// let user = "example"; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 106 | /// let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 107 | /// client.matrix_auth().login_username(user, "password").send().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 108 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 109 | /// if let Some(name) = client.account().get_display_name().await? { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 110 | /// println!("Logged in as user '{user}' with display name '{name}'"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 111 | /// } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 112 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 113 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 114 | pub async fn get_display_name(&self) -> Result<Option<String>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 115 | let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 116 | self.fetch_profile_field_of_static::<DisplayName>(user_id.to_owned()).await |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 117 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 118 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 119 | /// Set the display name of the account. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 120 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 121 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 122 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 123 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 124 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 125 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 126 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 127 | /// # let homeserver = Url::parse("http://example.com")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 128 | /// let user = "example"; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 129 | /// let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 130 | /// client.matrix_auth().login_username(user, "password").send().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 131 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 132 | /// client.account().set_display_name(Some("Alice")).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 133 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 134 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 135 | pub async fn set_display_name(&self, name: Option<&str>) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 136 | let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 137 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 138 | // Prefer the endpoint to delete profile fields, if it is supported. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 139 | if name.is_none() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 140 | let versions = self.client.supported_versions().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 141 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 142 | if delete_profile_field::v3::Request::PATH_BUILDER.is_supported(&versions) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 143 | return self.delete_profile_field(ProfileFieldName::DisplayName).await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 144 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 145 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 146 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 147 | // If name is `Some(_)`, this endpoint is the same as `set_profile_field`, but |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 148 | // we still need to use it in case it is `None` and the server doesn't support |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 149 | // the delete endpoint yet. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 150 | #[allow(deprecated)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 151 | let request = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 152 | set_display_name::v3::Request::new(user_id.to_owned(), name.map(ToOwned::to_owned)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 153 | self.client.send(request).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 154 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 155 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 156 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 157 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 158 | /// Request an OpenID token for the current account. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 159 | pub async fn request_openid_token(&self) -> Result<request_openid_token::v3::Response> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 160 | let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 161 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 162 | self.client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 163 | .send(request_openid_token::v3::Request::new(user_id.to_owned())) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 164 | .await |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 165 | .map_err(|error| Error::Http(Box::new(error))) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 166 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 167 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 168 | /// Get the MXC URI of the account's avatar, if set. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 169 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 170 | /// This always sends a request to the server to retrieve this information. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 171 | /// If successful, this fills the cache, and makes it so that |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 172 | /// [`Self::get_cached_avatar_url`] will always return something. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 173 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 174 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 175 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 176 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 177 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 178 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 179 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 180 | /// # let homeserver = Url::parse("http://example.com")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 181 | /// # let user = "example"; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 182 | /// let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 183 | /// client.matrix_auth().login_username(user, "password").send().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 184 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 185 | /// if let Some(url) = client.account().get_avatar_url().await? { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 186 | /// println!("Your avatar's mxc url is {url}"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 187 | /// } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 188 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 189 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 190 | pub async fn get_avatar_url(&self) -> Result<Option<OwnedMxcUri>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 191 | let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 192 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 193 | #[allow(deprecated)] // get_profile_field fails when the response is {"avatar_url":null} 🤷♂️ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 194 | let request = get_avatar_url::v3::Request::new(user_id.to_owned()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 195 | let avatar_url = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 196 | .client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 197 | .send(request) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 198 | .with_request_config(RequestConfig::short_retry().force_auth()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 199 | .await? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 200 | .avatar_url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 201 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 202 | if let Some(url) = avatar_url.clone() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 203 | // If an avatar is found cache it. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 204 | let _ = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 205 | .client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 206 | .state_store() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 207 | .set_kv_data( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 208 | StateStoreDataKey::UserAvatarUrl(user_id), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 209 | StateStoreDataValue::UserAvatarUrl(url), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 210 | ) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 211 | .await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 212 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 213 | // If there is no avatar the user has removed it and we uncache it. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 214 | let _ = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 215 | .client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 216 | .state_store() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 217 | .remove_kv_data(StateStoreDataKey::UserAvatarUrl(user_id)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 218 | .await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 219 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 220 | Ok(avatar_url) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 221 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 222 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 223 | /// Get the URL of the account's avatar, if is stored in cache. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 224 | pub async fn get_cached_avatar_url(&self) -> Result<Option<OwnedMxcUri>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 225 | let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 226 | let data = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 227 | .client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 228 | .state_store() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 229 | .get_kv_data(StateStoreDataKey::UserAvatarUrl(user_id)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 230 | .await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 231 | Ok(data.map(|v| v.into_user_avatar_url().expect("Session data is not a user avatar url"))) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 232 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 233 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 234 | /// Set the MXC URI of the account's avatar. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 235 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 236 | /// The avatar is unset if `url` is `None`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 237 | pub async fn set_avatar_url(&self, url: Option<&MxcUri>) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 238 | let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 239 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 240 | // Prefer the endpoint to delete profile fields, if it is supported. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 241 | if url.is_none() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 242 | let versions = self.client.supported_versions().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 243 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 244 | if delete_profile_field::v3::Request::PATH_BUILDER.is_supported(&versions) { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 245 | return self.delete_profile_field(ProfileFieldName::AvatarUrl).await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 246 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 247 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 248 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 249 | // If url is `Some(_)`, this endpoint is the same as `set_profile_field`, but |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 250 | // we still need to use it in case it is `None` and the server doesn't support |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 251 | // the delete endpoint yet. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 252 | #[allow(deprecated)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 253 | let request = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 254 | set_avatar_url::v3::Request::new(user_id.to_owned(), url.map(ToOwned::to_owned)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 255 | self.client.send(request).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 256 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 257 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 258 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 259 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 260 | /// Get the account's avatar, if set. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 261 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 262 | /// Returns the avatar. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 263 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 264 | /// If a thumbnail is requested no guarantee on the size of the image is |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 265 | /// given. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 266 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 267 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 268 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 269 | /// * `format` - The desired format of the avatar. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 270 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 271 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 272 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 273 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 274 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 275 | /// # use matrix_sdk::ruma::room_id; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 276 | /// # use matrix_sdk::media::MediaFormat; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 277 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 278 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 279 | /// # let homeserver = Url::parse("http://example.com")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 280 | /// # let user = "example"; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 281 | /// let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 282 | /// client.matrix_auth().login_username(user, "password").send().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 283 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 284 | /// if let Some(avatar) = client.account().get_avatar(MediaFormat::File).await? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 285 | /// { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 286 | /// std::fs::write("avatar.png", avatar); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 287 | /// } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 288 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 289 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 290 | pub async fn get_avatar(&self, format: MediaFormat) -> Result<Option<Vec<u8>>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 291 | if let Some(url) = self.get_avatar_url().await? { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 292 | let request = MediaRequestParameters { source: MediaSource::Plain(url), format }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 293 | Ok(Some(self.client.media().get_media_content(&request, true).await?)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 294 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 295 | Ok(None) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 296 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 297 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 298 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 299 | /// Upload and set the account's avatar. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 300 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 301 | /// This will upload the data produced by the reader to the homeserver's |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 302 | /// content repository, and set the user's avatar to the MXC URI for the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 303 | /// uploaded file. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 304 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 305 | /// This is a convenience method for calling [`Media::upload()`], |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 306 | /// followed by [`Account::set_avatar_url()`]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 307 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 308 | /// Returns the MXC URI of the uploaded avatar. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 309 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 310 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 311 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 312 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 313 | /// # use std::fs; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 314 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 315 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 316 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 317 | /// # let homeserver = Url::parse("http://localhost:8080")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 318 | /// # let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 319 | /// let image = fs::read("/home/example/selfie.jpg")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 320 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 321 | /// client.account().upload_avatar(&mime::IMAGE_JPEG, image).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 322 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 323 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 324 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 325 | /// [`Media::upload()`]: crate::Media::upload |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 326 | pub async fn upload_avatar(&self, content_type: &Mime, data: Vec<u8>) -> Result<OwnedMxcUri> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 327 | let upload_response = self.client.media().upload(content_type, data, None).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 328 | self.set_avatar_url(Some(&upload_response.content_uri)).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 329 | Ok(upload_response.content_uri) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 330 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 331 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 332 | /// Get the profile of this account. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 333 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 334 | /// Allows to get all the profile data in a single call. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 335 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 336 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 337 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 338 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 339 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 340 | /// use ruma::api::client::profile::{AvatarUrl, DisplayName}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 341 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 342 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 343 | /// # let homeserver = Url::parse("http://localhost:8080")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 344 | /// # let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 345 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 346 | /// let profile = client.account().fetch_user_profile().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 347 | /// let display_name = profile.get_static::<DisplayName>()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 348 | /// let avatar_url = profile.get_static::<AvatarUrl>()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 349 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 350 | /// println!("You are '{display_name:?}' with avatar '{avatar_url:?}'"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 351 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 352 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 353 | pub async fn fetch_user_profile(&self) -> Result<get_profile::v3::Response> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 354 | let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 355 | self.fetch_user_profile_of(user_id).await |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 356 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 357 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 358 | /// Get the profile for a given user id |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 359 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 360 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 361 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 362 | /// * `user_id` the matrix id this function downloads the profile for |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 363 | pub async fn fetch_user_profile_of( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 364 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 365 | user_id: &UserId, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 366 | ) -> Result<get_profile::v3::Response> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 367 | let request = get_profile::v3::Request::new(user_id.to_owned()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 368 | Ok(self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 369 | .client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 370 | .send(request) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 371 | .with_request_config(RequestConfig::short_retry().force_auth()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 372 | .await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 373 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 374 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 375 | /// Get the given field from the given user's profile. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 376 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 377 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 378 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 379 | /// * `user_id` - The ID of the user to get the profile field of. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 380 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 381 | /// * `field` - The name of the profile field to get. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 382 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 383 | /// # Returns |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 384 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 385 | /// Returns an error if the request fails or if deserialization of the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 386 | /// response fails. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 387 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 388 | /// If the field is not set, the server should respond with an error with an |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 389 | /// [`ErrorCode::NotFound`], but it might also respond with an empty |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 390 | /// response, which would result in `Ok(None)`. Note that this error code |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 391 | /// might also mean that the given user ID doesn't exist. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 392 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 393 | /// [`ErrorCode::NotFound`]: ruma::api::error::ErrorCode::NotFound |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 394 | pub async fn fetch_profile_field_of( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 395 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 396 | user_id: OwnedUserId, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 397 | field: ProfileFieldName, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 398 | ) -> Result<Option<ProfileFieldValue>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 399 | let request = get_profile_field::v3::Request::new(user_id, field); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 400 | let response = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 401 | .client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 402 | .send(request) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 403 | .with_request_config(RequestConfig::short_retry().force_auth()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 404 | .await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 405 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 406 | Ok(response.value) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 407 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 408 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 409 | /// Get the given statically-known field from the given user's profile. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 410 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 411 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 412 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 413 | /// * `user_id` - The ID of the user to get the profile field of. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 414 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 415 | /// # Returns |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 416 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 417 | /// Returns an error if the request fails or if deserialization of the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 418 | /// response fails. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 419 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 420 | /// If the field is not set, the server should respond with an error with an |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 421 | /// [`ErrorCode::NotFound`], but it might also respond with an empty |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 422 | /// response, which would result in `Ok(None)`. Note that this error code |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 423 | /// might also mean that the given user ID doesn't exist. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 424 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 425 | /// [`ErrorCode::NotFound`]: ruma::api::error::ErrorCode::NotFound |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 426 | pub async fn fetch_profile_field_of_static<F>( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 427 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 428 | user_id: OwnedUserId, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 429 | ) -> Result<Option<F::Value>> |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 430 | where |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 431 | F: StaticProfileField |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 432 | + std::fmt::Debug |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 433 | + Clone |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 434 | + SendOutsideWasm |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 435 | + SyncOutsideWasm |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 436 | + 'static, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 437 | F::Value: SendOutsideWasm + SyncOutsideWasm, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 438 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 439 | let request = get_profile_field::v3::Request::new_static::<F>(user_id); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 440 | let response = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 441 | .client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 442 | .send(request) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 443 | .with_request_config(RequestConfig::short_retry().force_auth()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 444 | .await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 445 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 446 | Ok(response.value) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 447 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 448 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 449 | /// Set the given field of our own user's profile. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 450 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 451 | /// [`Client::homeserver_capabilities()`] should be called first to check it |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 452 | /// the field can be set on the homeserver. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 453 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 454 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 455 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 456 | /// * `value` - The value of the profile field to set. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 457 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 458 | /// # Returns |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 459 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 460 | /// Returns an error if the request fails. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 461 | pub async fn set_profile_field(&self, value: ProfileFieldValue) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 462 | let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 463 | let request = set_profile_field::v3::Request::new(user_id.to_owned(), value); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 464 | self.client.send(request).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 465 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 466 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 467 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 468 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 469 | /// Delete the given field of our own user's profile. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 470 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 471 | /// [`Client::homeserver_capabilities()`] should be called first to check it |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 472 | /// the field can be modified on the homeserver. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 473 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 474 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 475 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 476 | /// * `field` - The profile field to delete. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 477 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 478 | /// # Returns |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 479 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 480 | /// Returns an error if the server doesn't support extended profile fields |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 481 | /// of if the request fails in some other way. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 482 | pub async fn delete_profile_field(&self, field: ProfileFieldName) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 483 | let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 484 | let request = delete_profile_field::v3::Request::new(user_id.to_owned(), field); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 485 | self.client.send(request).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 486 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 487 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 488 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 489 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 490 | /// Change the password of the account. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 491 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 492 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 493 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 494 | /// * `new_password` - The new password to set. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 495 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 496 | /// * `auth_data` - This request uses the [User-Interactive Authentication |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 497 | /// API][uiaa]. The first request needs to set this to `None` and will |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 498 | /// always fail with an [`UiaaResponse`]. The response will contain |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 499 | /// information for the interactive auth and the same request needs to be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 500 | /// made but this time with some `auth_data` provided. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 501 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 502 | /// # Returns |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 503 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 504 | /// This method might return an [`ErrorKind::WeakPassword`] error if the new |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 505 | /// password is considered insecure by the homeserver, with details about |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 506 | /// the strength requirements in the error's message. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 507 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 508 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 509 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 510 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 511 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 512 | /// # use matrix_sdk::ruma::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 513 | /// # api::client::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 514 | /// # account::change_password::v3::{Request as ChangePasswordRequest}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 515 | /// # uiaa::{AuthData, Dummy}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 516 | /// # }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 517 | /// # assign, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 518 | /// # }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 519 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 520 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 521 | /// # let homeserver = Url::parse("http://localhost:8080")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 522 | /// # let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 523 | /// client.account().change_password( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 524 | /// "myverysecretpassword", |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 525 | /// Some(AuthData::Dummy(Dummy::new())), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 526 | /// ).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 527 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 528 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 529 | /// [uiaa]: https://spec.matrix.org/v1.2/client-server-api/#user-interactive-authentication-api |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 530 | /// [`UiaaResponse`]: ruma::api::client::uiaa::UiaaResponse |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 531 | /// [`ErrorKind::WeakPassword`]: ruma::api::error::ErrorKind::WeakPassword |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 532 | pub async fn change_password( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 533 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 534 | new_password: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 535 | auth_data: Option<AuthData>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 536 | ) -> Result<change_password::v3::Response> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 537 | let request = assign!(change_password::v3::Request::new(new_password.to_owned()), { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 538 | auth: auth_data, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 539 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 540 | Ok(self.client.send(request).await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 541 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 542 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 543 | /// Deactivate this account definitively. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 544 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 545 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 546 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 547 | /// * `id_server` - The identity server from which to unbind the user’s |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 548 | /// [Third Party Identifiers][3pid]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 549 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 550 | /// * `auth_data` - This request uses the [User-Interactive Authentication |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 551 | /// API][uiaa]. The first request needs to set this to `None` and will |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 552 | /// always fail with an [`UiaaResponse`]. The response will contain |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 553 | /// information for the interactive auth and the same request needs to be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 554 | /// made but this time with some `auth_data` provided. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 555 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 556 | /// * `erase` - Whether the user would like their content to be erased as |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 557 | /// much as possible from the server. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 558 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 559 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 560 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 561 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 562 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 563 | /// # use matrix_sdk::ruma::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 564 | /// # api::client::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 565 | /// # account::change_password::v3::{Request as ChangePasswordRequest}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 566 | /// # uiaa::{AuthData, Dummy}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 567 | /// # }, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 568 | /// # assign, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 569 | /// # }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 570 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 571 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 572 | /// # let homeserver = Url::parse("http://localhost:8080")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 573 | /// # let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 574 | /// # let account = client.account(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 575 | /// let response = account.deactivate(None, None, false).await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 576 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 577 | /// // Proceed with UIAA. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 578 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 579 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 580 | /// [3pid]: https://spec.matrix.org/v1.2/appendices/#3pid-types |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 581 | /// [uiaa]: https://spec.matrix.org/v1.2/client-server-api/#user-interactive-authentication-api |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 582 | /// [`UiaaResponse`]: ruma::api::client::uiaa::UiaaResponse |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 583 | pub async fn deactivate( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 584 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 585 | id_server: Option<&str>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 586 | auth_data: Option<AuthData>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 587 | erase_data: bool, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 588 | ) -> Result<deactivate::v3::Response> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 589 | let request = assign!(deactivate::v3::Request::new(), { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 590 | id_server: id_server.map(ToOwned::to_owned), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 591 | auth: auth_data, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 592 | erase: erase_data, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 593 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 594 | Ok(self.client.send(request).await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 595 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 596 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 597 | /// Get the registered [Third Party Identifiers][3pid] on the homeserver of |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 598 | /// the account. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 599 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 600 | /// These 3PIDs may be used by the homeserver to authenticate the user |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 601 | /// during sensitive operations. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 602 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 603 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 604 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 605 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 606 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 607 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 608 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 609 | /// # let homeserver = Url::parse("http://localhost:8080")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 610 | /// # let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 611 | /// let threepids = client.account().get_3pids().await?.threepids; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 612 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 613 | /// for threepid in threepids { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 614 | /// println!( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 615 | /// "Found 3PID '{}' of type '{}'", |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 616 | /// threepid.address, threepid.medium |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 617 | /// ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 618 | /// } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 619 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 620 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 621 | /// [3pid]: https://spec.matrix.org/v1.2/appendices/#3pid-types |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 622 | pub async fn get_3pids(&self) -> Result<get_3pids::v3::Response> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 623 | let request = get_3pids::v3::Request::new(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 624 | Ok(self.client.send(request).await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 625 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 626 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 627 | /// Request a token to validate an email address as a [Third Party |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 628 | /// Identifier][3pid]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 629 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 630 | /// This is the first step in registering an email address as 3PID. Next, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 631 | /// call [`Account::add_3pid()`] with the same `client_secret` and the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 632 | /// returned `sid`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 633 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 634 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 635 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 636 | /// * `client_secret` - A client-generated secret string used to protect |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 637 | /// this session. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 638 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 639 | /// * `email` - The email address to validate. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 640 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 641 | /// * `send_attempt` - The attempt number. This number needs to be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 642 | /// incremented if you want to request another token for the same |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 643 | /// validation. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 644 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 645 | /// # Returns |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 646 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 647 | /// * `sid` - The session ID to be used in following requests for this 3PID. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 648 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 649 | /// * `submit_url` - If present, the user will submit the token to the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 650 | /// client, that must send it to this URL. If not, the client will not be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 651 | /// involved in the token submission. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 652 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 653 | /// This method might return an [`ErrorKind::ThreepidInUse`] error if the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 654 | /// email address is already registered for this account or another, or an |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 655 | /// [`ErrorKind::ThreepidDenied`] error if it is denied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 656 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 657 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 658 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 659 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 660 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 661 | /// # use matrix_sdk::ruma::{ClientSecret, uint}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 662 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 663 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 664 | /// # let homeserver = Url::parse("http://localhost:8080")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 665 | /// # let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 666 | /// # let account = client.account(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 667 | /// # let secret = ClientSecret::parse("secret")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 668 | /// let token_response = account |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 669 | /// .request_3pid_email_token(&secret, "john@matrix.org", uint!(0)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 670 | /// .await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 671 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 672 | /// // Wait for the user to confirm that the token was submitted or prompt |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 673 | /// // the user for the token and send it to submit_url. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 674 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 675 | /// let uiaa_response = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 676 | /// account.add_3pid(&secret, &token_response.sid, None).await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 677 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 678 | /// // Proceed with UIAA. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 679 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 680 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 681 | /// [3pid]: https://spec.matrix.org/v1.2/appendices/#3pid-types |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 682 | /// [`ErrorKind::ThreepidInUse`]: ruma::api::error::ErrorKind::ThreepidInUse |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 683 | /// [`ErrorKind::ThreepidDenied`]: ruma::api::error::ErrorKind::ThreepidDenied |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 684 | pub async fn request_3pid_email_token( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 685 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 686 | client_secret: &ClientSecret, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 687 | email: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 688 | send_attempt: UInt, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 689 | ) -> Result<request_3pid_management_token_via_email::v3::Response> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 690 | let request = request_3pid_management_token_via_email::v3::Request::new( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 691 | client_secret.to_owned(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 692 | email.to_owned(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 693 | send_attempt, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 694 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 695 | Ok(self.client.send(request).await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 696 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 697 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 698 | /// Request a token to validate a phone number as a [Third Party |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 699 | /// Identifier][3pid]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 700 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 701 | /// This is the first step in registering a phone number as 3PID. Next, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 702 | /// call [`Account::add_3pid()`] with the same `client_secret` and the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 703 | /// returned `sid`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 704 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 705 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 706 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 707 | /// * `client_secret` - A client-generated secret string used to protect |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 708 | /// this session. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 709 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 710 | /// * `country` - The two-letter uppercase ISO-3166-1 alpha-2 country code |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 711 | /// that the number in phone_number should be parsed as if it were dialled |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 712 | /// from. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 713 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 714 | /// * `phone_number` - The phone number to validate. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 715 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 716 | /// * `send_attempt` - The attempt number. This number needs to be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 717 | /// incremented if you want to request another token for the same |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 718 | /// validation. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 719 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 720 | /// # Returns |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 721 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 722 | /// * `sid` - The session ID to be used in following requests for this 3PID. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 723 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 724 | /// * `submit_url` - If present, the user will submit the token to the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 725 | /// client, that must send it to this URL. If not, the client will not be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 726 | /// involved in the token submission. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 727 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 728 | /// This method might return an [`ErrorKind::ThreepidInUse`] error if the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 729 | /// phone number is already registered for this account or another, or an |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 730 | /// [`ErrorKind::ThreepidDenied`] error if it is denied. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 731 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 732 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 733 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 734 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 735 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 736 | /// # use matrix_sdk::ruma::{ClientSecret, uint}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 737 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 738 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 739 | /// # let homeserver = Url::parse("http://localhost:8080")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 740 | /// # let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 741 | /// # let account = client.account(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 742 | /// # let secret = ClientSecret::parse("secret")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 743 | /// let token_response = account |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 744 | /// .request_3pid_msisdn_token(&secret, "FR", "0123456789", uint!(0)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 745 | /// .await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 746 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 747 | /// // Wait for the user to confirm that the token was submitted or prompt |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 748 | /// // the user for the token and send it to submit_url. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 749 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 750 | /// let uiaa_response = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 751 | /// account.add_3pid(&secret, &token_response.sid, None).await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 752 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 753 | /// // Proceed with UIAA. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 754 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 755 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 756 | /// [3pid]: https://spec.matrix.org/v1.2/appendices/#3pid-types |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 757 | /// [`ErrorKind::ThreepidInUse`]: ruma::api::error::ErrorKind::ThreepidInUse |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 758 | /// [`ErrorKind::ThreepidDenied`]: ruma::api::error::ErrorKind::ThreepidDenied |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 759 | pub async fn request_3pid_msisdn_token( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 760 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 761 | client_secret: &ClientSecret, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 762 | country: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 763 | phone_number: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 764 | send_attempt: UInt, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 765 | ) -> Result<request_3pid_management_token_via_msisdn::v3::Response> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 766 | let request = request_3pid_management_token_via_msisdn::v3::Request::new( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 767 | client_secret.to_owned(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 768 | country.to_owned(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 769 | phone_number.to_owned(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 770 | send_attempt, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 771 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 772 | Ok(self.client.send(request).await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 773 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 774 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 775 | /// Add a [Third Party Identifier][3pid] on the homeserver for this |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 776 | /// account. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 777 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 778 | /// This 3PID may be used by the homeserver to authenticate the user |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 779 | /// during sensitive operations. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 780 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 781 | /// This method should be called after |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 782 | /// [`Account::request_3pid_email_token()`] or |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 783 | /// [`Account::request_3pid_msisdn_token()`] to complete the 3PID |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 784 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 785 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 786 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 787 | /// * `client_secret` - The same client secret used in |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 788 | /// [`Account::request_3pid_email_token()`] or |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 789 | /// [`Account::request_3pid_msisdn_token()`]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 790 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 791 | /// * `sid` - The session ID returned in |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 792 | /// [`Account::request_3pid_email_token()`] or |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 793 | /// [`Account::request_3pid_msisdn_token()`]. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 794 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 795 | /// * `auth_data` - This request uses the [User-Interactive Authentication |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 796 | /// API][uiaa]. The first request needs to set this to `None` and will |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 797 | /// always fail with an [`UiaaResponse`]. The response will contain |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 798 | /// information for the interactive auth and the same request needs to be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 799 | /// made but this time with some `auth_data` provided. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 800 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 801 | /// [3pid]: https://spec.matrix.org/v1.2/appendices/#3pid-types |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 802 | /// [uiaa]: https://spec.matrix.org/v1.2/client-server-api/#user-interactive-authentication-api |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 803 | /// [`UiaaResponse`]: ruma::api::client::uiaa::UiaaResponse |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 804 | pub async fn add_3pid( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 805 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 806 | client_secret: &ClientSecret, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 807 | sid: &SessionId, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 808 | auth_data: Option<AuthData>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 809 | ) -> Result<add_3pid::v3::Response> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 810 | #[rustfmt::skip] // rustfmt wants to merge the next two lines |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 811 | let request = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 812 | assign!(add_3pid::v3::Request::new(client_secret.to_owned(), sid.to_owned()), { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 813 | auth: auth_data |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 814 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 815 | Ok(self.client.send(request).await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 816 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 817 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 818 | /// Delete a [Third Party Identifier][3pid] from the homeserver for this |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 819 | /// account. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 820 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 821 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 822 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 823 | /// * `address` - The 3PID being removed. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 824 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 825 | /// * `medium` - The type of the 3PID. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 826 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 827 | /// * `id_server` - The identity server to unbind from. If not provided, the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 828 | /// homeserver should unbind the 3PID from the identity server it was |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 829 | /// bound to previously. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 830 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 831 | /// # Returns |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 832 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 833 | /// * [`ThirdPartyIdRemovalStatus::Success`] if the 3PID was also unbound |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 834 | /// from the identity server. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 835 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 836 | /// * [`ThirdPartyIdRemovalStatus::NoSupport`] if the 3PID was not unbound |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 837 | /// from the identity server. This can also mean that the 3PID was not |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 838 | /// bound to an identity server in the first place. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 839 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 840 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 841 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 842 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 843 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 844 | /// # use matrix_sdk::ruma::thirdparty::Medium; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 845 | /// # use matrix_sdk::ruma::api::client::account::ThirdPartyIdRemovalStatus; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 846 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 847 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 848 | /// # let homeserver = Url::parse("http://localhost:8080")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 849 | /// # let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 850 | /// # let account = client.account(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 851 | /// match account |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 852 | /// .delete_3pid("paul@matrix.org", Medium::Email, None) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 853 | /// .await? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 854 | /// .id_server_unbind_result |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 855 | /// { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 856 | /// ThirdPartyIdRemovalStatus::Success => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 857 | /// println!("3PID unbound from the Identity Server"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 858 | /// } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 859 | /// _ => println!("Could not unbind 3PID from the Identity Server"), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 860 | /// } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 861 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 862 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 863 | /// [3pid]: https://spec.matrix.org/v1.2/appendices/#3pid-types |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 864 | /// [`ThirdPartyIdRemovalStatus::Success`]: ruma::api::client::account::ThirdPartyIdRemovalStatus::Success |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 865 | /// [`ThirdPartyIdRemovalStatus::NoSupport`]: ruma::api::client::account::ThirdPartyIdRemovalStatus::NoSupport |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 866 | pub async fn delete_3pid( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 867 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 868 | address: &str, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 869 | medium: Medium, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 870 | id_server: Option<&str>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 871 | ) -> Result<delete_3pid::v3::Response> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 872 | let request = assign!(delete_3pid::v3::Request::new(medium, address.to_owned()), { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 873 | id_server: id_server.map(ToOwned::to_owned), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 874 | }); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 875 | Ok(self.client.send(request).await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 876 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 877 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 878 | /// Get the content of an account data event of statically-known type, from |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 879 | /// storage. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 880 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 881 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 882 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 883 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 884 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 885 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 886 | /// # let client = Client::new("http://localhost:8080".parse()?).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 887 | /// # let account = client.account(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 888 | /// use matrix_sdk::ruma::events::ignored_user_list::IgnoredUserListEventContent; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 889 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 890 | /// let maybe_content = account.account_data::<IgnoredUserListEventContent>().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 891 | /// if let Some(raw_content) = maybe_content { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 892 | /// let content = raw_content.deserialize()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 893 | /// println!("Ignored users:"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 894 | /// for user_id in content.ignored_users.keys() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 895 | /// println!("- {user_id}"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 896 | /// } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 897 | /// } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 898 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 899 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 900 | pub async fn account_data<C>(&self) -> Result<Option<Raw<C>>> |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 901 | where |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 902 | C: GlobalAccountDataEventContent + StaticEventContent<IsPrefix = ruma::events::False>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 903 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 904 | get_raw_content(self.client.state_store().get_account_data_event_static::<C>().await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 905 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 906 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 907 | /// Get the content of an account data event of a given type, from storage. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 908 | pub async fn account_data_raw( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 909 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 910 | event_type: GlobalAccountDataEventType, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 911 | ) -> Result<Option<Raw<AnyGlobalAccountDataEventContent>>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 912 | get_raw_content(self.client.state_store().get_account_data_event(event_type).await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 913 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 914 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 915 | /// Fetch a global account data event from the server. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 916 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 917 | /// The content from the response will not be persisted in the store. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 918 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 919 | /// Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 920 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 921 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 922 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 923 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 924 | /// # let client = Client::new("http://localhost:8080".parse()?).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 925 | /// # let account = client.account(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 926 | /// use matrix_sdk::ruma::events::{ignored_user_list::IgnoredUserListEventContent, GlobalAccountDataEventType}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 927 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 928 | /// if let Some(raw_content) = account.fetch_account_data(GlobalAccountDataEventType::IgnoredUserList).await? { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 929 | /// let content = raw_content.deserialize_as_unchecked::<IgnoredUserListEventContent>()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 930 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 931 | /// println!("Ignored users:"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 932 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 933 | /// for user_id in content.ignored_users.keys() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 934 | /// println!("- {user_id}"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 935 | /// } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 936 | /// } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 937 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 938 | pub async fn fetch_account_data( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 939 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 940 | event_type: GlobalAccountDataEventType, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 941 | ) -> Result<Option<Raw<AnyGlobalAccountDataEventContent>>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 942 | let own_user = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 943 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 944 | let request = get_global_account_data::v3::Request::new(own_user.to_owned(), event_type); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 945 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 946 | match self.client.send(request).await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 947 | Ok(r) => Ok(Some(r.account_data)), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 948 | Err(e) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 949 | if let Some(kind) = e.client_api_error_kind() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 950 | if kind == &ErrorKind::NotFound { Ok(None) } else { Err(e.into()) } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 951 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 952 | Err(e.into()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 953 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 954 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 955 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 956 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 957 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 958 | /// Fetch an account data event of statically-known type from the server. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 959 | pub async fn fetch_account_data_static<C>(&self) -> Result<Option<Raw<C>>> |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 960 | where |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 961 | C: GlobalAccountDataEventContent + StaticEventContent<IsPrefix = ruma::events::False>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 962 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 963 | Ok(self.fetch_account_data(C::TYPE.into()).await?.map(Raw::cast_unchecked)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 964 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 965 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 966 | /// Set the given account data event. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 967 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 968 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 969 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 970 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 971 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 972 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 973 | /// # let client = Client::new("http://localhost:8080".parse()?).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 974 | /// # let account = client.account(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 975 | /// use matrix_sdk::ruma::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 976 | /// events::ignored_user_list::{IgnoredUser, IgnoredUserListEventContent}, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 977 | /// user_id, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 978 | /// }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 979 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 980 | /// let mut content = account |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 981 | /// .account_data::<IgnoredUserListEventContent>() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 982 | /// .await? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 983 | /// .map(|c| c.deserialize()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 984 | /// .transpose()? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 985 | /// .unwrap_or_default(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 986 | /// content |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 987 | /// .ignored_users |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 988 | /// .insert(user_id!("@foo:bar.com").to_owned(), IgnoredUser::new()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 989 | /// account.set_account_data(content).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 990 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 991 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 992 | pub async fn set_account_data<T>( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 993 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 994 | content: T, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 995 | ) -> Result<set_global_account_data::v3::Response> |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 996 | where |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 997 | T: GlobalAccountDataEventContent, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 998 | { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 999 | let own_user = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1000 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1001 | let request = set_global_account_data::v3::Request::new(own_user.to_owned(), &content)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1002 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1003 | Ok(self.client.send(request).await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1004 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1005 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1006 | /// Set the given raw account data event. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1007 | pub async fn set_account_data_raw( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1008 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1009 | event_type: GlobalAccountDataEventType, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1010 | content: Raw<AnyGlobalAccountDataEventContent>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1011 | ) -> Result<set_global_account_data::v3::Response> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1012 | let own_user = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1013 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1014 | let request = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1015 | set_global_account_data::v3::Request::new_raw(own_user.to_owned(), event_type, content); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1016 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1017 | Ok(self.client.send(request).await?) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1018 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1019 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1020 | /// Marks the room identified by `room_id` as a "direct chat" with each |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1021 | /// user in `user_ids`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1022 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1023 | /// # Arguments |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1024 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1025 | /// * `room_id` - The room ID of the direct message room. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1026 | /// * `user_ids` - The user IDs to be associated with this direct message |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1027 | /// room. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1028 | pub async fn mark_as_dm(&self, room_id: &RoomId, user_ids: &[OwnedUserId]) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1029 | use ruma::events::direct::DirectEventContent; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1030 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1031 | // This function does a read/update/store of an account data event stored on the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1032 | // homeserver. We first fetch the existing account data event, the event |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1033 | // contains a map which gets updated by this method, finally we upload the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1034 | // modified event. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1035 | // |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1036 | // To prevent multiple calls to this method trying to update the map of DMs same |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1037 | // time, and thus trampling on each other we introduce a lock which acts |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1038 | // as a semaphore. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1039 | let _guard = self.client.locks().mark_as_dm_lock.lock().await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1040 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1041 | // Now we need to mark the room as a DM for ourselves, we fetch the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1042 | // existing `m.direct` event and append the room to the list of DMs we |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1043 | // have with this user. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1044 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1045 | // We are fetching the content from the server because we currently can't rely |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1046 | // on `/sync` giving us the correct data in a timely manner. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1047 | let raw_content = self.fetch_account_data_static::<DirectEventContent>().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1048 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1049 | let mut content = if let Some(raw_content) = raw_content { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1050 | // Log the error and pass it upwards if we fail to deserialize the m.direct |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1051 | // event. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1052 | raw_content.deserialize().map_err(|err| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1053 | error!("unable to deserialize m.direct event content; aborting request to mark {room_id} as dm: {err}"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1054 | err |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1055 | })? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1056 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1057 | // If there was no m.direct event server-side, create a default one. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1058 | Default::default() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1059 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1060 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1061 | for user_id in user_ids { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1062 | content.entry(user_id.into()).or_default().push(room_id.to_owned()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1063 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1064 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1065 | // TODO: We should probably save the fact that we need to send this out |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1066 | // because otherwise we might end up in a state where we have a DM that |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1067 | // isn't marked as one. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1068 | self.set_account_data(content).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1069 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1070 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1071 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1072 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1073 | /// Adds the given user ID to the account's ignore list. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1074 | pub async fn ignore_user(&self, user_id: &UserId) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1075 | let own_user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1076 | if user_id == own_user_id { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1077 | return Err(Error::CantIgnoreLoggedInUser); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1078 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1079 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1080 | let mut ignored_user_list = self.get_ignored_user_list_event_content().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1081 | ignored_user_list.ignored_users.insert(user_id.to_owned(), IgnoredUser::new()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1082 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1083 | self.set_account_data(ignored_user_list).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1084 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1085 | // In theory, we should also clear some caches here, because they may include |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1086 | // events sent by the ignored user. In practice, we expect callers to |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1087 | // take care of this, or subsystems to listen to user list changes and |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1088 | // clear caches accordingly. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1089 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1090 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1091 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1092 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1093 | /// Removes the given user ID from the account's ignore list. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1094 | pub async fn unignore_user(&self, user_id: &UserId) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1095 | let mut ignored_user_list = self.get_ignored_user_list_event_content().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1096 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1097 | // Only update account data if the user was ignored in the first place. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1098 | if ignored_user_list.ignored_users.remove(user_id).is_some() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1099 | self.set_account_data(ignored_user_list).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1100 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1101 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1102 | // See comment in `ignore_user`. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1103 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1104 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1105 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1106 | async fn get_ignored_user_list_event_content(&self) -> Result<IgnoredUserListEventContent> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1107 | let ignored_user_list = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1108 | .account_data::<IgnoredUserListEventContent>() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1109 | .await? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1110 | .map(|c| c.deserialize()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1111 | .transpose()? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1112 | .unwrap_or_default(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1113 | Ok(ignored_user_list) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1114 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1115 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1116 | /// Get the current push rules from storage. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1117 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1118 | /// If no push rules event was found, or it fails to deserialize, a ruleset |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1119 | /// with the server-default push rules is returned. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1120 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1121 | /// Panics if called when the client is not logged in. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1122 | pub async fn push_rules(&self) -> Result<Ruleset> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1123 | Ok(self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1124 | .account_data::<PushRulesEventContent>() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1125 | .await? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1126 | .and_then(|r| match r.deserialize() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1127 | Ok(r) => Some(r.global), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1128 | Err(e) => { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1129 | error!("Push rules event failed to deserialize: {e}"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1130 | None |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1131 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1132 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1133 | .unwrap_or_else(|| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1134 | Ruleset::server_default( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1135 | self.client.user_id().expect("The client should be logged in"), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1136 | ) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1137 | })) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1138 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1139 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1140 | /// Retrieves the user's recently visited room list |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1141 | pub async fn get_recently_visited_rooms(&self) -> Result<Vec<OwnedRoomId>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1142 | let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1143 | let data = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1144 | .client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1145 | .state_store() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1146 | .get_kv_data(StateStoreDataKey::RecentlyVisitedRooms(user_id)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1147 | .await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1148 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1149 | Ok(data |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1150 | .map(|v| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1151 | v.into_recently_visited_rooms() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1152 | .expect("Session data is not a list of recently visited rooms") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1153 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1154 | .unwrap_or_default()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1155 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1156 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1157 | /// Moves/inserts the given room to the front of the recently visited list |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1158 | pub async fn track_recently_visited_room(&self, room_id: OwnedRoomId) -> Result<(), Error> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1159 | let user_id = self.client.user_id().ok_or(Error::AuthenticationRequired)?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1160 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1161 | // Get the previously stored recently visited rooms |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1162 | let mut recently_visited_rooms = self.get_recently_visited_rooms().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1163 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1164 | // Remove all other occurrences of the new room_id |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1165 | recently_visited_rooms.retain(|r| r != &room_id); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1166 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1167 | // And insert it as the most recent |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1168 | recently_visited_rooms.insert(0, room_id); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1169 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1170 | // Cap the whole list to the VISITED_ROOMS_LIMIT |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1171 | recently_visited_rooms.truncate(Self::VISITED_ROOMS_LIMIT); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1172 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1173 | let data = StateStoreDataValue::RecentlyVisitedRooms(recently_visited_rooms); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1174 | self.client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1175 | .state_store() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1176 | .set_kv_data(StateStoreDataKey::RecentlyVisitedRooms(user_id), data) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1177 | .await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1178 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1179 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1180 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1181 | /// Observes the media preview configuration. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1182 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1183 | /// This value is linked to the [MSC 4278](https://github.com/matrix-org/matrix-spec-proposals/pull/4278) which is still in an unstable state. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1184 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1185 | /// This will return the initial value of the configuration and a stream |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1186 | /// that will yield new values as they are received. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1187 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1188 | /// The initial value is the one that was stored in the account data |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1189 | /// when the client was started. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1190 | /// and the following code is using a temporary solution until we know which |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1191 | /// Matrix version will support the stable type. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1192 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1193 | /// # Examples |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1194 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1195 | /// ```no_run |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1196 | /// # use futures_util::{pin_mut, StreamExt}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1197 | /// # use matrix_sdk::Client; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1198 | /// # use matrix_sdk::ruma::events::media_preview_config::MediaPreviews; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1199 | /// # use url::Url; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1200 | /// # async { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1201 | /// # let homeserver = Url::parse("http://localhost:8080")?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1202 | /// # let client = Client::new(homeserver).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1203 | /// let account = client.account(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1204 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1205 | /// let (initial_config, config_stream) = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1206 | /// account.observe_media_preview_config().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1207 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1208 | /// println!("Initial media preview config: {:?}", initial_config); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1209 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1210 | /// pin_mut!(config_stream); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1211 | /// while let Some(new_config) = config_stream.next().await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1212 | /// println!("Updated media preview config: {:?}", new_config); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1213 | /// } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1214 | /// # anyhow::Ok(()) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1215 | /// ``` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1216 | pub async fn observe_media_preview_config( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1217 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1218 | ) -> Result< |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1219 | ( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1220 | Option<MediaPreviewConfigEventContent>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1221 | impl Stream<Item = MediaPreviewConfigEventContent> + use<>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1222 | ), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1223 | Error, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1224 | > { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1225 | // We need to create two observers, one for the stable event and one for the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1226 | // unstable and combine them into a single stream. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1227 | let first_observer = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1228 | .client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1229 | .observe_events::<GlobalAccountDataEvent<MediaPreviewConfigEventContent>, ()>(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1230 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1231 | let stream = first_observer.subscribe().map(|event| event.0.content); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1232 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1233 | let second_observer = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1234 | .client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1235 | .observe_events::<GlobalAccountDataEvent<UnstableMediaPreviewConfigEventContent>, ()>(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1236 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1237 | let second_stream = second_observer.subscribe().map(|event| event.0.content.0); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1238 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1239 | let mut combined_stream = stream::select(stream, second_stream); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1240 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1241 | let result_stream = async_stream::stream! { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1242 | // The observers need to be alive for the individual streams to be alive, so let's now |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1243 | // create a stream that takes ownership of them. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1244 | let _first_observer = first_observer; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1245 | let _second_observer = second_observer; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1246 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1247 | while let Some(item) = combined_stream.next().await { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1248 | yield item |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1249 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1250 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1251 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1252 | // We need to get the initial value of the media preview config event |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1253 | // we do this after creating the observers to make sure that we don't |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1254 | // create a race condition |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1255 | let initial_value = self.get_media_preview_config_event_content().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1256 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1257 | Ok((initial_value, result_stream)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1258 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1259 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1260 | /// Fetch the media preview configuration event content from the server. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1261 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1262 | /// Will check first for the stable event and then for the unstable one. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1263 | pub async fn fetch_media_preview_config_event_content( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1264 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1265 | ) -> Result<Option<MediaPreviewConfigEventContent>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1266 | // First we check if there is a value in the stable event |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1267 | let media_preview_config = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1268 | self.fetch_account_data_static::<MediaPreviewConfigEventContent>().await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1269 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1270 | let media_preview_config = if let Some(media_preview_config) = media_preview_config { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1271 | Some(media_preview_config) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1272 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1273 | // If there is no value in the stable event, we check the unstable |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1274 | self.fetch_account_data_static::<UnstableMediaPreviewConfigEventContent>() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1275 | .await? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1276 | .map(Raw::cast) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1277 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1278 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1279 | // We deserialize the content of the event, if is not found we return the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1280 | // default |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1281 | let media_preview_config = media_preview_config.and_then(|value| value.deserialize().ok()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1282 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1283 | Ok(media_preview_config) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1284 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1285 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1286 | /// Get the media preview configuration event content stored in the cache. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1287 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1288 | /// Will check first for the stable event and then for the unstable one. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1289 | pub async fn get_media_preview_config_event_content( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1290 | &self, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1291 | ) -> Result<Option<MediaPreviewConfigEventContent>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1292 | let media_preview_config = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1293 | .account_data::<MediaPreviewConfigEventContent>() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1294 | .await? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1295 | .and_then(|r| r.deserialize().ok()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1296 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1297 | if let Some(media_preview_config) = media_preview_config { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1298 | Ok(Some(media_preview_config)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1299 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1300 | Ok(self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1301 | .account_data::<UnstableMediaPreviewConfigEventContent>() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1302 | .await? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1303 | .and_then(|r| r.deserialize().ok()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1304 | .map(Into::into)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1305 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1306 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1307 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1308 | /// Set the media previews display policy in the timeline. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1309 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1310 | /// This will always use the unstable event until we know which Matrix |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1311 | /// version will support it. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1312 | pub async fn set_media_previews_display_policy(&self, policy: MediaPreviews) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1313 | let mut media_preview_config = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1314 | self.fetch_media_preview_config_event_content().await?.unwrap_or_default(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1315 | media_preview_config.media_previews = Some(policy); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1316 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1317 | // Updating the unstable account data |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1318 | let unstable_media_preview_config = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1319 | UnstableMediaPreviewConfigEventContent::from(media_preview_config); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1320 | self.set_account_data(unstable_media_preview_config).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1321 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1322 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1323 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1324 | /// Set the display policy for avatars in invite requests. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1325 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1326 | /// This will always use the unstable event until we know which matrix |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1327 | /// version will support it. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1328 | pub async fn set_invite_avatars_display_policy(&self, policy: InviteAvatars) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1329 | let mut media_preview_config = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1330 | self.fetch_media_preview_config_event_content().await?.unwrap_or_default(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1331 | media_preview_config.invite_avatars = Some(policy); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1332 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1333 | // Updating the unstable account data |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1334 | let unstable_media_preview_config = |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1335 | UnstableMediaPreviewConfigEventContent::from(media_preview_config); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1336 | self.set_account_data(unstable_media_preview_config).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1337 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1338 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1339 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1340 | /// Adds a recently used emoji to the list and uploads the updated |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1341 | /// `io.element.recent_emoji` content to the global account data. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1342 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1343 | /// Before updating the data, it'll fetch it from the homeserver, to make |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1344 | /// sure the updated values are always used. However, note this could still |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1345 | /// result in a race condition if it's used concurrently. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1346 | #[cfg(feature = "experimental-element-recent-emojis")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1347 | pub async fn add_recent_emoji(&self, emoji: &str) -> Result<()> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1348 | let Some(user_id) = self.client.user_id() else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1349 | return Err(Error::AuthenticationRequired); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1350 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1351 | let mut recent_emojis = self.get_recent_emojis(true).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1352 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1353 | let index = recent_emojis.iter().position(|(unicode, _)| unicode == emoji); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1354 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1355 | // Truncate to the max allowed size, which will remove any emojis that |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1356 | // haven't been used in a very long time. This will also ease the pressure on |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1357 | // `remove` and `insert` shifting lots of elements in the list |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1358 | recent_emojis.truncate(MAX_RECENT_EMOJI_COUNT); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1359 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1360 | // Remove the emoji from the list if it was present and get it's `count` value |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1361 | let count = if let Some(index) = index { recent_emojis.remove(index).1 } else { uint!(0) }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1362 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1363 | // Insert the emoji with the updated count at the start of the list, so it's |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1364 | // considered the most recently used emoji |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1365 | recent_emojis.insert(0, (emoji.to_owned(), count + uint!(1))); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1366 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1367 | // If the item was a new one, the list will now be `MAX_RECENT_EMOJI_COUNT` + 1, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1368 | // so truncate it again (this is a no-op if it already has the right size) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1369 | recent_emojis.truncate(MAX_RECENT_EMOJI_COUNT); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1370 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1371 | let request = UpdateGlobalAccountDataRequest::new( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1372 | user_id.to_owned(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1373 | &RecentEmojisContent::new(recent_emojis), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1374 | )?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1375 | let _ = self.client.send(request).await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1376 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1377 | Ok(()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1378 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1379 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1380 | /// Gets the list of recently used emojis from the `io.element.recent_emoji` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1381 | /// global account data. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1382 | /// |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1383 | /// If the `refresh` param is `true`, the data will be fetched from the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1384 | /// homeserver instead of the local storage. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1385 | #[cfg(feature = "experimental-element-recent-emojis")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1386 | pub async fn get_recent_emojis(&self, refresh: bool) -> Result<Vec<(String, UInt)>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1387 | let content = if refresh { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1388 | let Some(user_id) = self.client.user_id() else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1389 | return Err(Error::AuthenticationRequired); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1390 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1391 | let event_type = RecentEmojisContent::default().event_type(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1392 | let response = self |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1393 | .client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1394 | .send(get_global_account_data::v3::Request::new( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1395 | user_id.to_owned(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1396 | event_type.clone(), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1397 | )) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1398 | .await?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1399 | let content = response.account_data.cast_unchecked().deserialize()?; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1400 | Some(content) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1401 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1402 | self.client |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1403 | .state_store() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1404 | .get_account_data_event_static::<RecentEmojisContent>() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1405 | .await? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1406 | .map(|raw| raw.deserialize().map(|event| event.content)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1407 | .transpose()? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1408 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1409 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1410 | if let Some(content) = content { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1411 | // Sort by count, descending. For items with the same count, since they were |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1412 | // previously ordered by recency in the list, more recent emojis will be |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1413 | // returned first. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1414 | let sorted_emojis = content |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1415 | .recent_emoji |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1416 | .into_iter() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1417 | // Items with higher counts should be first |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1418 | .sorted_by(|(_, count_a), (_, count_b)| count_b.cmp(count_a)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1419 | // Make sure we take only up to MAX_RECENT_EMOJI_COUNT |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1420 | .take(MAX_RECENT_EMOJI_COUNT) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1421 | .collect(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1422 | Ok(sorted_emojis) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1423 | } else { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1424 | Ok(Vec::new()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1425 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1426 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1427 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1428 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1429 | fn get_raw_content<Ev, C>(raw: Option<Raw<Ev>>) -> Result<Option<Raw<C>>> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1430 | #[derive(Deserialize)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1431 | #[serde(bound = "C: Sized")] // Replace default Deserialize bound |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1432 | struct GetRawContent<C> { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1433 | content: Raw<C>, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1434 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1435 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1436 | Ok(raw |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1437 | .map(|event| event.deserialize_as_unchecked::<GetRawContent<C>>()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1438 | .transpose()? |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1439 | .map(|get_raw| get_raw.content)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1440 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1441 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1442 | #[cfg(test)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1443 | mod tests { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1444 | use assert_matches::assert_matches; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1445 | use matrix_sdk_test::async_test; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1446 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1447 | use crate::{Error, test_utils::client::MockClientBuilder}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1448 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1449 | #[async_test] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1450 | async fn test_dont_ignore_oneself() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1451 | let client = MockClientBuilder::new(None).build().await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1452 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1453 | // It's forbidden to ignore the logged-in user. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1454 | assert_matches!( |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1455 | client.account().ignore_user(client.user_id().unwrap()).await, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1456 | Err(Error::CantIgnoreLoggedInUser) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1457 | ); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1458 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1459 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1460 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1461 | #[cfg(test)] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1462 | #[cfg(feature = "experimental-element-recent-emojis")] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1463 | mod test_recent_emojis { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1464 | use js_int::{UInt, uint}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1465 | use matrix_sdk_base::recent_emojis::RecentEmojisContent; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1466 | use matrix_sdk_test::{async_test, event_factory::EventFactory}; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1467 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1468 | use crate::{ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1469 | account::MAX_RECENT_EMOJI_COUNT, config::SyncSettings, test_utils::mocks::MatrixMockServer, |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1470 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1471 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1472 | #[async_test] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1473 | async fn test_recent_emojis() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1474 | let server = MatrixMockServer::new().await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1475 | let client = server.client_builder().build().await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1476 | let user_id = client.user_id().expect("session_id"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1477 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1478 | server |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1479 | .mock_add_recent_emojis() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1480 | .ok(user_id) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1481 | .named("Update recent emojis global account data") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1482 | .mock_once() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1483 | .mount() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1484 | .await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1485 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1486 | let recent_emojis = client.account().get_recent_emojis(false).await.expect("recent emojis"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1487 | assert!(recent_emojis.is_empty()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1488 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1489 | let emoji_list = vec![ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1490 | (":/".to_owned(), uint!(1)), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1491 | (":)".to_owned(), uint!(12)), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1492 | (":D".to_owned(), uint!(12)), |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1493 | ]; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1494 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1495 | server |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1496 | .mock_get_recent_emojis() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1497 | .ok(user_id, emoji_list.clone()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1498 | .named("Fetch recent emojis") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1499 | .mock_once() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1500 | .mount() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1501 | .await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1502 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1503 | client.account().add_recent_emoji(":)").await.expect("adding emoji"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1504 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1505 | server |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1506 | .mock_sync() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1507 | .ok(|builder| { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1508 | let content = RecentEmojisContent::new(emoji_list); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1509 | let event_builder = EventFactory::new().global_account_data(content); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1510 | builder.add_global_account_data(event_builder); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1511 | }) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1512 | .named("Sync") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1513 | .mount() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1514 | .await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1515 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1516 | client.sync_once(SyncSettings::default()).await.expect("sync failed"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1517 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1518 | let recent_emojis = client.account().get_recent_emojis(false).await.expect("recent emojis"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1519 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1520 | // Assert size |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1521 | assert_eq!(recent_emojis.len(), 3); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1522 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1523 | // Assert ordering: first by times used, then by recency |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1524 | assert_eq!(recent_emojis[0].0, ":)"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1525 | assert_eq!(recent_emojis[1].0, ":D"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1526 | assert_eq!(recent_emojis[2].0, ":/"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1527 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1528 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1529 | #[async_test] |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1530 | async fn test_max_recent_emoji_count() { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1531 | let server = MatrixMockServer::new().await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1532 | let client = server.client_builder().build().await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1533 | let user_id = client.user_id().expect("session_id"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1534 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1535 | // This list is > the MAX_RECENT_EMOJI_COUNT |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1536 | let long_emoji_list = (0..MAX_RECENT_EMOJI_COUNT * 2) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1537 | .map(|i| (i.to_string(), uint!(1))) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1538 | .collect::<Vec<(String, UInt)>>(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1539 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1540 | // Initially we locally don't have any emojis |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1541 | let recent_emojis = client.account().get_recent_emojis(false).await.expect("recent emojis"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1542 | assert!(recent_emojis.is_empty()); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1543 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1544 | server |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1545 | .mock_get_recent_emojis() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1546 | .ok(user_id, long_emoji_list.clone()) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1547 | .named("Fetch recent emojis") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1548 | .expect(3) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1549 | .mount() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1550 | .await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1551 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1552 | // Now with a list of emojis longer than the max count, we fetch the emoji list |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1553 | let recent_emojis = client.account().get_recent_emojis(true).await.expect("recent emojis"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1554 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1555 | // It should only return until the max count |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1556 | assert_eq!(recent_emojis.len(), MAX_RECENT_EMOJI_COUNT); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1557 | assert_eq!(recent_emojis, long_emoji_list[..MAX_RECENT_EMOJI_COUNT]); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1558 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1559 | // Simulate the logic we expect when adding a new emoji: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1560 | // 1. Remove the existing emoji if present |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1561 | // 2. Increase its count value and insert it at the front. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1562 | // 3. Truncate at MAX_RECENT_EMOJI_COUNT |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1563 | let expected_updated_emoji_list = { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1564 | let mut list = long_emoji_list.clone(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1565 | let item = list.remove(50); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1566 | list.insert(0, (item.0, item.1 + uint!(1))); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1567 | list.truncate(MAX_RECENT_EMOJI_COUNT); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1568 | list |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1569 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1570 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1571 | // Now if we add a new emoji that was not in the list, the last one in the list |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1572 | // should be gone |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1573 | server |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1574 | .mock_add_recent_emojis() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1575 | .match_emojis_in_request_body(expected_updated_emoji_list) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1576 | .ok(user_id) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1577 | .named("Update recent emojis global account data with existing emoji") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1578 | .mock_once() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1579 | .mount() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1580 | .await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1581 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1582 | client.account().add_recent_emoji("50").await.expect("adding emoji"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1583 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1584 | // Do the same, but now with a new emoji that wasn't previously in the list |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1585 | let expected_updated_emoji_list = { |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1586 | let mut list = long_emoji_list.clone(); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1587 | let item = (":D".to_owned(), uint!(1)); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1588 | list.insert(0, item); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1589 | list.truncate(MAX_RECENT_EMOJI_COUNT); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1590 | list |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1591 | }; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1592 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1593 | // We should still have `MAX_RECENT_EMOJI_COUNT` items |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1594 | server |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1595 | .mock_add_recent_emojis() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1596 | .match_emojis_in_request_body(expected_updated_emoji_list) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1597 | .ok(user_id) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1598 | .named("Update recent emojis global account data with new emoji") |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1599 | .mock_once() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1600 | .mount() |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1601 | .await; |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1602 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1603 | client.account().add_recent_emoji(":D").await.expect("adding emoji"); |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1604 | } |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1605 | } |