vector-core 0.11.0

Core library for Vector: the single source of truth for all Vector clients, SDKs and interfaces.
Documentation
//! ID cache — maps chat identifiers and npubs to SQLite row IDs.
//!
//! All lookups are cached in memory after first DB hit. Caches are
//! preloaded at boot and cleared on account switch.

use std::collections::HashMap;
use std::sync::{Arc, RwLock};

use crate::profile::NpubMap;

// Row ids belong to ONE account's database. Resolving a chat or npub against
// another account's cache returns a live id for the wrong row, which reads and
// writes then follow silently.
struct ChatIds;
struct UserIds;

fn chat_id_cache() -> Arc<RwLock<HashMap<String, i64>>> {
    crate::db::current_session().scoped::<ChatIds, _>()
}

fn user_id_cache() -> Arc<RwLock<NpubMap<i64>>> {
    crate::db::current_session().scoped::<UserIds, _>()
}

/// Drop a chat's cached identifier→id mapping. Call after deleting a chat row so
/// a later recreate doesn't reuse the stale (now-deleted) integer id.
pub fn forget_chat_id(chat_identifier: &str) {
    chat_id_cache().write().unwrap().remove(chat_identifier);
}

/// Lookup-only: get integer chat ID from identifier. Errors if not found.
pub fn get_chat_id_by_identifier(chat_identifier: &str) -> Result<i64, String> {
    // Fast path: cache hit
    {
        let owner = chat_id_cache();
        let cache = owner.read().unwrap();
        if let Some(&id) = cache.get(chat_identifier) {
            return Ok(id);
        }
    }

    // Cache miss: query DB
    let conn = super::get_db_connection_guard_static()?;
    let id: i64 = conn.query_row(
        "SELECT id FROM chats WHERE chat_identifier = ?1",
        rusqlite::params![chat_identifier],
        |row| row.get(0)
    ).map_err(|_| format!("Chat not found: {}", chat_identifier))?;

    // Update cache
    {
        let owner = chat_id_cache();
        let mut cache = owner.write().unwrap();
        cache.insert(chat_identifier.to_string(), id);
    }

    Ok(id)
}

/// Get or create integer chat ID from identifier.
pub fn get_or_create_chat_id(chat_identifier: &str) -> Result<i64, String> {
    // Fast path: cache hit
    {
        let owner = chat_id_cache();
        let cache = owner.read().unwrap();
        if let Some(&id) = cache.get(chat_identifier) {
            return Ok(id);
        }
    }

    let conn = super::get_db_connection_guard_static()?;

    // Try existing
    let existing: Option<i64> = conn.query_row(
        "SELECT id FROM chats WHERE chat_identifier = ?1",
        rusqlite::params![chat_identifier],
        |row| row.get(0)
    ).ok();

    let id = if let Some(id) = existing {
        id
    } else {
        // Create stub chat entry. Discriminant must match ChatType::to_i32:
        // 0 = DirectMessage (npub), 2 = Community (non-npub). Value 1 was the
        // retired MlsGroup variant and is dropped by the get_all_chats load filter,
        // so a non-npub stub MUST be 2 or the chat vanishes on reload.
        let now = crate::clock::now_secs() as i64;
        let chat_type: i32 = if chat_identifier.starts_with("npub1") { 0 } else { 2 };

        // A DM stub's id IS its counterparty: write the participant now, or the
        // row boots with an empty roster and every participant-keyed lookup
        // (attachment downloads) misses forever.
        let participants = if chat_type == 0 {
            format!("[\"{}\"]", chat_identifier)
        } else {
            "[]".to_string()
        };

        conn.execute(
            "INSERT INTO chats (chat_identifier, chat_type, participants, created_at) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params![chat_identifier, chat_type, participants, now],
        ).map_err(|e| format!("Failed to create chat stub: {}", e))?;

        conn.last_insert_rowid()
    };

    // Update cache
    {
        let owner = chat_id_cache();
        let mut cache = owner.write().unwrap();
        cache.insert(chat_identifier.to_string(), id);
    }

    Ok(id)
}

/// Get or create integer user ID from npub. Returns None for empty npub.
pub fn get_or_create_user_id(npub: &str) -> Result<Option<i64>, String> {
    if npub.is_empty() {
        return Ok(None);
    }

    // Fast path: cache hit
    {
        let owner = user_id_cache();
        let cache = owner.read().unwrap();
        if let Some(&id) = cache.get(npub) {
            return Ok(Some(id));
        }
    }

    let conn = super::get_db_connection_guard_static()?;

    let existing: Option<i64> = conn.query_row(
        "SELECT id FROM profiles WHERE npub = ?1",
        rusqlite::params![npub],
        |row| row.get(0)
    ).ok();

    let id = if let Some(id) = existing {
        id
    } else {
        conn.execute(
            "INSERT INTO profiles (npub, name, display_name) VALUES (?1, '', '')",
            rusqlite::params![npub],
        ).map_err(|e| format!("Failed to create profile stub: {}", e))?;
        conn.last_insert_rowid()
    };

    // Update cache
    {
        let owner = user_id_cache();
        let mut cache = owner.write().unwrap();
        cache.insert(npub, id);
    }

    Ok(Some(id))
}

/// Preload all ID mappings into memory cache (call at boot).
pub fn preload_id_caches() -> Result<(), String> {
    let conn = match super::get_db_connection_guard_static() {
        Ok(c) => c,
        Err(_) => return Ok(()), // No DB yet, skip
    };

    // Load chat ID mappings
    {
        let mut stmt = conn.prepare("SELECT chat_identifier, id FROM chats")
            .map_err(|e| format!("Failed to prepare chat query: {}", e))?;
        let rows = stmt.query_map([], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
        }).map_err(|e| format!("Failed to query chats: {}", e))?;

        let owner = chat_id_cache();
        let mut cache = owner.write().unwrap();
        for row in rows.flatten() {
            cache.insert(row.0, row.1);
        }
    }

    // Load user ID mappings
    {
        let mut stmt = conn.prepare("SELECT npub, id FROM profiles")
            .map_err(|e| format!("Failed to prepare user query: {}", e))?;
        let rows = stmt.query_map([], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, i64>(1)?))
        }).map_err(|e| format!("Failed to query profiles: {}", e))?;

        let owner = user_id_cache();
        let mut cache = owner.write().unwrap();
        for row in rows.flatten() {
            cache.insert(&row.0, row.1);
        }
    }

    Ok(())
}

/// Clear all ID caches (call on account switch).
pub fn clear_id_caches() {
    chat_id_cache().write().unwrap().clear();
    user_id_cache().write().unwrap().clear();
}

#[cfg(test)]
mod tests {
    use super::*;
    use nostr_sdk::prelude::{Keys, ToBech32};

    fn init_test_db() -> (tempfile::TempDir, std::sync::MutexGuard<'static, ()>) {
        let guard = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
        crate::db::close_database();
        crate::db::clear_id_caches();
        let tmp = tempfile::tempdir().unwrap();
        let account = Keys::generate().public_key().to_bech32().unwrap();
        crate::db::set_app_data_dir(crate::db::shared_test_data_dir().to_path_buf());
        crate::db::set_current_account(account.clone()).unwrap();
        crate::db::init_database(&account).unwrap();
        (tmp, guard)
    }

    fn drop_row(npub: &str) {
        crate::db::get_write_connection_guard_static()
            .unwrap()
            .execute("DELETE FROM profiles WHERE npub = ?1", rusqlite::params![npub])
            .unwrap();
    }

    #[test]
    fn user_ids_are_cached_per_npub_and_never_merged() {
        let (_tmp, _guard) = init_test_db();
        let a = Keys::generate().public_key().to_bech32().unwrap();
        let b = Keys::generate().public_key().to_bech32().unwrap();
        let id_a = get_or_create_user_id(&a).unwrap().unwrap();
        let id_b = get_or_create_user_id(&b).unwrap().unwrap();
        assert_ne!(id_a, id_b);

        let upper = a.to_uppercase();
        let id_upper = get_or_create_user_id(&upper).unwrap().unwrap();
        assert_ne!(id_upper, id_a, "another spelling is another row, never the same key");
        let id_fake = get_or_create_user_id("npub1notbech32").unwrap().unwrap();

        for npub in [&a, &b, &upper] {
            drop_row(npub);
        }
        drop_row("npub1notbech32");
        assert_eq!(get_or_create_user_id(&a).unwrap(), Some(id_a), "served from the cache");
        assert_eq!(get_or_create_user_id(&b).unwrap(), Some(id_b));
        assert_eq!(get_or_create_user_id(&upper).unwrap(), Some(id_upper));
        assert_eq!(get_or_create_user_id("npub1notbech32").unwrap(), Some(id_fake));
        assert_eq!(get_or_create_user_id("").unwrap(), None);
    }

    #[test]
    fn preload_fills_the_user_cache_from_the_table() {
        let (_tmp, _guard) = init_test_db();
        let a = Keys::generate().public_key().to_bech32().unwrap();
        let id_a = get_or_create_user_id(&a).unwrap().unwrap();
        let id_fake = get_or_create_user_id("npub1legacy").unwrap().unwrap();
        clear_id_caches();
        preload_id_caches().unwrap();
        drop_row(&a);
        drop_row("npub1legacy");
        assert_eq!(get_or_create_user_id(&a).unwrap(), Some(id_a));
        assert_eq!(get_or_create_user_id("npub1legacy").unwrap(), Some(id_fake));
    }
}