crypto: More cleanup in the sqlite store.

master
Damir Jelić 2020-03-27 14:35:51 +01:00
parent e4dcca550c
commit c3654bd03f
1 changed files with 15 additions and 9 deletions

View File

@ -132,7 +132,7 @@ impl SqliteStore {
let loaded_sessions = self.sessions.get(sender_key).is_some(); let loaded_sessions = self.sessions.get(sender_key).is_some();
if !loaded_sessions { if !loaded_sessions {
let sessions = self.load_session_for(sender_key).await?; let sessions = self.load_sessions_for(sender_key).await?;
if !sessions.is_empty() { if !sessions.is_empty() {
self.sessions.set_for_sender(sender_key, sessions); self.sessions.set_for_sender(sender_key, sessions);
@ -142,12 +142,13 @@ impl SqliteStore {
Ok(self.sessions.get(sender_key)) Ok(self.sessions.get(sender_key))
} }
async fn load_session_for(&mut self, sender_key: &str) -> Result<Vec<Arc<Mutex<Session>>>> { async fn load_sessions_for(&mut self, sender_key: &str) -> Result<Vec<Arc<Mutex<Session>>>> {
let account_id = self.account_id.ok_or(CryptoStoreError::AccountUnset)?; let account_id = self.account_id.ok_or(CryptoStoreError::AccountUnset)?;
let mut connection = self.connection.lock().await; let mut connection = self.connection.lock().await;
let rows: Vec<(String, String, String, String)> = query_as( let rows: Vec<(String, String, String, String)> = query_as(
"SELECT pickle, sender_key, creation_time, last_use_time FROM sessions WHERE account_id = ? and sender_key = ?" "SELECT pickle, sender_key, creation_time, last_use_time
FROM sessions WHERE account_id = ? and sender_key = ?",
) )
.bind(account_id) .bind(account_id)
.bind(sender_key) .bind(sender_key)
@ -442,7 +443,12 @@ mod test {
let sess = session.lock().await; let sess = session.lock().await;
let sessions = store.load_sessions().await.expect("Can't load sessions"); let sessions = store
assert!(sessions.contains(&sess)); .load_sessions_for(&sess.sender_key)
.await
.expect("Can't load sessions");
let loaded_session = &sessions[0];
assert_eq!(*sess, *loaded_session.lock().await);
} }
} }