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.

reactions: Revert optimistic update when a reaction request fails.

Evy Kassirer5eaeae69d5b3
diff --git a/web/src/reactions.ts b/web/src/reactions.ts
index 39a08868025..574215b8c52 100644
--- a/web/src/reactions.ts
+++ b/web/src/reactions.ts
@@ -174,6 +174,13 @@ function send_reaction_request(
}, delay_secs * 1000);
} else {
waiting_for_server_request_ids.delete(reaction_request_id);
+ // The server didn't apply the update, so revert the
+ // optimistically-updated local state.
+ if (operation === "add") {
+ remove_reaction(reaction);
+ } else {
+ add_reaction(reaction);
+ }
if (is_transient_error) {
// The transient failure persisted past our retry budget,
// so the server is likely having a real problem.
diff --git a/web/tests/reactions.test.cjs b/web/tests/reactions.test.cjs
index efb13b711cd..38a6787a25f 100644
--- a/web/tests/reactions.test.cjs
+++ b/web/tests/reactions.test.cjs
@@ -451,6 +451,7 @@ test("sending precondition failures", ({override_rewire}) => {
test("sending failure retries", ({override_rewire}) => {
const message = sample_message_with_clean_reactions();
override_rewire(reactions, "add_reaction", noop);
+ override_rewire(reactions, "remove_reaction", noop);
override_rewire(retry_backoff, "get_retry_backoff_seconds", () => 10);
const stub = make_stub();
channel.post = stub.f;
@@ -503,6 +504,61 @@ test("sending failure retries", ({override_rewire}) => {
stub.get_args("args").args.success();
});
+test("sending failure reverts optimistic update", ({override_rewire}) => {
+ const message = sample_message_with_clean_reactions();
+
+ const add_stub = make_stub();
+ const remove_stub = make_stub();
+ override_rewire(reactions, "add_reaction", add_stub.f);
+ override_rewire(reactions, "remove_reaction", remove_stub.f);
+
+ blueslip.expect("error", "Error sending reaction", 2);
+
+ {
+ // A failed add is reverted by removing the reaction locally.
+ const stub = make_stub();
+ channel.post = stub.f;
+ reactions.toggle_emoji_reaction(message, "banana");
+ assert.equal(add_stub.num_calls, 1);
+
+ stub.get_args("args").args.error({
+ readyState: 4,
+ status: 400,
+ responseJSON: {msg: "Invalid message(s)"},
+ });
+ assert.equal(remove_stub.num_calls, 1);
+ assert.deepEqual(remove_stub.get_args("event").event, {
+ message_id: message.id,
+ user_id: alice_user_id,
+ reaction_type: "unicode_emoji",
+ emoji_name: "banana",
+ emoji_code: "1f34c",
+ });
+ }
+
+ {
+ // A failed remove is reverted by re-adding the reaction locally.
+ const stub = make_stub();
+ channel.del = stub.f;
+ reactions.toggle_emoji_reaction(message, "smile");
+ assert.equal(remove_stub.num_calls, 2);
+
+ stub.get_args("args").args.error({
+ readyState: 4,
+ status: 400,
+ responseJSON: {msg: "Invalid message(s)"},
+ });
+ assert.equal(add_stub.num_calls, 2);
+ assert.deepEqual(add_stub.get_args("event").event, {
+ message_id: message.id,
+ user_id: alice_user_id,
+ reaction_type: "unicode_emoji",
+ emoji_name: "smile",
+ emoji_code: "1f604",
+ });
+ }
+});
+
test("prevent_simultaneous_requests_updating_reaction", ({override_rewire}) => {
const message = sample_message_with_clean_reactions();
override_rewire(reactions, "add_reaction", noop);