mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-05-17 07:29:32 +00:00
chore: fix warning
This commit is contained in:
parent
fa5e48e8de
commit
55285aa24e
@ -379,8 +379,8 @@ mod tests {
|
|||||||
let key_hex = hex::encode(intro.ephemeral_key.as_bytes());
|
let key_hex = hex::encode(intro.ephemeral_key.as_bytes());
|
||||||
|
|
||||||
// Key should be persisted
|
// Key should be persisted
|
||||||
let loaded_key = manager.storage.load_inbox_key(&key_hex).unwrap();
|
let all_keys = manager.storage.load_all_inbox_keys().unwrap();
|
||||||
assert!(loaded_key.is_some());
|
assert!(all_keys.contains_key(&key_hex));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@ -40,7 +40,7 @@ const CHAT_SCHEMA: &str = "
|
|||||||
///
|
///
|
||||||
/// This struct wraps a SqliteDb and provides domain-specific
|
/// This struct wraps a SqliteDb and provides domain-specific
|
||||||
/// storage operations for chat state (identity, inbox keys, chat metadata).
|
/// storage operations for chat state (identity, inbox keys, chat metadata).
|
||||||
///
|
///
|
||||||
/// Note: Ratchet state persistence is delegated to double_ratchets::RatchetStorage.
|
/// Note: Ratchet state persistence is delegated to double_ratchets::RatchetStorage.
|
||||||
pub struct ChatStorage {
|
pub struct ChatStorage {
|
||||||
db: SqliteDb,
|
db: SqliteDb,
|
||||||
@ -53,11 +53,6 @@ impl ChatStorage {
|
|||||||
Self::run_migration(db)
|
Self::run_migration(db)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an in-memory storage (useful for testing).
|
|
||||||
pub fn in_memory() -> Result<Self, StorageError> {
|
|
||||||
Self::new(StorageConfig::InMemory)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a new chat storage with the given database.
|
/// Creates a new chat storage with the given database.
|
||||||
fn run_migration(db: SqliteDb) -> Result<Self, StorageError> {
|
fn run_migration(db: SqliteDb) -> Result<Self, StorageError> {
|
||||||
db.connection().execute_batch(CHAT_SCHEMA)?;
|
db.connection().execute_batch(CHAT_SCHEMA)?;
|
||||||
@ -116,33 +111,6 @@ impl ChatStorage {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Loads an inbox ephemeral key by its public key hex.
|
|
||||||
pub fn load_inbox_key(
|
|
||||||
&self,
|
|
||||||
public_key_hex: &str,
|
|
||||||
) -> Result<Option<StaticSecret>, StorageError> {
|
|
||||||
let mut stmt = self
|
|
||||||
.db
|
|
||||||
.connection()
|
|
||||||
.prepare("SELECT secret_key FROM inbox_keys WHERE public_key_hex = ?1")?;
|
|
||||||
|
|
||||||
let result = stmt.query_row(params![public_key_hex], |row| {
|
|
||||||
let secret_key: Vec<u8> = row.get(0)?;
|
|
||||||
Ok(secret_key)
|
|
||||||
});
|
|
||||||
|
|
||||||
match result {
|
|
||||||
Ok(secret_key) => {
|
|
||||||
let bytes: [u8; 32] = secret_key
|
|
||||||
.try_into()
|
|
||||||
.map_err(|_| StorageError::InvalidData("Invalid secret key length".into()))?;
|
|
||||||
Ok(Some(StaticSecret::from(bytes)))
|
|
||||||
}
|
|
||||||
Err(RusqliteError::QueryReturnedNoRows) => Ok(None),
|
|
||||||
Err(e) => Err(e.into()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Loads all inbox ephemeral keys.
|
/// Loads all inbox ephemeral keys.
|
||||||
pub fn load_all_inbox_keys(&self) -> Result<HashMap<String, StaticSecret>, StorageError> {
|
pub fn load_all_inbox_keys(&self) -> Result<HashMap<String, StaticSecret>, StorageError> {
|
||||||
let mut stmt = self
|
let mut stmt = self
|
||||||
@ -168,15 +136,6 @@ impl ChatStorage {
|
|||||||
Ok(keys)
|
Ok(keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deletes an inbox ephemeral key (after it has been used).
|
|
||||||
pub fn delete_inbox_key(&mut self, public_key_hex: &str) -> Result<(), StorageError> {
|
|
||||||
self.db.connection().execute(
|
|
||||||
"DELETE FROM inbox_keys WHERE public_key_hex = ?1",
|
|
||||||
params![public_key_hex],
|
|
||||||
)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==================== Chat Metadata Operations ====================
|
// ==================== Chat Metadata Operations ====================
|
||||||
|
|
||||||
/// Saves a chat record.
|
/// Saves a chat record.
|
||||||
@ -195,47 +154,6 @@ impl ChatStorage {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Loads a chat record by ID.
|
|
||||||
pub fn load_chat(&self, chat_id: &str) -> Result<Option<ChatRecord>, StorageError> {
|
|
||||||
let mut stmt = self.db.connection().prepare(
|
|
||||||
"SELECT chat_id, chat_type, remote_public_key, remote_address, created_at
|
|
||||||
FROM chats WHERE chat_id = ?1",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let result = stmt.query_row(params![chat_id], |row| {
|
|
||||||
let chat_id: String = row.get(0)?;
|
|
||||||
let chat_type: String = row.get(1)?;
|
|
||||||
let remote_public_key: Option<Vec<u8>> = row.get(2)?;
|
|
||||||
let remote_address: String = row.get(3)?;
|
|
||||||
let created_at: i64 = row.get(4)?;
|
|
||||||
Ok((
|
|
||||||
chat_id,
|
|
||||||
chat_type,
|
|
||||||
remote_public_key,
|
|
||||||
remote_address,
|
|
||||||
created_at,
|
|
||||||
))
|
|
||||||
});
|
|
||||||
|
|
||||||
match result {
|
|
||||||
Ok((chat_id, chat_type, remote_public_key, remote_address, created_at)) => {
|
|
||||||
let remote_public_key = remote_public_key.map(|bytes| {
|
|
||||||
let arr: [u8; 32] = bytes.try_into().expect("Invalid key length");
|
|
||||||
arr
|
|
||||||
});
|
|
||||||
Ok(Some(ChatRecord {
|
|
||||||
chat_id,
|
|
||||||
chat_type,
|
|
||||||
remote_public_key,
|
|
||||||
remote_address,
|
|
||||||
created_at,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
Err(RusqliteError::QueryReturnedNoRows) => Ok(None),
|
|
||||||
Err(e) => Err(e.into()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Lists all chat IDs.
|
/// Lists all chat IDs.
|
||||||
pub fn list_chat_ids(&self) -> Result<Vec<String>, StorageError> {
|
pub fn list_chat_ids(&self) -> Result<Vec<String>, StorageError> {
|
||||||
let mut stmt = self.db.connection().prepare("SELECT chat_id FROM chats")?;
|
let mut stmt = self.db.connection().prepare("SELECT chat_id FROM chats")?;
|
||||||
@ -276,7 +194,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_identity_roundtrip() {
|
fn test_identity_roundtrip() {
|
||||||
let mut storage = ChatStorage::in_memory().unwrap();
|
let mut storage = ChatStorage::new(StorageConfig::InMemory).unwrap();
|
||||||
|
|
||||||
// Initially no identity
|
// Initially no identity
|
||||||
assert!(storage.load_identity().unwrap().is_none());
|
assert!(storage.load_identity().unwrap().is_none());
|
||||||
@ -293,7 +211,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_chat_roundtrip() {
|
fn test_chat_roundtrip() {
|
||||||
let mut storage = ChatStorage::in_memory().unwrap();
|
let mut storage = ChatStorage::new(StorageConfig::InMemory).unwrap();
|
||||||
|
|
||||||
let secret = x25519_dalek::StaticSecret::random();
|
let secret = x25519_dalek::StaticSecret::random();
|
||||||
let remote_key = x25519_dalek::PublicKey::from(&secret);
|
let remote_key = x25519_dalek::PublicKey::from(&secret);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user