Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

topic muting: Clean up live update code.

Abhijeet Prasad Bodasfc53d2c66ae8
diff --git a/frontend_tests/node_tests/settings_muted_topics.js b/frontend_tests/node_tests/settings_muted_topics.js
index 92895b3257a..db9fbb272a7 100644
--- a/frontend_tests/node_tests/settings_muted_topics.js
+++ b/frontend_tests/node_tests/settings_muted_topics.js
@@ -59,8 +59,6 @@ run_test("settings", (override) => {
return topic_tr_html;
};
- topic_tr_html.remove = () => {};
-
let topic_data_called = 0;
topic_tr_html.attr = (opts) => {
if (opts === "data-stream-id") {
diff --git a/static/js/muting_ui.js b/static/js/muting_ui.js
index e3cc4e8db32..05b98a61d86 100644
--- a/static/js/muting_ui.js
+++ b/static/js/muting_ui.js
@@ -1,4 +1,5 @@
import $ from "jquery";
+import _ from "lodash";
import render_confirm_mute_user from "../templates/confirm_mute_user.hbs";
import render_topic_muted from "../templates/topic_muted.hbs";
@@ -21,20 +22,8 @@ import * as stream_list from "./stream_list";
import * as stream_popover from "./stream_popover";
import * as unread_ui from "./unread_ui";
-function timestamp_ms() {
- return Date.now();
-}
-
-let last_topic_update = 0;
-
-export function rerender_on_topic_update() {
- // Note: We tend to optimistically rerender muting preferences before
- // the backend actually acknowledges the mute. This gives a more
- // immediate feel to the user, and if the backend fails temporarily,
- // re-doing a mute or unmute is a pretty recoverable thing.
-
+export function rerender_for_muted_topic(old_muted_topics) {
stream_list.update_streams_sidebar();
- recent_topics.complete_rerender();
message_lists.current.update_topic_muting_and_rerender();
if (message_lists.current !== message_lists.home) {
message_lists.home.update_topic_muting_and_rerender();
@@ -42,88 +31,73 @@ export function rerender_on_topic_update() {
if (overlays.settings_open() && settings_muted_topics.loaded) {
settings_muted_topics.populate_list();
}
+
+ // We only update those topics which could have been affected, because
+ // we want to avoid doing a complete rerender of the recent topics view,
+ // because that can be expensive.
+ const current_muted_topics = muting.get_muted_topics();
+ const maybe_affected_topics = _.unionWith(old_muted_topics, current_muted_topics, _.isEqual);
+
+ for (const topic_data of maybe_affected_topics) {
+ recent_topics.update_topic_is_muted(topic_data.stream_id, topic_data.topic);
+ }
}
-export function persist_topic_mute(stream_id, topic_name) {
+export function handle_topic_updates(muted_topics) {
+ const old_muted_topics = muting.get_muted_topics();
+ muting.set_muted_topics(muted_topics);
+ stream_popover.hide_topic_popover();
+ unread_ui.update_unread_counts();
+ rerender_for_muted_topic(old_muted_topics);
+}
+
+export function mute_topic(stream_id, topic) {
+ const stream_name = stream_data.maybe_get_stream_name(stream_id);
const data = {
stream_id,
- topic: topic_name,
+ topic,
op: "add",
};
- last_topic_update = timestamp_ms();
+
channel.patch({
url: "/json/users/me/subscriptions/muted_topics",
idempotent: true,
data,
+ success() {
+ feedback_widget.show({
+ populate(container) {
+ const rendered_html = render_topic_muted();
+ container.html(rendered_html);
+ container.find(".stream").text(stream_name);
+ container.find(".topic").text(topic);
+ },
+ on_undo() {
+ unmute_topic(stream_id, topic);
+ },
+ title_text: $t({defaultMessage: "Topic muted"}),
+ undo_button_text: $t({defaultMessage: "Unmute"}),
+ });
+ },
});
}
-export function persist_topic_unmute(stream_id, topic_name) {
+export function unmute_topic(stream_id, topic) {
+ // Accidentally unmuting a topic isn't as much an issue as accidentally muting
+ // a topic, so we don't show a popup after unmuting.
const data = {
stream_id,
- topic: topic_name,
+ topic,
op: "remove",
};
- last_topic_update = timestamp_ms();
+
channel.patch({
url: "/json/users/me/subscriptions/muted_topics",
idempotent: true,
data,
- });
-}
-
-export function handle_topic_updates(muted_topics) {
- if (timestamp_ms() < last_topic_update + 1000) {
- // This topic update is either the one that we just rendered, or,
- // much less likely, it's coming from another device and would probably
- // be overwriting this device's preferences with stale data.
- return;
- }
-
- update_muted_topics(muted_topics);
- rerender_on_topic_update();
-}
-
-export function update_muted_topics(muted_topics) {
- muting.set_muted_topics(muted_topics);
- unread_ui.update_unread_counts();
-}
-
-export function mute_topic(stream_id, topic) {
- const stream_name = stream_data.maybe_get_stream_name(stream_id);
-
- stream_popover.hide_topic_popover();
- muting.add_muted_topic(stream_id, topic);
- unread_ui.update_unread_counts();
- rerender_on_topic_update();
- persist_topic_mute(stream_id, topic);
- feedback_widget.show({
- populate(container) {
- const rendered_html = render_topic_muted();
- container.html(rendered_html);
- container.find(".stream").text(stream_name);
- container.find(".topic").text(topic);
- },
- on_undo() {
- unmute_topic(stream_id, topic);
+ success() {
+ feedback_widget.dismiss();
},
- title_text: $t({defaultMessage: "Topic muted"}),
- undo_button_text: $t({defaultMessage: "Unmute"}),
});
- recent_topics.update_topic_is_muted(stream_id, topic);
-}
-
-export function unmute_topic(stream_id, topic) {
- // we don't run a unmute_notify function because it isn't an issue as much
- // if someone accidentally unmutes a stream rather than if they mute it
- // and miss out on info.
- stream_popover.hide_topic_popover();
- muting.remove_muted_topic(stream_id, topic);
- unread_ui.update_unread_counts();
- rerender_on_topic_update();
- persist_topic_unmute(stream_id, topic);
- feedback_widget.dismiss();
- recent_topics.update_topic_is_muted(stream_id, topic);
}
export function toggle_topic_mute(message) {
diff --git a/static/js/settings_muted_topics.js b/static/js/settings_muted_topics.js
index 6129aae3e6c..de43fcdb2a0 100644
--- a/static/js/settings_muted_topics.js
+++ b/static/js/settings_muted_topics.js
@@ -43,7 +43,6 @@ export function set_up() {
e.stopPropagation();
muting_ui.unmute_topic(stream_id, topic);
- $row.remove();
});
populate_list();