Convert to SQLx and SQLite #64

Manually merged
projectmoon merged 17 commits from sqlite into master 2021-05-20 15:34:52 +00:00
4 changed files with 36 additions and 45 deletions
Showing only changes of commit 66fb6e7cf8 - Show all commits

38
Cargo.lock generated
View File

@ -342,9 +342,9 @@ checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.1.3" version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "281f563b2c3a0e535ab12d81d3c5859045795256ad269afa7c19542585b68f93" checksum = "ed00c67cb5d0a7d64a44f6ad2668db7e7530311dd53ea79bcd4fb022c64911c8"
dependencies = [ dependencies = [
"libc", "libc",
] ]
@ -1123,7 +1123,7 @@ checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
[[package]] [[package]]
name = "matrix-sdk" name = "matrix-sdk"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/matrix-org/matrix-rust-sdk?branch=master#0bdcc0fbf90286b7555fda48403151f02dde6717" source = "git+https://github.com/matrix-org/matrix-rust-sdk?branch=master#ffea84b64aa172b9b153a76588f4f609bf15c441"
dependencies = [ dependencies = [
"backoff", "backoff",
"bytes", "bytes",
@ -1147,7 +1147,7 @@ dependencies = [
[[package]] [[package]]
name = "matrix-sdk-base" name = "matrix-sdk-base"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/matrix-org/matrix-rust-sdk?branch=master#0bdcc0fbf90286b7555fda48403151f02dde6717" source = "git+https://github.com/matrix-org/matrix-rust-sdk?branch=master#ffea84b64aa172b9b153a76588f4f609bf15c441"
dependencies = [ dependencies = [
"chacha20poly1305", "chacha20poly1305",
"dashmap", "dashmap",
@ -1170,7 +1170,7 @@ dependencies = [
[[package]] [[package]]
name = "matrix-sdk-common" name = "matrix-sdk-common"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/matrix-org/matrix-rust-sdk?branch=master#0bdcc0fbf90286b7555fda48403151f02dde6717" source = "git+https://github.com/matrix-org/matrix-rust-sdk?branch=master#ffea84b64aa172b9b153a76588f4f609bf15c441"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"futures", "futures",
@ -1186,7 +1186,7 @@ dependencies = [
[[package]] [[package]]
name = "matrix-sdk-crypto" name = "matrix-sdk-crypto"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/matrix-org/matrix-rust-sdk?branch=master#0bdcc0fbf90286b7555fda48403151f02dde6717" source = "git+https://github.com/matrix-org/matrix-rust-sdk?branch=master#ffea84b64aa172b9b153a76588f4f609bf15c441"
dependencies = [ dependencies = [
"aes-ctr", "aes-ctr",
"aes-gcm", "aes-gcm",
@ -1845,7 +1845,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma" name = "ruma"
version = "0.0.3" version = "0.0.3"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"assign", "assign",
"js_int", "js_int",
@ -1861,7 +1861,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-api" name = "ruma-api"
version = "0.17.0-alpha.4" version = "0.17.0-alpha.4"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"bytes", "bytes",
"http", "http",
@ -1877,7 +1877,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-api-macros" name = "ruma-api-macros"
version = "0.17.0-alpha.4" version = "0.17.0-alpha.4"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -1888,7 +1888,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-client-api" name = "ruma-client-api"
version = "0.10.0-alpha.3" version = "0.10.0-alpha.3"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"assign", "assign",
"bytes", "bytes",
@ -1908,7 +1908,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-common" name = "ruma-common"
version = "0.5.0" version = "0.5.0"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"js_int", "js_int",
@ -1923,7 +1923,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events" name = "ruma-events"
version = "0.22.0-alpha.3" version = "0.22.0-alpha.3"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"indoc", "indoc",
"js_int", "js_int",
@ -1938,7 +1938,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events-macros" name = "ruma-events-macros"
version = "0.22.0-alpha.3" version = "0.22.0-alpha.3"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -1949,7 +1949,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-federation-api" name = "ruma-federation-api"
version = "0.1.0-alpha.2" version = "0.1.0-alpha.2"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@ -1964,7 +1964,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers" name = "ruma-identifiers"
version = "0.19.0" version = "0.19.0"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"paste", "paste",
"ruma-identifiers-macros", "ruma-identifiers-macros",
@ -1977,7 +1977,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-macros" name = "ruma-identifiers-macros"
version = "0.19.0" version = "0.19.0"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"quote", "quote",
"ruma-identifiers-validation", "ruma-identifiers-validation",
@ -1987,12 +1987,12 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-validation" name = "ruma-identifiers-validation"
version = "0.3.0" version = "0.3.0"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
[[package]] [[package]]
name = "ruma-serde" name = "ruma-serde"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"bytes", "bytes",
"form_urlencoded", "form_urlencoded",
@ -2006,7 +2006,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-serde-macros" name = "ruma-serde-macros"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/ruma/ruma?rev=3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5#3bdead1cf207e3ab9c8fcbfc454c054c726ba6f5" source = "git+https://github.com/ruma/ruma?rev=e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6#e1ab817e0bef78cb8241d6d3c1ced7d6b414c7f6"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",

View File

@ -161,22 +161,9 @@ impl DiceBot {
let client = self.client.clone(); let client = self.client.clone();
self.login(&client).await?; self.login(&client).await?;
// Initial sync without event handler prevents responding to
// messages received while bot was offline. TODO: selectively
// respond to old messages? e.g. comands missed while offline.
//info!("Performing intial sync (no commands will be responded to)");
// self.client.sync_once(SyncSettings::default()).await?;
client.set_event_handler(Box::new(self)).await; client.set_event_handler(Box::new(self)).await;
info!("Listening for commands"); info!("Listening for commands");
// let token = client
// .sync_token()
// .await
// .ok_or(BotError::SyncTokenRequired)?;
// let settings = SyncSettings::default().token(token);
// TODO replace with sync_with_callback for cleaner shutdown // TODO replace with sync_with_callback for cleaner shutdown
// process. // process.
client.sync(SyncSettings::default()).await; client.sync(SyncSettings::default()).await;

View File

@ -20,9 +20,9 @@ use matrix_sdk::{
room::Room, room::Room,
EventHandler, EventHandler,
}; };
use std::clone::Clone;
use std::ops::Sub; use std::ops::Sub;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
use std::{clone::Clone, time::UNIX_EPOCH};
/// Check if a message is recent enough to actually process. If the /// Check if a message is recent enough to actually process. If the
/// message is within "oldest_message_age" seconds, this function /// message is within "oldest_message_age" seconds, this function
@ -32,7 +32,11 @@ fn check_message_age(
event: &SyncMessageEvent<MessageEventContent>, event: &SyncMessageEvent<MessageEventContent>,
oldest_message_age: u64, oldest_message_age: u64,
) -> bool { ) -> bool {
let sending_time = event.origin_server_ts; let sending_time = event
.origin_server_ts
.to_system_time()
.unwrap_or(UNIX_EPOCH);
let oldest_timestamp = SystemTime::now().sub(Duration::from_secs(oldest_message_age)); let oldest_timestamp = SystemTime::now().sub(Duration::from_secs(oldest_message_age));
if sending_time > oldest_timestamp { if sending_time > oldest_timestamp {
@ -127,17 +131,20 @@ impl EventHandler for DiceBot {
false false
}; };
// user_joing is true if a user is joining this room, and
// false if they have left for some reason. This user may be
// us, or another user in the room.
use MembershipChange::*; use MembershipChange::*;
let adding_user = match event.membership_change() { let user_joining = match event.membership_change() {
Joined => true, Joined => true,
Banned | Left | Kicked | KickedAndBanned => false, Banned | Left | Kicked | KickedAndBanned => false,
_ => return, _ => return,
}; };
let result = if event_affects_us && !adding_user { let result = if event_affects_us && !user_joining {
info!("Clearing all information for room ID {}", room_id); info!("Clearing all information for room ID {}", room_id);
self.db.clear_info(room_id_str).await.map_err(|e| e.into()) self.db.clear_info(room_id_str).await.map_err(|e| e.into())
} else if event_affects_us && adding_user { } else if event_affects_us && user_joining {
info!("Joined room {}; recording room information", room_id); info!("Joined room {}; recording room information", room_id);
record_room_information( record_room_information(
&self.client, &self.client,
@ -147,13 +154,13 @@ impl EventHandler for DiceBot {
&event.state_key, &event.state_key,
) )
.await .await
} else if !event_affects_us && adding_user { } else if !event_affects_us && user_joining {
info!("Adding user {} to room ID {}", username, room_id); info!("Adding user {} to room ID {}", username, room_id);
self.db self.db
.add_user_to_room(username, room_id_str) .add_user_to_room(username, room_id_str)
.await .await
.map_err(|e| e.into()) .map_err(|e| e.into())
} else if !event_affects_us && !adding_user { } else if !event_affects_us && !user_joining {
info!("Removing user {} from room ID {}", username, room_id); info!("Removing user {} from room ID {}", username, room_id);
self.db self.db
.remove_user_from_room(username, room_id_str) .remove_user_from_room(username, room_id_str)

View File

@ -6,7 +6,7 @@ use sqlx::SqlitePool;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
async fn record_event(conn: &SqlitePool, event_id: &str, room_id: &str) -> Result<(), DataError> { async fn record_event(conn: &SqlitePool, room_id: &str, event_id: &str) -> Result<(), DataError> {
use std::convert::TryFrom; use std::convert::TryFrom;
let now: i64 = i64::try_from( let now: i64 = i64::try_from(
SystemTime::now() SystemTime::now()
@ -122,6 +122,8 @@ impl Rooms for Database {
} }
async fn clear_info(&self, room_id: &str) -> Result<(), DataError> { async fn clear_info(&self, room_id: &str) -> Result<(), DataError> {
// We do not clear event history here, because if we rejoin a
// room, we would re-process events we've already seen.
sqlx::query("DELETE FROM room_info where room_id = ?") sqlx::query("DELETE FROM room_info where room_id = ?")
.bind(room_id) .bind(room_id)
.execute(&self.conn) .execute(&self.conn)
@ -132,11 +134,6 @@ impl Rooms for Database {
.execute(&self.conn) .execute(&self.conn)
.await?; .await?;
sqlx::query("DELETE FROM room_events where room_id = ?")
.bind(room_id)
.execute(&self.conn)
.await?;
Ok(()) Ok(())
} }
} }