From f3a41ac7964b95aedf40acfdd20c95a26183972a Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Tue, 15 Aug 2017 10:34:15 -0700 Subject: [PATCH] unread: Convert mark_stream_as_read to use stream IDs. The logic is simpler and more robust. --- static/js/stream_popover.js | 2 +- static/js/unread_ops.js | 4 ++-- zerver/tests/test_unread.py | 10 +++++----- zerver/views/messages.py | 10 +++------- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/static/js/stream_popover.js b/static/js/stream_popover.js index 13edcf2699..482a8b0f96 100644 --- a/static/js/stream_popover.js +++ b/static/js/stream_popover.js @@ -211,7 +211,7 @@ exports.register_stream_handlers = function () { $('body').on('click', '.mark_stream_as_read', function (e) { var sub = stream_popover_sub(e); exports.hide_stream_popover(); - unread_ops.mark_stream_as_read(sub.name); + unread_ops.mark_stream_as_read(sub.stream_id); e.stopPropagation(); }); diff --git a/static/js/unread_ops.js b/static/js/unread_ops.js index cb87eb562c..5fdb7aca06 100644 --- a/static/js/unread_ops.js +++ b/static/js/unread_ops.js @@ -118,11 +118,11 @@ exports.mark_current_list_as_read = function mark_current_list_as_read(options) exports.mark_messages_as_read(current_msg_list.all_messages(), options); }; -exports.mark_stream_as_read = function mark_stream_as_read(stream, cont) { +exports.mark_stream_as_read = function mark_stream_as_read(stream_id, cont) { channel.post({ url: '/json/mark_stream_as_read', idempotent: true, - data: {stream_name: stream}, + data: {stream_id: stream_id}, success: cont}); }; diff --git a/zerver/tests/test_unread.py b/zerver/tests/test_unread.py index 9a1fa6bac4..f20a750c2e 100644 --- a/zerver/tests/test_unread.py +++ b/zerver/tests/test_unread.py @@ -213,7 +213,7 @@ class UnreadCountTests(ZulipTestCase): # type: () -> None self.login(self.example_email("hamlet")) user_profile = self.example_user('hamlet') - self.subscribe_to_stream(user_profile.email, "test_stream", user_profile.realm) + stream = self.subscribe_to_stream(user_profile.email, "test_stream", user_profile.realm) self.subscribe_to_stream(self.example_email("cordelia"), "test_stream", user_profile.realm) message_id = self.send_message(self.example_email("hamlet"), "test_stream", Recipient.STREAM, "hello") @@ -222,7 +222,7 @@ class UnreadCountTests(ZulipTestCase): events = [] # type: List[Mapping[str, Any]] with tornado_redirected_to_list(events): result = self.client_post("/json/mark_stream_as_read", { - "stream_name": "test_stream" + "stream_id": stream.id }) self.assert_json_success(result) @@ -253,11 +253,11 @@ class UnreadCountTests(ZulipTestCase): def test_mark_all_in_invalid_stream_read(self): # type: () -> None self.login(self.example_email("hamlet")) - invalid_stream_name = "" + invalid_stream_id = "12345678" result = self.client_post("/json/mark_stream_as_read", { - "stream_name": invalid_stream_name + "stream_id": invalid_stream_id }) - self.assert_json_error(result, 'No such stream \'\'') + self.assert_json_error(result, 'Invalid stream id') def test_mark_all_topics_unread_with_invalid_stream_name(self): # type: () -> None diff --git a/zerver/views/messages.py b/zerver/views/messages.py index f2fc3fe03c..b3960468a2 100644 --- a/zerver/views/messages.py +++ b/zerver/views/messages.py @@ -801,13 +801,9 @@ def mark_all_as_read(request, user_profile): @has_request_variables def mark_stream_as_read(request, user_profile, - stream_name=REQ()): - # type: (HttpRequest, UserProfile, Text) -> HttpResponse - try: - stream = get_stream(stream_name, user_profile.realm) - except Stream.DoesNotExist: - raise JsonableError(_('No such stream \'%s\'') % (stream_name,)) - + stream_id=REQ(validator=check_int)): + # type: (HttpRequest, UserProfile, int) -> HttpResponse + stream, recipient, sub = access_stream_by_id(user_profile, stream_id) count = do_mark_stream_messages_as_read(user_profile, stream) log_data_str = "[%s updated]" % (count,)