matrix_ui_serializable/
commands.rs

1//! All the actions exposed to the frontend that returns a `Result`.
2
3use std::path::PathBuf;
4
5use crate::{
6    init::{
7        login::{LoginRequest, MatrixClientConfig},
8        singletons::get_client,
9    },
10    models::{async_requests::MatrixRequest, events::FrontendDevice},
11    room::notifications::MobilePushNotificationConfig,
12    utils::guess_device_type,
13};
14use matrix_sdk::ruma::{OwnedDeviceId, OwnedRoomId, OwnedUserId, UserId};
15
16/// Calling this function will create a new Matrix client and SQLite DB. It returns
17/// the serialized session to be persisted and then passed by your adapter when initiating
18/// the lib. This command must be implemented by the adapter.
19/// # Panics
20/// If the app_data_dir `PathBuf` doesn't allow to write the SQLite DB to the given path.
21pub async fn login_and_create_new_session(
22    config: MatrixClientConfig,
23    mobile_push_config: Option<MobilePushNotificationConfig>,
24    app_data_dir: PathBuf,
25) -> crate::Result<String> {
26    crate::init::session::login_and_get_session(
27        LoginRequest::LoginByPassword(config),
28        mobile_push_config,
29        app_data_dir,
30    )
31    .await
32}
33
34/// Submit a request to the Matrix Client that will be executed asynchronously.
35pub fn submit_async_request(request: MatrixRequest) -> crate::Result<()> {
36    crate::models::async_requests::submit_async_request(request);
37    Ok(())
38}
39
40/// Fetches a given User Profile and adds it to this lib's User Profile cache.
41pub async fn fetch_user_profile(
42    user_id: OwnedUserId,
43    room_id: Option<OwnedRoomId>,
44) -> crate::Result<bool> {
45    Ok(crate::user::user_profile::fetch_user_profile(user_id, room_id).await)
46}
47
48/// Get the list of this user's account registered devices.
49pub async fn get_devices(user_id: &UserId) -> crate::Result<Vec<FrontendDevice>> {
50    let client = get_client().expect("Client should be defined at this state");
51    let devices: Vec<FrontendDevice> = client
52        .encryption()
53        .get_user_devices(user_id)
54        .await?
55        .devices()
56        .filter(|device| !device.is_deleted())
57        .map(|device| FrontendDevice {
58            device_id: device.device_id().to_owned(),
59            display_name: device.display_name().map(|n| n.to_string()),
60            is_verified: device.is_verified(),
61            is_verified_with_cross_signing: device.is_verified_with_cross_signing(),
62            registration_date: device.first_time_seen_ts(),
63            guessed_type: guess_device_type(device.display_name()),
64            is_current_device: device.device_id().eq(client.device_id().unwrap()),
65        })
66        .collect();
67    Ok(devices)
68}
69
70/// Start the SAS V1 Emoji verification process with another user's device.
71pub async fn verify_device(user_id: OwnedUserId, device_id: OwnedDeviceId) -> crate::Result<()> {
72    crate::events::emoji_verification::verify_device(&user_id, &device_id)
73        .await
74        .map_err(|e| crate::Error::Anyhow(e))
75}