[Fix] Conversations now load multiple times.
This commit is contained in:
parent
c914e866d4
commit
bc17bc7ed7
3 changed files with 25 additions and 21 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1739,6 +1739,7 @@ dependencies = [
|
||||||
"hyper",
|
"hyper",
|
||||||
"hyper-util",
|
"hyper-util",
|
||||||
"json",
|
"json",
|
||||||
|
"lazy_static",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"open",
|
"open",
|
||||||
"pnet",
|
"pnet",
|
||||||
|
|
|
||||||
|
|
@ -53,3 +53,4 @@ open = "5.3.3"
|
||||||
chrono = "0.4.43"
|
chrono = "0.4.43"
|
||||||
serde_json = "1.0.149"
|
serde_json = "1.0.149"
|
||||||
rusqlite = "0.39.0"
|
rusqlite = "0.39.0"
|
||||||
|
lazy_static = "1.5.0"
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
use crate::users::contact::Contact;
|
use crate::users::contact::Contact;
|
||||||
use crate::util::file_util::get_directory;
|
use crate::util::file_util::get_directory;
|
||||||
use rusqlite::{Connection, params};
|
use rusqlite::{Connection, params};
|
||||||
|
use std::sync::{LazyLock, Mutex};
|
||||||
|
|
||||||
fn db_path() -> String {
|
static DB_CONN: LazyLock<Mutex<Connection>> = LazyLock::new(|| {
|
||||||
format!("{}/messages.sqlite3", get_directory())
|
let conn = Connection::open(format!("{}/messages.sqlite3", get_directory()))
|
||||||
}
|
.expect("Failed to open DB");
|
||||||
|
|
||||||
fn open_db() -> rusqlite::Result<Connection> {
|
|
||||||
let conn = Connection::open(db_path())?;
|
|
||||||
conn.execute_batch(
|
conn.execute_batch(
|
||||||
r#"
|
r#"
|
||||||
PRAGMA journal_mode = WAL;
|
PRAGMA journal_mode = WAL;
|
||||||
|
|
@ -25,15 +23,13 @@ fn open_db() -> rusqlite::Result<Connection> {
|
||||||
CREATE INDEX IF NOT EXISTS idx_contacts_owner
|
CREATE INDEX IF NOT EXISTS idx_contacts_owner
|
||||||
ON contacts (storage_owner, last_message_at DESC, user_id ASC);
|
ON contacts (storage_owner, last_message_at DESC, user_id ASC);
|
||||||
"#,
|
"#,
|
||||||
)?;
|
)
|
||||||
Ok(conn)
|
.expect("Failed to initialize DB");
|
||||||
}
|
Mutex::new(conn)
|
||||||
|
});
|
||||||
|
|
||||||
pub fn mod_user(storage_owner: i64, contact: &Contact) {
|
pub fn mod_user(storage_owner: i64, contact: &Contact) {
|
||||||
let conn = match open_db() {
|
let conn = DB_CONN.lock().unwrap();
|
||||||
Ok(c) => c,
|
|
||||||
Err(_) => return,
|
|
||||||
};
|
|
||||||
|
|
||||||
let _ = conn.execute(
|
let _ = conn.execute(
|
||||||
r#"
|
r#"
|
||||||
|
|
@ -57,7 +53,7 @@ pub fn mod_user(storage_owner: i64, contact: &Contact) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_user(storage_owner: i64, user_id: i64) -> Option<Contact> {
|
pub fn get_user(storage_owner: i64, user_id: i64) -> Option<Contact> {
|
||||||
let conn = open_db().ok()?;
|
let conn = DB_CONN.lock().unwrap();
|
||||||
|
|
||||||
let row = conn.query_row(
|
let row = conn.query_row(
|
||||||
r#"
|
r#"
|
||||||
|
|
@ -82,17 +78,17 @@ pub fn get_user(storage_owner: i64, user_id: i64) -> Option<Contact> {
|
||||||
match row {
|
match row {
|
||||||
Ok(contact) => Some(contact),
|
Ok(contact) => Some(contact),
|
||||||
Err(rusqlite::Error::QueryReturnedNoRows) => None,
|
Err(rusqlite::Error::QueryReturnedNoRows) => None,
|
||||||
Err(_) => None,
|
Err(e) => {
|
||||||
|
eprintln!("Error querying user in get_user: {}", e);
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_users(storage_owner: i64) -> Vec<Contact> {
|
pub fn get_users(storage_owner: i64) -> Vec<Contact> {
|
||||||
let mut contacts_out = Vec::new();
|
let mut contacts_out = Vec::new();
|
||||||
|
|
||||||
let conn = match open_db() {
|
let conn = DB_CONN.lock().unwrap();
|
||||||
Ok(c) => c,
|
|
||||||
Err(_) => return contacts_out,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut stmt = match conn.prepare(
|
let mut stmt = match conn.prepare(
|
||||||
r#"
|
r#"
|
||||||
|
|
@ -106,7 +102,10 @@ pub fn get_users(storage_owner: i64) -> Vec<Contact> {
|
||||||
"#,
|
"#,
|
||||||
) {
|
) {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(_) => return contacts_out,
|
Err(e) => {
|
||||||
|
eprintln!("Failed to prepare statement in get_users: {}", e);
|
||||||
|
return contacts_out;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let rows = match stmt.query_map(params![storage_owner], |r| {
|
let rows = match stmt.query_map(params![storage_owner], |r| {
|
||||||
|
|
@ -120,7 +119,10 @@ pub fn get_users(storage_owner: i64) -> Vec<Contact> {
|
||||||
})
|
})
|
||||||
}) {
|
}) {
|
||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(_) => return contacts_out,
|
Err(e) => {
|
||||||
|
eprintln!("Failed to query map in get_users: {}", e);
|
||||||
|
return contacts_out;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
for row in rows {
|
for row in rows {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue