Skip to content

Commit 8317e2a

Browse files
committed
When finding new files/messages/replies, work back in time
Retrieving submissions and replies for the most recently active sources first will make the initial sync feel faster, as the visible part of the source list is updated first, and will make it possible to for journalists to work with what are likely the most interesting sources sooner.
1 parent 734071f commit 8317e2a

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

securedrop_client/storage.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,9 @@ def update_draft_replies(
429429

430430

431431
def find_new_files(session: Session) -> List[File]:
432-
return session.query(File).filter_by(is_downloaded=False).all()
432+
q = session.query(File).join(Source).filter_by(is_downloaded=False)
433+
q = q.order_by(desc(Source.last_updated))
434+
return q.all()
433435

434436

435437
def find_new_messages(session: Session) -> List[Message]:
@@ -441,10 +443,15 @@ def find_new_messages(session: Session) -> List[Message]:
441443
* The message has not yet had decryption attempted.
442444
* Decryption previously failed on a message.
443445
"""
444-
return session.query(Message).filter(
445-
or_(Message.is_downloaded == False,
446+
q = session.query(Message).join(Source).filter(
447+
or_(
448+
Message.is_downloaded == False,
446449
Message.is_decrypted == False,
447-
Message.is_decrypted == None)).all() # noqa: E711
450+
Message.is_decrypted == None
451+
)
452+
) # noqa: E712
453+
q = q.order_by(desc(Source.last_updated))
454+
return q.all()
448455

449456

450457
def find_new_replies(session: Session) -> List[Reply]:
@@ -456,10 +463,15 @@ def find_new_replies(session: Session) -> List[Reply]:
456463
* The reply has not yet had decryption attempted.
457464
* Decryption previously failed on a reply.
458465
"""
459-
return session.query(Reply).filter(
460-
or_(Reply.is_downloaded == False,
466+
q = session.query(Reply).join(Source).filter(
467+
or_(
468+
Reply.is_downloaded == False,
461469
Reply.is_decrypted == False,
462-
Reply.is_decrypted == None)).all() # noqa: E711
470+
Reply.is_decrypted == None
471+
)
472+
) # noqa: E712
473+
q = q.order_by(desc(Source.last_updated))
474+
return q.all()
463475

464476

465477
def mark_as_not_downloaded(uuid: str, session: Session) -> None:

tests/test_storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ def test_find_new_files(mocker, session):
903903
mock_submission = mocker.MagicMock()
904904
mock_submission.is_downloaded = False
905905
mock_submissions = [mock_submission]
906-
mock_session.query().filter_by().all.return_value = mock_submissions
906+
mock_session.query().join().filter_by().order_by().all.return_value = mock_submissions
907907
submissions = find_new_files(mock_session)
908908
assert submissions[0].is_downloaded is False
909909

0 commit comments

Comments
 (0)