huskies: merge 670_refactor_hoist_chat_history_persistence_into_a_shared_module_replaces_658
This commit is contained in:
@@ -1,40 +1,21 @@
|
||||
//! Slack conversation history persistence.
|
||||
|
||||
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;
|
||||
|
||||
/// Per-channel conversation history, keyed by channel ID.
|
||||
pub type SlackConversationHistory = Arc<TokioMutex<HashMap<String, RoomConversation>>>;
|
||||
|
||||
/// On-disk format for persisted Slack conversation history.
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct PersistedSlackHistory {
|
||||
channels: HashMap<String, RoomConversation>,
|
||||
}
|
||||
|
||||
/// Path to the persisted Slack conversation history file.
|
||||
const SLACK_HISTORY_FILE: &str = ".huskies/slack_history.json";
|
||||
|
||||
/// Load Slack conversation history from disk.
|
||||
pub fn load_slack_history(project_root: &std::path::Path) -> HashMap<String, RoomConversation> {
|
||||
let path = project_root.join(SLACK_HISTORY_FILE);
|
||||
let data = match std::fs::read_to_string(&path) {
|
||||
Ok(d) => d,
|
||||
Err(_) => return HashMap::new(),
|
||||
};
|
||||
let persisted: PersistedSlackHistory = match serde_json::from_str(&data) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
slog!("[slack] Failed to parse history file: {e}");
|
||||
return HashMap::new();
|
||||
}
|
||||
};
|
||||
persisted.channels
|
||||
load_chat_history(project_root, SLACK_HISTORY_FILE, "slack")
|
||||
}
|
||||
|
||||
/// Save Slack conversation history to disk.
|
||||
@@ -42,18 +23,7 @@ pub(super) fn save_slack_history(
|
||||
project_root: &std::path::Path,
|
||||
history: &HashMap<String, RoomConversation>,
|
||||
) {
|
||||
let persisted = PersistedSlackHistory {
|
||||
channels: history.clone(),
|
||||
};
|
||||
let path = project_root.join(SLACK_HISTORY_FILE);
|
||||
match serde_json::to_string_pretty(&persisted) {
|
||||
Ok(json) => {
|
||||
if let Err(e) = std::fs::write(&path, json) {
|
||||
slog!("[slack] Failed to write history file: {e}");
|
||||
}
|
||||
}
|
||||
Err(e) => slog!("[slack] Failed to serialise history: {e}"),
|
||||
}
|
||||
save_chat_history(project_root, SLACK_HISTORY_FILE, "slack", history);
|
||||
}
|
||||
|
||||
// ── Tests ───────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user