unread: Convert mark_topic_as_read to use stream IDs.

This is safer in its handling of potentially renamed streams.
This commit is contained in:
Tim Abbott
2017-08-15 10:28:32 -07:00
parent f3a41ac796
commit 842bf77efb
4 changed files with 14 additions and 18 deletions

View File

@@ -328,7 +328,7 @@ exports.register_topic_handlers = function () {
var topic = $(e.currentTarget).attr('data-topic-name'); var topic = $(e.currentTarget).attr('data-topic-name');
exports.hide_topic_popover(); exports.hide_topic_popover();
unread_ops.mark_topic_as_read(sub.name, topic); unread_ops.mark_topic_as_read(sub.stream_id, topic);
e.stopPropagation(); e.stopPropagation();
}); });
}; };

View File

@@ -126,11 +126,11 @@ exports.mark_stream_as_read = function mark_stream_as_read(stream_id, cont) {
success: cont}); success: cont});
}; };
exports.mark_topic_as_read = function mark_topic_as_read(stream, topic, cont) { exports.mark_topic_as_read = function mark_topic_as_read(stream_id, topic, cont) {
channel.post({ channel.post({
url: '/json/mark_topic_as_read', url: '/json/mark_topic_as_read',
idempotent: true, idempotent: true,
data: {stream_name: stream, topic_name: topic}, data: {stream_id: stream_id, topic_name: topic},
success: cont}); success: cont});
}; };

View File

@@ -4,7 +4,7 @@ from __future__ import absolute_import
from typing import Any, Dict, List, Mapping from typing import Any, Dict, List, Mapping
from zerver.models import ( from zerver.models import (
get_user, Recipient, UserMessage get_user, Recipient, UserMessage, get_stream, get_realm
) )
from zerver.lib.test_helpers import tornado_redirected_to_list from zerver.lib.test_helpers import tornado_redirected_to_list
@@ -262,12 +262,12 @@ class UnreadCountTests(ZulipTestCase):
def test_mark_all_topics_unread_with_invalid_stream_name(self): def test_mark_all_topics_unread_with_invalid_stream_name(self):
# type: () -> None # type: () -> None
self.login(self.example_email("hamlet")) self.login(self.example_email("hamlet"))
invalid_stream_name = "bogus" invalid_stream_id = "12345678"
result = self.client_post("/json/mark_topic_as_read", { result = self.client_post("/json/mark_topic_as_read", {
"stream_name": invalid_stream_name, "stream_id": invalid_stream_id,
'topic_name': 'whatever', 'topic_name': 'whatever',
}) })
self.assert_json_error(result, "No such stream 'bogus'") self.assert_json_error(result, "Invalid stream id")
def test_mark_all_in_stream_topic_read(self): def test_mark_all_in_stream_topic_read(self):
# type: () -> None # type: () -> None
@@ -280,7 +280,7 @@ class UnreadCountTests(ZulipTestCase):
events = [] # type: List[Mapping[str, Any]] events = [] # type: List[Mapping[str, Any]]
with tornado_redirected_to_list(events): with tornado_redirected_to_list(events):
result = self.client_post("/json/mark_topic_as_read", { result = self.client_post("/json/mark_topic_as_read", {
"stream_name": "test_stream", "stream_id": get_stream("test_stream", user_profile.realm).id,
"topic_name": "test_topic", "topic_name": "test_topic",
}) })
@@ -312,7 +312,7 @@ class UnreadCountTests(ZulipTestCase):
self.login(self.example_email("hamlet")) self.login(self.example_email("hamlet"))
invalid_topic_name = "abc" invalid_topic_name = "abc"
result = self.client_post("/json/mark_topic_as_read", { result = self.client_post("/json/mark_topic_as_read", {
"stream_name": "Denmark", "stream_id": get_stream("Denmark", get_realm("zulip")).id,
"topic_name": invalid_topic_name, "topic_name": invalid_topic_name,
}) })
self.assert_json_error(result, 'No such topic \'abc\'') self.assert_json_error(result, 'No such topic \'abc\'')

View File

@@ -35,7 +35,7 @@ from zerver.lib.message import (
) )
from zerver.lib.response import json_success, json_error from zerver.lib.response import json_success, json_error
from zerver.lib.sqlalchemy_utils import get_sqlalchemy_connection from zerver.lib.sqlalchemy_utils import get_sqlalchemy_connection
from zerver.lib.streams import is_public_stream_by_name from zerver.lib.streams import access_stream_by_id, is_public_stream_by_name
from zerver.lib.timestamp import datetime_to_timestamp from zerver.lib.timestamp import datetime_to_timestamp
from zerver.lib.utils import statsd from zerver.lib.utils import statsd
from zerver.lib.validator import \ from zerver.lib.validator import \
@@ -815,18 +815,14 @@ def mark_stream_as_read(request,
@has_request_variables @has_request_variables
def mark_topic_as_read(request, def mark_topic_as_read(request,
user_profile, user_profile,
stream_name=REQ(), stream_id=REQ(validator=check_int),
topic_name=REQ()): topic_name=REQ()):
# type: (HttpRequest, UserProfile, Text, Text) -> HttpResponse # type: (HttpRequest, UserProfile, int, Text) -> HttpResponse
try: stream, recipient, sub = access_stream_by_id(user_profile, stream_id)
stream = get_stream(stream_name, user_profile.realm)
except Stream.DoesNotExist:
raise JsonableError(_('No such stream \'%s\'') % (stream_name,))
if topic_name: if topic_name:
topic_exists = UserMessage.objects.filter(user_profile=user_profile, topic_exists = UserMessage.objects.filter(user_profile=user_profile,
message__recipient__type_id=stream.id, message__recipient=recipient,
message__recipient__type=Recipient.STREAM,
message__subject__iexact=topic_name).exists() message__subject__iexact=topic_name).exists()
if not topic_exists: if not topic_exists:
raise JsonableError(_('No such topic \'%s\'') % (topic_name,)) raise JsonableError(_('No such topic \'%s\'') % (topic_name,))