Skip to content

Commit

Permalink
WIP ensure sync updates state
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalo-bulnes committed Dec 25, 2021
1 parent 9ebdbc7 commit d6bba42
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
13 changes: 12 additions & 1 deletion securedrop_client/api_jobs/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ class MetadataSyncJob(ApiJob):

NUMBER_OF_TIMES_TO_RETRY_AN_API_CALL = 2

def __init__(self, data_dir: str) -> None:
def __init__(self, data_dir: str, state: Any) -> None:
super().__init__(remaining_attempts=self.NUMBER_OF_TIMES_TO_RETRY_AN_API_CALL)
self.data_dir = data_dir
self._state = state

def call_api(self, api_client: API, session: Session) -> Any:
"""
Expand All @@ -40,6 +41,16 @@ def call_api(self, api_client: API, session: Session) -> Any:
sources, submissions, replies = get_remote_data(api_client)

update_local_storage(session, sources, submissions, replies, self.data_dir)

for source in sources:
source_id = source.uuid
files = []
for submission in submissions:
if submission.source_id == source_id and submission.is_file():
files.append(submission.uuid)
conversation_id = source_id
self._state.upsert_conversation(conversation_id, files)

user = api_client.get_current_user()
if "uuid" in user and "username" in user and "first_name" in user and "last_name" in user:
create_or_update_user(
Expand Down
1 change: 1 addition & 0 deletions securedrop_client/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def start_app(args, qt_args) -> NoReturn: # type: ignore [no-untyped-def]
gui_state_writer = state.Writer(gui_state)
gui = Window(gui_state)
gui.source_selection_changed.connect(gui_state_writer.set_selected_conversation)
gui.conversation_updated.connect(gui_state_writer.upsert_conversation)

controller = Controller(
"http://localhost:8081/",
Expand Down
1 change: 1 addition & 0 deletions securedrop_client/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Window(QMainWindow):
icon = "icon.png"

source_selection_changed = pyqtSignal(str)
# conversation_updated = pyqtSignal(str, str) # consersation_id, pickled list of files

def __init__(self, state: Any) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion securedrop_client/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def __init__( # type: ignore [no-untyped-def]
self.data_dir = os.path.join(self.home, "data")

# Background sync to keep client up-to-date with server changes
self.api_sync = ApiSync(self.api, self.session_maker, self.gpg, self.data_dir)
self.api_sync = ApiSync(self.api, self.session_maker, self.gpg, self.data_dir, state)
self.api_sync.sync_started.connect(self.on_sync_started, type=Qt.QueuedConnection)
self.api_sync.sync_success.connect(self.on_sync_success, type=Qt.QueuedConnection)
self.api_sync.sync_failure.connect(self.on_sync_failure, type=Qt.QueuedConnection)
Expand Down
12 changes: 10 additions & 2 deletions securedrop_client/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from securedrop_client.api_jobs.base import ApiInaccessibleError
from securedrop_client.api_jobs.sync import MetadataSyncJob
from securedrop_client.crypto import GpgHelper
from securedrop_client.gui import state

logger = logging.getLogger(__name__)

Expand All @@ -23,7 +24,12 @@ class ApiSync(QObject):
TIME_BETWEEN_SYNCS_MS = 1000 * 15 # fifteen seconds between syncs

def __init__(
self, api_client: API, session_maker: scoped_session, gpg: GpgHelper, data_dir: str
self,
api_client: API,
session_maker: scoped_session,
gpg: GpgHelper,
data_dir: str,
state: state.State,
):
super().__init__()
self.api_client = api_client
Expand All @@ -37,6 +43,7 @@ def __init__(
self.sync_started,
self.on_sync_success,
self.on_sync_failure,
state,
)
self.api_sync_bg_task.moveToThread(self.sync_thread)

Expand Down Expand Up @@ -102,6 +109,7 @@ def __init__( # type: ignore [no-untyped-def]
sync_started: pyqtSignal,
on_sync_success,
on_sync_failure,
state: state.State,
):
super().__init__()

Expand All @@ -113,7 +121,7 @@ def __init__( # type: ignore [no-untyped-def]
self.on_sync_success = on_sync_success
self.on_sync_failure = on_sync_failure

self.job = MetadataSyncJob(self.data_dir)
self.job = MetadataSyncJob(self.data_dir, state)
self.job.success_signal.connect(self.on_sync_success, type=Qt.QueuedConnection)
self.job.failure_signal.connect(self.on_sync_failure, type=Qt.QueuedConnection)

Expand Down

0 comments on commit d6bba42

Please sign in to comment.