From 90fa797f9b6c454f149e504fbbdbcb05a7c88c83 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 5 Jan 2017 08:34:27 -0800 Subject: [PATCH] Use stream id to live-update messages for name changes. When we change a stream name, we now use the stream id as the key to find messages we need to live update. This eliminates some possible race conditions from two users renaming a stream. This commit introduces message_live_update.js. The new call stack is this: subs.update_subscription_properties subs.update_stream_name message_live_update.update_stream_name message_list.update_stream_name --- .eslintrc.json | 1 + static/js/message_list.js | 11 +++++------ static/js/message_live_update.js | 17 +++++++++++++++++ static/js/subs.js | 8 +++----- zproject/settings.py | 1 + 5 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 static/js/message_live_update.js diff --git a/.eslintrc.json b/.eslintrc.json index 646c2d1345..39a7ac6124 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -34,6 +34,7 @@ "compose_fade": false, "subs": false, "timerender": false, + "message_live_update": false, "message_edit": false, "reload": false, "composebox_typeahead": false, diff --git a/static/js/message_list.js b/static/js/message_list.js index 0e93fa6c6b..5c87650e80 100644 --- a/static/js/message_list.js +++ b/static/js/message_list.js @@ -555,13 +555,12 @@ exports.MessageList.prototype = { return item_list[cur_idx]; }, - change_display_recipient: function MessageList_change_display_recipient(old_recipient, - new_recipient) { - // This method only works for streams. + update_stream_name: function MessageList_update_stream_name(stream_id, + new_stream_name) { _.each(this._items, function (item) { - if (item.display_recipient === old_recipient) { - item.display_recipient = new_recipient; - item.stream = new_recipient; + if (item.stream_id && (item.stream_id === stream_id)) { + item.display_recipient = new_stream_name; + item.stream = new_stream_name; } }); this.view.rerender_the_whole_thing(); diff --git a/static/js/message_live_update.js b/static/js/message_live_update.js new file mode 100644 index 0000000000..9ccee6a766 --- /dev/null +++ b/static/js/message_live_update.js @@ -0,0 +1,17 @@ +var message_live_update = (function () { + +var exports = {}; + +exports.update_stream_name = function (stream_id, new_name) { + _.each([home_msg_list, current_msg_list, message_list.all], function (list) { + list.update_stream_name(stream_id, new_name); + }); +}; + +return exports; + +}()); +if (typeof module !== 'undefined') { + module.exports = message_live_update; +} + diff --git a/static/js/subs.js b/static/js/subs.js index c87c263996..fb3e2b47a9 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -177,7 +177,7 @@ function update_stream_pin(sub, value) { sub.pin_to_top = value; } -function update_stream_name(stream_id, old_name, new_name) { +function update_stream_name(stream_id, new_name) { // Rename the stream internally. var sub = stream_data.rename_sub(stream_id, new_name); @@ -195,9 +195,7 @@ function update_stream_name(stream_id, old_name, new_name) { sub_row.attr("data-stream-name", new_name); // Update the message feed. - _.each([home_msg_list, current_msg_list, message_list.all], function (list) { - list.change_display_recipient(old_name, new_name); - }); + message_live_update.update_stream_name(stream_id, new_name); } function update_stream_description(sub, description) { @@ -659,7 +657,7 @@ exports.update_subscription_properties = function (stream_name, property, value) update_stream_audible_notifications(sub, value); break; case 'name': - update_stream_name(sub.stream_id, sub.name, value); + update_stream_name(sub.stream_id, value); break; case 'description': update_stream_description(sub, value); diff --git a/zproject/settings.py b/zproject/settings.py index c38a3b9d8b..8ae5547700 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -793,6 +793,7 @@ JS_SPECS = { 'js/filter.js', 'js/message_list_view.js', 'js/message_list.js', + 'js/message_live_update.js', 'js/narrow.js', 'js/reload.js', 'js/compose_fade.js',