Skip to content

Commit

Permalink
Merge pull request #1131 from freedomofpress/show-correct-preview-as-…
Browse files Browse the repository at this point in the history
…msgs-decrypt

Only show a preview if its the *last* message/file/reply for a source
  • Loading branch information
kushaldas authored Aug 4, 2020
2 parents 514b0ff + 89079e8 commit 291350c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
20 changes: 13 additions & 7 deletions securedrop_client/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ def set_snippet(self, source_uuid: str, collection_item_uuid: str, content: str)
"""
source_widget = self.get_source_widget(source_uuid)
if source_widget:
source_widget.set_snippet(source_uuid, content)
source_widget.set_snippet(source_uuid, collection_item_uuid, content)


class SourceWidget(QWidget):
Expand Down Expand Up @@ -1081,25 +1081,31 @@ def update(self):
self.timestamp.setText(_(arrow.get(self.source.last_updated).format("DD MMM")))
self.name.setText(self.source.journalist_designation)

if not self.source.server_collection:
self.set_snippet(self.source_uuid, "")
else:
last_collection_obj = self.source.server_collection[-1]
self.set_snippet(self.source_uuid, str(last_collection_obj))
self.set_snippet(self.source_uuid)

if self.source.document_count == 0:
self.paperclip.hide()
self.star.update(self.source.is_starred)
except sqlalchemy.exc.InvalidRequestError as e:
logger.debug(f"Could not update SourceWidget for source {self.source_uuid}: {e}")

def set_snippet(self, source_uuid: str, content: str):
def set_snippet(self, source_uuid: str, collection_uuid: str = None, content: str = None):
"""
Update the preview snippet if the source_uuid matches our own.
"""
if source_uuid != self.source_uuid:
return

if not self.source.server_collection:
return

last_activity = self.source.server_collection[-1]
if collection_uuid and collection_uuid != last_activity.uuid:
return

if not content:
content = str(last_activity)

self.preview.setText(content)

def delete_source(self, event):
Expand Down
16 changes: 8 additions & 8 deletions tests/gui/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ def test_SourceList_set_snippet(mocker):

sl.set_snippet("mock_uuid", "msg_uuid", "msg_content")

source_widget.set_snippet.assert_called_once_with("mock_uuid", "msg_content")
source_widget.set_snippet.assert_called_once_with("mock_uuid", "msg_uuid", "msg_content")


def test_SourceList_get_source_widget(mocker):
Expand Down Expand Up @@ -1248,8 +1248,8 @@ def test_SourceWidget_set_snippet_draft_only(mocker, session_maker, session, hom
session.commit()

sw = SourceWidget(controller, source)
sw.set_snippet(source.uuid, f.filename)
assert sw.preview.text() == f.filename
sw.set_snippet(source.uuid, reply.uuid, f.filename)
assert sw.preview.text() == "File: " + f.filename


def test_SourceWidget_set_snippet(mocker, session_maker, session, homedir):
Expand All @@ -1265,19 +1265,19 @@ def test_SourceWidget_set_snippet(mocker, session_maker, session, homedir):
session.commit()

sw = SourceWidget(controller, source)
sw.set_snippet(source.uuid, f.filename)
assert sw.preview.text() == f.filename
sw.set_snippet(source.uuid, "mock_file_uuid", f.filename)
assert sw.preview.text() == "File: " + f.filename

# check when a different source is specified
sw.set_snippet("not-the-source-uuid", "something new")
assert sw.preview.text() == f.filename
sw.set_snippet("not-the-source-uuid", "mock_file_uuid", "something new")
assert sw.preview.text() == "File: " + f.filename

source_uuid = source.uuid
session.delete(source)
session.commit()

# check when the source has been deleted that it catches sqlalchemy.exc.InvalidRequestError
sw.set_snippet(source_uuid, "something new")
sw.set_snippet(source_uuid, "mock_file_uuid", "something new")


def test_SourceWidget_update_truncate_latest_msg(mocker):
Expand Down

0 comments on commit 291350c

Please sign in to comment.