From 6ef5736bfe28680d41fc61073ae45516ad391f01 Mon Sep 17 00:00:00 2001 From: evykassirer Date: Tue, 1 Nov 2022 17:13:42 -0700 Subject: [PATCH] drafts: Rename undefined draft topics to empty string. This fixes an error state that came out of #22094. The code causing the error was fixed in #23238 but some drafts still have undefined topics which has some strange side effects. One day we'll have typescript help catch this kind of thing! More details on CZO: https://chat.zulip.org/#narrow/stream/9-issues/topic/live.20update.20issue.20with.20drafts/near/1457913 --- frontend_tests/node_tests/drafts.js | 28 ++++++++++++++++++++++++--- static/js/drafts.js | 30 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/frontend_tests/node_tests/drafts.js b/frontend_tests/node_tests/drafts.js index 5b847fead7..1ef623c7b7 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 9c54a2c606..ea62317dd3 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(); });