[Fix] Durability

This commit is contained in:
Alex Emmet 2026-08-30 21:28:17 +02:00
commit ec3f5e6a6e
No known key found for this signature in database
9 changed files with 317 additions and 118 deletions

View file

@ -4,6 +4,42 @@ use crate::util::db;
use crate::util::sync::{self, EntityType, Operation};
use rusqlite::params;
pub(crate) fn upsert_contact(
tx: &rusqlite::Transaction<'_>,
storage_owner: i64,
contact: &Contact,
) -> Result<(), StorageError> {
tx.execute(
r#"
INSERT INTO contacts (storage_owner, user_id, user_name, created_at, last_message_at)
VALUES (?1, ?2, ?3, ?4, ?5)
ON CONFLICT(storage_owner, user_id) DO UPDATE SET
user_name = COALESCE(excluded.user_name, contacts.user_name),
created_at = MIN(contacts.created_at, excluded.created_at),
last_message_at = CASE
WHEN excluded.last_message_at IS NULL THEN contacts.last_message_at
WHEN contacts.last_message_at IS NULL THEN excluded.last_message_at
ELSE MAX(contacts.last_message_at, excluded.last_message_at)
END
"#,
params![
storage_owner,
contact.user_id,
contact.user_name,
contact.created_at,
contact.last_message_at,
],
)?;
sync::record_event(
tx,
storage_owner,
EntityType::Contact,
contact.user_id,
Operation::Upsert,
)?;
Ok(())
}
pub fn has_user(storage_owner: i64, user_id: i64) -> Result<bool, StorageError> {
db::with_db(|conn| {
Ok(conn.query_row(
@ -15,31 +51,7 @@ pub fn has_user(storage_owner: i64, user_id: i64) -> Result<bool, StorageError>
}
pub fn mod_user(storage_owner: i64, contact: &Contact) -> Result<(), StorageError> {
db::with_immediate_transaction(|tx| {
tx.execute(
r#"
INSERT INTO contacts (storage_owner, user_id, user_name, last_message_at)
VALUES (?1, ?2, ?3, ?4)
ON CONFLICT(storage_owner, user_id) DO UPDATE SET
user_name = excluded.user_name,
last_message_at = excluded.last_message_at
"#,
params![
storage_owner,
contact.user_id,
contact.user_name,
contact.last_message_at,
],
)?;
sync::record_event(
tx,
storage_owner,
EntityType::Contact,
contact.user_id,
Operation::Upsert,
)?;
Ok(())
})
db::with_immediate_transaction(|tx| upsert_contact(tx, storage_owner, contact))
}
pub fn get_users_by_ids(storage_owner: i64, ids: &[i64]) -> Result<Vec<Contact>, StorageError> {
@ -57,7 +69,7 @@ pub fn get_user(storage_owner: i64, user_id: i64) -> Result<Option<Contact>, Sto
db::with_db(|conn| {
match conn.query_row(
r#"
SELECT user_id, user_name, last_message_at
SELECT user_id, user_name, created_at, last_message_at
FROM contacts
WHERE storage_owner = ?1 AND user_id = ?2
LIMIT 1
@ -67,7 +79,8 @@ pub fn get_user(storage_owner: i64, user_id: i64) -> Result<Option<Contact>, Sto
Ok(Contact {
user_id: r.get(0)?,
user_name: r.get(1)?,
last_message_at: r.get(2)?,
created_at: r.get(2)?,
last_message_at: r.get(3)?,
})
},
) {
@ -82,7 +95,7 @@ pub fn get_users(storage_owner: i64) -> Result<Vec<Contact>, StorageError> {
db::with_db(|conn| {
let mut stmt = conn.prepare(
r#"
SELECT user_id, user_name, last_message_at
SELECT user_id, user_name, created_at, last_message_at
FROM contacts
WHERE storage_owner = ?1
ORDER BY
@ -96,7 +109,8 @@ pub fn get_users(storage_owner: i64) -> Result<Vec<Contact>, StorageError> {
Ok(Contact {
user_id: r.get(0)?,
user_name: r.get(1)?,
last_message_at: r.get(2)?,
created_at: r.get(2)?,
last_message_at: r.get(3)?,
})
})?;