reactions: Remove view namespace.

This will make a migration to typescript easier,
and was unnecessary extra complexity.
This commit is contained in:
evykassirer
2023-12-27 20:02:26 -08:00
committed by Tim Abbott
parent af6a30db7e
commit fc1bd590b6
2 changed files with 72 additions and 68 deletions

View File

@@ -13,9 +13,7 @@ import * as people from "./people";
import * as spectators from "./spectators"; import * as spectators from "./spectators";
import {user_settings} from "./user_settings"; import {user_settings} from "./user_settings";
export const view = { const waiting_for_server_request_ids = new Set();
waiting_for_server_request_ids: new Set(),
}; // function namespace
export function get_local_reaction_id(reaction_info) { export function get_local_reaction_id(reaction_info) {
return [reaction_info.reaction_type, reaction_info.emoji_code].join(","); return [reaction_info.reaction_type, reaction_info.emoji_code].join(",");
@@ -68,7 +66,7 @@ function update_ui_and_send_reaction_ajax(message_id, reaction_info) {
// unique request ID combining the message ID and the local ID, // unique request ID combining the message ID and the local ID,
// which identifies just which emoji to use. // which identifies just which emoji to use.
const reaction_request_id = [message_id, local_id].join(","); const reaction_request_id = [message_id, local_id].join(",");
if (view.waiting_for_server_request_ids.has(reaction_request_id)) { if (waiting_for_server_request_ids.has(reaction_request_id)) {
return; return;
} }
@@ -82,10 +80,10 @@ function update_ui_and_send_reaction_ajax(message_id, reaction_info) {
url: "/json/messages/" + message_id + "/reactions", url: "/json/messages/" + message_id + "/reactions",
data: reaction_info, data: reaction_info,
success() { success() {
view.waiting_for_server_request_ids.delete(reaction_request_id); waiting_for_server_request_ids.delete(reaction_request_id);
}, },
error(xhr) { error(xhr) {
view.waiting_for_server_request_ids.delete(reaction_request_id); waiting_for_server_request_ids.delete(reaction_request_id);
if (xhr.readyState !== 0) { if (xhr.readyState !== 0) {
if ( if (
xhr.responseJSON?.code === "REACTION_ALREADY_EXISTS" || xhr.responseJSON?.code === "REACTION_ALREADY_EXISTS" ||
@@ -100,7 +98,7 @@ function update_ui_and_send_reaction_ajax(message_id, reaction_info) {
}, },
}; };
view.waiting_for_server_request_ids.add(reaction_request_id); waiting_for_server_request_ids.add(reaction_request_id);
if (operation === "add") { if (operation === "add") {
channel.post(args); channel.post(args);
} else if (operation === "remove") { } else if (operation === "remove") {
@@ -251,7 +249,7 @@ export function add_reaction(event) {
if (clean_reaction_object) { if (clean_reaction_object) {
clean_reaction_object.user_ids.push(user_id); clean_reaction_object.user_ids.push(user_id);
update_user_fields(clean_reaction_object, message.clean_reactions); update_user_fields(clean_reaction_object, message.clean_reactions);
view.update_existing_reaction(clean_reaction_object, message, user_id); update_existing_reaction(clean_reaction_object, message, user_id);
} else { } else {
clean_reaction_object = make_clean_reaction({ clean_reaction_object = make_clean_reaction({
local_id, local_id,
@@ -263,11 +261,11 @@ export function add_reaction(event) {
message.clean_reactions.set(local_id, clean_reaction_object); message.clean_reactions.set(local_id, clean_reaction_object);
update_user_fields(clean_reaction_object, message.clean_reactions); update_user_fields(clean_reaction_object, message.clean_reactions);
view.insert_new_reaction(clean_reaction_object, message, user_id); insert_new_reaction(clean_reaction_object, message, user_id);
} }
} }
view.update_existing_reaction = function (clean_reaction_object, message, acting_user_id) { export function update_existing_reaction(clean_reaction_object, message, acting_user_id) {
// Our caller ensures that this message already has a reaction // Our caller ensures that this message already has a reaction
// for this emoji and sets up our user_list. This function // for this emoji and sets up our user_list. This function
// simply updates the DOM. // simply updates the DOM.
@@ -285,9 +283,9 @@ view.update_existing_reaction = function (clean_reaction_object, message, acting
} }
update_vote_text_on_message(message); update_vote_text_on_message(message);
}; }
view.insert_new_reaction = function (clean_reaction_object, message, user_id) { export function insert_new_reaction(clean_reaction_object, message, user_id) {
// Our caller ensures we are the first user to react to this // Our caller ensures we are the first user to react to this
// message with this emoji. We then render the emoji/title/count // message with this emoji. We then render the emoji/title/count
// and insert it before the add button. // and insert it before the add button.
@@ -323,7 +321,7 @@ view.insert_new_reaction = function (clean_reaction_object, message, user_id) {
$new_reaction.insertBefore($reaction_button_element); $new_reaction.insertBefore($reaction_button_element);
update_vote_text_on_message(message); update_vote_text_on_message(message);
}; }
export function remove_reaction(event) { export function remove_reaction(event) {
const message_id = event.message_id; const message_id = event.message_id;
@@ -358,10 +356,10 @@ export function remove_reaction(event) {
const should_display_reactors = check_should_display_reactors(message.clean_reactions); const should_display_reactors = check_should_display_reactors(message.clean_reactions);
update_user_fields(clean_reaction_object, should_display_reactors); update_user_fields(clean_reaction_object, should_display_reactors);
view.remove_reaction(clean_reaction_object, message, user_id); remove_reaction_from_view(clean_reaction_object, message, user_id);
} }
view.remove_reaction = function (clean_reaction_object, message, user_id) { export function remove_reaction_from_view(clean_reaction_object, message, user_id) {
const local_id = get_local_reaction_id(clean_reaction_object); const local_id = get_local_reaction_id(clean_reaction_object);
const $reaction = find_reaction(message.id, local_id); const $reaction = find_reaction(message.id, local_id);
const reaction_count = clean_reaction_object.user_ids.length; const reaction_count = clean_reaction_object.user_ids.length;
@@ -387,7 +385,7 @@ view.remove_reaction = function (clean_reaction_object, message, user_id) {
} }
update_vote_text_on_message(message); update_vote_text_on_message(message);
}; }
export function get_emojis_used_by_user_for_message_id(message_id) { export function get_emojis_used_by_user_for_message_id(message_id) {
const user_id = page_params.user_id; const user_id = page_params.user_id;

View File

@@ -269,7 +269,7 @@ test("reactions from unknown users", () => {
test("unknown realm emojis (add)", () => { test("unknown realm emojis (add)", () => {
assert.throws( assert.throws(
() => () =>
reactions.view.insert_new_reaction( reactions.insert_new_reaction(
{ {
reaction_type: "realm_emoji", reaction_type: "realm_emoji",
emoji_name: "false_emoji", emoji_name: "false_emoji",
@@ -288,7 +288,7 @@ test("unknown realm emojis (add)", () => {
test("unknown realm emojis (insert)", () => { test("unknown realm emojis (insert)", () => {
assert.throws( assert.throws(
() => () =>
reactions.view.insert_new_reaction( reactions.insert_new_reaction(
{ {
reaction_type: "realm_emoji", reaction_type: "realm_emoji",
emoji_name: "fake_emoji", emoji_name: "fake_emoji",
@@ -560,10 +560,7 @@ test("emoji_reaction_title", ({override}) => {
); );
}); });
test("add_reaction/remove_reaction", ({override}) => { test("add_reaction/remove_reaction", ({override, override_rewire}) => {
// This function tests reaction events by mocking out calls to
// the view.
const message = { const message = {
id: 2001, id: 2001,
reactions: [], reactions: [],
@@ -573,21 +570,21 @@ test("add_reaction/remove_reaction", ({override}) => {
override(message_store, "get", () => message); override(message_store, "get", () => message);
let view_calls = []; let function_calls = [];
override(reactions.view, "insert_new_reaction", (clean_reaction_object, message, user_id) => { override_rewire(reactions, "insert_new_reaction", (clean_reaction_object, message, user_id) => {
view_calls.push({ function_calls.push({
name: "insert_new_reaction", name: "insert_new_reaction",
clean_reaction_object, clean_reaction_object,
message, message,
user_id, user_id,
}); });
}); });
override( override_rewire(
reactions.view, reactions,
"update_existing_reaction", "update_existing_reaction",
(clean_reaction_object, message, user_id) => { (clean_reaction_object, message, user_id) => {
view_calls.push({ function_calls.push({
name: "update_existing_reaction", name: "update_existing_reaction",
clean_reaction_object, clean_reaction_object,
message, message,
@@ -595,16 +592,25 @@ test("add_reaction/remove_reaction", ({override}) => {
}); });
}, },
); );
override(reactions.view, "remove_reaction", (clean_reaction_object, message, user_id) => { override_rewire(
view_calls.push({name: "remove_reaction", clean_reaction_object, message, user_id}); reactions,
}); "remove_reaction_from_view",
(clean_reaction_object, message, user_id) => {
function_calls.push({
name: "remove_reaction_from_view",
clean_reaction_object,
message,
user_id,
});
},
);
function test_view_calls(test_params) { function test_function_calls(test_params) {
view_calls = []; function_calls = [];
test_params.run_code(); test_params.run_code();
assert.deepEqual(view_calls, test_params.expected_view_calls); assert.deepEqual(function_calls, test_params.expected_function_calls);
assert.deepEqual( assert.deepEqual(
new Set(reactions.get_emojis_used_by_user_for_message_id(message.message_id)), new Set(reactions.get_emojis_used_by_user_for_message_id(message.message_id)),
new Set(test_params.alice_emojis), new Set(test_params.alice_emojis),
@@ -648,11 +654,11 @@ test("add_reaction/remove_reaction", ({override}) => {
user_ids: [alice.user_id], user_ids: [alice.user_id],
vote_text: "translated: You", vote_text: "translated: You",
}; };
test_view_calls({ test_function_calls({
run_code() { run_code() {
reactions.add_reaction(alice_8ball_event); reactions.add_reaction(alice_8ball_event);
}, },
expected_view_calls: [ expected_function_calls: [
{ {
name: "insert_new_reaction", name: "insert_new_reaction",
clean_reaction_object: clean_reaction_object_alice, clean_reaction_object: clean_reaction_object_alice,
@@ -671,11 +677,11 @@ test("add_reaction/remove_reaction", ({override}) => {
}); });
// Add redundant reaction. // Add redundant reaction.
test_view_calls({ test_function_calls({
run_code() { run_code() {
reactions.add_reaction(alice_8ball_event); reactions.add_reaction(alice_8ball_event);
}, },
expected_view_calls: [], expected_function_calls: [],
alice_emojis: ["8ball"], alice_emojis: ["8ball"],
}); });
@@ -692,11 +698,11 @@ test("add_reaction/remove_reaction", ({override}) => {
user_ids: [alice.user_id, bob.user_id], user_ids: [alice.user_id, bob.user_id],
vote_text: "translated: You, Bob van Roberts", vote_text: "translated: You, Bob van Roberts",
}; };
test_view_calls({ test_function_calls({
run_code() { run_code() {
reactions.add_reaction(bob_8ball_event); reactions.add_reaction(bob_8ball_event);
}, },
expected_view_calls: [ expected_function_calls: [
{ {
name: "update_existing_reaction", name: "update_existing_reaction",
clean_reaction_object: clean_reaction_object_bob, clean_reaction_object: clean_reaction_object_bob,
@@ -727,11 +733,11 @@ test("add_reaction/remove_reaction", ({override}) => {
user_ids: [cali.user_id], user_ids: [cali.user_id],
vote_text: "Cali", vote_text: "Cali",
}; };
test_view_calls({ test_function_calls({
run_code() { run_code() {
reactions.add_reaction(cali_airplane_event); reactions.add_reaction(cali_airplane_event);
}, },
expected_view_calls: [ expected_function_calls: [
{ {
name: "insert_new_reaction", name: "insert_new_reaction",
clean_reaction_object: clean_reaction_object_cali, clean_reaction_object: clean_reaction_object_cali,
@@ -750,13 +756,13 @@ test("add_reaction/remove_reaction", ({override}) => {
alice_emojis: ["8ball"], alice_emojis: ["8ball"],
}); });
test_view_calls({ test_function_calls({
run_code() { run_code() {
reactions.remove_reaction(bob_8ball_event); reactions.remove_reaction(bob_8ball_event);
}, },
expected_view_calls: [ expected_function_calls: [
{ {
name: "remove_reaction", name: "remove_reaction_from_view",
clean_reaction_object: clean_reaction_object_alice, clean_reaction_object: clean_reaction_object_alice,
message: { message: {
clean_reactions: new Map( clean_reactions: new Map(
@@ -773,13 +779,13 @@ test("add_reaction/remove_reaction", ({override}) => {
alice_emojis: ["8ball"], alice_emojis: ["8ball"],
}); });
test_view_calls({ test_function_calls({
run_code() { run_code() {
reactions.remove_reaction(alice_8ball_event); reactions.remove_reaction(alice_8ball_event);
}, },
expected_view_calls: [ expected_function_calls: [
{ {
name: "remove_reaction", name: "remove_reaction_from_view",
clean_reaction_object: { clean_reaction_object: {
count: 0, count: 0,
class: "message_reaction", class: "message_reaction",
@@ -808,16 +814,16 @@ test("add_reaction/remove_reaction", ({override}) => {
}); });
// Test redundant remove. // Test redundant remove.
test_view_calls({ test_function_calls({
run_code() { run_code() {
reactions.remove_reaction(alice_8ball_event); reactions.remove_reaction(alice_8ball_event);
}, },
expected_view_calls: [], expected_function_calls: [],
alice_emojis: [], alice_emojis: [],
}); });
}); });
test("view.insert_new_reaction (me w/unicode emoji)", ({mock_template}) => { test("insert_new_reaction (me w/unicode emoji)", ({mock_template}) => {
const clean_reaction_object = { const clean_reaction_object = {
class: "message_reaction", class: "message_reaction",
count: 1, count: 1,
@@ -863,7 +869,7 @@ test("view.insert_new_reaction (me w/unicode emoji)", ({mock_template}) => {
insert_called = true; insert_called = true;
}; };
reactions.view.insert_new_reaction( reactions.insert_new_reaction(
clean_reaction_object, clean_reaction_object,
{ {
id: message_id, id: message_id,
@@ -881,7 +887,7 @@ test("view.insert_new_reaction (me w/unicode emoji)", ({mock_template}) => {
assert.ok(insert_called); assert.ok(insert_called);
}); });
test("view.insert_new_reaction (them w/zulip emoji)", ({mock_template}) => { test("insert_new_reaction (them w/zulip emoji)", ({mock_template}) => {
const clean_reaction_object = { const clean_reaction_object = {
class: "message_reaction", class: "message_reaction",
count: 1, count: 1,
@@ -929,7 +935,7 @@ test("view.insert_new_reaction (them w/zulip emoji)", ({mock_template}) => {
insert_called = true; insert_called = true;
}; };
reactions.view.insert_new_reaction( reactions.insert_new_reaction(
clean_reaction_object, clean_reaction_object,
{ {
id: message_id, id: message_id,
@@ -947,7 +953,7 @@ test("view.insert_new_reaction (them w/zulip emoji)", ({mock_template}) => {
assert.ok(insert_called); assert.ok(insert_called);
}); });
test("view.update_existing_reaction (me)", () => { test("update_existing_reaction (me)", () => {
const clean_reaction_object = { const clean_reaction_object = {
class: "message_reaction", class: "message_reaction",
count: 2, count: 2,
@@ -965,7 +971,7 @@ test("view.update_existing_reaction (me)", () => {
const $reaction_count = $.create("reaction-count-stub"); const $reaction_count = $.create("reaction-count-stub");
$our_reaction.set_find_results(".message_reaction_count", $reaction_count); $our_reaction.set_find_results(".message_reaction_count", $reaction_count);
reactions.view.update_existing_reaction( reactions.update_existing_reaction(
clean_reaction_object, clean_reaction_object,
{ {
id: message_id, id: message_id,
@@ -994,7 +1000,7 @@ test("view.update_existing_reaction (me)", () => {
); );
}); });
test("view.update_existing_reaction (them)", () => { test("update_existing_reaction (them)", () => {
const clean_reaction_object = { const clean_reaction_object = {
class: "message_reaction", class: "message_reaction",
count: 4, count: 4,
@@ -1012,7 +1018,7 @@ test("view.update_existing_reaction (them)", () => {
const $reaction_count = $.create("reaction-count-stub"); const $reaction_count = $.create("reaction-count-stub");
$our_reaction.set_find_results(".message_reaction_count", $reaction_count); $our_reaction.set_find_results(".message_reaction_count", $reaction_count);
reactions.view.update_existing_reaction( reactions.update_existing_reaction(
clean_reaction_object, clean_reaction_object,
{ {
id: message_id, id: message_id,
@@ -1053,7 +1059,7 @@ test("view.update_existing_reaction (them)", () => {
); );
}); });
test("view.remove_reaction (me)", () => { test("remove_reaction_from_view (me)", () => {
const clean_reaction_object = { const clean_reaction_object = {
class: "message_reaction", class: "message_reaction",
count: 2, count: 2,
@@ -1072,7 +1078,7 @@ test("view.remove_reaction (me)", () => {
const $reaction_button = $.create("reaction-button-stub"); const $reaction_button = $.create("reaction-button-stub");
$message_reactions.find = () => $reaction_button; $message_reactions.find = () => $reaction_button;
reactions.view.remove_reaction( reactions.remove_reaction_from_view(
clean_reaction_object, clean_reaction_object,
{ {
id: message_id, id: message_id,
@@ -1101,7 +1107,7 @@ test("view.remove_reaction (me)", () => {
); );
}); });
test("view.remove_reaction (them)", () => { test("remove_reaction_from_view (them)", () => {
const clean_reaction_object = { const clean_reaction_object = {
class: "message_reaction", class: "message_reaction",
count: 1, count: 1,
@@ -1120,7 +1126,7 @@ test("view.remove_reaction (them)", () => {
const $reaction_button = $.create("reaction-button-stub"); const $reaction_button = $.create("reaction-button-stub");
$message_reactions.find = () => $reaction_button; $message_reactions.find = () => $reaction_button;
reactions.view.remove_reaction( reactions.remove_reaction_from_view(
clean_reaction_object, clean_reaction_object,
{ {
id: message_id, id: message_id,
@@ -1143,7 +1149,7 @@ test("view.remove_reaction (them)", () => {
); );
}); });
test("view.remove_reaction (last person)", () => { test("remove_reaction_from_view (last person)", () => {
const clean_reaction_object = { const clean_reaction_object = {
class: "message_reaction", class: "message_reaction",
count: 1, count: 1,
@@ -1163,7 +1169,7 @@ test("view.remove_reaction (last person)", () => {
$our_reaction.remove = () => { $our_reaction.remove = () => {
removed = true; removed = true;
}; };
reactions.view.remove_reaction( reactions.remove_reaction_from_view(
clean_reaction_object, clean_reaction_object,
{id: message_id, reactions: []}, {id: message_id, reactions: []},
bob.user_id, bob.user_id,
@@ -1208,11 +1214,11 @@ test("remove spurious user", ({override}) => {
reactions.remove_reaction(event); reactions.remove_reaction(event);
}); });
test("remove last user", ({override}) => { test("remove last user", ({override, override_rewire}) => {
const message = {...sample_message}; const message = {...sample_message};
override(message_store, "get", () => message); override(message_store, "get", () => message);
override(reactions.view, "remove_reaction", noop); override_rewire(reactions, "remove_reaction_from_view", noop);
function assert_names(names) { function assert_names(names) {
assert.deepEqual( assert.deepEqual(
@@ -1244,8 +1250,8 @@ test("local_reaction_id", () => {
assert.equal(local_id, "unicode_emoji,1f44d"); assert.equal(local_id, "unicode_emoji,1f44d");
}); });
test("process_reaction_click", ({override}) => { test("process_reaction_click", ({override, override_rewire}) => {
override(reactions.view, "remove_reaction", noop); override_rewire(reactions, "remove_reaction_from_view", noop);
const message = {...sample_message}; const message = {...sample_message};
override(message_store, "get", () => message); override(message_store, "get", () => message);