mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-09 15:58:48 +00:00
Merge pull request #469 from PR0M3TH3AN/codex/remove-sync_index_from_nostr-calls
Refactor sync trigger to background thread
This commit is contained in:
@@ -741,6 +741,7 @@ def handle_settings(password_manager: PasswordManager) -> None:
|
|||||||
password_manager.lock_vault()
|
password_manager.lock_vault()
|
||||||
print(colored("Vault locked. Please re-enter your password.", "yellow"))
|
print(colored("Vault locked. Please re-enter your password.", "yellow"))
|
||||||
password_manager.unlock_vault()
|
password_manager.unlock_vault()
|
||||||
|
password_manager.start_background_sync()
|
||||||
pause()
|
pause()
|
||||||
elif choice == "13":
|
elif choice == "13":
|
||||||
handle_display_stats(password_manager)
|
handle_display_stats(password_manager)
|
||||||
@@ -777,6 +778,7 @@ def display_menu(
|
|||||||
if callable(display_fn):
|
if callable(display_fn):
|
||||||
display_fn()
|
display_fn()
|
||||||
pause()
|
pause()
|
||||||
|
password_manager.start_background_sync()
|
||||||
while True:
|
while True:
|
||||||
fp, parent_fp, child_fp = getattr(
|
fp, parent_fp, child_fp = getattr(
|
||||||
password_manager,
|
password_manager,
|
||||||
@@ -793,6 +795,7 @@ def display_menu(
|
|||||||
print(colored("Session timed out. Vault locked.", "yellow"))
|
print(colored("Session timed out. Vault locked.", "yellow"))
|
||||||
password_manager.lock_vault()
|
password_manager.lock_vault()
|
||||||
password_manager.unlock_vault()
|
password_manager.unlock_vault()
|
||||||
|
password_manager.start_background_sync()
|
||||||
continue
|
continue
|
||||||
# Periodically push updates to Nostr
|
# Periodically push updates to Nostr
|
||||||
if (
|
if (
|
||||||
@@ -815,6 +818,7 @@ def display_menu(
|
|||||||
print(colored("Session timed out. Vault locked.", "yellow"))
|
print(colored("Session timed out. Vault locked.", "yellow"))
|
||||||
password_manager.lock_vault()
|
password_manager.lock_vault()
|
||||||
password_manager.unlock_vault()
|
password_manager.unlock_vault()
|
||||||
|
password_manager.start_background_sync()
|
||||||
continue
|
continue
|
||||||
password_manager.update_activity()
|
password_manager.update_activity()
|
||||||
if not choice:
|
if not choice:
|
||||||
|
@@ -19,6 +19,7 @@ from typing import Optional
|
|||||||
import shutil
|
import shutil
|
||||||
import time
|
import time
|
||||||
import builtins
|
import builtins
|
||||||
|
import threading
|
||||||
from termcolor import colored
|
from termcolor import colored
|
||||||
from utils.color_scheme import color_text
|
from utils.color_scheme import color_text
|
||||||
from utils.input_utils import timed_input
|
from utils.input_utils import timed_input
|
||||||
@@ -234,7 +235,6 @@ class PasswordManager:
|
|||||||
self.initialize_managers()
|
self.initialize_managers()
|
||||||
self.locked = False
|
self.locked = False
|
||||||
self.update_activity()
|
self.update_activity()
|
||||||
self.sync_index_from_nostr()
|
|
||||||
self.last_unlock_duration = time.perf_counter() - start
|
self.last_unlock_duration = time.perf_counter() - start
|
||||||
print(
|
print(
|
||||||
colored(
|
colored(
|
||||||
@@ -358,7 +358,6 @@ class PasswordManager:
|
|||||||
# Initialize BIP85 and other managers
|
# Initialize BIP85 and other managers
|
||||||
self.initialize_bip85()
|
self.initialize_bip85()
|
||||||
self.initialize_managers()
|
self.initialize_managers()
|
||||||
self.sync_index_from_nostr()
|
|
||||||
print(
|
print(
|
||||||
colored(
|
colored(
|
||||||
f"Seed profile {fingerprint} selected and managers initialized.",
|
f"Seed profile {fingerprint} selected and managers initialized.",
|
||||||
@@ -967,6 +966,24 @@ class PasswordManager:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Unable to sync index from Nostr: {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:
|
def sync_index_from_nostr_if_missing(self) -> None:
|
||||||
"""Retrieve the password database from Nostr if it doesn't exist locally."""
|
"""Retrieve the password database from Nostr if it doesn't exist locally."""
|
||||||
index_file = self.fingerprint_dir / "seedpass_entries_db.json.enc"
|
index_file = self.fingerprint_dir / "seedpass_entries_db.json.enc"
|
||||||
|
@@ -22,6 +22,7 @@ def test_auto_sync_triggers_post(monkeypatch):
|
|||||||
update_activity=lambda: None,
|
update_activity=lambda: None,
|
||||||
lock_vault=lambda: None,
|
lock_vault=lambda: None,
|
||||||
unlock_vault=lambda: None,
|
unlock_vault=lambda: None,
|
||||||
|
start_background_sync=lambda: None,
|
||||||
)
|
)
|
||||||
|
|
||||||
called = False
|
called = False
|
||||||
|
@@ -46,6 +46,7 @@ def _make_pm(called, locked=None):
|
|||||||
update_activity=update,
|
update_activity=update,
|
||||||
lock_vault=lock,
|
lock_vault=lock,
|
||||||
unlock_vault=unlock,
|
unlock_vault=unlock,
|
||||||
|
start_background_sync=lambda: None,
|
||||||
)
|
)
|
||||||
return pm, locked
|
return pm, locked
|
||||||
|
|
||||||
|
@@ -34,6 +34,7 @@ def test_inactivity_triggers_lock(monkeypatch):
|
|||||||
update_activity=update_activity,
|
update_activity=update_activity,
|
||||||
lock_vault=lock_vault,
|
lock_vault=lock_vault,
|
||||||
unlock_vault=unlock_vault,
|
unlock_vault=unlock_vault,
|
||||||
|
start_background_sync=lambda: None,
|
||||||
)
|
)
|
||||||
|
|
||||||
monkeypatch.setattr(main, "timed_input", lambda *_: "")
|
monkeypatch.setattr(main, "timed_input", lambda *_: "")
|
||||||
@@ -70,6 +71,7 @@ def test_input_timeout_triggers_lock(monkeypatch):
|
|||||||
update_activity=update_activity,
|
update_activity=update_activity,
|
||||||
lock_vault=lock_vault,
|
lock_vault=lock_vault,
|
||||||
unlock_vault=unlock_vault,
|
unlock_vault=unlock_vault,
|
||||||
|
start_background_sync=lambda: None,
|
||||||
)
|
)
|
||||||
|
|
||||||
responses = iter([TimeoutError(), ""])
|
responses = iter([TimeoutError(), ""])
|
||||||
|
@@ -30,6 +30,7 @@ def _make_pm(calls):
|
|||||||
update_activity=lambda: None,
|
update_activity=lambda: None,
|
||||||
lock_vault=lambda: None,
|
lock_vault=lambda: None,
|
||||||
unlock_vault=lambda: None,
|
unlock_vault=lambda: None,
|
||||||
|
start_background_sync=lambda: None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@@ -24,6 +24,7 @@ def _make_pm(calls):
|
|||||||
update_activity=lambda: None,
|
update_activity=lambda: None,
|
||||||
lock_vault=lambda: None,
|
lock_vault=lambda: None,
|
||||||
unlock_vault=lambda: None,
|
unlock_vault=lambda: None,
|
||||||
|
start_background_sync=lambda: None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@@ -23,6 +23,7 @@ def _make_pm(called):
|
|||||||
update_activity=lambda: None,
|
update_activity=lambda: None,
|
||||||
lock_vault=lambda: None,
|
lock_vault=lambda: None,
|
||||||
unlock_vault=lambda: None,
|
unlock_vault=lambda: None,
|
||||||
|
start_background_sync=lambda: None,
|
||||||
)
|
)
|
||||||
return pm
|
return pm
|
||||||
|
|
||||||
|
@@ -22,5 +22,7 @@ def test_unlock_triggers_sync(monkeypatch, tmp_path):
|
|||||||
monkeypatch.setattr(PasswordManager, "sync_index_from_nostr", fake_sync)
|
monkeypatch.setattr(PasswordManager, "sync_index_from_nostr", fake_sync)
|
||||||
|
|
||||||
pm.unlock_vault()
|
pm.unlock_vault()
|
||||||
|
pm.start_background_sync()
|
||||||
|
time.sleep(0.05)
|
||||||
|
|
||||||
assert called["sync"]
|
assert called["sync"]
|
||||||
|
Reference in New Issue
Block a user