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: Rename undefined draft topics to empty string.

evykassirer6ef5736bfe28
diff --git a/frontend_tests/node_tests/drafts.js b/frontend_tests/node_tests/drafts.js
index 5b847fead7f..1ef623c7b7f 100644
--- a/frontend_tests/node_tests/drafts.js
+++ b/frontend_tests/node_tests/drafts.js
@@ -382,9 +382,7 @@ test("rename_stream_recipient", ({override_rewire}) => {
// There were some buggy drafts that had their topics
// renamed to `undefined` in #23238.
// TODO/compatibility: The next two tests can be deleted
-// in 2023 since all relevant drafts will have either
-// been run through this code or else been deleted after
-// 30 days.
+// when we get to delete drafts.fix_drafts_with_undefined_topics.
test("catch_buggy_draft_error", () => {
const stream_A = {
subscribed: false,
@@ -428,6 +426,30 @@ test("catch_buggy_draft_error", () => {
assert.equal(draft.topic, undefined);
});
+test("fix_buggy_draft", ({override_rewire}) => {
+ override_rewire(drafts, "set_count", noop);
+
+ const buggy_draft = {
+ stream: "stream name",
+ stream_id: 1,
+ // This is the bug: topic never be undefined for a stream
+ // message draft.
+ topic: undefined,
+ type: "stream",
+ content: "Test stream message",
+ updatedAt: Date.now(),
+ };
+ const data = {id1: buggy_draft};
+ const ls = localstorage();
+ ls.set("drafts", data);
+ const draft_model = drafts.draft_model;
+
+ drafts.fix_drafts_with_undefined_topics();
+ const draft = draft_model.getDraft("id1");
+ assert.equal(draft.stream, "stream name");
+ assert.equal(draft.topic, "");
+});
+
test("delete_all_drafts", () => {
const draft_model = drafts.draft_model;
const ls = localstorage();
diff --git a/static/js/drafts.js b/static/js/drafts.js
index 9c54a2c606b..ea62317dd3d 100644
--- a/static/js/drafts.js
+++ b/static/js/drafts.js
@@ -106,6 +106,32 @@ export const draft_model = (function () {
return exports;
})();
+// A one-time fix for buggy drafts that had their topics renamed to
+// `undefined` when the topic was moved to another stream without
+// changing the topic. The bug was introduced in
+// 4c8079c49a81b08b29871f9f1625c6149f48b579 and fixed in
+// aebdf6af8c6675fbd2792888d701d582c4a1110a; but servers running
+// intermediate versions may have generated some bugged drafts with
+// this invalid topic value.
+//
+// TODO/compatibility: This can be deleted once servers can no longer
+// directly upgrade from Zulip 6.0beta1 and earlier development branch where the bug was present,
+// since we expect bugged drafts will have either been run through
+// this code or else been deleted after 30 (DRAFT_LIFETIME) days.
+let fixed_buggy_drafts = false;
+export function fix_drafts_with_undefined_topics() {
+ const data = draft_model.get();
+ for (const draft_id of Object.keys(data)) {
+ const draft = data[draft_id];
+ if (draft.type === "stream" && draft.topic === undefined) {
+ const draft = data[draft_id];
+ draft.topic = "";
+ draft_model.editDraft(draft_id, draft, false);
+ }
+ }
+ fixed_buggy_drafts = true;
+}
+
export function sync_count() {
const drafts = draft_model.get();
set_count(Object.keys(drafts).length);
@@ -773,6 +799,10 @@ export function set_initial_element(drafts) {
export function initialize() {
remove_old_drafts();
+ if (!fixed_buggy_drafts) {
+ fix_drafts_with_undefined_topics();
+ }
+
window.addEventListener("beforeunload", () => {
update_draft();
});