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.
This commit is contained in:
evykassirer 2022-10-07 09:58:30 -07:00 committed by Tim Abbott
parent cefed552f6
commit 61a782d252
4 changed files with 27 additions and 11 deletions

View File

@ -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;
});

View File

@ -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);

View File

@ -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);

View File

@ -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