matrix: Specify client id when logging in and store the state with sled
This commit is contained in:
parent
ac59bc31b5
commit
a087d2774f
4 changed files with 27 additions and 4 deletions
|
@ -39,16 +39,32 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
async fn client_login(username: String, password: String, device_id: Option<String>) -> Client {
|
||||||
pub async fn matrix(username: String, password: String) {
|
|
||||||
let user = <&UserId>::try_from(username.as_str()).unwrap();
|
let user = <&UserId>::try_from(username.as_str()).unwrap();
|
||||||
let client = Client::builder()
|
let client = Client::builder()
|
||||||
.server_name(user.server_name())
|
.server_name(user.server_name())
|
||||||
|
.sled_store("./sled_store", None)
|
||||||
|
.unwrap()
|
||||||
.build()
|
.build()
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
client.login_username(user, &password).send().await.unwrap();
|
|
||||||
|
|
||||||
|
let mut login_builder = client.login_username(user, &password);
|
||||||
|
let d_id = &device_id.as_ref();
|
||||||
|
if device_id.is_some() {
|
||||||
|
login_builder = login_builder.device_id(d_id.unwrap());
|
||||||
|
}
|
||||||
|
login_builder.send().await.unwrap();
|
||||||
|
if device_id.is_none() {
|
||||||
|
println!("Logged in with a new device id: \"{}\"; you can save it in your configuration so we can use it next time.", client.device_id().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
client
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
pub async fn matrix(username: String, password: String, device_id: Option<String>) {
|
||||||
|
let client = client_login(username, password, device_id).await;
|
||||||
client.sync_once(SyncSettings::default()).await.unwrap();
|
client.sync_once(SyncSettings::default()).await.unwrap();
|
||||||
client.add_event_handler(on_room_message);
|
client.add_event_handler(on_room_message);
|
||||||
let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
|
let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
|
||||||
|
|
|
@ -4,6 +4,7 @@ token = ""
|
||||||
[matrix]
|
[matrix]
|
||||||
username = ""
|
username = ""
|
||||||
password = ""
|
password = ""
|
||||||
|
device_id = ""
|
||||||
|
|
||||||
[gpio]
|
[gpio]
|
||||||
device = ""
|
device = ""
|
||||||
|
|
|
@ -33,6 +33,8 @@ pub struct Matrix {
|
||||||
pub username: Option<String>,
|
pub username: Option<String>,
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
pub password: Option<String>,
|
pub password: Option<String>,
|
||||||
|
#[clap(short, long)]
|
||||||
|
pub device_id: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "csv")]
|
#[cfg(feature = "csv")]
|
||||||
|
|
|
@ -16,7 +16,11 @@ fn main() {
|
||||||
#[cfg(feature = "matrix")]
|
#[cfg(feature = "matrix")]
|
||||||
cli::Command::Matrix(_) => {
|
cli::Command::Matrix(_) => {
|
||||||
let matrix = config.get::<cli::Matrix>("matrix").unwrap();
|
let matrix = config.get::<cli::Matrix>("matrix").unwrap();
|
||||||
labadoor_matrix::matrix(matrix.username.unwrap(), matrix.password.unwrap());
|
labadoor_matrix::matrix(
|
||||||
|
matrix.username.unwrap(),
|
||||||
|
matrix.password.unwrap(),
|
||||||
|
matrix.device_id,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
#[cfg(feature = "csv")]
|
#[cfg(feature = "csv")]
|
||||||
cli::Command::CSV(_) => {
|
cli::Command::CSV(_) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue