mirror of
https://gitlab.com/ArkHost/HelixNotes.git
synced 2026-11-21 19:04:56 +01:00
Improve editor performance and harden note saving
Replace the source editor with CodeMirror and reduce typing-time work. Virtualize note lists, apply targeted refreshes, and batch search indexing. Improve vault and in-note search, and bound graph layout work. Make saves revision-safe and flush pending changes before navigation or close. Preserve originating-vault ownership across async operations and detached windows. Harden sync, attachment writes, and notebook/tag appearance updates. Keep Trash previews read-only and skip restoring trashed notes. Update AI model presets and isolate streamed responses by request. Fix local image rendering and preserve portable image paths. Include regression tests for the changed behavior.
This commit is contained in:
1 parent
3607cf1b3f
commit
640b5e3d10
63 files changed
+9382
-3145
No files matched your search
Generated
+4
-1
@@ -1,6 +1,6 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "adler2"
|
||||
@@ -1778,6 +1778,7 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
name = "helixnotes"
|
||||
version = "1.3.5"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"arboard",
|
||||
"chrono",
|
||||
"dirs",
|
||||
@@ -1787,6 +1788,7 @@ dependencies = [
|
||||
"log",
|
||||
"notify",
|
||||
"objc2-app-kit",
|
||||
"parking_lot",
|
||||
"png 0.17.16",
|
||||
"quick-xml",
|
||||
"rayon",
|
||||
@@ -1809,6 +1811,7 @@ dependencies = [
|
||||
"tauri-plugin-single-instance",
|
||||
"tauri-plugin-updater",
|
||||
"tauri-plugin-window-state",
|
||||
"tempfile",
|
||||
"tokio",
|
||||
"urlencoding",
|
||||
"uuid",
|
||||
|
||||
@@ -32,7 +32,9 @@ serde_json = "1"
|
||||
serde_yaml = "0.9"
|
||||
log = "0.4"
|
||||
notify = "8"
|
||||
parking_lot = "0.12"
|
||||
walkdir = "2"
|
||||
tempfile = "3"
|
||||
chrono = { version = "0.4", features = ["serde", "unstable-locales"] }
|
||||
sys-locale = "0.3"
|
||||
uuid = { version = "1", features = ["v4"] }
|
||||
@@ -41,6 +43,7 @@ gray_matter = "0.2"
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
dirs = "6"
|
||||
regex = "1"
|
||||
aho-corasick = "1"
|
||||
zip = { version = "2", default-features = false, features = ["deflate"] }
|
||||
reqwest = { version = "0.12", default-features = false, features = ["json", "stream", "rustls-tls", "blocking"] }
|
||||
futures = "0.3"
|
||||
|
||||
+78
-46
@@ -89,6 +89,7 @@ pub fn ai_request(
|
||||
let _ = app.emit(
|
||||
"ai-stream",
|
||||
AiStreamEvent {
|
||||
request_id: request_id.clone(),
|
||||
event_type: "error".to_string(),
|
||||
text: None,
|
||||
error: Some(e),
|
||||
@@ -109,13 +110,40 @@ fn normalize_openai_base(base: &str) -> String {
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn openai_request_body(
|
||||
model: &str,
|
||||
system_prompt: Option<&str>,
|
||||
user_message: &str,
|
||||
stream: bool,
|
||||
max_tokens: u32,
|
||||
) -> serde_json::Value {
|
||||
// GPT-5/6 reject legacy output limits and sampling with their default reasoning.
|
||||
let reasoning = model.starts_with("gpt-5") || model.starts_with("gpt-6");
|
||||
let token_key = if reasoning {
|
||||
"max_completion_tokens"
|
||||
} else {
|
||||
"max_tokens"
|
||||
};
|
||||
let mut messages = Vec::with_capacity(if system_prompt.is_some() { 2 } else { 1 });
|
||||
if let Some(system) = system_prompt {
|
||||
messages.push(json!({ "role": "system", "content": system }));
|
||||
}
|
||||
messages.push(json!({ "role": "user", "content": user_message }));
|
||||
let mut body =
|
||||
json!({ "model": model, "stream": stream, token_key: max_tokens, "messages": messages });
|
||||
if stream && !reasoning {
|
||||
body["temperature"] = json!(0.7);
|
||||
}
|
||||
body
|
||||
}
|
||||
|
||||
async fn stream_anthropic(
|
||||
app: &AppHandle,
|
||||
api_key: &str,
|
||||
model: &str,
|
||||
system_prompt: &str,
|
||||
user_message: &str,
|
||||
_request_id: &str,
|
||||
request_id: &str,
|
||||
) -> Result<(), String> {
|
||||
let client = Client::new();
|
||||
|
||||
@@ -168,6 +196,7 @@ async fn stream_anthropic(
|
||||
let _ = app.emit(
|
||||
"ai-stream",
|
||||
AiStreamEvent {
|
||||
request_id: request_id.to_owned(),
|
||||
event_type: "done".to_string(),
|
||||
text: None,
|
||||
error: None,
|
||||
@@ -185,6 +214,7 @@ async fn stream_anthropic(
|
||||
let _ = app.emit(
|
||||
"ai-stream",
|
||||
AiStreamEvent {
|
||||
request_id: request_id.to_owned(),
|
||||
event_type: "text".to_string(),
|
||||
text: Some(text.to_string()),
|
||||
error: None,
|
||||
@@ -196,6 +226,7 @@ async fn stream_anthropic(
|
||||
let _ = app.emit(
|
||||
"ai-stream",
|
||||
AiStreamEvent {
|
||||
request_id: request_id.to_owned(),
|
||||
event_type: "done".to_string(),
|
||||
text: None,
|
||||
error: None,
|
||||
@@ -210,6 +241,7 @@ async fn stream_anthropic(
|
||||
let _ = app.emit(
|
||||
"ai-stream",
|
||||
AiStreamEvent {
|
||||
request_id: request_id.to_owned(),
|
||||
event_type: "error".to_string(),
|
||||
text: None,
|
||||
error: Some(msg.to_string()),
|
||||
@@ -228,6 +260,7 @@ async fn stream_anthropic(
|
||||
let _ = app.emit(
|
||||
"ai-stream",
|
||||
AiStreamEvent {
|
||||
request_id: request_id.to_owned(),
|
||||
event_type: "done".to_string(),
|
||||
text: None,
|
||||
error: None,
|
||||
@@ -244,37 +277,11 @@ async fn stream_openai(
|
||||
model: &str,
|
||||
system_prompt: &str,
|
||||
user_message: &str,
|
||||
_request_id: &str,
|
||||
request_id: &str,
|
||||
) -> Result<(), String> {
|
||||
let client = Client::new();
|
||||
|
||||
let is_gpt5 = model.starts_with("gpt-5");
|
||||
let token_key = if is_gpt5 {
|
||||
"max_completion_tokens"
|
||||
} else {
|
||||
"max_tokens"
|
||||
};
|
||||
|
||||
let mut body = json!({
|
||||
"model": model,
|
||||
"stream": true,
|
||||
token_key: 4096,
|
||||
"messages": [
|
||||
{
|
||||
"role": "system",
|
||||
"content": system_prompt
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": user_message
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
// GPT-5 models don't support temperature
|
||||
if !is_gpt5 {
|
||||
body["temperature"] = json!(0.7);
|
||||
}
|
||||
let body = openai_request_body(model, Some(system_prompt), user_message, true, 4096);
|
||||
|
||||
let mut req = client.post(url).header("content-type", "application/json");
|
||||
|
||||
@@ -312,6 +319,7 @@ async fn stream_openai(
|
||||
let _ = app.emit(
|
||||
"ai-stream",
|
||||
AiStreamEvent {
|
||||
request_id: request_id.to_owned(),
|
||||
event_type: "done".to_string(),
|
||||
text: None,
|
||||
error: None,
|
||||
@@ -327,6 +335,7 @@ async fn stream_openai(
|
||||
let _ = app.emit(
|
||||
"ai-stream",
|
||||
AiStreamEvent {
|
||||
request_id: request_id.to_owned(),
|
||||
event_type: "text".to_string(),
|
||||
text: Some(content.to_string()),
|
||||
error: None,
|
||||
@@ -341,6 +350,7 @@ async fn stream_openai(
|
||||
let _ = app.emit(
|
||||
"ai-stream",
|
||||
AiStreamEvent {
|
||||
request_id: request_id.to_owned(),
|
||||
event_type: "done".to_string(),
|
||||
text: None,
|
||||
error: None,
|
||||
@@ -355,6 +365,7 @@ async fn stream_openai(
|
||||
let _ = app.emit(
|
||||
"ai-stream",
|
||||
AiStreamEvent {
|
||||
request_id: request_id.to_owned(),
|
||||
event_type: "error".to_string(),
|
||||
text: None,
|
||||
error: Some(err.to_string()),
|
||||
@@ -371,6 +382,7 @@ async fn stream_openai(
|
||||
let _ = app.emit(
|
||||
"ai-stream",
|
||||
AiStreamEvent {
|
||||
request_id: request_id.to_owned(),
|
||||
event_type: "done".to_string(),
|
||||
text: None,
|
||||
error: None,
|
||||
@@ -445,23 +457,7 @@ async fn test_anthropic(api_key: &str, model: &str) -> Result<String, String> {
|
||||
|
||||
async fn test_openai(url: &str, api_key: Option<&str>, model: &str) -> Result<String, String> {
|
||||
let client = Client::new();
|
||||
let is_gpt5 = model.starts_with("gpt-5");
|
||||
let token_key = if is_gpt5 {
|
||||
"max_completion_tokens"
|
||||
} else {
|
||||
"max_tokens"
|
||||
};
|
||||
|
||||
let body = json!({
|
||||
"model": model,
|
||||
token_key: 20,
|
||||
"messages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "Hi"
|
||||
}
|
||||
]
|
||||
});
|
||||
let body = openai_request_body(model, None, "Hi", false, 20);
|
||||
|
||||
let mut req = client.post(url).header("content-type", "application/json");
|
||||
|
||||
@@ -483,3 +479,39 @@ async fn test_openai(url: &str, api_key: Option<&str>, model: &str) -> Result<St
|
||||
Err(format!("API error {}: {}", status, body_text))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod request_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn gpt6_streaming_requests_use_supported_parameters() {
|
||||
let body = openai_request_body(
|
||||
"gpt-6-astra",
|
||||
Some("Return Markdown"),
|
||||
"Revise this",
|
||||
true,
|
||||
4096,
|
||||
);
|
||||
assert_eq!(body["max_completion_tokens"], 4096);
|
||||
assert!(body.get("max_tokens").is_none());
|
||||
assert!(body.get("temperature").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prior_openai_and_local_models_keep_their_parameter_contract() {
|
||||
let prior = openai_request_body("gpt-5.5", None, "Hi", false, 20);
|
||||
assert!(prior.get("max_completion_tokens").is_some());
|
||||
assert!(prior.get("max_tokens").is_none());
|
||||
let local = openai_request_body(
|
||||
"gemma3:4b",
|
||||
Some("Return Markdown"),
|
||||
"Revise this",
|
||||
true,
|
||||
4096,
|
||||
);
|
||||
assert_eq!(local["max_tokens"], 4096);
|
||||
assert_eq!(local["temperature"], 0.7);
|
||||
assert!(local.get("max_completion_tokens").is_none());
|
||||
}
|
||||
}
|
||||
+1902
-585
File diff suppressed because it is too large.
Load diff
+36
-23
@@ -37,8 +37,6 @@ pub fn run() {
|
||||
let config = commands::load_app_config();
|
||||
#[cfg(desktop)]
|
||||
let show_tray = config.show_tray_icon;
|
||||
#[cfg(desktop)]
|
||||
let close_to_tray = config.close_to_tray && show_tray;
|
||||
let app_state = AppState::new(config);
|
||||
|
||||
// Inject the compile-time platform so the frontend never sniffs the (sometimes
|
||||
@@ -164,6 +162,7 @@ pub fn run() {
|
||||
commands::restore_external_vault,
|
||||
commands::remove_vault,
|
||||
commands::get_app_config,
|
||||
commands::complete_window_close,
|
||||
commands::set_theme,
|
||||
commands::set_system_themes,
|
||||
commands::set_accent_color,
|
||||
@@ -216,8 +215,10 @@ pub fn run() {
|
||||
commands::save_attachment,
|
||||
commands::get_notebook_icons,
|
||||
commands::set_notebook_icon,
|
||||
commands::import_notebook_icon,
|
||||
commands::get_tag_styles,
|
||||
commands::set_tag_style,
|
||||
commands::import_tag_icon,
|
||||
commands::set_general_settings,
|
||||
commands::get_quick_access,
|
||||
commands::add_quick_access,
|
||||
@@ -337,32 +338,44 @@ pub fn run() {
|
||||
queue_macos_traffic_light_fix(window);
|
||||
}
|
||||
|
||||
match event {
|
||||
tauri::WindowEvent::CloseRequested { api, .. } => {
|
||||
// Only hide to tray for the main window
|
||||
if close_to_tray && window.label() == "main" {
|
||||
api.prevent_close();
|
||||
let _ = window.hide();
|
||||
// Closing the main window also requests a save-aware close of note windows.
|
||||
if matches!(event, tauri::WindowEvent::Destroyed) && window.label() == "main" {
|
||||
for (label, win) in window.app_handle().webview_windows() {
|
||||
if label.starts_with("note-") {
|
||||
let _ = win.close();
|
||||
}
|
||||
}
|
||||
tauri::WindowEvent::Destroyed
|
||||
// When main window is destroyed, close all note windows
|
||||
if window.label() == "main" => {
|
||||
let app = window.app_handle();
|
||||
for (label, win) in app.webview_windows() {
|
||||
if label.starts_with("note-") {
|
||||
let _ = win.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
builder
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
let app = builder
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while building tauri application");
|
||||
app.run(|app, event| {
|
||||
#[cfg(desktop)]
|
||||
if let tauri::RunEvent::ExitRequested {
|
||||
code: Some(_), api, ..
|
||||
} = event
|
||||
{
|
||||
let windows = app.webview_windows();
|
||||
if !windows.is_empty() {
|
||||
// Menu/tray Quit must give each editor the same save opportunity
|
||||
// as a window close. The app exits once the last window closes.
|
||||
api.prevent_exit();
|
||||
app.state::<AppState>()
|
||||
.quitting
|
||||
.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
for window in windows.values() {
|
||||
if let Err(error) = window.close() {
|
||||
log::error!("Failed to close window during quit: {error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(mobile)]
|
||||
let _ = (app, event);
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(desktop)]
|
||||
@@ -371,7 +384,7 @@ fn setup_tray(app: &mut tauri::App) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?;
|
||||
let menu = MenuBuilder::new(app).items(&[&show, &quit]).build()?;
|
||||
|
||||
let _tray = TrayIconBuilder::new()
|
||||
let _tray = TrayIconBuilder::with_id("helixnotes")
|
||||
.icon(
|
||||
Image::from_path("icons/32x32.png")
|
||||
.unwrap_or_else(|_| app.default_window_icon().cloned().unwrap()),
|
||||
|
||||
+1302
-216
File diff suppressed because it is too large.
Load diff
+133
-3
@@ -1,30 +1,160 @@
|
||||
use crate::search::SearchIndex;
|
||||
use crate::search::{SearchIndex, SearchIndexShutdown};
|
||||
use crate::types::AppConfig;
|
||||
use crate::vault::watcher::VaultWatcher;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::{Arc, Mutex, Weak};
|
||||
|
||||
struct CachedSearchIndex {
|
||||
index: Weak<SearchIndex>,
|
||||
shutdown: Arc<SearchIndexShutdown>,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
pub config: Mutex<AppConfig>,
|
||||
pub search_index: Mutex<Option<Arc<SearchIndex>>>,
|
||||
search_indexes: Mutex<HashMap<String, CachedSearchIndex>>,
|
||||
pub watcher: Mutex<Option<VaultWatcher>>,
|
||||
pub vault_transition: tokio::sync::Mutex<()>,
|
||||
pub note_save: Arc<tokio::sync::Mutex<()>>,
|
||||
pub importing: AtomicBool,
|
||||
pub syncing: AtomicBool,
|
||||
#[cfg(desktop)]
|
||||
pub quitting: AtomicBool,
|
||||
pub pending_open_file: Mutex<Option<String>>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
// A refresh worker may outlive a vault switch. Reuse its writer when opening
|
||||
// that vault again instead of competing for Tantivy's exclusive writer lock.
|
||||
pub fn search_index_for_vault(&self, vault_path: &str) -> Result<Arc<SearchIndex>, String> {
|
||||
let mut indexes = self
|
||||
.search_indexes
|
||||
.lock()
|
||||
.map_err(|error| error.to_string())?;
|
||||
indexes
|
||||
.retain(|_, cached| cached.index.strong_count() > 0 || !cached.shutdown.is_finished());
|
||||
if let Some(cached) = indexes.get(vault_path) {
|
||||
if let Some(index) = cached.index.upgrade() {
|
||||
return Ok(index);
|
||||
}
|
||||
// The last Arc is gone, but its writer may still be shutting down.
|
||||
cached.shutdown.wait();
|
||||
}
|
||||
let index = Arc::new(SearchIndex::new(vault_path)?);
|
||||
indexes.insert(
|
||||
vault_path.to_owned(),
|
||||
CachedSearchIndex {
|
||||
index: Arc::downgrade(&index),
|
||||
shutdown: index.shutdown_signal(),
|
||||
},
|
||||
);
|
||||
Ok(index)
|
||||
}
|
||||
|
||||
pub fn validate_vault(&self, vault_path: &str) -> Result<(), String> {
|
||||
let config = self.config.lock().map_err(|error| error.to_string())?;
|
||||
if config.active_vault.as_deref() == Some(vault_path)
|
||||
|| config.vaults.iter().any(|vault| vault.path == vault_path)
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Unknown vault".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn vault_snapshot(&self, vault_path: &str) -> Result<Option<Arc<SearchIndex>>, String> {
|
||||
let search = self
|
||||
.search_index
|
||||
.lock()
|
||||
.map_err(|error| error.to_string())?;
|
||||
let config = self.config.lock().map_err(|error| error.to_string())?;
|
||||
if config.active_vault.as_deref() == Some(vault_path) {
|
||||
return Ok(search.clone());
|
||||
}
|
||||
if !config.vaults.iter().any(|vault| vault.path == vault_path) {
|
||||
return Err("Unknown vault".to_string());
|
||||
}
|
||||
drop(config);
|
||||
drop(search);
|
||||
// Detached windows retain access to configured vaults. Never pair their
|
||||
// writes with the main window's index, nor hold config during disk I/O.
|
||||
self.search_index_for_vault(vault_path).map(Some)
|
||||
}
|
||||
|
||||
pub fn active_vault_snapshot(&self) -> Result<(String, Option<Arc<SearchIndex>>), String> {
|
||||
// Match vault transition and watcher lock order. The vault and its
|
||||
// optional index must remain paired even if a switch follows the read.
|
||||
let search = self
|
||||
.search_index
|
||||
.lock()
|
||||
.map_err(|error| error.to_string())?;
|
||||
let config = self.config.lock().map_err(|error| error.to_string())?;
|
||||
Ok((
|
||||
config.active_vault.clone().ok_or("No active vault")?,
|
||||
search.clone(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn active_search(&self) -> Result<(Arc<SearchIndex>, String), String> {
|
||||
let (vault_path, search) = self.active_vault_snapshot()?;
|
||||
Ok((search.ok_or("Search index not initialized")?, vault_path))
|
||||
}
|
||||
|
||||
#[cfg(desktop)]
|
||||
pub fn should_hide_to_tray(&self, label: &str, tray_available: bool) -> Result<bool, String> {
|
||||
if label != "main"
|
||||
|| !tray_available
|
||||
|| self.quitting.load(std::sync::atomic::Ordering::Relaxed)
|
||||
{
|
||||
return Ok(false);
|
||||
}
|
||||
let config = self.config.lock().map_err(|error| error.to_string())?;
|
||||
Ok(config.show_tray_icon && config.close_to_tray)
|
||||
}
|
||||
|
||||
pub fn new(config: AppConfig) -> Self {
|
||||
Self {
|
||||
config: Mutex::new(config),
|
||||
search_index: Mutex::new(None),
|
||||
search_indexes: Mutex::new(HashMap::new()),
|
||||
watcher: Mutex::new(None),
|
||||
vault_transition: tokio::sync::Mutex::new(()),
|
||||
note_save: Arc::new(tokio::sync::Mutex::new(())),
|
||||
importing: AtomicBool::new(false),
|
||||
syncing: AtomicBool::new(false),
|
||||
#[cfg(desktop)]
|
||||
quitting: AtomicBool::new(false),
|
||||
pending_open_file: Mutex::new(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, desktop))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn close_policy_respects_live_settings_window_type_and_explicit_quit() {
|
||||
let mut state = AppState::new(AppConfig {
|
||||
show_tray_icon: true,
|
||||
close_to_tray: true,
|
||||
..AppConfig::default()
|
||||
});
|
||||
assert!(state.should_hide_to_tray("main", true).unwrap());
|
||||
assert!(!state.should_hide_to_tray("note-123", true).unwrap());
|
||||
assert!(!state.should_hide_to_tray("main", false).unwrap());
|
||||
state
|
||||
.quitting
|
||||
.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
assert!(!state.should_hide_to_tray("main", true).unwrap());
|
||||
state
|
||||
.quitting
|
||||
.store(false, std::sync::atomic::Ordering::Relaxed);
|
||||
state.config.get_mut().unwrap().close_to_tray = false;
|
||||
assert!(!state.should_hide_to_tray("main", true).unwrap());
|
||||
state.config.get_mut().unwrap().close_to_tray = true;
|
||||
state.config.get_mut().unwrap().show_tray_icon = false;
|
||||
assert!(!state.should_hide_to_tray("main", true).unwrap());
|
||||
}
|
||||
}
|
||||
+478
-46
@@ -12,10 +12,12 @@
|
||||
|
||||
use crate::state::AppState;
|
||||
use crate::vault::operations::helixnotes_dir;
|
||||
use crate::vault::watcher::changed_entry;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::io::{self, Read};
|
||||
use std::path::{Path, PathBuf};
|
||||
use tauri::{Emitter, Manager};
|
||||
use walkdir::WalkDir;
|
||||
@@ -69,15 +71,25 @@ fn save_manifest(vault: &str, m: &SyncManifest) {
|
||||
|
||||
// ───────────────────────── helpers ─────────────────────────
|
||||
|
||||
const HASH_BUFFER_SIZE: usize = 64 * 1024;
|
||||
|
||||
fn sha256_hex(bytes: &[u8]) -> String {
|
||||
let mut h = Sha256::new();
|
||||
h.update(bytes);
|
||||
let digest = h.finalize();
|
||||
let mut s = String::with_capacity(64);
|
||||
for b in digest {
|
||||
s.push_str(&format!("{:02x}", b));
|
||||
format!("{:x}", Sha256::digest(bytes))
|
||||
}
|
||||
|
||||
fn sha256_reader(
|
||||
reader: &mut impl Read,
|
||||
buffer: &mut [u8; HASH_BUFFER_SIZE],
|
||||
) -> io::Result<String> {
|
||||
let mut hasher = Sha256::new();
|
||||
loop {
|
||||
match reader.read(buffer) {
|
||||
Ok(0) => return Ok(format!("{:x}", hasher.finalize())),
|
||||
Ok(count) => hasher.update(&buffer[..count]),
|
||||
Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn normalize_etag(s: &str) -> String {
|
||||
@@ -109,12 +121,63 @@ fn conflict_relpath(relpath: &str, stamp: &str) -> String {
|
||||
format!("{}{} (conflict {}){}", dir, stem, stamp, ext)
|
||||
}
|
||||
|
||||
fn write_local(vault: &Path, relpath: &str, bytes: &[u8]) -> Result<(), String> {
|
||||
fn write_local(
|
||||
vault: &Path,
|
||||
relpath: &str,
|
||||
bytes: &[u8],
|
||||
changes: &mut HashMap<PathBuf, &'static str>,
|
||||
) -> Result<(), String> {
|
||||
let target = vault.join(relpath);
|
||||
if fs::read(&target).is_ok_and(|current| current == bytes) {
|
||||
return Ok(());
|
||||
}
|
||||
if let Some(parent) = target.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| e.to_string())?;
|
||||
}
|
||||
fs::write(&target, bytes).map_err(|e| e.to_string())
|
||||
if matches!(
|
||||
relpath,
|
||||
".helixnotes/notebook_icons.json" | ".helixnotes/tag_styles.json"
|
||||
) {
|
||||
let content = std::str::from_utf8(bytes).map_err(|error| error.to_string())?;
|
||||
crate::vault::operations::attachment_metadata_references(
|
||||
target.file_name().and_then(|name| name.to_str()).unwrap(),
|
||||
content,
|
||||
)?;
|
||||
}
|
||||
crate::vault::operations::replace_file_atomically(&target, bytes)?;
|
||||
changes.insert(target, "modify");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn require_unchanged_local(path: &Path, expected_hash: Option<&str>) -> Result<(), String> {
|
||||
let actual = match fs::File::open(path) {
|
||||
Ok(mut file) => Some(
|
||||
sha256_reader(&mut file, &mut [0; HASH_BUFFER_SIZE])
|
||||
.map_err(|error| error.to_string())?,
|
||||
),
|
||||
Err(error) if error.kind() == io::ErrorKind::NotFound => None,
|
||||
Err(error) => return Err(error.to_string()),
|
||||
};
|
||||
if actual.as_deref() != expected_hash {
|
||||
return Err(format!(
|
||||
"Sync stopped because {} changed locally during sync",
|
||||
path.display()
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_local_if_unchanged(
|
||||
write_guard: &tokio::sync::Mutex<()>,
|
||||
vault: &Path,
|
||||
relpath: &str,
|
||||
bytes: &[u8],
|
||||
expected_hash: Option<&str>,
|
||||
changes: &mut HashMap<PathBuf, &'static str>,
|
||||
) -> Result<(), String> {
|
||||
let _guard = write_guard.blocking_lock();
|
||||
require_unchanged_local(&vault.join(relpath), expected_hash)?;
|
||||
write_local(vault, relpath, bytes, changes)
|
||||
}
|
||||
|
||||
struct LocalFile {
|
||||
@@ -138,6 +201,7 @@ fn is_synced_relpath(rel: &str) -> bool {
|
||||
fn scan_local(vault: &str) -> Result<HashMap<String, LocalFile>, String> {
|
||||
let vault_path = Path::new(vault);
|
||||
let mut map = HashMap::new();
|
||||
let mut buffer = [0; HASH_BUFFER_SIZE];
|
||||
for entry in WalkDir::new(vault_path).into_iter().filter_map(|e| e.ok()) {
|
||||
let p = entry.path();
|
||||
if !p.is_file() {
|
||||
@@ -150,14 +214,15 @@ fn scan_local(vault: &str) -> Result<HashMap<String, LocalFile>, String> {
|
||||
if !is_synced_relpath(&rel) {
|
||||
continue;
|
||||
}
|
||||
let bytes = match fs::read(p) {
|
||||
Ok(b) => b,
|
||||
Err(_) => continue,
|
||||
};
|
||||
let hash =
|
||||
match fs::File::open(p).and_then(|mut file| sha256_reader(&mut file, &mut buffer)) {
|
||||
Ok(hash) => hash,
|
||||
Err(_) => continue,
|
||||
};
|
||||
map.insert(
|
||||
rel,
|
||||
LocalFile {
|
||||
hash: sha256_hex(&bytes),
|
||||
hash,
|
||||
path: p.to_path_buf(),
|
||||
},
|
||||
);
|
||||
@@ -511,6 +576,8 @@ fn apply_changes(
|
||||
local: &HashMap<String, LocalFile>,
|
||||
remote: &HashMap<String, String>,
|
||||
manifest: &SyncManifest,
|
||||
changes: &mut HashMap<PathBuf, &'static str>,
|
||||
write_guard: &tokio::sync::Mutex<()>,
|
||||
) -> Result<(SyncManifest, SyncSummary), String> {
|
||||
let vault_path = Path::new(vault);
|
||||
let mut new_m = SyncManifest::default();
|
||||
@@ -559,7 +626,14 @@ fn apply_changes(
|
||||
sum.uploaded += 1;
|
||||
} else if !local_changed && remote_changed {
|
||||
let bytes = client.get(key)?;
|
||||
write_local(vault_path, key, &bytes)?;
|
||||
write_local_if_unchanged(
|
||||
write_guard,
|
||||
vault_path,
|
||||
key,
|
||||
&bytes,
|
||||
Some(&lf.hash),
|
||||
changes,
|
||||
)?;
|
||||
new_m.files.insert(
|
||||
key.clone(),
|
||||
ManifestEntry {
|
||||
@@ -582,8 +656,17 @@ fn apply_changes(
|
||||
);
|
||||
} else {
|
||||
// keep-both: remote becomes canonical; local saved as a conflict copy.
|
||||
let lbytes = fs::read(&lf.path).map_err(|e| e.to_string())?;
|
||||
write_local(vault_path, key, &rbytes)?;
|
||||
let ckey = conflict_relpath(key, &stamp);
|
||||
let lbytes = {
|
||||
let _guard = write_guard.blocking_lock();
|
||||
require_unchanged_local(&lf.path, Some(&lf.hash))?;
|
||||
require_unchanged_local(&vault_path.join(&ckey), None)?;
|
||||
let lbytes = fs::read(&lf.path).map_err(|e| e.to_string())?;
|
||||
// Preserve the local bytes before replacing their canonical path.
|
||||
write_local(vault_path, &ckey, &lbytes, changes)?;
|
||||
write_local(vault_path, key, &rbytes, changes)?;
|
||||
lbytes
|
||||
};
|
||||
new_m.files.insert(
|
||||
key.clone(),
|
||||
ManifestEntry {
|
||||
@@ -591,8 +674,6 @@ fn apply_changes(
|
||||
remote_etag: re.clone(),
|
||||
},
|
||||
);
|
||||
let ckey = conflict_relpath(key, &stamp);
|
||||
write_local(vault_path, &ckey, &lbytes)?;
|
||||
let cetag = client.upload(&ckey, &lbytes, &mut created)?;
|
||||
new_m.files.insert(
|
||||
ckey,
|
||||
@@ -634,7 +715,10 @@ fn apply_changes(
|
||||
sum.uploaded += 1;
|
||||
} else {
|
||||
// remote deleted, local unchanged -> delete local
|
||||
let _ = fs::remove_file(&lf.path);
|
||||
let _guard = write_guard.blocking_lock();
|
||||
require_unchanged_local(&lf.path, Some(&lf.hash))?;
|
||||
fs::remove_file(&lf.path).map_err(|e| e.to_string())?;
|
||||
changes.insert(lf.path.clone(), "remove");
|
||||
sum.deleted_local += 1;
|
||||
}
|
||||
}
|
||||
@@ -643,7 +727,7 @@ fn apply_changes(
|
||||
None => {
|
||||
// new remote -> download
|
||||
let bytes = client.get(key)?;
|
||||
write_local(vault_path, key, &bytes)?;
|
||||
write_local_if_unchanged(write_guard, vault_path, key, &bytes, None, changes)?;
|
||||
new_m.files.insert(
|
||||
key.clone(),
|
||||
ManifestEntry {
|
||||
@@ -657,7 +741,14 @@ fn apply_changes(
|
||||
if &me.remote_etag != re {
|
||||
// local deleted but remote changed -> resurrect (download)
|
||||
let bytes = client.get(key)?;
|
||||
write_local(vault_path, key, &bytes)?;
|
||||
write_local_if_unchanged(
|
||||
write_guard,
|
||||
vault_path,
|
||||
key,
|
||||
&bytes,
|
||||
None,
|
||||
changes,
|
||||
)?;
|
||||
new_m.files.insert(
|
||||
key.clone(),
|
||||
ManifestEntry {
|
||||
@@ -689,7 +780,7 @@ pub fn test_connection(cfg: WebdavConfig) -> Result<String, String> {
|
||||
}
|
||||
|
||||
/// Run a full sync. Mutes the file watcher while applying local writes, then
|
||||
/// rebuilds the search index. Returns a summary of what changed.
|
||||
/// queues only changed notes for search and publishes their final local state.
|
||||
pub fn run_sync(
|
||||
app: tauri::AppHandle,
|
||||
vault: String,
|
||||
@@ -698,6 +789,9 @@ pub fn run_sync(
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
let state = app.state::<AppState>();
|
||||
// Keep the original vault's index even if the user switches vaults while
|
||||
// WebDAV is working. No config lock is held during network or file I/O.
|
||||
let search = state.vault_snapshot(&vault)?;
|
||||
let client = WebdavClient::new(cfg)?;
|
||||
|
||||
let _ = app.emit("sync-progress", serde_json::json!({ "status": "listing" }));
|
||||
@@ -706,8 +800,10 @@ pub fn run_sync(
|
||||
.into_iter()
|
||||
.filter(|(k, _)| is_synced_relpath(k))
|
||||
.collect();
|
||||
let local = scan_local(&vault)?;
|
||||
let manifest = load_manifest(&vault);
|
||||
let (local, manifest) = {
|
||||
let _guard = state.note_save.blocking_lock();
|
||||
(scan_local(&vault)?, load_manifest(&vault))
|
||||
};
|
||||
|
||||
// Safety guard (issue #102): never treat an empty remote listing as "everything was
|
||||
// deleted remotely". If a prior sync exists (non-empty manifest) but the remote came back
|
||||
@@ -724,35 +820,241 @@ pub fn run_sync(
|
||||
}
|
||||
|
||||
let _ = app.emit("sync-progress", serde_json::json!({ "status": "syncing" }));
|
||||
// Mute the watcher across local writes so a pull doesn't storm the reindex path.
|
||||
state.importing.store(true, Ordering::Relaxed);
|
||||
let applied = apply_changes(&client, &vault, &local, &remote, &manifest);
|
||||
state.importing.store(false, Ordering::Relaxed);
|
||||
let mut changes = HashMap::new();
|
||||
// Mute the watcher across local writes; its deferred batch also preserves
|
||||
// external edits that arrive while the network operation is in progress.
|
||||
{
|
||||
let _guard = state.note_save.blocking_lock();
|
||||
state.importing.store(true, Ordering::Release);
|
||||
}
|
||||
let applied = apply_changes(
|
||||
&client,
|
||||
&vault,
|
||||
&local,
|
||||
&remote,
|
||||
&manifest,
|
||||
&mut changes,
|
||||
&state.note_save,
|
||||
);
|
||||
state.importing.store(false, Ordering::Release);
|
||||
|
||||
// Publish even a partial apply: a later network failure must not leave
|
||||
// successfully downloaded/deleted notes absent from search or the UI.
|
||||
let active = state
|
||||
.config
|
||||
.lock()
|
||||
.ok()
|
||||
.is_some_and(|config| config.active_vault.as_deref() == Some(vault.as_str()));
|
||||
let metadata_root = helixnotes_dir(&vault);
|
||||
for (path, event_type) in changes {
|
||||
let is_note = path.extension().and_then(|extension| extension.to_str()) == Some("md")
|
||||
&& !path.starts_with(&metadata_root);
|
||||
if is_note {
|
||||
if let Some(search) = &search {
|
||||
if let Err(error) = search.queue_index_note(&path.to_string_lossy()) {
|
||||
log::error!("Could not queue synced note for search: {error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
if !active {
|
||||
continue;
|
||||
}
|
||||
let event = if is_note {
|
||||
changed_entry(path, event_type, Path::new(&vault))
|
||||
} else {
|
||||
crate::types::FileEvent {
|
||||
vault_path: vault.clone(),
|
||||
event_type: event_type.to_string(),
|
||||
path: path.to_string_lossy().to_string(),
|
||||
note: None,
|
||||
}
|
||||
};
|
||||
let _ = app.emit("file-changed", event);
|
||||
}
|
||||
|
||||
let (new_manifest, summary) = applied?;
|
||||
save_manifest(&vault, &new_manifest);
|
||||
|
||||
// Re-index after files changed (search is non-critical; ignore failures).
|
||||
if let Ok(guard) = state.search_index.lock() {
|
||||
if let Some(search) = guard.as_ref() {
|
||||
let _ = search.rebuild(&vault);
|
||||
}
|
||||
}
|
||||
|
||||
// The watcher was muted during apply, so nudge the UI to refresh its lists.
|
||||
let _ = app.emit(
|
||||
"file-changed",
|
||||
crate::types::FileEvent {
|
||||
event_type: "modify".to_string(),
|
||||
path: vault.clone(),
|
||||
},
|
||||
);
|
||||
Ok(summary)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::is_synced_relpath;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn synced_metadata_waits_for_local_patches_and_never_overwrites_a_newer_style() {
|
||||
use crate::types::TagStyleChange;
|
||||
use crate::vault::operations;
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let root = vault.path().to_str().unwrap();
|
||||
operations::ensure_vault_structure(root).unwrap();
|
||||
operations::patch_tag_style(root, "Work", TagStyleChange::Color(Some("#123456".into())))
|
||||
.unwrap();
|
||||
let rel = ".helixnotes/tag_styles.json";
|
||||
let path = vault.path().join(rel);
|
||||
let hash = sha256_hex(&fs::read(&path).unwrap());
|
||||
let guard = tokio::sync::Mutex::new(());
|
||||
let held = guard.blocking_lock();
|
||||
std::thread::scope(|scope| {
|
||||
let pending = scope.spawn(|| {
|
||||
let mut changes = HashMap::new();
|
||||
let result = write_local_if_unchanged(
|
||||
&guard,
|
||||
vault.path(),
|
||||
rel,
|
||||
br##"{"Work":{"color":"#ffffff"}}"##,
|
||||
Some(&hash),
|
||||
&mut changes,
|
||||
);
|
||||
assert!(result.is_err());
|
||||
assert!(changes.is_empty());
|
||||
});
|
||||
operations::patch_tag_style(
|
||||
root,
|
||||
"Work",
|
||||
TagStyleChange::Icon(Some("builtin:star".into())),
|
||||
)
|
||||
.unwrap();
|
||||
drop(held);
|
||||
pending.join().unwrap();
|
||||
});
|
||||
let persisted = operations::load_tag_styles(root).unwrap();
|
||||
assert_eq!(persisted["Work"].icon.as_deref(), Some("builtin:star"));
|
||||
assert_eq!(persisted["Work"].color.as_deref(), Some("#123456"));
|
||||
|
||||
let current = fs::read(&path).unwrap();
|
||||
let mut original = fs::File::open(&path).unwrap();
|
||||
let mut changes = HashMap::new();
|
||||
write_local_if_unchanged(
|
||||
&guard,
|
||||
vault.path(),
|
||||
rel,
|
||||
br##"{"Work":{"color":"#abcdef","icon":"builtin:star"}}"##,
|
||||
Some(&sha256_hex(¤t)),
|
||||
&mut changes,
|
||||
)
|
||||
.unwrap();
|
||||
let mut old_bytes = Vec::new();
|
||||
original.read_to_end(&mut old_bytes).unwrap();
|
||||
assert_eq!(old_bytes, current);
|
||||
assert_eq!(
|
||||
operations::load_tag_styles(root).unwrap()["Work"]
|
||||
.color
|
||||
.as_deref(),
|
||||
Some("#abcdef")
|
||||
);
|
||||
|
||||
let before = fs::read(&path).unwrap();
|
||||
assert!(write_local_if_unchanged(
|
||||
&guard,
|
||||
vault.path(),
|
||||
rel,
|
||||
b"{broken",
|
||||
Some(&sha256_hex(&before)),
|
||||
&mut HashMap::new()
|
||||
)
|
||||
.is_err());
|
||||
assert_eq!(fs::read(&path).unwrap(), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn streaming_hash_matches_bytes_across_buffer_boundaries() {
|
||||
let mut buffer = [0; HASH_BUFFER_SIZE];
|
||||
for len in [0, 256, HASH_BUFFER_SIZE, HASH_BUFFER_SIZE * 2 + 137] {
|
||||
let bytes: Vec<u8> = (0..len).map(|i| (i % 256) as u8).collect();
|
||||
assert_eq!(
|
||||
sha256_reader(&mut bytes.as_slice(), &mut buffer).unwrap(),
|
||||
sha256_hex(&bytes),
|
||||
"hash mismatch for {len} bytes"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn streaming_hash_retries_interruptions_and_accepts_short_reads() {
|
||||
struct InterruptedShortReader<'a> {
|
||||
remaining: &'a [u8],
|
||||
interrupt_next: bool,
|
||||
}
|
||||
|
||||
impl Read for InterruptedShortReader<'_> {
|
||||
fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
|
||||
self.interrupt_next = !self.interrupt_next;
|
||||
if self.interrupt_next {
|
||||
return Err(io::ErrorKind::Interrupted.into());
|
||||
}
|
||||
let len = buffer.len().min(7);
|
||||
self.remaining.read(&mut buffer[..len])
|
||||
}
|
||||
}
|
||||
|
||||
let bytes: Vec<u8> = (0..513).map(|i| (i % 256) as u8).collect();
|
||||
let mut reader = InterruptedShortReader {
|
||||
remaining: &bytes,
|
||||
interrupt_next: false,
|
||||
};
|
||||
let mut buffer = [0; HASH_BUFFER_SIZE];
|
||||
assert_eq!(
|
||||
sha256_reader(&mut reader, &mut buffer).unwrap(),
|
||||
sha256_hex(&bytes)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn streaming_hash_rejects_partial_content_after_read_error() {
|
||||
struct FailingReader;
|
||||
|
||||
impl Read for FailingReader {
|
||||
fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
|
||||
Err(io::ErrorKind::PermissionDenied.into())
|
||||
}
|
||||
}
|
||||
|
||||
let mut reader = b"partial content".as_slice().chain(FailingReader);
|
||||
let mut buffer = [0; HASH_BUFFER_SIZE];
|
||||
assert_eq!(
|
||||
sha256_reader(&mut reader, &mut buffer).unwrap_err().kind(),
|
||||
io::ErrorKind::PermissionDenied
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_scan_hashes_large_attachments_and_independent_files() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
fs::create_dir_all(vault.path().join(".helixnotes/attachments")).unwrap();
|
||||
let attachment: Vec<u8> = (0..HASH_BUFFER_SIZE * 2 + 31)
|
||||
.map(|i| (i % 256) as u8)
|
||||
.collect();
|
||||
let files = [
|
||||
(".helixnotes/attachments/large.bin", attachment.as_slice()),
|
||||
(
|
||||
".helixnotes/attachments/small.bin",
|
||||
b"\0\xff\x80".as_slice(),
|
||||
),
|
||||
("empty.md", b"".as_slice()),
|
||||
("note.md", b"note content".as_slice()),
|
||||
];
|
||||
for (relpath, bytes) in files {
|
||||
fs::write(vault.path().join(relpath), bytes).unwrap();
|
||||
}
|
||||
fs::write(vault.path().join("ignored.bin"), b"not synced").unwrap();
|
||||
|
||||
let local = scan_local(vault.path().to_str().unwrap()).unwrap();
|
||||
let actual: HashMap<_, _> = local
|
||||
.into_iter()
|
||||
.map(|(relpath, file)| (relpath, (file.hash, file.path)))
|
||||
.collect();
|
||||
let expected: HashMap<_, _> = files
|
||||
.into_iter()
|
||||
.map(|(relpath, bytes)| {
|
||||
(
|
||||
relpath.to_string(),
|
||||
(sha256_hex(bytes), vault.path().join(relpath)),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn syncs_notebook_icon_mapping_and_assets_only() {
|
||||
@@ -775,4 +1077,134 @@ mod tests {
|
||||
assert!(!is_synced_relpath(path), "expected {path} to stay local");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unchanged_sync_has_no_local_mutations() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().join("note.md");
|
||||
fs::write(&path, "unchanged content").unwrap();
|
||||
let modified = fs::metadata(&path).unwrap().modified().unwrap();
|
||||
let vault_path = vault.path().to_str().unwrap();
|
||||
let local = scan_local(vault_path).unwrap();
|
||||
let remote = HashMap::from([("note.md".to_string(), "etag".to_string())]);
|
||||
let manifest = SyncManifest {
|
||||
files: HashMap::from([(
|
||||
"note.md".to_string(),
|
||||
ManifestEntry {
|
||||
local_hash: local["note.md"].hash.clone(),
|
||||
remote_etag: "etag".to_string(),
|
||||
},
|
||||
)]),
|
||||
};
|
||||
let client = WebdavClient::new(WebdavConfig {
|
||||
url: "http://127.0.0.1:0/".to_string(),
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
})
|
||||
.unwrap();
|
||||
let mut changes = HashMap::new();
|
||||
let (next, summary) = apply_changes(
|
||||
&client,
|
||||
vault_path,
|
||||
&local,
|
||||
&remote,
|
||||
&manifest,
|
||||
&mut changes,
|
||||
&tokio::sync::Mutex::new(()),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(changes.is_empty());
|
||||
assert_eq!(summary.uploaded, 0);
|
||||
assert_eq!(summary.downloaded, 0);
|
||||
assert_eq!(summary.deleted_local, 0);
|
||||
assert_eq!(summary.deleted_remote, 0);
|
||||
assert_eq!(summary.conflicts, 0);
|
||||
assert_eq!(next.files["note.md"].local_hash, local["note.md"].hash);
|
||||
assert_eq!(fs::metadata(&path).unwrap().modified().unwrap(), modified);
|
||||
assert_eq!(fs::read_to_string(path).unwrap(), "unchanged content");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remote_deletion_records_only_the_removed_local_note() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().join("note.md");
|
||||
fs::write(&path, "unchanged content").unwrap();
|
||||
let vault_path = vault.path().to_str().unwrap();
|
||||
let local = scan_local(vault_path).unwrap();
|
||||
let manifest = SyncManifest {
|
||||
files: HashMap::from([(
|
||||
"note.md".to_string(),
|
||||
ManifestEntry {
|
||||
local_hash: local["note.md"].hash.clone(),
|
||||
remote_etag: "etag".to_string(),
|
||||
},
|
||||
)]),
|
||||
};
|
||||
let client = WebdavClient::new(WebdavConfig {
|
||||
url: "http://127.0.0.1:0/".to_string(),
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
})
|
||||
.unwrap();
|
||||
let mut changes = HashMap::new();
|
||||
let (next, summary) = apply_changes(
|
||||
&client,
|
||||
vault_path,
|
||||
&local,
|
||||
&HashMap::new(),
|
||||
&manifest,
|
||||
&mut changes,
|
||||
&tokio::sync::Mutex::new(()),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!path.exists());
|
||||
assert!(!next.files.contains_key("note.md"));
|
||||
assert_eq!(summary.deleted_local, 1);
|
||||
assert_eq!(changes, HashMap::from([(path, "remove")]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_download_journal_ignores_identical_bytes_and_preserves_partial_changes() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().join("Notes/note.md");
|
||||
let mut changes = HashMap::new();
|
||||
write_local(
|
||||
vault.path(),
|
||||
"Notes/note.md",
|
||||
b"first content",
|
||||
&mut changes,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(fs::read(&path).unwrap(), b"first content");
|
||||
changes.clear();
|
||||
let modified = fs::metadata(&path).unwrap().modified().unwrap();
|
||||
write_local(
|
||||
vault.path(),
|
||||
"Notes/note.md",
|
||||
b"first content",
|
||||
&mut changes,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(changes.is_empty());
|
||||
assert_eq!(fs::metadata(&path).unwrap().modified().unwrap(), modified);
|
||||
|
||||
write_local(
|
||||
vault.path(),
|
||||
"Notes/note.md",
|
||||
b"remote content",
|
||||
&mut changes,
|
||||
)
|
||||
.unwrap();
|
||||
fs::write(vault.path().join("blocked"), b"not a directory").unwrap();
|
||||
assert!(write_local(
|
||||
vault.path(),
|
||||
"blocked/note.md",
|
||||
b"cannot write",
|
||||
&mut changes
|
||||
)
|
||||
.is_err());
|
||||
assert_eq!(changes, HashMap::from([(path.clone(), "modify")]));
|
||||
let event = changed_entry(path, "modify", vault.path());
|
||||
assert_eq!(event.note.unwrap().preview, "remote content");
|
||||
}
|
||||
}
|
||||
+29
-2
@@ -43,6 +43,7 @@ pub struct NotebookEntry {
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct TagStyle {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub icon: Option<String>,
|
||||
@@ -50,6 +51,14 @@ pub struct TagStyle {
|
||||
pub color: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "kind", content = "value", rename_all = "lowercase")]
|
||||
pub enum TagStyleChange {
|
||||
Icon(Option<String>),
|
||||
Color(Option<String>),
|
||||
Reset,
|
||||
}
|
||||
|
||||
impl TagStyle {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.icon
|
||||
@@ -298,7 +307,7 @@ fn default_max_versions() -> u32 {
|
||||
}
|
||||
|
||||
fn default_ai_model() -> String {
|
||||
"claude-sonnet-4-6".to_string()
|
||||
"claude-sonnet-5".to_string()
|
||||
}
|
||||
|
||||
fn default_system_light_theme() -> String {
|
||||
@@ -358,7 +367,7 @@ impl Default for AppConfig {
|
||||
ollama_api_key: None,
|
||||
openai_compatible_base_url: None,
|
||||
openai_compatible_api_key: None,
|
||||
ai_model: "claude-sonnet-4-6".to_string(),
|
||||
ai_model: default_ai_model(),
|
||||
ai_writing_style: None,
|
||||
default_view_mode: false,
|
||||
new_notes_in_source_mode: false,
|
||||
@@ -441,18 +450,35 @@ impl Default for VaultState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Half-open UTF-16 offsets into the corresponding plain-text result field.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct SearchMatch {
|
||||
pub start: usize,
|
||||
pub end: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SearchResult {
|
||||
pub path: String,
|
||||
pub title: String,
|
||||
pub snippet: String,
|
||||
pub score: f32,
|
||||
pub title_matches: Vec<SearchMatch>,
|
||||
pub snippet_matches: Vec<SearchMatch>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct SearchPage {
|
||||
pub results: Vec<SearchResult>,
|
||||
pub total: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FileEvent {
|
||||
pub vault_path: String,
|
||||
pub event_type: String,
|
||||
pub path: String,
|
||||
pub note: Option<NoteEntry>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -489,6 +515,7 @@ pub struct VersionEntry {
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AiStreamEvent {
|
||||
pub request_id: String,
|
||||
pub event_type: String, // "text", "done", "error"
|
||||
pub text: Option<String>,
|
||||
pub error: Option<String>,
|
||||
|
||||
+565
-100
@@ -1,6 +1,6 @@
|
||||
use crate::types::{
|
||||
NoteContent, NoteEntry, NoteMeta, NoteTitleEntry, NotebookEntry, TagStyle, TrashContents,
|
||||
TrashNotebookEntry, VaultState,
|
||||
NoteContent, NoteEntry, NoteMeta, NoteTitleEntry, NotebookEntry, TagStyle, TagStyleChange,
|
||||
TrashContents, TrashNotebookEntry, VaultState,
|
||||
};
|
||||
use crate::vault::frontmatter;
|
||||
use chrono::{DateTime, Local, Locale, Utc};
|
||||
@@ -8,7 +8,7 @@ use rayon::prelude::*;
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use uuid::Uuid;
|
||||
use walkdir::WalkDir;
|
||||
@@ -440,9 +440,9 @@ fn read_note_entry(path: &Path, vault_root: &Path) -> Result<NoteEntry, String>
|
||||
read_note_entry_from_str(&raw, path, vault_root)
|
||||
}
|
||||
|
||||
/// Android-only: reads just the frontmatter (first 2KB) for tags/title/pinned,
|
||||
/// uses filesystem timestamps for dates. No preview text.
|
||||
#[cfg(mobile)]
|
||||
/// Mobile listing reads the first 2KB and uses the filesystem modification time.
|
||||
/// Persisted creation dates survive atomic file replacement.
|
||||
#[cfg(any(mobile, test))]
|
||||
fn read_note_entry_metadata_only(path: &Path, vault_root: &Path) -> Result<NoteEntry, String> {
|
||||
// Read first 2KB - enough for frontmatter with tags, title, pinned
|
||||
let mut file = fs::File::open(path).map_err(|e| e.to_string())?;
|
||||
@@ -459,13 +459,15 @@ fn read_note_entry_metadata_only(path: &Path, vault_root: &Path) -> Result<NoteE
|
||||
|
||||
let (mut meta, content) = frontmatter::parse_note(&raw, &filename);
|
||||
|
||||
// Always use filesystem timestamps on Android (faster than parsing date strings)
|
||||
// Creation time belongs to the note, not the replacement file's new inode.
|
||||
if let Ok(fs_meta) = fs::metadata(path) {
|
||||
if let Ok(m) = fs_meta.modified() {
|
||||
meta.modified = m.into();
|
||||
}
|
||||
if let Ok(c) = fs_meta.created() {
|
||||
meta.created = c.into();
|
||||
if !raw.contains("\ncreated:") && !raw.starts_with("created:") {
|
||||
if let Ok(c) = fs_meta.created() {
|
||||
meta.created = c.into();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -486,7 +488,7 @@ fn read_note_entry_metadata_only(path: &Path, vault_root: &Path) -> Result<NoteE
|
||||
}
|
||||
|
||||
/// Fast version: reads only the first ~2KB of the file (enough for frontmatter + preview).
|
||||
fn read_note_entry_fast(path: &Path, vault_root: &Path) -> Result<NoteEntry, String> {
|
||||
pub(crate) fn read_note_entry_fast(path: &Path, vault_root: &Path) -> Result<NoteEntry, String> {
|
||||
let mut file = fs::File::open(path).map_err(|e| e.to_string())?;
|
||||
let mut buf = vec![0u8; 2048];
|
||||
let bytes_read = file.read(&mut buf).map_err(|e| e.to_string())?;
|
||||
@@ -588,25 +590,88 @@ fn read_note_content(validated: &Path, reported_path: &str) -> Result<NoteConten
|
||||
})
|
||||
}
|
||||
|
||||
pub fn save_note(vault_path: &str, path: &str, meta: &NoteMeta, body: &str) -> Result<(), String> {
|
||||
let path = ensure_note_path(vault_path, Path::new(path))?;
|
||||
let mut updated_meta = meta.clone();
|
||||
updated_meta.modified = Utc::now();
|
||||
pub struct SavedNote {
|
||||
pub id: String,
|
||||
pub previous_raw: String,
|
||||
}
|
||||
|
||||
// Generate UUID on first save if note didn't have one
|
||||
if updated_meta.id.is_empty() {
|
||||
/// Returns the pre-save content only when the note actually changed.
|
||||
pub fn save_note(
|
||||
vault_path: &str,
|
||||
path: &str,
|
||||
meta: &NoteMeta,
|
||||
body: &str,
|
||||
) -> Result<Option<SavedNote>, String> {
|
||||
let validated = ensure_note_path(vault_path, Path::new(path))?;
|
||||
// Replace the actual file, not a symlink pointing to it.
|
||||
let target = canonicalize_path(&validated, "note path")?;
|
||||
let existing = read_note_content(&target, path)?;
|
||||
let mut updated_meta = meta.clone();
|
||||
|
||||
// A stale editor may still have an empty ID after the first save.
|
||||
if !existing.meta.id.is_empty() {
|
||||
updated_meta.id.clone_from(&existing.meta.id);
|
||||
} else if updated_meta.id.is_empty() {
|
||||
updated_meta.id = Uuid::new_v4().to_string();
|
||||
}
|
||||
|
||||
// Read existing file to preserve unknown frontmatter fields
|
||||
let existing = fs::read_to_string(&path).unwrap_or_default();
|
||||
let raw = if existing.is_empty() {
|
||||
frontmatter::update_note_raw(&updated_meta, body)
|
||||
} else {
|
||||
frontmatter::merge_frontmatter(&existing, &updated_meta, body)
|
||||
};
|
||||
// The editor appends one terminal LF; the note parser omits it. Do not
|
||||
// trim spaces here: Markdown hard breaks and indentation are meaningful.
|
||||
let comparable_body = body.strip_suffix('\n').unwrap_or(body);
|
||||
if existing.content == comparable_body
|
||||
&& existing.meta.id == updated_meta.id
|
||||
&& existing.meta.title == updated_meta.title
|
||||
&& existing.meta.tags == updated_meta.tags
|
||||
&& existing.meta.pinned == updated_meta.pinned
|
||||
&& existing.meta.created == updated_meta.created
|
||||
{
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
fs::write(path, raw).map_err(|e| e.to_string())?;
|
||||
updated_meta.modified = Utc::now();
|
||||
let raw = frontmatter::merge_frontmatter(&existing.raw, &updated_meta, body);
|
||||
replace_file_atomically(&target, raw.as_bytes())?;
|
||||
|
||||
Ok(Some(SavedNote {
|
||||
id: updated_meta.id,
|
||||
previous_raw: existing.raw,
|
||||
}))
|
||||
}
|
||||
|
||||
pub(crate) fn replace_file_atomically(path: &Path, content: &[u8]) -> Result<(), String> {
|
||||
let permissions = match fs::metadata(path) {
|
||||
Ok(metadata) => {
|
||||
let permissions = metadata.permissions();
|
||||
if permissions.readonly() {
|
||||
return Err("File is read-only".to_string());
|
||||
}
|
||||
Some(permissions)
|
||||
}
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
|
||||
Err(error) => return Err(error.to_string()),
|
||||
};
|
||||
let parent = path.parent().ok_or("File has no parent directory")?;
|
||||
let mut temporary = tempfile::Builder::new()
|
||||
.prefix(".helixnotes-save-")
|
||||
.tempfile_in(parent)
|
||||
.map_err(|error| error.to_string())?;
|
||||
temporary
|
||||
.write_all(content)
|
||||
.map_err(|error| error.to_string())?;
|
||||
temporary.flush().map_err(|error| error.to_string())?;
|
||||
if let Some(permissions) = permissions {
|
||||
temporary
|
||||
.as_file()
|
||||
.set_permissions(permissions)
|
||||
.map_err(|error| error.to_string())?;
|
||||
}
|
||||
temporary
|
||||
.as_file()
|
||||
.sync_all()
|
||||
.map_err(|error| error.to_string())?;
|
||||
// tempfile uses an atomic replacement on Unix and Windows, and owns cleanup
|
||||
// on every failure before the replacement succeeds.
|
||||
temporary.persist(path).map_err(|error| error.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1097,7 +1162,7 @@ pub fn rename_notebook(vault_path: &str, path: &str, new_name: &str) -> Result<S
|
||||
return Err("A notebook with that name already exists".to_string());
|
||||
}
|
||||
|
||||
fs::rename(src, &new_path).map_err(|e| e.to_string())?;
|
||||
move_notebook_with_icons(vault_path, src, &new_path)?;
|
||||
Ok(new_path.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
@@ -1149,7 +1214,7 @@ pub fn move_notebook(
|
||||
return Err("A notebook with that name already exists in the destination".to_string());
|
||||
}
|
||||
|
||||
fs::rename(src, &dest).map_err(|e| e.to_string())?;
|
||||
move_notebook_with_icons(vault_path, src, &dest)?;
|
||||
Ok(dest.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
@@ -1394,16 +1459,29 @@ pub fn save_attachment(vault_path: &str, name: &str, data: &[u8]) -> Result<Stri
|
||||
let attachments_dir = helixnotes_dir(vault_path).join("attachments");
|
||||
fs::create_dir_all(&attachments_dir).map_err(|e| e.to_string())?;
|
||||
|
||||
let timestamp = Utc::now().format("%Y%m%d%H%M%S");
|
||||
let safe_name = sanitize_filename(name);
|
||||
let filename = format!("{}_{}", timestamp, safe_name);
|
||||
let dest = attachments_dir.join(&filename);
|
||||
|
||||
fs::write(&dest, data).map_err(|e| e.to_string())?;
|
||||
|
||||
// Return relative path from vault root
|
||||
let relative = format!(".helixnotes/attachments/{}", filename);
|
||||
Ok(relative)
|
||||
let mut temporary = tempfile::Builder::new()
|
||||
.prefix(".helixnotes-asset-")
|
||||
.tempfile_in(&attachments_dir)
|
||||
.map_err(|error| error.to_string())?;
|
||||
temporary
|
||||
.write_all(data)
|
||||
.map_err(|error| error.to_string())?;
|
||||
temporary.flush().map_err(|error| error.to_string())?;
|
||||
temporary
|
||||
.as_file()
|
||||
.sync_all()
|
||||
.map_err(|error| error.to_string())?;
|
||||
loop {
|
||||
let filename = format!("{}_{}", Uuid::new_v4(), safe_name);
|
||||
match temporary.persist_noclobber(attachments_dir.join(&filename)) {
|
||||
Ok(_) => return Ok(format!(".helixnotes/attachments/{filename}")),
|
||||
Err(error) if error.error.kind() == std::io::ErrorKind::AlreadyExists => {
|
||||
temporary = error.file;
|
||||
}
|
||||
Err(error) => return Err(error.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn normalize_notebook_icon_key(path: &str) -> String {
|
||||
@@ -1414,11 +1492,13 @@ pub fn load_notebook_icons(
|
||||
vault_path: &str,
|
||||
) -> Result<std::collections::HashMap<String, String>, String> {
|
||||
let icons_path = helixnotes_dir(vault_path).join("notebook_icons.json");
|
||||
if !icons_path.exists() {
|
||||
return Ok(std::collections::HashMap::new());
|
||||
}
|
||||
|
||||
let data = fs::read_to_string(&icons_path).map_err(|e| e.to_string())?;
|
||||
let data = match fs::read_to_string(&icons_path) {
|
||||
Ok(data) => data,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
|
||||
return Ok(std::collections::HashMap::new())
|
||||
}
|
||||
Err(error) => return Err(error.to_string()),
|
||||
};
|
||||
let icons: std::collections::HashMap<String, String> =
|
||||
serde_json::from_str(&data).map_err(|e| e.to_string())?;
|
||||
Ok(icons
|
||||
@@ -1442,12 +1522,105 @@ pub fn set_notebook_icon(
|
||||
icons.remove(¬ebook_key);
|
||||
}
|
||||
}
|
||||
let icons_path = helixnotes_dir(vault_path).join("notebook_icons.json");
|
||||
let data = serde_json::to_string_pretty(&icons).map_err(|e| e.to_string())?;
|
||||
fs::write(&icons_path, data).map_err(|e| e.to_string())?;
|
||||
save_notebook_icons(vault_path, &icons)
|
||||
}
|
||||
|
||||
fn save_notebook_icons(
|
||||
vault_path: &str,
|
||||
icons: &std::collections::HashMap<String, String>,
|
||||
) -> Result<(), String> {
|
||||
let dir = helixnotes_dir(vault_path);
|
||||
fs::create_dir_all(&dir).map_err(|error| error.to_string())?;
|
||||
let data = serde_json::to_vec_pretty(icons).map_err(|error| error.to_string())?;
|
||||
replace_file_atomically(&dir.join("notebook_icons.json"), &data)
|
||||
}
|
||||
|
||||
fn move_notebook_with_icons(vault_path: &str, src: &Path, dest: &Path) -> Result<(), String> {
|
||||
let icons = load_notebook_icons(vault_path)?;
|
||||
let old_key = normalize_notebook_icon_key(
|
||||
&src.strip_prefix(vault_path)
|
||||
.map_err(|error| error.to_string())?
|
||||
.to_string_lossy(),
|
||||
);
|
||||
let new_key = normalize_notebook_icon_key(
|
||||
&dest
|
||||
.strip_prefix(vault_path)
|
||||
.map_err(|error| error.to_string())?
|
||||
.to_string_lossy(),
|
||||
);
|
||||
let prefix = format!("{old_key}/");
|
||||
let mut changed = false;
|
||||
let migrated = icons
|
||||
.into_iter()
|
||||
.map(|(key, value)| {
|
||||
let key = if key == old_key {
|
||||
changed = true;
|
||||
new_key.clone()
|
||||
} else if let Some(suffix) = key.strip_prefix(&prefix) {
|
||||
changed = true;
|
||||
format!("{new_key}/{suffix}")
|
||||
} else {
|
||||
key
|
||||
};
|
||||
(key, value)
|
||||
})
|
||||
.collect();
|
||||
fs::rename(src, dest).map_err(|error| error.to_string())?;
|
||||
if changed {
|
||||
if let Err(error) = save_notebook_icons(vault_path, &migrated) {
|
||||
return Err(match fs::rename(dest, src) {
|
||||
Ok(()) => error,
|
||||
Err(rollback) => format!("{error}; notebook move rollback failed: {rollback}"),
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback_import<T>(
|
||||
vault_path: &str,
|
||||
relative: &str,
|
||||
result: Result<T, String>,
|
||||
) -> Result<T, String> {
|
||||
result.map_err(
|
||||
|error| match fs::remove_file(Path::new(vault_path).join(relative)) {
|
||||
Ok(()) => error,
|
||||
Err(rollback) => format!("{error}; imported attachment rollback failed: {rollback}"),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn import_notebook_icon(
|
||||
vault_path: &str,
|
||||
notebook_relative: &str,
|
||||
name: &str,
|
||||
data: &[u8],
|
||||
) -> Result<String, String> {
|
||||
let relative = save_attachment(vault_path, name, data)?;
|
||||
rollback_import(
|
||||
vault_path,
|
||||
&relative,
|
||||
set_notebook_icon(vault_path, notebook_relative, Some(&relative)),
|
||||
)?;
|
||||
Ok(relative)
|
||||
}
|
||||
|
||||
pub fn import_tag_icon(
|
||||
vault_path: &str,
|
||||
tag: &str,
|
||||
name: &str,
|
||||
data: &[u8],
|
||||
) -> Result<TagStyle, String> {
|
||||
let relative = save_attachment(vault_path, name, data)?;
|
||||
let result = patch_tag_style(
|
||||
vault_path,
|
||||
tag,
|
||||
TagStyleChange::Icon(Some(relative.clone())),
|
||||
)
|
||||
.and_then(|style| style.ok_or_else(|| "Imported tag icon was not registered".to_string()));
|
||||
rollback_import(vault_path, &relative, result)
|
||||
}
|
||||
|
||||
fn tag_styles_path(vault_path: &str) -> PathBuf {
|
||||
helixnotes_dir(vault_path).join("tag_styles.json")
|
||||
}
|
||||
@@ -1488,15 +1661,18 @@ pub fn load_tag_styles(
|
||||
vault_path: &str,
|
||||
) -> Result<std::collections::HashMap<String, TagStyle>, String> {
|
||||
let styles_path = tag_styles_path(vault_path);
|
||||
if !styles_path.exists() {
|
||||
return Ok(std::collections::HashMap::new());
|
||||
}
|
||||
|
||||
let data = fs::read_to_string(&styles_path).map_err(|e| e.to_string())?;
|
||||
let data = match fs::read_to_string(&styles_path) {
|
||||
Ok(data) => data,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
|
||||
return Ok(std::collections::HashMap::new())
|
||||
}
|
||||
Err(error) => return Err(error.to_string()),
|
||||
};
|
||||
let styles: std::collections::HashMap<String, TagStyle> =
|
||||
serde_json::from_str(&data).map_err(|e| e.to_string())?;
|
||||
let mut collapsed = std::collections::HashMap::new();
|
||||
for (tag, style) in styles {
|
||||
normalize_tag_color(style.color.as_deref())?;
|
||||
let key = normalize_tag_style_key(&tag);
|
||||
if key.is_empty() || style.is_empty() {
|
||||
continue;
|
||||
@@ -1507,7 +1683,11 @@ pub fn load_tag_styles(
|
||||
Ok(collapsed)
|
||||
}
|
||||
|
||||
pub fn set_tag_style(vault_path: &str, tag: &str, style: Option<TagStyle>) -> Result<(), String> {
|
||||
pub fn patch_tag_style(
|
||||
vault_path: &str,
|
||||
tag: &str,
|
||||
change: TagStyleChange,
|
||||
) -> Result<Option<TagStyle>, String> {
|
||||
let key = normalize_tag_style_key(tag);
|
||||
if key.is_empty() {
|
||||
return Err("Tag name is required".to_string());
|
||||
@@ -1518,28 +1698,45 @@ pub fn set_tag_style(vault_path: &str, tag: &str, style: Option<TagStyle>) -> Re
|
||||
.keys()
|
||||
.find(|existing| tag_style_key_fold(existing) == tag_style_key_fold(&key))
|
||||
.cloned()
|
||||
.unwrap_or_else(|| key.clone());
|
||||
match style {
|
||||
Some(style) => {
|
||||
let next = TagStyle {
|
||||
icon: normalize_tag_icon(style.icon.as_deref()),
|
||||
color: normalize_tag_color(style.color.as_deref())?,
|
||||
};
|
||||
remove_matching_tag_styles(&mut styles, &key);
|
||||
if !next.is_empty() {
|
||||
styles.insert(stored_key, next);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
remove_matching_tag_styles(&mut styles, &key);
|
||||
}
|
||||
.unwrap_or(key);
|
||||
let mut next = styles.remove(&stored_key).unwrap_or_default();
|
||||
match change {
|
||||
TagStyleChange::Icon(value) => next.icon = normalize_tag_icon(value.as_deref()),
|
||||
TagStyleChange::Color(value) => next.color = normalize_tag_color(value.as_deref())?,
|
||||
TagStyleChange::Reset => next = TagStyle::default(),
|
||||
}
|
||||
let next = (!next.is_empty()).then_some(next);
|
||||
if let Some(style) = &next {
|
||||
styles.insert(stored_key, style.clone());
|
||||
}
|
||||
|
||||
let dir = helixnotes_dir(vault_path);
|
||||
fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
|
||||
let data = serde_json::to_string_pretty(&styles).map_err(|e| e.to_string())?;
|
||||
fs::write(tag_styles_path(vault_path), data).map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
replace_file_atomically(&tag_styles_path(vault_path), data.as_bytes())?;
|
||||
Ok(next)
|
||||
}
|
||||
|
||||
pub(crate) fn attachment_metadata_references(
|
||||
mapping: &str,
|
||||
content: &str,
|
||||
) -> Result<Vec<String>, String> {
|
||||
if mapping == "notebook_icons.json" {
|
||||
let icons: std::collections::HashMap<String, String> =
|
||||
serde_json::from_str(content).map_err(|error| error.to_string())?;
|
||||
Ok(icons.into_values().collect())
|
||||
} else {
|
||||
let styles: std::collections::HashMap<String, TagStyle> =
|
||||
serde_json::from_str(content).map_err(|error| error.to_string())?;
|
||||
let mut references = Vec::new();
|
||||
for style in styles.into_values() {
|
||||
normalize_tag_color(style.color.as_deref())?;
|
||||
if let Some(icon) = style.icon {
|
||||
references.push(icon);
|
||||
}
|
||||
}
|
||||
Ok(references)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_quick_access(vault_path: &str) -> Result<Vec<String>, String> {
|
||||
@@ -1660,11 +1857,140 @@ pub fn sanitize_filename(name: &str) -> String {
|
||||
mod tests {
|
||||
use super::{
|
||||
compare_natural_names, create_notebook, duplicate_note, get_note_switcher_titles,
|
||||
helixnotes_dir, load_notebook_icons, load_tag_styles, permanent_delete, read_note,
|
||||
restore_notebook, scan_notebooks, set_notebook_icon, set_tag_style,
|
||||
helixnotes_dir, load_notebook_icons, load_tag_styles, patch_tag_style, permanent_delete,
|
||||
read_note, replace_file_atomically, restore_notebook, save_note, scan_notebooks,
|
||||
set_notebook_icon,
|
||||
};
|
||||
use crate::types::TagStyle;
|
||||
use crate::types::{TagStyle, TagStyleChange};
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
|
||||
#[test]
|
||||
fn same_named_attachment_writes_never_replace_each_other() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().to_str().unwrap();
|
||||
super::ensure_vault_structure(path).unwrap();
|
||||
let assets = std::thread::scope(|scope| {
|
||||
let writes: Vec<_> = (0..16u8)
|
||||
.map(|byte| {
|
||||
scope.spawn(move || {
|
||||
let relative =
|
||||
super::save_attachment(path, "same.png", &[byte; 8]).unwrap();
|
||||
(relative, byte)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
writes
|
||||
.into_iter()
|
||||
.map(|thread| thread.join().unwrap())
|
||||
.collect::<Vec<_>>()
|
||||
});
|
||||
let mut names = std::collections::HashSet::new();
|
||||
for (relative, byte) in assets {
|
||||
assert!(names.insert(relative.clone()));
|
||||
assert_eq!(fs::read(vault.path().join(relative)).unwrap(), [byte; 8]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metadata_setters_atomically_replace_without_truncating_open_readers() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().to_str().unwrap();
|
||||
super::ensure_vault_structure(path).unwrap();
|
||||
set_notebook_icon(path, "Book", Some("builtin:folder")).unwrap();
|
||||
patch_tag_style(path, "Work", TagStyleChange::Color(Some("#123456".into()))).unwrap();
|
||||
let icons_path = helixnotes_dir(path).join("notebook_icons.json");
|
||||
let styles_path = helixnotes_dir(path).join("tag_styles.json");
|
||||
let icons_before = fs::read(&icons_path).unwrap();
|
||||
let styles_before = fs::read(&styles_path).unwrap();
|
||||
let mut icons_reader = fs::File::open(&icons_path).unwrap();
|
||||
let mut styles_reader = fs::File::open(&styles_path).unwrap();
|
||||
set_notebook_icon(path, "Book", Some("builtin:star")).unwrap();
|
||||
patch_tag_style(
|
||||
path,
|
||||
"Work",
|
||||
TagStyleChange::Icon(Some("builtin:briefcase".into())),
|
||||
)
|
||||
.unwrap();
|
||||
let mut icons_old = Vec::new();
|
||||
let mut styles_old = Vec::new();
|
||||
icons_reader.read_to_end(&mut icons_old).unwrap();
|
||||
styles_reader.read_to_end(&mut styles_old).unwrap();
|
||||
assert_eq!(icons_old, icons_before);
|
||||
assert_eq!(styles_old, styles_before);
|
||||
assert_eq!(load_notebook_icons(path).unwrap()["Book"], "builtin:star");
|
||||
assert_eq!(
|
||||
load_tag_styles(path).unwrap()["Work"],
|
||||
TagStyle {
|
||||
icon: Some("builtin:briefcase".into()),
|
||||
color: Some("#123456".into()),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_icon_registration_rolls_back_only_the_new_unique_attachment() {
|
||||
for mapping in ["notebook_icons.json", "tag_styles.json"] {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().to_str().unwrap();
|
||||
super::ensure_vault_structure(path).unwrap();
|
||||
let original = super::save_attachment(path, "same.png", b"keep").unwrap();
|
||||
fs::write(helixnotes_dir(path).join(mapping), b"{broken").unwrap();
|
||||
let result = if mapping == "notebook_icons.json" {
|
||||
super::import_notebook_icon(path, "Book", "same.png", b"reject")
|
||||
} else {
|
||||
super::import_tag_icon(path, "Work", "same.png", b"reject")
|
||||
.map(|style| style.icon.unwrap())
|
||||
};
|
||||
assert!(result.is_err());
|
||||
assert_eq!(fs::read(vault.path().join(&original)).unwrap(), b"keep");
|
||||
let remaining: Vec<_> = fs::read_dir(helixnotes_dir(path).join("attachments"))
|
||||
.unwrap()
|
||||
.map(|entry| entry.unwrap().path())
|
||||
.collect();
|
||||
assert_eq!(remaining, vec![vault.path().join(original)]);
|
||||
assert_eq!(
|
||||
fs::read(helixnotes_dir(path).join(mapping)).unwrap(),
|
||||
b"{broken"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_icon_mapping_prevents_notebook_move_before_renaming_files() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().to_str().unwrap();
|
||||
super::ensure_vault_structure(path).unwrap();
|
||||
let notebook = create_notebook(path, None, "Book").unwrap();
|
||||
let note = super::create_note(path, Some("Book"), "Keep").unwrap();
|
||||
let before = fs::read(¬e.path).unwrap();
|
||||
let destination = create_notebook(path, None, "Archive").unwrap();
|
||||
fs::write(helixnotes_dir(path).join("notebook_icons.json"), b"{broken").unwrap();
|
||||
assert!(super::rename_notebook(path, ¬ebook.path, "Renamed").is_err());
|
||||
assert!(super::move_notebook(path, ¬ebook.path, &destination.path).is_err());
|
||||
assert_eq!(fs::read(¬e.path).unwrap(), before);
|
||||
assert!(!vault.path().join("Renamed").exists());
|
||||
assert!(!vault.path().join("Archive/Book").exists());
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn failed_metadata_replacement_keeps_registration_and_rolls_back_notebook_move() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().to_str().unwrap();
|
||||
super::ensure_vault_structure(path).unwrap();
|
||||
let notebook = create_notebook(path, None, "Book").unwrap();
|
||||
set_notebook_icon(path, "Book", Some("builtin:folder")).unwrap();
|
||||
let mapping = helixnotes_dir(path).join("notebook_icons.json");
|
||||
let before = fs::read(&mapping).unwrap();
|
||||
fs::set_permissions(&mapping, fs::Permissions::from_mode(0o440)).unwrap();
|
||||
assert!(set_notebook_icon(path, "Book", Some("builtin:star")).is_err());
|
||||
assert!(super::rename_notebook(path, ¬ebook.path, "Renamed").is_err());
|
||||
assert!(std::path::Path::new(¬ebook.path).is_dir());
|
||||
assert!(!vault.path().join("Renamed").exists());
|
||||
assert_eq!(fs::read(&mapping).unwrap(), before);
|
||||
}
|
||||
use uuid::Uuid;
|
||||
|
||||
#[test]
|
||||
@@ -1761,13 +2087,16 @@ mod tests {
|
||||
std::env::temp_dir().join(format!("helixnotes-tag-style-test-{}", Uuid::new_v4()));
|
||||
let vault_path = vault.to_string_lossy();
|
||||
|
||||
set_tag_style(
|
||||
patch_tag_style(
|
||||
&vault_path,
|
||||
" Work ",
|
||||
Some(TagStyle {
|
||||
icon: Some("builtin:briefcase".into()),
|
||||
color: Some("#E11D48".into()),
|
||||
}),
|
||||
TagStyleChange::Icon(Some("builtin:briefcase".into())),
|
||||
)
|
||||
.unwrap();
|
||||
patch_tag_style(
|
||||
&vault_path,
|
||||
"Work",
|
||||
TagStyleChange::Color(Some("#E11D48".into())),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
@@ -1783,7 +2112,7 @@ mod tests {
|
||||
assert_eq!(work.icon.as_deref(), Some("builtin:briefcase"));
|
||||
assert_eq!(work.color.as_deref(), Some("#e11d48"));
|
||||
|
||||
set_tag_style(&vault_path, "Work", None).unwrap();
|
||||
patch_tag_style(&vault_path, "Work", TagStyleChange::Reset).unwrap();
|
||||
assert!(load_tag_styles(&vault_path).unwrap().is_empty());
|
||||
|
||||
fs::remove_dir_all(vault).unwrap();
|
||||
@@ -1795,17 +2124,20 @@ mod tests {
|
||||
std::env::temp_dir().join(format!("helixnotes-tag-style-case-test-{}", Uuid::new_v4()));
|
||||
let vault_path = vault.to_string_lossy();
|
||||
|
||||
set_tag_style(
|
||||
patch_tag_style(
|
||||
&vault_path,
|
||||
"Work",
|
||||
Some(TagStyle {
|
||||
icon: Some("builtin:briefcase".into()),
|
||||
color: Some("#e11d48".into()),
|
||||
}),
|
||||
TagStyleChange::Icon(Some("builtin:briefcase".into())),
|
||||
)
|
||||
.unwrap();
|
||||
patch_tag_style(
|
||||
&vault_path,
|
||||
"Work",
|
||||
TagStyleChange::Color(Some("#e11d48".into())),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
set_tag_style(&vault_path, "work", None).unwrap();
|
||||
patch_tag_style(&vault_path, "work", TagStyleChange::Reset).unwrap();
|
||||
|
||||
let stored: serde_json::Value = serde_json::from_str(
|
||||
&fs::read_to_string(helixnotes_dir(&vault_path).join("tag_styles.json")).unwrap(),
|
||||
@@ -1848,13 +2180,10 @@ mod tests {
|
||||
));
|
||||
let vault_path = vault.to_string_lossy();
|
||||
|
||||
let err = set_tag_style(
|
||||
let err = patch_tag_style(
|
||||
&vault_path,
|
||||
"daily",
|
||||
Some(TagStyle {
|
||||
icon: None,
|
||||
color: Some("red".into()),
|
||||
}),
|
||||
TagStyleChange::Color(Some("red".into())),
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(err.contains("hex"));
|
||||
@@ -1870,24 +2199,13 @@ mod tests {
|
||||
));
|
||||
let vault_path = vault.to_string_lossy();
|
||||
|
||||
set_tag_style(
|
||||
patch_tag_style(
|
||||
&vault_path,
|
||||
"daily",
|
||||
Some(TagStyle {
|
||||
icon: Some("builtin:calendar".into()),
|
||||
color: None,
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
set_tag_style(
|
||||
&vault_path,
|
||||
"daily",
|
||||
Some(TagStyle {
|
||||
icon: None,
|
||||
color: None,
|
||||
}),
|
||||
TagStyleChange::Icon(Some("builtin:calendar".into())),
|
||||
)
|
||||
.unwrap();
|
||||
patch_tag_style(&vault_path, "daily", TagStyleChange::Icon(None)).unwrap();
|
||||
assert!(load_tag_styles(&vault_path).unwrap().is_empty());
|
||||
|
||||
fs::remove_dir_all(vault).unwrap();
|
||||
@@ -2057,4 +2375,151 @@ mod tests {
|
||||
|
||||
fs::remove_dir_all(vault).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mobile_listing_preserves_creation_date_after_atomic_save() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().join("Note.md");
|
||||
fs::write(&path, "---\nid: existing\ntitle: Note\ncreated: 2020-01-01T00:00:00Z\n---\n# Note\n\nOld body.\n").unwrap();
|
||||
let note = read_note(vault.path().to_str().unwrap(), path.to_str().unwrap()).unwrap();
|
||||
save_note(
|
||||
vault.path().to_str().unwrap(),
|
||||
path.to_str().unwrap(),
|
||||
¬e.meta,
|
||||
"New body.",
|
||||
)
|
||||
.unwrap();
|
||||
let mobile = super::read_note_entry_metadata_only(&path, vault.path()).unwrap();
|
||||
let desktop = super::read_note_entry_fast(&path, vault.path()).unwrap();
|
||||
assert_eq!(mobile.meta.created, note.meta.created);
|
||||
assert_eq!(mobile.meta.created, desktop.meta.created);
|
||||
assert_eq!(mobile.preview, "New body.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unchanged_serialized_save_ignores_modified_without_rewriting_the_file() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().join("Note.md");
|
||||
let original = "---\nid: existing\ntitle: Note\ncreated: 2020-01-01T00:00:00Z\nmodified: 2020-01-02T00:00:00Z\naliases: [Legacy]\n---\n# Note\n\nUnchanged body.\n";
|
||||
fs::write(&path, original).unwrap();
|
||||
let vault_path = vault.path().to_string_lossy();
|
||||
let note_path = path.to_string_lossy();
|
||||
let note = read_note(&vault_path, ¬e_path).unwrap();
|
||||
let before = fs::metadata(&path).unwrap().modified().unwrap();
|
||||
let mut meta = note.meta;
|
||||
meta.modified = chrono::Utc::now();
|
||||
let serialized_body = format!("{}\n", note.content);
|
||||
|
||||
assert!(save_note(&vault_path, ¬e_path, &meta, &serialized_body)
|
||||
.unwrap()
|
||||
.is_none());
|
||||
assert_eq!(fs::read_to_string(&path).unwrap(), original);
|
||||
assert_eq!(fs::metadata(&path).unwrap().modified().unwrap(), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_keeps_authored_terminal_spaces_before_the_serializer_newline() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().join("Note.md");
|
||||
let original = "---\nid: existing\ntitle: Note\ncreated: 2020-01-01T00:00:00Z\n---\n# Note\n\nFinal line.\n";
|
||||
fs::write(&path, original).unwrap();
|
||||
let vault_path = vault.path().to_string_lossy();
|
||||
let note_path = path.to_string_lossy();
|
||||
let note = read_note(&vault_path, ¬e_path).unwrap();
|
||||
let body = format!("{} \n", note.content);
|
||||
|
||||
assert!(save_note(&vault_path, ¬e_path, ¬e.meta, &body)
|
||||
.unwrap()
|
||||
.is_some());
|
||||
assert!(fs::read_to_string(&path)
|
||||
.unwrap()
|
||||
.ends_with("Final line. \n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saves_keep_first_identity_unknown_fields_and_previous_content() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().join("Note.md");
|
||||
let original = "---\ntitle: Note\ncreated: 2020-01-01T00:00:00Z\naliases: [Legacy]\n---\nOriginal body.\n";
|
||||
fs::write(&path, original).unwrap();
|
||||
let vault_path = vault.path().to_string_lossy();
|
||||
let note_path = path.to_string_lossy();
|
||||
let meta = read_note(&vault_path, ¬e_path).unwrap().meta;
|
||||
assert!(meta.id.is_empty());
|
||||
|
||||
let first = save_note(&vault_path, ¬e_path, &meta, "# Note\n\nFirst change.\n")
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
Uuid::parse_str(&first.id).unwrap();
|
||||
assert_eq!(first.previous_raw, original);
|
||||
let first_raw = fs::read_to_string(&path).unwrap();
|
||||
let second = save_note(&vault_path, ¬e_path, &meta, "# Note\n\nSecond change.\n")
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(second.id, first.id);
|
||||
assert_eq!(second.previous_raw, first_raw);
|
||||
|
||||
let saved = read_note(&vault_path, ¬e_path).unwrap();
|
||||
assert_eq!(saved.meta.id, first.id);
|
||||
assert_eq!(saved.meta.created, meta.created);
|
||||
assert!(saved.content.contains("Second change."));
|
||||
let parsed = gray_matter::Matter::<gray_matter::engine::YAML>::new().parse(&saved.raw);
|
||||
let fields: serde_yaml::Value = parsed.data.unwrap().deserialize().unwrap();
|
||||
assert_eq!(fields["aliases"][0].as_str(), Some("Legacy"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn atomic_replacement_does_not_truncate_an_open_original() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().join("Note.md");
|
||||
fs::write(&path, "original contents").unwrap();
|
||||
let mut original = fs::File::open(&path).unwrap();
|
||||
|
||||
replace_file_atomically(&path, b"replacement contents").unwrap();
|
||||
|
||||
let mut old_contents = String::new();
|
||||
original.read_to_string(&mut old_contents).unwrap();
|
||||
assert_eq!(old_contents, "original contents");
|
||||
assert_eq!(fs::read_to_string(&path).unwrap(), "replacement contents");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_atomic_replacement_cleans_up_without_removing_destination() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let destination = vault.path().join("Note.md");
|
||||
fs::create_dir(&destination).unwrap();
|
||||
fs::write(destination.join("original"), "keep me").unwrap();
|
||||
|
||||
assert!(replace_file_atomically(&destination, b"replacement").is_err());
|
||||
|
||||
assert_eq!(
|
||||
fs::read_to_string(destination.join("original")).unwrap(),
|
||||
"keep me"
|
||||
);
|
||||
let remaining: Vec<_> = fs::read_dir(vault.path())
|
||||
.unwrap()
|
||||
.map(|entry| entry.unwrap().file_name())
|
||||
.collect();
|
||||
assert_eq!(remaining, vec![std::ffi::OsString::from("Note.md")]);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn atomic_save_preserves_permissions_and_refuses_read_only_notes() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let path = vault.path().join("Note.md");
|
||||
fs::write(&path, "original").unwrap();
|
||||
fs::set_permissions(&path, fs::Permissions::from_mode(0o640)).unwrap();
|
||||
replace_file_atomically(&path, b"saved").unwrap();
|
||||
assert_eq!(
|
||||
fs::metadata(&path).unwrap().permissions().mode() & 0o777,
|
||||
0o640
|
||||
);
|
||||
|
||||
fs::set_permissions(&path, fs::Permissions::from_mode(0o440)).unwrap();
|
||||
assert!(replace_file_atomically(&path, b"must not save").is_err());
|
||||
assert_eq!(fs::read_to_string(&path).unwrap(), "saved");
|
||||
}
|
||||
}
|
||||
+274
-54
@@ -1,15 +1,18 @@
|
||||
use notify::{Config, EventKind, PollWatcher, RecommendedWatcher, RecursiveMode, Watcher};
|
||||
use std::path::Path;
|
||||
use notify::event::{CreateKind, ModifyKind, RemoveKind, RenameMode};
|
||||
use notify::{Config, Event, EventKind, PollWatcher, RecommendedWatcher, RecursiveMode, Watcher};
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use std::sync::mpsc;
|
||||
use std::time::Duration;
|
||||
use std::time::{Duration, Instant};
|
||||
use tauri::{AppHandle, Emitter, Manager};
|
||||
|
||||
use crate::state::AppState;
|
||||
use crate::types::FileEvent;
|
||||
use crate::vault::operations::helixnotes_dir;
|
||||
use crate::vault::operations::{is_hidden, read_note_entry_fast};
|
||||
|
||||
const IOS_POLL_INTERVAL: Duration = Duration::from_secs(10);
|
||||
const NATIVE_POLL_INTERVAL: Duration = Duration::from_secs(1);
|
||||
const EVENT_BATCH_INTERVAL: Duration = Duration::from_millis(50);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum WatcherBackend {
|
||||
@@ -46,6 +49,69 @@ impl VaultWatcher {
|
||||
}
|
||||
}
|
||||
|
||||
fn collect_changes(event: Event, vault_root: &Path, changes: &mut HashMap<PathBuf, &'static str>) {
|
||||
let (default_type, may_be_directory) = match event.kind {
|
||||
EventKind::Create(kind) => ("create", kind != CreateKind::File),
|
||||
EventKind::Remove(kind) => ("remove", kind != RemoveKind::File),
|
||||
EventKind::Modify(ModifyKind::Name(_)) => ("modify", true),
|
||||
EventKind::Modify(_) => ("modify", false),
|
||||
_ => return,
|
||||
};
|
||||
for (index, path) in event.paths.into_iter().enumerate() {
|
||||
let Ok(relative) = path.strip_prefix(vault_root) else {
|
||||
continue;
|
||||
};
|
||||
if !relative.components().all(|component| match component {
|
||||
Component::Normal(name) => !is_hidden(Path::new(name)),
|
||||
Component::CurDir => true,
|
||||
_ => false,
|
||||
}) {
|
||||
continue;
|
||||
}
|
||||
// Removed/renamed directories no longer pass is_dir(). Retain unknown
|
||||
// missing paths so external notebook changes still reach the UI.
|
||||
if path.extension().and_then(|extension| extension.to_str()) != Some("md")
|
||||
&& !path.is_dir()
|
||||
&& !(may_be_directory && !path.exists())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let event_type = match event.kind {
|
||||
EventKind::Modify(ModifyKind::Name(RenameMode::From)) => "remove",
|
||||
EventKind::Modify(ModifyKind::Name(RenameMode::Both)) if index == 0 => "remove",
|
||||
_ => default_type,
|
||||
};
|
||||
changes.insert(path, event_type);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn changed_entry(path: PathBuf, event_type: &str, vault_root: &Path) -> FileEvent {
|
||||
let is_note =
|
||||
path.extension().and_then(|extension| extension.to_str()) == Some("md") && path.is_file();
|
||||
let note = if is_note {
|
||||
read_note_entry_fast(&path, vault_root).ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
// Atomic replacement may also report removal of the old destination. The
|
||||
// final filesystem state, not the platform's rename spelling, is authoritative.
|
||||
let event_type = if path.exists() {
|
||||
if event_type == "remove" {
|
||||
"modify"
|
||||
} else {
|
||||
event_type
|
||||
}
|
||||
} else {
|
||||
"remove"
|
||||
};
|
||||
FileEvent {
|
||||
vault_path: vault_root.to_string_lossy().to_string(),
|
||||
event_type: event_type.to_string(),
|
||||
path: path.to_string_lossy().to_string(),
|
||||
note,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start_watcher(app: AppHandle, vault_path: String) -> Result<VaultWatcher, String> {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
@@ -64,58 +130,80 @@ pub fn start_watcher(app: AppHandle, vault_path: String) -> Result<VaultWatcher,
|
||||
.watch(Path::new(&vault_path), RecursiveMode::Recursive)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let hn_dir = helixnotes_dir(&vault_path);
|
||||
|
||||
std::thread::spawn(move || {
|
||||
while let Ok(result) = rx.recv() {
|
||||
match result {
|
||||
Ok(event) => {
|
||||
// Skip all file events while importing
|
||||
let state = app.state::<AppState>();
|
||||
if state.importing.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
continue;
|
||||
}
|
||||
// Skip .helixnotes directory events
|
||||
let dominated_by_hn = event.paths.iter().all(|p| p.starts_with(&hn_dir));
|
||||
if dominated_by_hn {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Only care about .md file events
|
||||
let has_md = event.paths.iter().any(|p| {
|
||||
p.extension().and_then(|x| x.to_str()) == Some("md") || p.is_dir()
|
||||
});
|
||||
|
||||
if !has_md {
|
||||
continue;
|
||||
}
|
||||
|
||||
let event_type = match event.kind {
|
||||
EventKind::Create(_) => "create",
|
||||
EventKind::Modify(_) => "modify",
|
||||
EventKind::Remove(_) => "remove",
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
for path in &event.paths {
|
||||
let fe = FileEvent {
|
||||
event_type: event_type.to_string(),
|
||||
path: path.to_string_lossy().to_string(),
|
||||
};
|
||||
let _ = app.emit("file-changed", &fe);
|
||||
}
|
||||
|
||||
// On mobile, throttle event emission to prevent IPC flooding
|
||||
// on FUSE filesystems where Syncthing/other apps generate
|
||||
// constant file activity that blocks the Tauri command channel.
|
||||
#[cfg(mobile)]
|
||||
{
|
||||
std::thread::sleep(Duration::from_secs(2));
|
||||
while rx.try_recv().is_ok() {}
|
||||
}
|
||||
let vault_root = Path::new(&vault_path);
|
||||
let mut changes = HashMap::new();
|
||||
loop {
|
||||
let first = if changes.is_empty() {
|
||||
match rx.recv() {
|
||||
Ok(event) => Some(event),
|
||||
Err(_) => return,
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("File watcher error: {}", e);
|
||||
} else {
|
||||
match rx.recv_timeout(EVENT_BATCH_INTERVAL) {
|
||||
Ok(event) => Some(event),
|
||||
Err(mpsc::RecvTimeoutError::Timeout) => None,
|
||||
Err(mpsc::RecvTimeoutError::Disconnected) => return,
|
||||
}
|
||||
};
|
||||
let deadline = Instant::now() + EVENT_BATCH_INTERVAL;
|
||||
let mut next = first;
|
||||
while let Some(result) = next {
|
||||
match result {
|
||||
Ok(event) => {
|
||||
collect_changes(event, vault_root, &mut changes);
|
||||
}
|
||||
Err(error) => log::error!("File watcher error: {error}"),
|
||||
}
|
||||
let remaining = deadline.saturating_duration_since(Instant::now());
|
||||
if remaining.is_zero() {
|
||||
break;
|
||||
}
|
||||
next = rx.recv_timeout(remaining).ok();
|
||||
}
|
||||
let state = app.state::<AppState>();
|
||||
// Keep the batch while muted, rather than losing concurrent external
|
||||
// edits. Nothing is indexed or emitted until local imports finish.
|
||||
if state.importing.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
continue;
|
||||
}
|
||||
let search = {
|
||||
// Match the runtime commit's lock order so an old watcher cannot
|
||||
// send its final batch into the newly selected vault's index.
|
||||
let Ok(search) = state.search_index.lock() else {
|
||||
continue;
|
||||
};
|
||||
let active = state
|
||||
.config
|
||||
.lock()
|
||||
.ok()
|
||||
.is_some_and(|config| config.active_vault.as_deref() == Some(&vault_path));
|
||||
if !active {
|
||||
continue;
|
||||
}
|
||||
search.clone()
|
||||
};
|
||||
let mut refresh_vault = false;
|
||||
for (path, event_type) in changes.drain() {
|
||||
if path.extension().and_then(|extension| extension.to_str()) == Some("md")
|
||||
&& !path.is_dir()
|
||||
{
|
||||
if let Some(search) = &search {
|
||||
if let Err(error) = search.queue_index_note(&path.to_string_lossy()) {
|
||||
log::error!("Could not queue external note for search: {error}");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
refresh_vault = true;
|
||||
}
|
||||
let event = changed_entry(path, event_type, vault_root);
|
||||
let _ = app.emit("file-changed", &event);
|
||||
}
|
||||
if refresh_vault {
|
||||
if let Some(search) = &search {
|
||||
if let Err(error) = search.queue_vault_refresh(&vault_path) {
|
||||
log::error!("Could not queue notebook search refresh: {error}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -152,4 +240,136 @@ mod tests {
|
||||
Duration::from_secs(1)
|
||||
);
|
||||
}
|
||||
|
||||
fn mapped_events(events: Vec<Event>, vault_root: &Path) -> Vec<FileEvent> {
|
||||
let mut changes = HashMap::new();
|
||||
for event in events {
|
||||
collect_changes(event, vault_root, &mut changes);
|
||||
}
|
||||
changes
|
||||
.into_iter()
|
||||
.map(|(path, event_type)| changed_entry(path, event_type, vault_root))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn atomic_replace_emits_only_enriched_destination() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let target = vault.path().join("Note.md");
|
||||
let temporary = vault.path().join(".helixnotes-save-123");
|
||||
std::fs::write(&target, "---\ntitle: New title\n---\nUpdated preview.").unwrap();
|
||||
let events = mapped_events(
|
||||
vec![
|
||||
Event::new(EventKind::Modify(ModifyKind::Name(RenameMode::Both)))
|
||||
.add_path(temporary)
|
||||
.add_path(target.clone()),
|
||||
Event::new(EventKind::Remove(RemoveKind::File)).add_path(target.clone()),
|
||||
Event::new(EventKind::Create(CreateKind::File))
|
||||
.add_path(vault.path().join(".helixnotes/history/snapshot.md")),
|
||||
Event::new(EventKind::Create(CreateKind::File))
|
||||
.add_path(vault.path().join(".hidden/secret.md")),
|
||||
],
|
||||
vault.path(),
|
||||
);
|
||||
|
||||
assert_eq!(events.len(), 1);
|
||||
assert_eq!(events[0].path, target.to_string_lossy());
|
||||
assert_eq!(events[0].event_type, "modify");
|
||||
let note = events[0].note.as_ref().unwrap();
|
||||
assert_eq!(note.meta.title, "New title");
|
||||
assert_eq!(note.relative_path, "Note.md");
|
||||
assert_eq!(note.preview, "Updated preview.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_note_and_directory_renames_keep_both_sides() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let old_note = vault.path().join("Before.md");
|
||||
let new_note = vault.path().join("After.md");
|
||||
let old_directory = vault.path().join("Old.notebook");
|
||||
let new_directory = vault.path().join("New.notebook");
|
||||
std::fs::write(&new_note, "renamed note").unwrap();
|
||||
std::fs::create_dir(&new_directory).unwrap();
|
||||
let events = mapped_events(
|
||||
vec![
|
||||
Event::new(EventKind::Modify(ModifyKind::Name(RenameMode::Both)))
|
||||
.add_path(old_note.clone())
|
||||
.add_path(new_note.clone()),
|
||||
Event::new(EventKind::Modify(ModifyKind::Name(RenameMode::Both)))
|
||||
.add_path(old_directory.clone())
|
||||
.add_path(new_directory.clone()),
|
||||
],
|
||||
vault.path(),
|
||||
);
|
||||
|
||||
for removed in [old_note, old_directory] {
|
||||
let event = events
|
||||
.iter()
|
||||
.find(|event| event.path == removed.to_string_lossy())
|
||||
.unwrap();
|
||||
assert_eq!(event.event_type, "remove");
|
||||
assert!(event.note.is_none());
|
||||
}
|
||||
let note = events
|
||||
.iter()
|
||||
.find(|event| event.path == new_note.to_string_lossy())
|
||||
.unwrap();
|
||||
assert_eq!(note.event_type, "modify");
|
||||
assert!(note.note.is_some());
|
||||
let directory = events
|
||||
.iter()
|
||||
.find(|event| event.path == new_directory.to_string_lossy())
|
||||
.unwrap();
|
||||
assert_eq!(directory.event_type, "modify");
|
||||
assert!(directory.note.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn batching_keeps_distinct_external_changes_and_deleted_directories() {
|
||||
let vault = tempfile::tempdir().unwrap();
|
||||
let first = vault.path().join("First.md");
|
||||
let second = vault.path().join("Second.md");
|
||||
let removed = vault.path().join("Removed.notebook");
|
||||
std::fs::write(&first, "first external change").unwrap();
|
||||
std::fs::write(&second, "second external change").unwrap();
|
||||
let events = mapped_events(
|
||||
vec![
|
||||
Event::new(EventKind::Create(CreateKind::File)).add_path(first.clone()),
|
||||
Event::new(EventKind::Modify(ModifyKind::Any)).add_path(first.clone()),
|
||||
Event::new(EventKind::Create(CreateKind::File)).add_path(second.clone()),
|
||||
Event::new(EventKind::Remove(RemoveKind::Folder)).add_path(removed.clone()),
|
||||
],
|
||||
vault.path(),
|
||||
);
|
||||
|
||||
let first_event = events
|
||||
.iter()
|
||||
.find(|event| event.path == first.to_string_lossy())
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
first_event.note.as_ref().unwrap().preview,
|
||||
"first external change"
|
||||
);
|
||||
let second_event = events
|
||||
.iter()
|
||||
.find(|event| event.path == second.to_string_lossy())
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
second_event.note.as_ref().unwrap().preview,
|
||||
"second external change"
|
||||
);
|
||||
let removed_event = events
|
||||
.iter()
|
||||
.find(|event| event.path == removed.to_string_lossy())
|
||||
.unwrap();
|
||||
assert_eq!(removed_event.event_type, "remove");
|
||||
assert!(removed_event.note.is_none());
|
||||
assert_eq!(
|
||||
events
|
||||
.iter()
|
||||
.filter(|event| event.path == first.to_string_lossy())
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user