From a67cff49649bd0e74ff8780ac672ce4289febb49 Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Thu, 28 Jan 2021 05:01:09 +0000 Subject: [PATCH] reactions: Don't process clicks from spectators. The reaction count goes up locally if we don't have this check. This is misleading for spectators. --- frontend_tests/node_tests/reactions.js | 29 ++++++++++++++++++-------- static/js/reactions.js | 8 +++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/frontend_tests/node_tests/reactions.js b/frontend_tests/node_tests/reactions.js index 64c44ddf48..a225577cbf 100644 --- a/frontend_tests/node_tests/reactions.js +++ b/frontend_tests/node_tests/reactions.js @@ -44,6 +44,9 @@ const emoji_picker = mock_esm("../../static/js/emoji_picker", { }); const message_lists = mock_esm("../../static/js/message_lists"); const message_store = mock_esm("../../static/js/message_store"); +const login_to_access = mock_esm("../../static/js/login_to_access", { + show() {}, +}); message_lists.current = { selected_message() { @@ -907,15 +910,23 @@ test("process_reaction_click", ({override}) => { emoji_name: "smile", emoji_code: "1f642", }; - { - const stub = make_stub(); - channel.del = stub.f; - reactions.process_reaction_click(message.id, "unicode_emoji,1f642"); - assert.equal(stub.num_calls, 1); - const args = stub.get_args("args").args; - assert.equal(args.url, "/json/messages/1001/reactions"); - assert.deepEqual(args.data, expected_reaction_info); - } + + // Test spectator cannot react. + page_params.is_spectator = true; + let stub = make_stub(); + login_to_access.show = stub.f; + reactions.process_reaction_click(message.id, "unicode_emoji,1f642"); + let args = stub.get_args("args").args; + assert.equal(args, undefined); + + page_params.is_spectator = false; + stub = make_stub(); + channel.del = stub.f; + reactions.process_reaction_click(message.id, "unicode_emoji,1f642"); + assert.equal(stub.num_calls, 1); + args = stub.get_args("args").args; + assert.equal(args.url, "/json/messages/1001/reactions"); + assert.deepEqual(args.data, expected_reaction_info); }); test("warnings", () => { diff --git a/static/js/reactions.js b/static/js/reactions.js index d9d6418d3f..0932a7fe8e 100644 --- a/static/js/reactions.js +++ b/static/js/reactions.js @@ -8,6 +8,7 @@ import * as blueslip from "./blueslip"; import * as channel from "./channel"; import * as emoji_picker from "./emoji_picker"; import {$t} from "./i18n"; +import * as login_to_access from "./login_to_access"; import * as message_lists from "./message_lists"; import * as message_store from "./message_store"; import {page_params} from "./page_params"; @@ -110,6 +111,13 @@ export function toggle_emoji_reaction(message_id, emoji_name) { } export function process_reaction_click(message_id, local_id) { + if (page_params.is_spectator) { + // Spectators can't react, since they don't have accounts. We + // stop here to avoid a confusing reaction local echo. + login_to_access.show(); + return; + } + const message = get_message(message_id); if (!message) {