Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message view: Hide messages sent by muted users.
Abhijeet Prasad Bodas5d796987f201
diff --git a/frontend_tests/node_tests/message_list_view.js b/frontend_tests/node_tests/message_list_view.jsindex 0ce6d23353e..2fd4803f41d 100644--- a/frontend_tests/node_tests/message_list_view.js+++ b/frontend_tests/node_tests/message_list_view.js@@ -47,6 +47,7 @@ mock_esm("../../static/js/rows", {const {Filter} = zrequire("../js/filter");const {MessageListView} = zrequire("../js/message_list_view");const message_list = zrequire("message_list");+const muting = zrequire("muting");let next_timestamp = 1500000000;@@ -121,6 +122,81 @@ run_test("msg_edited_vars", () => {})();});+run_test("muted_message_vars", () => {+ // This verifies that the variables for muted/hidden messages are set+ // correctly.++ function build_message_context(message = {}, message_context = {}) {+ message_context = {+ ...message_context,+ };+ message_context.msg = {+ ...message,+ };+ return message_context;+ }++ function build_message_group(messages) {+ return {message_containers: messages};+ }++ function build_list(message_groups) {+ const list = new MessageListView(undefined, undefined, true);+ list._message_groups = message_groups;+ return list;+ }++ function calculate_variables(list, messages) {+ list.set_calculated_message_container_variables(messages[0]);+ list.set_calculated_message_container_variables(messages[1]);+ list.set_calculated_message_container_variables(messages[2]);+ return list._message_groups[0].message_containers;+ }++ (function test_hidden_message_variables() {+ // Make a representative message group of three messages.+ const messages = [+ build_message_context({sender_id: 10}, {include_sender: true}),+ build_message_context({mentioned: true, sender_id: 10}, {include_sender: false}),+ build_message_context({sender_id: 10}, {include_sender: false}),+ ];+ const message_group = build_message_group(messages);+ const list = build_list([message_group]);+ list._add_msg_edited_vars = noop;++ // Sender is not muted.+ let result = calculate_variables(list, messages);++ // Check that `is_hidden` is false on all messages, and `include_sender` has not changed.+ assert.equal(result[0].is_hidden, false);+ assert.equal(result[1].is_hidden, false);+ assert.equal(result[2].is_hidden, false);++ assert.equal(result[0].include_sender, true);+ assert.equal(result[1].include_sender, false);+ assert.equal(result[2].include_sender, false);++ // Additionally test that, `contains_mention` is true on that message has a mention.+ assert.equal(result[1].contains_mention, true);++ // Now, mute the sender.+ muting.add_muted_user(10);+ result = calculate_variables(list, messages);++ // Check that `is_hidden` is true and `include_sender` is false on all messages.+ assert.equal(result[0].is_hidden, true);+ assert.equal(result[1].is_hidden, true);+ assert.equal(result[2].is_hidden, true);++ assert.equal(result[0].include_sender, false);+ assert.equal(result[1].include_sender, false);+ assert.equal(result[2].include_sender, false);++ // Additionally test that, `contains_mention` is false even on that message which has a mention.+ assert.equal(result[1].contains_mention, false);+ })();+});+run_test("merge_message_groups", () => {// MessageListView has lots of DOM code, so we are going to test the message// group mearging logic on its own.diff --git a/static/js/message_list_view.js b/static/js/message_list_view.jsindex b28862a9a88..484ee87d343 100644--- a/static/js/message_list_view.js+++ b/static/js/message_list_view.js@@ -223,10 +223,11 @@ export class MessageListView {// * `edited_status_msg` -- when label appears for a "/me" message.const last_edit_timestr = this._get_msg_timestring(message_container);const include_sender = message_container.include_sender;+ const is_hidden = message_container.is_hidden;const status_message = Boolean(message_container.status_message);if (last_edit_timestr !== undefined) {message_container.last_edit_timestr = last_edit_timestr;- message_container.edited_in_left_col = !include_sender;+ message_container.edited_in_left_col = !include_sender && !is_hidden;message_container.edited_alongside_sender = include_sender && !status_message;message_container.edited_status_msg = include_sender && status_message;} else {@@ -240,8 +241,21 @@ export class MessageListView {set_calculated_message_container_variables(message_container) {set_timestr(message_container);+ /*+ If the message needs to be hidden because the sender was muted, we do+ a few things:+ 1. Hide the sender avatar and name.+ 2. Hide reactions on that message.+ 3. Do not give a background color to that message even if it mentions the+ current user.+ */++ const is_hidden = muting.is_user_muted(message_container.msg.sender_id);++ message_container.is_hidden = is_hidden;// Make sure the right thing happens if the message was edited to mention us.- message_container.contains_mention = message_container.msg.mentioned;+ message_container.contains_mention = message_container.msg.mentioned && !is_hidden;+ message_container.include_sender = message_container.include_sender && !is_hidden;this._maybe_format_me_message(message_container);// Once all other variables are updated@@ -1295,6 +1309,11 @@ export class MessageListView {}_maybe_format_me_message(message_container) {+ if (message_container.is_hidden) {+ // If the message is to be hidden anyway, no need to render+ // it differently.+ return;+ }if (message_container.msg.is_me_message) {// Slice the '<p>/me ' off the front, and '</p>' off the first line// 'p' tag is sliced off to get sender in the same line as thediff --git a/static/js/muting_ui.js b/static/js/muting_ui.jsindex 9c5cbe4f3dc..d68462e9e7f 100644--- a/static/js/muting_ui.js+++ b/static/js/muting_ui.js@@ -157,6 +157,11 @@ export function unmute_user(user_id) {}export function rerender_for_muted_user() {+ message_lists.current.rerender();+ if (message_lists.current !== message_lists.home) {+ message_lists.home.rerender();+ }+if (overlays.settings_open() && settings_muted_users.loaded) {settings_muted_users.populate_list();}diff --git a/static/templates/message_body.hbs b/static/templates/message_body.hbsindex 1b87f0e597c..354f32df7f0 100644--- a/static/templates/message_body.hbs+++ b/static/templates/message_body.hbs@@ -35,7 +35,11 @@</div>{{#unless status_message}}-<div class="message_content rendered_markdown">{{#if use_match_properties}}{{rendered_markdown msg/match_content}}{{else}}{{rendered_markdown msg/content}}{{/if}}</div>+ {{#unless is_hidden}}+ <div class="message_content rendered_markdown">{{#if use_match_properties}}{{rendered_markdown msg/match_content}}{{else}}{{rendered_markdown msg/content}}{{/if}}</div>+ {{else}}+ {{> message_hidden_dialog}}+ {{/unless}}{{/unless}}{{#if edited_in_left_col}}@@ -47,4 +51,7 @@</div><div class="message_expander message_length_controller" title="{{t 'Expand message (-)' }}">{{t "[More…]" }}</div><div class="message_condenser message_length_controller" title="{{t 'Condense message (-)' }}">{{t "[Condense message]" }}</div>++{{#unless is_hidden}}<div class="message_reactions">{{> message_reactions }}</div>+{{/unless}}diff --git a/static/templates/message_hidden_dialog.hbs b/static/templates/message_hidden_dialog.hbsnew file mode 100644index 00000000000..53b2d49deda--- /dev/null+++ b/static/templates/message_hidden_dialog.hbs@@ -0,0 +1,3 @@+<em>+ {{#tr}}This message was hidden because you have muted the sender.{{/tr}}+</em>