|
|
@ -22,11 +22,12 @@ use matrix_sdk::{ |
|
|
|
};
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
use db::DataStore;
|
|
|
|
|
|
|
|
mod create_relation_event;
|
|
|
|
mod config;
|
|
|
|
mod db;
|
|
|
|
|
|
|
|
struct EventCallback {
|
|
|
|
client: Client,
|
|
|
@ -113,14 +114,22 @@ async fn login( |
|
|
|
username: String,
|
|
|
|
password: String,
|
|
|
|
config: ClientConfig,
|
|
|
|
statsd_client: statsd::Client
|
|
|
|
statsd_client: statsd::Client,
|
|
|
|
datastore: DataStore
|
|
|
|
) -> Result<(), matrix_sdk::Error> {
|
|
|
|
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
|
|
|
|
let mut client = Client::new_with_config(homeserver_url, config).unwrap();
|
|
|
|
|
|
|
|
let login_response = client.login(username, password, None, Some("Cat Disruptor".to_string())).await?;
|
|
|
|
let login_response = client.login(
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
datastore.get_device_id(),
|
|
|
|
Some("Cat Disruptor".to_string())
|
|
|
|
).await?;
|
|
|
|
|
|
|
|
println!("Logged in as {}", login_response.user_id);
|
|
|
|
println!("Device id: {}", login_response.device_id); // TODO store this
|
|
|
|
datastore.save_device_id(login_response.device_id);
|
|
|
|
let event_callback = EventCallback::new(client.clone(), login_response.user_id, statsd_client);
|
|
|
|
client.add_event_emitter(Box::new(event_callback)).await;
|
|
|
|
client.sync_forever(SyncSettings::new(), |_| async {}).await;
|
|
|
@ -148,7 +157,13 @@ async fn main() -> Result<(), matrix_sdk::Error> { |
|
|
|
}
|
|
|
|
|
|
|
|
let matrix_config = ClientConfig::new()
|
|
|
|
.store_path(store_path);
|
|
|
|
.store_path(store_path.clone());
|
|
|
|
|
|
|
|
// We need to store the device ID so that restarting the bot doesn't create new sessions.
|
|
|
|
// In the future we will probably store other things in the database.
|
|
|
|
// Sqlite would be overkill otherwise.
|
|
|
|
let datastore = DataStore::open(store_path.join("cat_disruptor.sqlite"));
|
|
|
|
|
|
|
|
login(config.homeserver_url, config.username, config.password, matrix_config, statsd_client).await
|
|
|
|
login(config.homeserver_url, config.username, config.password,
|
|
|
|
matrix_config, statsd_client, datastore).await
|
|
|
|
}
|