huskies: merge 670_refactor_hoist_chat_history_persistence_into_a_shared_module_replaces_658
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
//! WhatsApp conversation history — per-number message history and messaging window tracking.
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex as TokioMutex;
|
||||
|
||||
use crate::chat::history::{load_chat_history, save_chat_history};
|
||||
use crate::chat::transport::matrix::RoomConversation;
|
||||
use crate::slog;
|
||||
|
||||
// ── Messaging window tracker ─────────────────────────────────────────────
|
||||
|
||||
@@ -73,30 +72,12 @@ impl MessagingWindowTracker {
|
||||
/// Per-sender conversation history, keyed by phone number.
|
||||
pub type WhatsAppConversationHistory = Arc<TokioMutex<HashMap<String, RoomConversation>>>;
|
||||
|
||||
/// On-disk format for persisted WhatsApp conversation history.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct PersistedWhatsAppHistory {
|
||||
senders: HashMap<String, RoomConversation>,
|
||||
}
|
||||
|
||||
/// Path to the persisted WhatsApp conversation history file.
|
||||
const WHATSAPP_HISTORY_FILE: &str = ".huskies/whatsapp_history.json";
|
||||
|
||||
/// Load WhatsApp conversation history from disk.
|
||||
pub fn load_whatsapp_history(project_root: &std::path::Path) -> HashMap<String, RoomConversation> {
|
||||
let path = project_root.join(WHATSAPP_HISTORY_FILE);
|
||||
let data = match std::fs::read_to_string(&path) {
|
||||
Ok(d) => d,
|
||||
Err(_) => return HashMap::new(),
|
||||
};
|
||||
let persisted: PersistedWhatsAppHistory = match serde_json::from_str(&data) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
slog!("[whatsapp] Failed to parse history file: {e}");
|
||||
return HashMap::new();
|
||||
}
|
||||
};
|
||||
persisted.senders
|
||||
load_chat_history(project_root, WHATSAPP_HISTORY_FILE, "whatsapp")
|
||||
}
|
||||
|
||||
/// Save WhatsApp conversation history to disk.
|
||||
@@ -104,18 +85,7 @@ pub(super) fn save_whatsapp_history(
|
||||
project_root: &std::path::Path,
|
||||
history: &HashMap<String, RoomConversation>,
|
||||
) {
|
||||
let persisted = PersistedWhatsAppHistory {
|
||||
senders: history.clone(),
|
||||
};
|
||||
let path = project_root.join(WHATSAPP_HISTORY_FILE);
|
||||
match serde_json::to_string_pretty(&persisted) {
|
||||
Ok(json) => {
|
||||
if let Err(e) = std::fs::write(&path, json) {
|
||||
slog!("[whatsapp] Failed to write history file: {e}");
|
||||
}
|
||||
}
|
||||
Err(e) => slog!("[whatsapp] Failed to serialise history: {e}"),
|
||||
}
|
||||
save_chat_history(project_root, WHATSAPP_HISTORY_FILE, "whatsapp", history);
|
||||
}
|
||||
|
||||
// ── Tests ───────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user