diff --git a/frontend_tests/node_tests/hash_util.js b/frontend_tests/node_tests/hash_util.js index 1c27faf7ed..98f50b6d7e 100644 --- a/frontend_tests/node_tests/hash_util.js +++ b/frontend_tests/node_tests/hash_util.js @@ -20,12 +20,12 @@ var hamlet = { people.add_in_realm(hamlet); -var sub = { +var frontend = { stream_id: 99, name: 'frontend', }; -stream_data.add_sub(sub.name, sub); +stream_data.add_sub('frontend', frontend); run_test('hash_util', () => { // Test encodeHashComponent @@ -131,7 +131,7 @@ run_test('test_stream_edit_uri', () => { run_test('test_by_conversation_and_time_uri', () => { var message = { type: 'stream', - stream: 'frontend', + stream_id: frontend.stream_id, subject: 'testing', id: 42, }; diff --git a/static/js/hash_util.js b/static/js/hash_util.js index 06204c6410..79c58ce0ab 100644 --- a/static/js/hash_util.js +++ b/static/js/hash_util.js @@ -43,6 +43,14 @@ exports.encode_operand = function (operator, operand) { return exports.encodeHashComponent(operand); }; +exports.encode_stream_id = function (stream_id) { + // stream_data appends the stream name, but it does not do the + // URI encoding piece + var slug = stream_data.id_to_slug(stream_id); + + return exports.encodeHashComponent(slug); +}; + exports.encode_stream_name = function (operand) { // stream_data prefixes the stream id, but it does not do the // URI encoding piece @@ -76,8 +84,8 @@ exports.by_stream_uri = function (stream) { return "#narrow/stream/" + exports.encode_stream_name(stream); }; -exports.by_stream_topic_uri = function (stream, subject) { - return "#narrow/stream/" + exports.encode_stream_name(stream) + +exports.by_stream_topic_uri = function (stream_id, subject) { + return "#narrow/stream/" + exports.encode_stream_id(stream_id) + "/subject/" + exports.encodeHashComponent(subject); }; @@ -132,7 +140,7 @@ exports.by_conversation_and_time_uri = function (message) { if (message.type === "stream") { return absolute_url + - exports.by_stream_topic_uri(message.stream, message.subject) + + exports.by_stream_topic_uri(message.stream_id, message.subject) + suffix; } diff --git a/static/js/message_list_view.js b/static/js/message_list_view.js index 3a27a2f75e..056bf820fd 100644 --- a/static/js/message_list_view.js +++ b/static/js/message_list_view.js @@ -243,7 +243,7 @@ MessageListView.prototype = { hash_util.by_stream_uri(message_container.msg.stream); message_container.topic_url = hash_util.by_stream_topic_uri( - message_container.msg.stream, + message_container.msg.stream_id, message_container.msg.subject); } else { message_container.pm_with_url = diff --git a/static/js/stream_data.js b/static/js/stream_data.js index 3fefdb2f63..097340b2a5 100644 --- a/static/js/stream_data.js +++ b/static/js/stream_data.js @@ -97,6 +97,16 @@ exports.get_sub_by_name = function (name) { return subs_by_stream_id.get(stream_id); }; +exports.id_to_slug = function (stream_id) { + var name = exports.maybe_get_stream_name(stream_id) || 'unknown'; + + // The name part of the URL doesn't really matter, so we try to + // make it pretty. + name = name.replace(' ', '-'); + + return stream_id + '-' + name; +}; + exports.name_to_slug = function (name) { var stream_id = exports.get_stream_id(name); diff --git a/static/js/topic_list.js b/static/js/topic_list.js index 97b4bffc56..a2fadc38d5 100644 --- a/static/js/topic_list.js +++ b/static/js/topic_list.js @@ -87,7 +87,7 @@ exports.widget = function (parent_elem, my_stream_id) { unread: num_unread, is_zero: num_unread === 0, is_muted: muting.is_topic_muted(my_stream_id, topic_name), - url: hash_util.by_stream_topic_uri(my_stream_name, topic_name), + url: hash_util.by_stream_topic_uri(my_stream_id, topic_name), }; var li = $(templates.render('topic_list_item', topic_info)); self.topic_items.set(topic_name, li);