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.

message_helper: Set clean reactions when processing the raw message.

evykassirer7364c02ed671
diff --git a/web/src/message_helper.js b/web/src/message_helper.js
index 6a38cef6713..0e380d9e436 100644
--- a/web/src/message_helper.js
+++ b/web/src/message_helper.js
@@ -3,6 +3,7 @@ import * as message_store from "./message_store";
import * as message_user_ids from "./message_user_ids";
import * as people from "./people";
import * as pm_conversations from "./pm_conversations";
+import * as reactions from "./reactions";
import * as recent_senders from "./recent_senders";
import * as stream_topic_history from "./stream_topic_history";
import * as user_status from "./user_status";
@@ -36,6 +37,16 @@ export function process_new_message(message) {
message.status_emoji_info = user_status.get_status_emoji(message.sender_id);
}
+ if (!message.reactions) {
+ message.reactions = [];
+ }
+ // TODO: Rather than adding this field to the message object, it
+ // might be cleaner to create an independent map from message_id
+ // => clean_reactions data for the message, with care being taken
+ // to make sure reify_message_id moves the data structure
+ // properly.
+ message.clean_reactions = reactions.generate_clean_reactions(message);
+
switch (message.type) {
case "stream":
if (message.topic === undefined) {
diff --git a/web/src/message_store.ts b/web/src/message_store.ts
index 76cd5718926..82e42184898 100644
--- a/web/src/message_store.ts
+++ b/web/src/message_store.ts
@@ -2,9 +2,9 @@ import _ from "lodash";
import * as blueslip from "./blueslip";
import * as people from "./people";
-import type {RawReaction} from "./reactions";
import type {Submessage, TopicLink} from "./types";
import type {UserStatusEmojiInfo} from "./user_status";
+import { RawReaction } from "./reactions";
const stored_messages = new Map<number, Message>();
diff --git a/web/src/reactions.ts b/web/src/reactions.ts
index eaa64e32a81..19b0a21417e 100644
--- a/web/src/reactions.ts
+++ b/web/src/reactions.ts
@@ -11,7 +11,7 @@ import type {EmojiRenderingDetails} from "./emoji";
import {$t} from "./i18n";
import * as message_lists from "./message_lists";
import * as message_store from "./message_store";
-import type {Message, MessageCleanReaction} from "./message_store";
+import type {Message, MessageCleanReaction, RawMessage} from "./message_store";
import {page_params} from "./page_params";
import * as people from "./people";
import * as spectators from "./spectators";
@@ -34,7 +34,7 @@ export function get_local_reaction_id(rendering_details: EmojiRenderingDetails):
}
export function current_user_has_reacted_to_emoji(message: Message, local_id: string): boolean {
- set_clean_reactions(message);
+ update_clean_reactions(message);
const clean_reaction_object = message.clean_reactions.get(local_id);
return clean_reaction_object?.user_ids.includes(current_user.user_id) ?? false;
@@ -47,7 +47,7 @@ function get_message(message_id: number): Message | undefined {
return undefined;
}
- set_clean_reactions(message);
+ update_clean_reactions(message);
return message;
}
@@ -276,7 +276,7 @@ export function add_reaction(event: ReactionEvent): void {
return;
}
- set_clean_reactions(message);
+ update_clean_reactions(message);
const local_id = get_local_reaction_id(event);
const user_id = event.user_id;
@@ -391,7 +391,7 @@ export function remove_reaction(event: ReactionEvent): void {
return;
}
- set_clean_reactions(message);
+ update_clean_reactions(message);
const clean_reaction_object = message.clean_reactions.get(local_id);
@@ -452,7 +452,7 @@ export function get_emojis_used_by_user_for_message_id(message_id: number): stri
assert(user_id !== undefined);
const message = message_store.get(message_id);
assert(message !== undefined);
- set_clean_reactions(message);
+ update_clean_reactions(message);
const names = [];
for (const clean_reaction_object of message.clean_reactions.values()) {
@@ -465,34 +465,21 @@ export function get_emojis_used_by_user_for_message_id(message_id: number): stri
}
export function get_message_reactions(message: Message): MessageCleanReaction[] {
- set_clean_reactions(message);
+ update_clean_reactions(message);
return [...message.clean_reactions.values()];
}
-export function set_clean_reactions(message: Message): void {
+export function generate_clean_reactions(message: RawMessage): Map<string, MessageCleanReaction> {
/*
- set_clean_reactions processes the raw message.reactions object,
+ generate_clean_reactions processes the raw message.reactions object,
which will contain one object for each individual reaction, even
if two users react with the same emoji.
- As output, it sets message.cleaned_reactions, which is a more
- compressed format with one entry per reaction pill that should
- be displayed visually to users.
+ Its output, `cleaned_reactions`, is a more compressed format with
+ one entry per reaction pill that should be displayed visually to
+ users.
*/
- if (message.clean_reactions) {
- // Update display details for the reaction. In particular,
- // user_settings.display_emoji_reaction_users or the names of
- // the users appearing in the reaction may have changed since
- // this reaction was first rendered.
- const reaction_counts_and_user_ids = get_reaction_counts_and_user_ids(message);
- const should_display_reactors = check_should_display_reactors(reaction_counts_and_user_ids);
- for (const clean_reaction of message.clean_reactions.values()) {
- update_user_fields(clean_reaction, should_display_reactors);
- }
- return;
- }
-
// This first loop creates a temporary distinct_reactions data
// structure, which will accumulate the set of users who have
// reacted with each distinct reaction.
@@ -518,12 +505,7 @@ export function set_clean_reactions(message: Message): void {
user_ids.push(user_id);
}
- // TODO: Rather than adding this field to the message object, it
- // might be cleaner to create an independent map from message_id
- // => clean_reactions data for the message, with care being taken
- // to make sure reify_message_id moves the data structure
- // properly.
- message.clean_reactions = new Map();
+ const clean_reactions = new Map<string, MessageCleanReaction>();
const reaction_counts_and_user_ids = [...distinct_reactions.keys()].map((local_id) => {
const user_ids = user_map.get(local_id);
@@ -541,17 +523,25 @@ export function set_clean_reactions(message: Message): void {
const user_ids = user_map.get(local_id);
assert(user_ids !== undefined);
- message.clean_reactions.set(
+ clean_reactions.set(
local_id,
make_clean_reaction({local_id, user_ids, should_display_reactors, ...reaction}),
);
}
- // We don't maintain message.reactions when users react to
- // messages we already have a copy of, so it's safest to delete it
- // after we've processed the reactions data for a message into the
- // clean_reactions data structure, which we do maintain.
- delete message.reactions;
+ return clean_reactions;
+}
+
+export function update_clean_reactions(message: Message): void {
+ // Update display details for the reaction. In particular,
+ // user_settings.display_emoji_reaction_users or the names of
+ // the users appearing in the reaction may have changed since
+ // this reaction was first rendered.
+ const reaction_counts_and_user_ids = get_reaction_counts_and_user_ids(message);
+ const should_display_reactors = check_should_display_reactors(reaction_counts_and_user_ids);
+ for (const clean_reaction of message.clean_reactions.values()) {
+ update_user_fields(clean_reaction, should_display_reactors);
+ }
}
function make_clean_reaction({
@@ -681,7 +671,7 @@ export function update_vote_text_on_message(message: Message): void {
// users depends on total reactions on the message, we need to
// recalculate this whenever adjusting reaction rendering on a
// message.
- set_clean_reactions(message);
+ update_clean_reactions(message);
const reaction_counts_and_user_ids = get_reaction_counts_and_user_ids(message);
const should_display_reactors = check_should_display_reactors(reaction_counts_and_user_ids);
for (const [reaction, clean_reaction] of message.clean_reactions.entries()) {
diff --git a/web/tests/dispatch.test.js b/web/tests/dispatch.test.js
index 706436e0ba0..f190decc843 100644
--- a/web/tests/dispatch.test.js
+++ b/web/tests/dispatch.test.js
@@ -41,7 +41,9 @@ const muted_users_ui = mock_esm("../src/muted_users_ui");
const narrow_title = mock_esm("../src/narrow_title");
const navbar_alerts = mock_esm("../src/navbar_alerts");
const pm_list = mock_esm("../src/pm_list");
-const reactions = mock_esm("../src/reactions");
+const reactions = mock_esm("../src/reactions", {
+ generate_clean_reactions() {},
+});
const realm_icon = mock_esm("../src/realm_icon");
const realm_logo = mock_esm("../src/realm_logo");
const realm_playground = mock_esm("../src/realm_playground");
diff --git a/web/tests/message_events.test.js b/web/tests/message_events.test.js
index e2255380715..e6adbf4dafe 100644
--- a/web/tests/message_events.test.js
+++ b/web/tests/message_events.test.js
@@ -122,6 +122,7 @@ run_test("update_messages", () => {
assert.deepEqual(rendered_mgs, [
{
alerted: false,
+ clean_reactions: new Map(),
collapsed: false,
content: "<b>new content</b>",
display_recipient: denmark.name,
diff --git a/web/tests/reactions.test.js b/web/tests/reactions.test.js
index d282273fe28..226f6dc3bfb 100644
--- a/web/tests/reactions.test.js
+++ b/web/tests/reactions.test.js
@@ -113,9 +113,20 @@ function test(label, f) {
});
}
-test("basics", () => {
+function sample_message_with_clean_reactions() {
const message = {...sample_message};
+ convert_reactions_to_clean_reactions(message);
+ return message;
+}
+
+function convert_reactions_to_clean_reactions(message) {
+ message.clean_reactions = reactions.generate_clean_reactions(message);
+ delete message.reactions;
+}
+
+test("basics", () => {
settings_data.user_can_access_all_other_users = () => true;
+ const message = sample_message_with_clean_reactions();
const result = reactions.get_message_reactions(message);
assert.ok(reactions.current_user_has_reacted_to_emoji(message, "unicode_emoji,1f642"));
assert.ok(!reactions.current_user_has_reacted_to_emoji(message, "bogus"));
@@ -222,6 +233,7 @@ test("reactions from unknown users", () => {
],
};
+ convert_reactions_to_clean_reactions(message);
const result = reactions.get_message_reactions(message);
result.sort((a, b) => a.count - b.count);
@@ -308,7 +320,7 @@ test("unknown realm emojis (insert)", () => {
});
test("sending", ({override, override_rewire}) => {
- const message = {...sample_message};
+ const message = sample_message_with_clean_reactions();
assert.equal(message.id, 1001);
override(message_store, "get", (message_id) => {
assert.equal(message_id, message.id);
@@ -400,7 +412,7 @@ test("sending", ({override, override_rewire}) => {
});
test("prevent_simultaneous_requests_updating_reaction", ({override_rewire}) => {
- const message = {...sample_message};
+ const message = sample_message_with_clean_reactions();
override_rewire(reactions, "add_reaction", noop);
const stub = make_stub();
channel.post = stub.f;
@@ -486,6 +498,7 @@ test("update_vote_text_on_message", ({override_rewire}) => {
},
],
};
+ convert_reactions_to_clean_reactions(message);
user_settings.display_emoji_reaction_users = true;
@@ -550,7 +563,7 @@ test("get_reaction_sections", () => {
});
test("emoji_reaction_title", ({override}) => {
- const message = {...sample_message};
+ const message = sample_message_with_clean_reactions();
override(message_store, "get", () => message);
const local_id = "unicode_emoji,1f642";
@@ -565,6 +578,7 @@ test("add_reaction/remove_reaction", ({override, override_rewire}) => {
id: 2001,
reactions: [],
};
+ convert_reactions_to_clean_reactions(message);
user_settings.display_emoji_reaction_users = true;
@@ -869,21 +883,20 @@ test("insert_new_reaction (me w/unicode emoji)", ({mock_template}) => {
insert_called = true;
};
- reactions.insert_new_reaction(
- clean_reaction_object,
- {
- id: message_id,
- reactions: [
- {
- emoji_name: "8ball",
- user_id: alice.user_id,
- reaction_type: "unicode_emoji",
- emoji_code: "1f3b1",
- },
- ],
- },
- alice.user_id,
- );
+ const message = {
+ id: message_id,
+ reactions: [
+ {
+ emoji_name: "8ball",
+ user_id: alice.user_id,
+ reaction_type: "unicode_emoji",
+ emoji_code: "1f3b1",
+ },
+ ],
+ };
+ convert_reactions_to_clean_reactions(message);
+
+ reactions.insert_new_reaction(clean_reaction_object, message, alice.user_id);
assert.ok(insert_called);
});
@@ -935,21 +948,19 @@ test("insert_new_reaction (them w/zulip emoji)", ({mock_template}) => {
insert_called = true;
};
- reactions.insert_new_reaction(
- clean_reaction_object,
- {
- id: message_id,
- reactions: [
- {
- emoji_name: "8ball",
- user_id: bob.user_id,
- reaction_type: "unicode_emoji",
- emoji_code: "1f3b1",
- },
- ],
- },
- bob.user_id,
- );
+ const message = {
+ id: message_id,
+ reactions: [
+ {
+ emoji_name: "8ball",
+ user_id: bob.user_id,
+ reaction_type: "unicode_emoji",
+ emoji_code: "1f3b1",
+ },
+ ],
+ };
+ convert_reactions_to_clean_reactions(message);
+ reactions.insert_new_reaction(clean_reaction_object, message, bob.user_id);
assert.ok(insert_called);
});
@@ -971,27 +982,25 @@ test("update_existing_reaction (me)", () => {
const $reaction_count = $.create("reaction-count-stub");
$our_reaction.set_find_results(".message_reaction_count", $reaction_count);
- reactions.update_existing_reaction(
- clean_reaction_object,
- {
- id: message_id,
- reactions: [
- {
- emoji_code: "1f3b1",
- emoji_name: "8ball",
- reaction_type: "unicode_emoji",
- user_id: alice.user_id,
- },
- {
- emoji_code: "1f3b1",
- emoji_name: "8ball",
- reaction_type: "unicode_emoji",
- user_id: bob.user_id,
- },
- ],
- },
- alice.user_id,
- );
+ const message = {
+ id: message_id,
+ reactions: [
+ {
+ emoji_code: "1f3b1",
+ emoji_name: "8ball",
+ reaction_type: "unicode_emoji",
+ user_id: alice.user_id,
+ },
+ {
+ emoji_code: "1f3b1",
+ emoji_name: "8ball",
+ reaction_type: "unicode_emoji",
+ user_id: bob.user_id,
+ },
+ ],
+ };
+ convert_reactions_to_clean_reactions(message);
+ reactions.update_existing_reaction(clean_reaction_object, message, alice.user_id);
assert.ok($our_reaction.hasClass("reacted"));
assert.equal(
@@ -1018,39 +1027,38 @@ test("update_existing_reaction (them)", () => {
const $reaction_count = $.create("reaction-count-stub");
$our_reaction.set_find_results(".message_reaction_count", $reaction_count);
- reactions.update_existing_reaction(
- clean_reaction_object,
- {
- id: message_id,
- reactions: [
- {
- emoji_code: "1f3b1",
- emoji_name: "8ball",
- reaction_type: "unicode_emoji",
- user_id: alice.user_id,
- },
- {
- emoji_code: "1f3b1",
- emoji_name: "8ball",
- reaction_type: "unicode_emoji",
- user_id: bob.user_id,
- },
- {
- emoji_code: "1f3b1",
- emoji_name: "8ball",
- reaction_type: "unicode_emoji",
- user_id: cali.user_id,
- },
- {
- emoji_code: "1f3b1",
- emoji_name: "8ball",
- reaction_type: "unicode_emoji",
- user_id: alexus.user_id,
- },
- ],
- },
- alexus.user_id,
- );
+ const message = {
+ id: message_id,
+ reactions: [
+ {
+ emoji_code: "1f3b1",
+ emoji_name: "8ball",
+ reaction_type: "unicode_emoji",
+ user_id: alice.user_id,
+ },
+ {
+ emoji_code: "1f3b1",
+ emoji_name: "8ball",
+ reaction_type: "unicode_emoji",
+ user_id: bob.user_id,
+ },
+ {
+ emoji_code: "1f3b1",
+ emoji_name: "8ball",
+ reaction_type: "unicode_emoji",
+ user_id: cali.user_id,
+ },
+ {
+ emoji_code: "1f3b1",
+ emoji_name: "8ball",
+ reaction_type: "unicode_emoji",
+ user_id: alexus.user_id,
+ },
+ ],
+ };
+ convert_reactions_to_clean_reactions(message);
+
+ reactions.update_existing_reaction(clean_reaction_object, message, alexus.user_id);
assert.ok(!$our_reaction.hasClass("reacted"));
assert.equal(
@@ -1078,27 +1086,26 @@ test("remove_reaction_from_view (me)", () => {
const $reaction_button = $.create("reaction-button-stub");
$message_reactions.find = () => $reaction_button;
- reactions.remove_reaction_from_view(
- clean_reaction_object,
- {
- id: message_id,
- reactions: [
- {
- emoji_code: "1f3b1",
- emoji_name: "8ball",
- reaction_type: "unicode_emoji",
- user_id: bob.user_id,
- },
- {
- emoji_code: "1f3b1",
- emoji_name: "8ball",
- reaction_type: "unicode_emoji",
- user_id: cali.user_id,
- },
- ],
- },
- alice.user_id,
- );
+ const message = {
+ id: message_id,
+ reactions: [
+ {
+ emoji_code: "1f3b1",
+ emoji_name: "8ball",
+ reaction_type: "unicode_emoji",
+ user_id: bob.user_id,
+ },
+ {
+ emoji_code: "1f3b1",
+ emoji_name: "8ball",
+ reaction_type: "unicode_emoji",
+ user_id: cali.user_id,
+ },
+ ],
+ };
+ convert_reactions_to_clean_reactions(message);
+
+ reactions.remove_reaction_from_view(clean_reaction_object, message, alice.user_id);
assert.ok(!$message_reactions.hasClass("reacted"));
assert.equal(
@@ -1126,21 +1133,20 @@ test("remove_reaction_from_view (them)", () => {
const $reaction_button = $.create("reaction-button-stub");
$message_reactions.find = () => $reaction_button;
- reactions.remove_reaction_from_view(
- clean_reaction_object,
- {
- id: message_id,
- reactions: [
- {
- emoji_code: "1f3b1",
- emoji_name: "8ball",
- reaction_type: "unicode_emoji",
- user_id: alice.user_id,
- },
- ],
- },
- bob.user_id,
- );
+ const message = {
+ id: message_id,
+ reactions: [
+ {
+ emoji_code: "1f3b1",
+ emoji_name: "8ball",
+ reaction_type: "unicode_emoji",
+ user_id: alice.user_id,
+ },
+ ],
+ };
+ convert_reactions_to_clean_reactions(message);
+
+ reactions.remove_reaction_from_view(clean_reaction_object, message, bob.user_id);
assert.ok($message_reactions.hasClass("reacted"));
assert.equal(
@@ -1169,11 +1175,10 @@ test("remove_reaction_from_view (last person)", () => {
$our_reaction.remove = () => {
removed = true;
};
- reactions.remove_reaction_from_view(
- clean_reaction_object,
- {id: message_id, reactions: []},
- bob.user_id,
- );
+
+ const message = {id: message_id, reactions: []};
+ convert_reactions_to_clean_reactions(message);
+ reactions.remove_reaction_from_view(clean_reaction_object, message, bob.user_id);
assert.ok(removed);
});
@@ -1196,8 +1201,7 @@ test("bogus_event", ({override}) => {
test("remove spurious user", ({override}) => {
// get coverage for removing non-user (it should just
// silently fail)
-
- const message = {...sample_message};
+ const message = sample_message_with_clean_reactions();
override(message_store, "get", () => message);
const event = {
@@ -1212,7 +1216,7 @@ test("remove spurious user", ({override}) => {
});
test("remove last user", ({override, override_rewire}) => {
- const message = {...sample_message};
+ const message = sample_message_with_clean_reactions();
override(message_store, "get", () => message);
override_rewire(reactions, "remove_reaction_from_view", noop);
@@ -1250,7 +1254,7 @@ test("local_reaction_id", () => {
test("process_reaction_click", ({override, override_rewire}) => {
override_rewire(reactions, "remove_reaction_from_view", noop);
- const message = {...sample_message};
+ const message = sample_message_with_clean_reactions();
override(message_store, "get", () => message);
const expected_reaction_info = {
@@ -1290,7 +1294,7 @@ test("code coverage", ({override}) => {
override(message_store, "get", (id) => {
assert.equal(id, 42);
return {
- reactions: [],
+ clean_reactions: new Map(),
};
});
@@ -1309,7 +1313,7 @@ test("duplicates", () => {
};
blueslip.expect("error", "server sent duplicate reactions");
- reactions.set_clean_reactions(dup_reaction_message);
+ convert_reactions_to_clean_reactions(dup_reaction_message);
});
test("process_reaction_click undefined", ({override}) => {
@@ -1320,7 +1324,7 @@ test("process_reaction_click undefined", ({override}) => {
});
test("process_reaction_click bad local id", ({override}) => {
- const stub_message = {id: 4001, reactions: []};
+ const stub_message = {id: 4001, clean_reactions: new Map()};
override(message_store, "get", () => stub_message);
blueslip.expect("error", "Data integrity problem for reaction");
reactions.process_reaction_click("some-msg-id", "bad-local-id");