Merge pull request #469 from PR0M3TH3AN/codex/remove-sync_index_from_nostr-calls

Refactor sync trigger to background thread
This commit is contained in:
thePR0M3TH3AN
2025-07-11 22:42:53 -04:00
committed by GitHub
9 changed files with 32 additions and 2 deletions

View File

@@ -741,6 +741,7 @@ def handle_settings(password_manager: PasswordManager) -> None:
password_manager.lock_vault()
print(colored("Vault locked. Please re-enter your password.", "yellow"))
password_manager.unlock_vault()
password_manager.start_background_sync()
pause()
elif choice == "13":
handle_display_stats(password_manager)
@@ -777,6 +778,7 @@ def display_menu(
if callable(display_fn):
display_fn()
pause()
password_manager.start_background_sync()
while True:
fp, parent_fp, child_fp = getattr(
password_manager,
@@ -793,6 +795,7 @@ def display_menu(
print(colored("Session timed out. Vault locked.", "yellow"))
password_manager.lock_vault()
password_manager.unlock_vault()
password_manager.start_background_sync()
continue
# Periodically push updates to Nostr
if (
@@ -815,6 +818,7 @@ def display_menu(
print(colored("Session timed out. Vault locked.", "yellow"))
password_manager.lock_vault()
password_manager.unlock_vault()
password_manager.start_background_sync()
continue
password_manager.update_activity()
if not choice:

View File

@@ -19,6 +19,7 @@ from typing import Optional
import shutil
import time
import builtins
import threading
from termcolor import colored
from utils.color_scheme import color_text
from utils.input_utils import timed_input
@@ -234,7 +235,6 @@ class PasswordManager:
self.initialize_managers()
self.locked = False
self.update_activity()
self.sync_index_from_nostr()
self.last_unlock_duration = time.perf_counter() - start
print(
colored(
@@ -358,7 +358,6 @@ class PasswordManager:
# Initialize BIP85 and other managers
self.initialize_bip85()
self.initialize_managers()
self.sync_index_from_nostr()
print(
colored(
f"Seed profile {fingerprint} selected and managers initialized.",
@@ -967,6 +966,24 @@ class PasswordManager:
except Exception as e:
logger.warning(f"Unable to sync index from Nostr: {e}")
def start_background_sync(self) -> None:
"""Launch a thread to synchronize the vault without blocking the UI."""
if (
hasattr(self, "_sync_thread")
and self._sync_thread
and self._sync_thread.is_alive()
):
return
def _worker() -> None:
try:
self.sync_index_from_nostr()
except Exception as exc:
logger.warning(f"Background sync failed: {exc}")
self._sync_thread = threading.Thread(target=_worker, daemon=True)
self._sync_thread.start()
def sync_index_from_nostr_if_missing(self) -> None:
"""Retrieve the password database from Nostr if it doesn't exist locally."""
index_file = self.fingerprint_dir / "seedpass_entries_db.json.enc"

View File

@@ -22,6 +22,7 @@ def test_auto_sync_triggers_post(monkeypatch):
update_activity=lambda: None,
lock_vault=lambda: None,
unlock_vault=lambda: None,
start_background_sync=lambda: None,
)
called = False

View File

@@ -46,6 +46,7 @@ def _make_pm(called, locked=None):
update_activity=update,
lock_vault=lock,
unlock_vault=unlock,
start_background_sync=lambda: None,
)
return pm, locked

View File

@@ -34,6 +34,7 @@ def test_inactivity_triggers_lock(monkeypatch):
update_activity=update_activity,
lock_vault=lock_vault,
unlock_vault=unlock_vault,
start_background_sync=lambda: None,
)
monkeypatch.setattr(main, "timed_input", lambda *_: "")
@@ -70,6 +71,7 @@ def test_input_timeout_triggers_lock(monkeypatch):
update_activity=update_activity,
lock_vault=lock_vault,
unlock_vault=unlock_vault,
start_background_sync=lambda: None,
)
responses = iter([TimeoutError(), ""])

View File

@@ -30,6 +30,7 @@ def _make_pm(calls):
update_activity=lambda: None,
lock_vault=lambda: None,
unlock_vault=lambda: None,
start_background_sync=lambda: None,
)

View File

@@ -24,6 +24,7 @@ def _make_pm(calls):
update_activity=lambda: None,
lock_vault=lambda: None,
unlock_vault=lambda: None,
start_background_sync=lambda: None,
)

View File

@@ -23,6 +23,7 @@ def _make_pm(called):
update_activity=lambda: None,
lock_vault=lambda: None,
unlock_vault=lambda: None,
start_background_sync=lambda: None,
)
return pm

View File

@@ -22,5 +22,7 @@ def test_unlock_triggers_sync(monkeypatch, tmp_path):
monkeypatch.setattr(PasswordManager, "sync_index_from_nostr", fake_sync)
pm.unlock_vault()
pm.start_background_sync()
time.sleep(0.05)
assert called["sync"]