reactions: Pass full message objects to view functions.

This is not the cleanest API, but it will be necessary in the next
commit in order to update the message's rendering of other emoji.
This commit is contained in:
Tim Abbott 2022-10-21 17:42:51 -07:00
parent 02b670fa66
commit 98c478d994
2 changed files with 130 additions and 103 deletions

View File

@ -237,12 +237,15 @@ test("basics", () => {
test("unknown realm emojis (add)", () => { test("unknown realm emojis (add)", () => {
assert.throws( assert.throws(
() => () =>
reactions.view.insert_new_reaction({ reactions.view.insert_new_reaction(
{
reaction_type: "realm_emoji", reaction_type: "realm_emoji",
emoji_name: "false_emoji", emoji_name: "false_emoji",
emoji_code: "broken", emoji_code: "broken",
user_ids: [alice.user_id], },
}), 1000,
alice.user_id,
),
{ {
name: "Error", name: "Error",
message: "Cannot find realm emoji for code 'broken'.", message: "Cannot find realm emoji for code 'broken'.",
@ -253,12 +256,15 @@ 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.view.insert_new_reaction(
{
reaction_type: "realm_emoji", reaction_type: "realm_emoji",
emoji_name: "fake_emoji", emoji_name: "fake_emoji",
emoji_code: "bogus", emoji_code: "bogus",
user_id: bob.user_id, },
}), 1000,
bob.user_id,
),
{ {
name: "Error", name: "Error",
message: "Cannot find realm emoji for code 'bogus'.", message: "Cannot find realm emoji for code 'bogus'.",
@ -425,32 +431,28 @@ test("add_reaction/remove_reaction", ({override}) => {
let view_calls = []; let view_calls = [];
override( override(reactions.view, "insert_new_reaction", (clean_reaction_object, message, user_id) => {
reactions.view,
"insert_new_reaction",
(clean_reaction_object, message_id, user_id) => {
view_calls.push({ view_calls.push({
name: "insert_new_reaction", name: "insert_new_reaction",
clean_reaction_object, clean_reaction_object,
message_id, message,
user_id, user_id,
}); });
}, });
);
override( override(
reactions.view, reactions.view,
"update_existing_reaction", "update_existing_reaction",
(clean_reaction_object, message_id, user_id) => { (clean_reaction_object, message, user_id) => {
view_calls.push({ view_calls.push({
name: "update_existing_reaction", name: "update_existing_reaction",
clean_reaction_object, clean_reaction_object,
message_id, message,
user_id, user_id,
}); });
}, },
); );
override(reactions.view, "remove_reaction", (clean_reaction_object, message_id, user_id) => { override(reactions.view, "remove_reaction", (clean_reaction_object, message, user_id) => {
view_calls.push({name: "remove_reaction", clean_reaction_object, message_id, user_id}); view_calls.push({name: "remove_reaction", clean_reaction_object, message, user_id});
}); });
function test_view_calls(test_params) { function test_view_calls(test_params) {
@ -489,14 +491,7 @@ test("add_reaction/remove_reaction", ({override}) => {
user_id: cali.user_id, user_id: cali.user_id,
}; };
test_view_calls({ const clean_reaction_object_alice = {
run_code() {
reactions.add_reaction(alice_8ball_event);
},
expected_view_calls: [
{
name: "insert_new_reaction",
clean_reaction_object: {
class: "message_reaction reacted", class: "message_reaction reacted",
count: 1, count: 1,
emoji_alt_code: false, emoji_alt_code: false,
@ -507,8 +502,23 @@ test("add_reaction/remove_reaction", ({override}) => {
local_id: "unicode_emoji,1f3b1", local_id: "unicode_emoji,1f3b1",
reaction_type: alice_8ball_event.reaction_type, reaction_type: alice_8ball_event.reaction_type,
user_ids: [alice.user_id], user_ids: [alice.user_id],
};
test_view_calls({
run_code() {
reactions.add_reaction(alice_8ball_event);
},
expected_view_calls: [
{
name: "insert_new_reaction",
clean_reaction_object: clean_reaction_object_alice,
message: {
id: alice_8ball_event.message_id,
clean_reactions: new Map(
Object.entries({
"unicode_emoji,1f3b1": clean_reaction_object_alice,
}),
),
}, },
message_id: alice_8ball_event.message_id,
user_id: alice_8ball_event.user_id, user_id: alice_8ball_event.user_id,
}, },
], ],
@ -524,14 +534,7 @@ test("add_reaction/remove_reaction", ({override}) => {
alice_emojis: ["8ball"], alice_emojis: ["8ball"],
}); });
test_view_calls({ const clean_reaction_object_bob = {
run_code() {
reactions.add_reaction(bob_8ball_event);
},
expected_view_calls: [
{
name: "update_existing_reaction",
clean_reaction_object: {
class: "message_reaction reacted", class: "message_reaction reacted",
count: 2, count: 2,
emoji_alt_code: false, emoji_alt_code: false,
@ -542,22 +545,30 @@ test("add_reaction/remove_reaction", ({override}) => {
local_id: "unicode_emoji,1f3b1", local_id: "unicode_emoji,1f3b1",
reaction_type: bob_8ball_event.reaction_type, reaction_type: bob_8ball_event.reaction_type,
user_ids: [alice.user_id, bob.user_id], user_ids: [alice.user_id, bob.user_id],
};
test_view_calls({
run_code() {
reactions.add_reaction(bob_8ball_event);
},
expected_view_calls: [
{
name: "update_existing_reaction",
clean_reaction_object: clean_reaction_object_bob,
message: {
id: bob_8ball_event.message_id,
clean_reactions: new Map(
Object.entries({
"unicode_emoji,1f3b1": clean_reaction_object_bob,
}),
),
}, },
message_id: bob_8ball_event.message_id,
user_id: bob_8ball_event.user_id, user_id: bob_8ball_event.user_id,
}, },
], ],
alice_emojis: ["8ball"], alice_emojis: ["8ball"],
}); });
test_view_calls({ const clean_reaction_object_cali = {
run_code() {
reactions.add_reaction(cali_airplane_event);
},
expected_view_calls: [
{
name: "insert_new_reaction",
clean_reaction_object: {
class: "message_reaction", class: "message_reaction",
count: 1, count: 1,
emoji_alt_code: false, emoji_alt_code: false,
@ -568,8 +579,24 @@ test("add_reaction/remove_reaction", ({override}) => {
local_id: "unicode_emoji,2708", local_id: "unicode_emoji,2708",
reaction_type: cali_airplane_event.reaction_type, reaction_type: cali_airplane_event.reaction_type,
user_ids: [cali.user_id], user_ids: [cali.user_id],
};
test_view_calls({
run_code() {
reactions.add_reaction(cali_airplane_event);
},
expected_view_calls: [
{
name: "insert_new_reaction",
clean_reaction_object: clean_reaction_object_cali,
message: {
id: cali_airplane_event.message_id,
clean_reactions: new Map(
Object.entries({
"unicode_emoji,1f3b1": clean_reaction_object_bob,
"unicode_emoji,2708": clean_reaction_object_cali,
}),
),
}, },
message_id: cali_airplane_event.message_id,
user_id: cali_airplane_event.user_id, user_id: cali_airplane_event.user_id,
}, },
], ],
@ -583,19 +610,16 @@ test("add_reaction/remove_reaction", ({override}) => {
expected_view_calls: [ expected_view_calls: [
{ {
name: "remove_reaction", name: "remove_reaction",
clean_reaction_object: { clean_reaction_object: clean_reaction_object_alice,
class: "message_reaction reacted", message: {
count: 1, clean_reactions: new Map(
emoji_alt_code: false, Object.entries({
emoji_code: bob_8ball_event.emoji_code, "unicode_emoji,1f3b1": clean_reaction_object_alice,
emoji_name: bob_8ball_event.emoji_name, "unicode_emoji,2708": clean_reaction_object_cali,
is_realm_emoji: false, }),
label: "translated: You (click to remove) reacted with :8ball:", ),
local_id: "unicode_emoji,1f3b1", id: bob_8ball_event.message_id,
reaction_type: bob_8ball_event.reaction_type,
user_ids: [alice.user_id],
}, },
message_id: bob_8ball_event.message_id,
user_id: bob_8ball_event.user_id, user_id: bob_8ball_event.user_id,
}, },
], ],
@ -610,18 +634,17 @@ test("add_reaction/remove_reaction", ({override}) => {
{ {
name: "remove_reaction", name: "remove_reaction",
clean_reaction_object: { clean_reaction_object: {
class: "message_reaction reacted", ...clean_reaction_object_alice,
count: 1,
emoji_alt_code: false,
emoji_code: alice_8ball_event.emoji_code,
emoji_name: alice_8ball_event.emoji_name,
is_realm_emoji: false,
label: "translated: You (click to remove) reacted with :8ball:",
local_id: "unicode_emoji,1f3b1",
reaction_type: alice_8ball_event.reaction_type,
user_ids: [], user_ids: [],
}, },
message_id: alice_8ball_event.message_id, message: {
clean_reactions: new Map(
Object.entries({
"unicode_emoji,2708": clean_reaction_object_cali,
}),
),
id: alice_8ball_event.message_id,
},
user_id: alice_8ball_event.user_id, user_id: alice_8ball_event.user_id,
}, },
], ],
@ -681,7 +704,7 @@ test("view.insert_new_reaction (me w/unicode emoji)", ({mock_template}) => {
insert_called = true; insert_called = true;
}; };
reactions.view.insert_new_reaction(clean_reaction_object, message_id, alice.user_id); reactions.view.insert_new_reaction(clean_reaction_object, {id: message_id}, alice.user_id);
assert.ok(insert_called); assert.ok(insert_called);
}); });
@ -730,7 +753,7 @@ test("view.insert_new_reaction (them w/zulip emoji)", ({mock_template}) => {
insert_called = true; insert_called = true;
}; };
reactions.view.insert_new_reaction(clean_reaction_object, message_id, bob.user_id); reactions.view.insert_new_reaction(clean_reaction_object, {id: message_id}, bob.user_id);
assert.ok(insert_called); assert.ok(insert_called);
}); });
@ -752,7 +775,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(clean_reaction_object, message_id, alice.user_id); reactions.view.update_existing_reaction(clean_reaction_object, {id: message_id}, alice.user_id);
assert.equal($reaction_count.text(), "2"); assert.equal($reaction_count.text(), "2");
assert.ok($our_reaction.hasClass("reacted")); assert.ok($our_reaction.hasClass("reacted"));
@ -780,7 +803,11 @@ 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(clean_reaction_object, message_id, alexus.user_id); reactions.view.update_existing_reaction(
clean_reaction_object,
{id: message_id},
alexus.user_id,
);
assert.equal($reaction_count.text(), "4"); assert.equal($reaction_count.text(), "4");
assert.ok(!$our_reaction.hasClass("reacted")); assert.ok(!$our_reaction.hasClass("reacted"));
@ -809,7 +836,7 @@ test("view.remove_reaction (me)", () => {
$our_reaction.addClass("reacted"); $our_reaction.addClass("reacted");
$our_reaction.set_find_results(".message_reaction_count", $reaction_count); $our_reaction.set_find_results(".message_reaction_count", $reaction_count);
reactions.view.remove_reaction(clean_reaction_object, message_id, alice.user_id); reactions.view.remove_reaction(clean_reaction_object, {id: message_id}, alice.user_id);
assert.equal($reaction_count.text(), "2"); assert.equal($reaction_count.text(), "2");
assert.ok(!$our_reaction.hasClass("reacted")); assert.ok(!$our_reaction.hasClass("reacted"));
@ -839,7 +866,7 @@ test("view.remove_reaction (them)", () => {
$our_reaction.set_find_results(".message_reaction_count", $reaction_count); $our_reaction.set_find_results(".message_reaction_count", $reaction_count);
$our_reaction.addClass("reacted"); $our_reaction.addClass("reacted");
reactions.view.remove_reaction(clean_reaction_object, message_id, bob.user_id); reactions.view.remove_reaction(clean_reaction_object, {id: message_id}, bob.user_id);
assert.equal($reaction_count.text(), "1"); assert.equal($reaction_count.text(), "1");
assert.ok($our_reaction.hasClass("reacted")); assert.ok($our_reaction.hasClass("reacted"));
@ -869,7 +896,7 @@ test("view.remove_reaction (last person)", () => {
$our_reaction.remove = () => { $our_reaction.remove = () => {
removed = true; removed = true;
}; };
reactions.view.remove_reaction(clean_reaction_object, message_id, bob.user_id); reactions.view.remove_reaction(clean_reaction_object, {id: message_id}, bob.user_id);
assert.ok(removed); assert.ok(removed);
}); });

View File

@ -249,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); update_user_fields(clean_reaction_object);
view.update_existing_reaction(clean_reaction_object, message_id, user_id); view.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,
@ -260,16 +260,16 @@ export function add_reaction(event) {
}); });
message.clean_reactions.set(local_id, clean_reaction_object); message.clean_reactions.set(local_id, clean_reaction_object);
view.insert_new_reaction(clean_reaction_object, message_id, user_id); view.insert_new_reaction(clean_reaction_object, message, user_id);
} }
} }
view.update_existing_reaction = function (clean_reaction_object, message_id, acting_user_id) { view.update_existing_reaction = function (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.
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);
set_reaction_count($reaction, clean_reaction_object.user_ids.length); set_reaction_count($reaction, clean_reaction_object.user_ids.length);
@ -284,13 +284,13 @@ view.update_existing_reaction = function (clean_reaction_object, message_id, act
} }
}; };
view.insert_new_reaction = function (clean_reaction_object, message_id, user_id) { view.insert_new_reaction = function (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.
const context = { const context = {
message_id, message_id: message.id,
...emoji.get_emoji_details_for_rendering(clean_reaction_object), ...emoji.get_emoji_details_for_rendering(clean_reaction_object),
}; };
@ -315,7 +315,7 @@ view.insert_new_reaction = function (clean_reaction_object, message_id, user_id)
const $new_reaction = $(render_message_reaction(context)); const $new_reaction = $(render_message_reaction(context));
// Now insert it before the add button. // Now insert it before the add button.
const $reaction_button_element = get_add_reaction_button(message_id); const $reaction_button_element = get_add_reaction_button(message.id);
$new_reaction.insertBefore($reaction_button_element); $new_reaction.insertBefore($reaction_button_element);
}; };
@ -351,12 +351,12 @@ export function remove_reaction(event) {
message.clean_reactions.delete(local_id); message.clean_reactions.delete(local_id);
} }
view.remove_reaction(clean_reaction_object, message_id, user_id); view.remove_reaction(clean_reaction_object, message, user_id);
} }
view.remove_reaction = function (clean_reaction_object, message_id, user_id) { view.remove_reaction = function (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;
if (reaction_count === 0) { if (reaction_count === 0) {