examples: Clean up the emoji example a bit.

master
Damir Jelić 2020-08-03 14:51:45 +02:00
parent f4517c150c
commit 3245fbb1c9
1 changed files with 44 additions and 10 deletions

View File

@ -1,7 +1,38 @@
use std::{env, io, process::exit}; use std::{env, io, process::exit};
use url::Url; use url::Url;
use matrix_sdk::{self, events::AnyToDeviceEvent, Client, ClientConfig, SyncSettings}; use matrix_sdk::{self, events::AnyToDeviceEvent, Client, ClientConfig, Sas, SyncSettings};
async fn wait_for_confirmation(sas: Sas) {
println!("Emoji: {:?}", sas.emoji());
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("error: unable to read user input");
match input.trim().to_lowercase().as_ref() {
"yes" | "true" | "ok" => {
sas.confirm().await.unwrap();
if sas.is_done() {
print_result(sas);
}
}
_ => sas.cancel().await.unwrap(),
}
}
fn print_result(sas: Sas) {
let device = sas.other_device();
println!(
"Successfully verified device {} {} {:?}",
device.user_id(),
device.device_id(),
device.trust_state()
);
}
async fn login( async fn login(
homeserver_url: String, homeserver_url: String,
@ -29,7 +60,6 @@ async fn login(
.deserialize() .deserialize()
.expect("Can't deserialize to-device event"); .expect("Can't deserialize to-device event");
println!("Received to-device event {:?}", e);
match e { match e {
AnyToDeviceEvent::KeyVerificationStart(e) => { AnyToDeviceEvent::KeyVerificationStart(e) => {
let sas = client let sas = client
@ -38,23 +68,27 @@ async fn login(
.expect("Sas object wasn't created"); .expect("Sas object wasn't created");
sas.accept().await.unwrap(); sas.accept().await.unwrap();
} }
AnyToDeviceEvent::KeyVerificationKey(e) => { AnyToDeviceEvent::KeyVerificationKey(e) => {
let sas = client let sas = client
.get_verification(&e.content.transaction_id) .get_verification(&e.content.transaction_id)
.await .await
.expect("Sas object wasn't created"); .expect("Sas object wasn't created");
println!("Emoji: {:?}", sas.emoji());
let mut input = String::new(); tokio::spawn(wait_for_confirmation(sas));
io::stdin() }
.read_line(&mut input)
.expect("error: unable to read user input");
match input.trim().to_lowercase().as_ref() { AnyToDeviceEvent::KeyVerificationMac(e) => {
"yes" | "true" | "ok" => sas.confirm().await.unwrap(), let sas = client
_ => sas.cancel().await.unwrap(), .get_verification(&e.content.transaction_id)
.await
.expect("Sas object wasn't created");
if sas.is_done() {
print_result(sas);
} }
} }
_ => (), _ => (),
} }
} }