Browse Source

SQLite store for device id

pull/2/head
Stephen 9 months ago
parent
commit
575cf234c1
5 changed files with 105 additions and 8 deletions
  1. +44
    -1
      Cargo.lock
  2. +2
    -1
      Cargo.toml
  3. +1
    -1
      config.example
  4. +38
    -0
      src/db.rs
  5. +20
    -5
      src/main.rs

+ 44
- 1
Cargo.lock View File

@ -124,7 +124,7 @@ dependencies = [
[[package]]
name = "cat_disruptor_7000"
version = "0.3.0"
version = "0.4.0"
dependencies = [
"async-trait",
"matrix-sdk",
@ -133,6 +133,7 @@ dependencies = [
"ruma-client-api",
"ruma-events 0.16.0",
"ruma-identifiers 0.16.2",
"rusqlite",
"serde",
"serde_json",
"statsd",
@ -269,6 +270,18 @@ dependencies = [
]
[[package]]
name = "fallible-iterator"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fallible-streaming-iterator"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@ -668,6 +681,12 @@ dependencies = [
]
[[package]]
name = "linked-hash-map"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
[[package]]
name = "log"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
@ -688,6 +707,15 @@ dependencies = [
]
[[package]]
name = "lru-cache"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c"
dependencies = [
"linked-hash-map",
]
[[package]]
name = "maplit"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@ -1385,6 +1413,21 @@ dependencies = [
]
[[package]]
name = "rusqlite"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57edf4c4cea4d7e0fab069acb5da9e8e8e5403c78abc81b1f37d83af02148ea5"
dependencies = [
"bitflags",
"fallible-iterator",
"fallible-streaming-iterator",
"libsqlite3-sys",
"lru-cache",
"memchr",
"time",
]
[[package]]
name = "rustc_version"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"


+ 2
- 1
Cargo.toml View File

@ -1,6 +1,6 @@
[package]
name = "cat_disruptor_7000"
version = "0.3.0"
version = "0.4.0"
authors = ["Stephen D <@stephen:m.scd31.com>"]
edition = "2018"
@ -20,6 +20,7 @@ tracing = "0.1"
tracing-subscriber = "0.2"
uuid = { version = "0.8.1", features = ["v4"] }
statsd = "^0.13.1"
rusqlite = "0.22"
[package.metadata.deb]
maintainer = "Stephen D <@stephen:m.scd31.com>"


+ 1
- 1
config.example View File

@ -2,6 +2,6 @@
"homeserver_url": "changeme",
"username": "changeme",
"password": "changeme",
"store_path": "crypto",
"store_path": "store",
"statsd_server": "127.0.0.1:8125"
}

+ 38
- 0
src/db.rs View File

@ -0,0 +1,38 @@
use std::path::PathBuf;
use rusqlite::{params, Connection};
pub struct DataStore {
conn: Connection
}
impl DataStore {
pub fn open(filename: PathBuf) -> Self {
let conn = Connection::open(filename).unwrap();
conn.execute(
"CREATE TABLE IF NOT EXISTS device_ids (
id INTEGER PRIMARY KEY,
device_id VARCHAR(10) NOT NULL UNIQUE
)",
params![]
).unwrap();
DataStore { conn }
}
pub fn get_device_id(&self) -> Option<String> {
match self.conn.query_row("SELECT device_id FROM device_ids ORDER BY id DESC LIMIT 1",
params![],
|x| { x.get(0)}
) {
Ok(x) => Some(x),
Err(_) => None
}
}
pub fn save_device_id(&self, device_id: String) {
self.conn.execute("INSERT OR IGNORE INTO device_ids (device_id) VALUES (?1)",
params![device_id]).unwrap();
}
}

+ 20
- 5
src/main.rs View File

@ -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
}

Loading…
Cancel
Save