From 61a782d252cf5a8e6976ca5085a03980615b97e0 Mon Sep 17 00:00:00 2001 From: evykassirer Date: Fri, 7 Oct 2022 09:58:30 -0700 Subject: [PATCH] drafts: Don't update draft count when successfully sending messages. Fixes #21757. Previously the draft count would briefly increase between the action of sending a message and the success of sending that message. Now the draft count will only increase if the message fails to send. --- frontend_tests/node_tests/compose.js | 4 ++++ static/js/compose.js | 5 +++++ static/js/drafts.js | 21 ++++++++++++++------- static/js/echo.js | 8 ++++---- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/frontend_tests/node_tests/compose.js b/frontend_tests/node_tests/compose.js index 574ae2c8d5..c0a6fa625b 100644 --- a/frontend_tests/node_tests/compose.js +++ b/frontend_tests/node_tests/compose.js @@ -147,6 +147,10 @@ test_ui("send_message", ({override, override_rewire}) => { return stub_state; } + const $container = $(".top_left_drafts"); + const $child = $(".unread_count"); + $container.set_find_results(".unread_count", $child); + override(server_events, "assert_get_events_running", () => { stub_state.get_events_running_called += 1; }); diff --git a/static/js/compose.js b/static/js/compose.js index d2861293b1..61b19477fe 100644 --- a/static/js/compose.js +++ b/static/js/compose.js @@ -11,6 +11,7 @@ import * as compose_fade from "./compose_fade"; import * as compose_state from "./compose_state"; import * as compose_ui from "./compose_ui"; import * as compose_validate from "./compose_validate"; +import * as drafts from "./drafts"; import * as echo from "./echo"; import * as flatpickr from "./flatpickr"; import {$t, $t_html} from "./i18n"; @@ -256,6 +257,10 @@ export function send_message(request = create_message_object()) { } echo.message_send_error(message.id, response); + + // We might not have updated the draft count because we assumed the + // message would send. Ensure that the displayed count is correct. + drafts.sync_count(); } transmit.send_message(request, success, error); diff --git a/static/js/drafts.js b/static/js/drafts.js index ab3875c918..95e2641d14 100644 --- a/static/js/drafts.js +++ b/static/js/drafts.js @@ -56,12 +56,14 @@ export const draft_model = (function () { return get()[id] || false; }; - function save(drafts) { + function save(drafts, update_count = true) { ls.set(KEY, drafts); - set_count(Object.keys(drafts).length); + if (update_count) { + set_count(Object.keys(drafts).length); + } } - exports.addDraft = function (draft) { + exports.addDraft = function (draft, update_count = true) { const drafts = get(); // use the base16 of the current time + a random string to reduce @@ -70,7 +72,7 @@ export const draft_model = (function () { draft.updatedAt = getTimestamp(); drafts[id] = draft; - save(drafts); + save(drafts, update_count); return id; }; @@ -104,6 +106,11 @@ export const draft_model = (function () { return exports; })(); +export function sync_count() { + const drafts = draft_model.get(); + set_count(Object.keys(drafts).length); +} + export function delete_all_drafts() { const drafts = draft_model.get(); for (const [id] of Object.entries(drafts)) { @@ -229,9 +236,9 @@ export function update_draft(opts = {}) { return draft_id; } - // We have never saved a draft for this message, so add - // one. - const new_draft_id = draft_model.addDraft(draft); + // We have never saved a draft for this message, so add one. + const update_count = opts.update_count === undefined ? true : opts.update_count; + const new_draft_id = draft_model.addDraft(draft, update_count); $("#compose-textarea").data("draft-id", new_draft_id); maybe_notify(no_notify); diff --git a/static/js/echo.js b/static/js/echo.js index 71f064ad5c..3b0f034f44 100644 --- a/static/js/echo.js +++ b/static/js/echo.js @@ -235,10 +235,10 @@ export function try_deliver_locally(message_request) { // Save a locally echoed message in drafts, so it cannot be // lost. It will be cleared if the message is sent successfully. - // We ask the drafts system to not notify the user, since they'd - // be quite distracting in the very common case that the message - // sends normally. - const draft_id = drafts.update_draft({no_notify: true}); + // We ask the drafts system to not notify the user or update the + // draft count, since that would be quite distracting in the very + // common case that the message sends normally. + const draft_id = drafts.update_draft({no_notify: true, update_count: false}); message_request.draft_id = draft_id; // Now that we've committed to delivering the message locally, we