pub struct Account { /* private fields */ }
Expand description

A high-level API to manage the client owner’s account.

All the methods on this struct send a request to the homeserver.

Implementations

Get the display name of the account.

Example
let user = "example";
let client = Client::new(homeserver).await?;
client.login(user, "password", None, None).await?;

if let Some(name) = client.account().get_display_name().await? {
    println!("Logged in as user '{user}' with display name '{name}'");
}

Set the display name of the account.

Example
let user = "example";
let client = Client::new(homeserver).await?;
client.login(user, "password", None, None).await?;

client.account().set_display_name(Some("Alice")).await?;

Get the MXC URI of the account’s avatar, if set.

Example
let client = Client::new(homeserver).await?;
client.login(user, "password", None, None).await?;

if let Some(url) = client.account().get_avatar_url().await? {
    println!("Your avatar's mxc url is {url}");
}

Set the MXC URI of the account’s avatar.

The avatar is unset if url is None.

Get the account’s avatar, if set.

Returns the avatar.

If a thumbnail is requested no guarantee on the size of the image is given.

Arguments
  • format - The desired format of the avatar.
Example
let client = Client::new(homeserver).await?;
client.login(user, "password", None, None).await?;

if let Some(avatar) = client.account().get_avatar(MediaFormat::File).await?
{
    std::fs::write("avatar.png", avatar);
}

Upload and set the account’s avatar.

This will upload the data produced by the reader to the homeserver’s content repository, and set the user’s avatar to the MXC URI for the uploaded file.

This is a convenience method for calling Media::upload(), followed by Account::set_avatar_url().

Returns the MXC URI of the uploaded avatar.

Example
let image = fs::read("/home/example/selfie.jpg")?;

client.account().upload_avatar(&mime::IMAGE_JPEG, &image).await?;

Get the profile of the account.

Allows to get both the display name and avatar URL in a single call.

Example
if let profile = client.account().get_profile().await? {
    println!(
        "You are '{:?}' with avatar '{:?}'",
        profile.displayname, profile.avatar_url
    );
}

Change the password of the account.

Arguments
  • new_password - The new password to set.

  • auth_data - This request uses the User-Interactive Authentication API. The first request needs to set this to None and will always fail with an UiaaResponse. The response will contain information for the interactive auth and the same request needs to be made but this time with some auth_data provided.

Returns

This method might return an ErrorKind::WeakPassword error if the new password is considered insecure by the homeserver, with details about the strength requirements in the error’s message.

Example
client.account().change_password(
    "myverysecretpassword",
    Some(AuthData::Dummy(Dummy::new())),
).await?;

Deactivate this account definitively.

Arguments
  • id_server - The identity server from which to unbind the user’s Third Party Identifiers.

  • auth_data - This request uses the User-Interactive Authentication API. The first request needs to set this to None and will always fail with an UiaaResponse. The response will contain information for the interactive auth and the same request needs to be made but this time with some auth_data provided.

Example
let response = account.deactivate(None, None).await;

// Proceed with UIAA.

Get the registered Third Party Identifiers on the homeserver of the account.

These 3PIDs may be used by the homeserver to authenticate the user during sensitive operations.

Example
let threepids = client.account().get_3pids().await?.threepids;

for threepid in threepids {
    println!(
        "Found 3PID '{}' of type '{}'",
        threepid.address, threepid.medium
    );
}

Request a token to validate an email address as a Third Party Identifier.

This is the first step in registering an email address as 3PID. Next, call Account::add_3pid() with the same client_secret and the returned sid.

Arguments
  • client_secret - A client-generated secret string used to protect this session.

  • email - The email address to validate.

  • send_attempt - The attempt number. This number needs to be incremented if you want to request another token for the same validation.

Returns
  • sid - The session ID to be used in following requests for this 3PID.

  • submit_url - If present, the user will submit the token to the client, that must send it to this URL. If not, the client will not be involved in the token submission.

This method might return an ErrorKind::ThreepidInUse error if the email address is already registered for this account or another, or an ErrorKind::ThreepidDenied error if it is denied.

Example
let token_response = account
    .request_3pid_email_token(&secret, "john@matrix.org", uint!(0))
    .await?;

// Wait for the user to confirm that the token was submitted or prompt
// the user for the token and send it to submit_url.

let uiaa_response =
    account.add_3pid(&secret, &token_response.sid, None).await;

// Proceed with UIAA.

Request a token to validate a phone number as a Third Party Identifier.

This is the first step in registering a phone number as 3PID. Next, call Account::add_3pid() with the same client_secret and the returned sid.

Arguments
  • client_secret - A client-generated secret string used to protect this session.

  • country - The two-letter uppercase ISO-3166-1 alpha-2 country code that the number in phone_number should be parsed as if it were dialled from.

  • phone_number - The phone number to validate.

  • send_attempt - The attempt number. This number needs to be incremented if you want to request another token for the same validation.

Returns
  • sid - The session ID to be used in following requests for this 3PID.

  • submit_url - If present, the user will submit the token to the client, that must send it to this URL. If not, the client will not be involved in the token submission.

This method might return an ErrorKind::ThreepidInUse error if the phone number is already registered for this account or another, or an ErrorKind::ThreepidDenied error if it is denied.

Example
let token_response = account
    .request_3pid_msisdn_token(&secret, "FR", "0123456789", uint!(0))
    .await?;

// Wait for the user to confirm that the token was submitted or prompt
// the user for the token and send it to submit_url.

let uiaa_response =
    account.add_3pid(&secret, &token_response.sid, None).await;

// Proceed with UIAA.

Add a Third Party Identifier on the homeserver for this account.

This 3PID may be used by the homeserver to authenticate the user during sensitive operations.

This method should be called after Account::request_3pid_email_token() or Account::request_3pid_msisdn_token() to complete the 3PID

Arguments

Delete a Third Party Identifier from the homeserver for this account.

Arguments
  • address - The 3PID being removed.

  • medium - The type of the 3PID.

  • id_server - The identity server to unbind from. If not provided, the homeserver should unbind the 3PID from the identity server it was bound to previously.

Returns
Example
match account
        .delete_3pid("paul@matrix.org", Medium::Email, None)
        .await?
        .id_server_unbind_result
    {
        ThirdPartyIdRemovalStatus::Success => {
            println!("3PID unbound from the Identity Server");
        }
        _ => println!("Could not unbind 3PID from the Identity Server"),
    }

Get the content of an account data event of statically-known type.

Example
use matrix_sdk::ruma::events::ignored_user_list::IgnoredUserListEventContent;

let maybe_content = account.account_data::<IgnoredUserListEventContent>().await?;
if let Some(raw_content) = maybe_content {
    let content = raw_content.deserialize()?;
    println!("Ignored users:");
    for user_id in content.ignored_users {
        println!("- {user_id}");
    }
}

Get the content of an account data event of a given type.

Set the given account data event.

Example
use matrix_sdk::ruma::{
    events::ignored_user_list::IgnoredUserListEventContent, user_id,
};

let mut content = account
    .account_data::<IgnoredUserListEventContent>()
    .await?
    .map(|c| c.deserialize())
    .transpose()?
    .unwrap_or_default();
content.ignored_users.push(user_id!("@foo:bar.com").to_owned());
account.set_account_data(content).await?;

Set the given raw account data event.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more