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.

drafts: Don't notify if contents of draft have not been changed.

YashRE423e853d6f8f42
diff --git a/frontend_tests/node_tests/drafts.js b/frontend_tests/node_tests/drafts.js
index 69651c40571..fcee1b436cd 100644
--- a/frontend_tests/node_tests/drafts.js
+++ b/frontend_tests/node_tests/drafts.js
@@ -271,27 +271,39 @@ test("update_draft", ({override}) => {
override(Date, "now", () => 5);
override(Math, "random", () => 2);
draft_id = drafts.update_draft();
+ assert.equal(draft_id, "5-2");
assert.ok(tippy_show_called);
assert.ok(tippy_destroy_called);
- assert.equal(draft_id, "5-2");
override(Date, "now", () => 6);
+ override(compose_state, "message_content", () => "dummy content edited once");
tippy_show_called = false;
tippy_destroy_called = false;
draft_id = drafts.update_draft();
+ assert.equal(draft_id, "5-2");
assert.ok(tippy_show_called);
assert.ok(tippy_destroy_called);
- assert.equal(draft_id, "5-2");
override(Date, "now", () => 7);
+ // message contents not edited
tippy_show_called = false;
tippy_destroy_called = false;
- draft_id = drafts.update_draft({no_notify: true});
+ draft_id = drafts.update_draft();
+ assert.equal(draft_id, "5-2");
assert.ok(!tippy_show_called);
assert.ok(!tippy_destroy_called);
+
+ override(Date, "now", () => 8);
+
+ override(compose_state, "message_content", () => "dummy content edited a second time");
+ tippy_show_called = false;
+ tippy_destroy_called = false;
+ draft_id = drafts.update_draft({no_notify: true});
assert.equal(draft_id, "5-2");
+ assert.ok(!tippy_show_called);
+ assert.ok(!tippy_destroy_called);
});
test("delete_all_drafts", () => {
diff --git a/static/js/drafts.js b/static/js/drafts.js
index e45274d334a..ed11c5bd3da 100644
--- a/static/js/drafts.js
+++ b/static/js/drafts.js
@@ -1,6 +1,7 @@
import {subDays} from "date-fns";
import Handlebars from "handlebars/runtime";
import $ from "jquery";
+import _ from "lodash";
import tippy from "tippy.js";
import render_confirm_delete_all_drafts from "../templates/confirm_dialog/confirm_delete_all_drafts.hbs";
@@ -74,12 +75,19 @@ export const draft_model = (function () {
exports.editDraft = function (id, draft) {
const drafts = get();
+ let changed = false;
+
+ function check_if_equal(draft_a, draft_b) {
+ return _.isEqual(_.omit(draft_a, ['updatedAt']), _.omit(draft_b, ['updatedAt']));
+ }
if (drafts[id]) {
+ changed = !check_if_equal(drafts[id], draft);
draft.updatedAt = getTimestamp();
drafts[id] = draft;
save(drafts);
}
+ return changed;
};
exports.deleteDraft = function (id) {
@@ -199,8 +207,10 @@ export function update_draft(opts = {}) {
if (draft_id !== undefined) {
// We don't save multiple drafts of the same message;
// just update the existing draft.
- draft_model.editDraft(draft_id, draft);
- maybe_notify(no_notify);
+ const changed = draft_model.editDraft(draft_id, draft);
+ if (changed) {
+ maybe_notify(no_notify);
+ }
return draft_id;
}