From e95cc444189919af3eead62e1f869d35285b1c74 Mon Sep 17 00:00:00 2001 From: Evan Morikawa Date: Sun, 6 Sep 2015 18:54:25 -0700 Subject: [PATCH] fix(draft): remove old local draft after sending Fixes T3507 --- spec-nylas/tasks/send-draft-spec.coffee | 5 ++++- src/flux/tasks/send-draft.coffee | 23 +++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/spec-nylas/tasks/send-draft-spec.coffee b/spec-nylas/tasks/send-draft-spec.coffee index 3d4eff8af..41ac86594 100644 --- a/spec-nylas/tasks/send-draft-spec.coffee +++ b/spec-nylas/tasks/send-draft-spec.coffee @@ -92,6 +92,8 @@ describe "SendDraftTask", -> Promise.resolve(@draft) spyOn(DatabaseStore, 'unpersistModel').andCallFake (draft) -> Promise.resolve() + spyOn(DatabaseStore, 'persistModel').andCallFake (draft) -> + Promise.resolve() spyOn(atom, "playSound") spyOn(Actions, "postNotification") spyOn(Actions, "sendDraftSuccess") @@ -165,7 +167,6 @@ describe "SendDraftTask", -> expect(options.returnsModel).toBe(false) it "should write the saved message to the database with the same client ID", -> - spyOn(DatabaseStore, 'persistModel') waitsForPromise => @task.performRemote().then => expect(DatabaseStore.persistModel).toHaveBeenCalled() @@ -191,6 +192,8 @@ describe "SendDraftTask", -> spyOn(Actions, "dequeueTask") spyOn(DatabaseStore, 'unpersistModel').andCallFake (draft) -> Promise.resolve() + spyOn(DatabaseStore, 'persistModel').andCallFake (draft) -> + Promise.resolve() describe "when the server responds with `Invalid message public ID`", -> it "should resend the draft without the reply_to_message_id key set", -> diff --git a/src/flux/tasks/send-draft.coffee b/src/flux/tasks/send-draft.coffee index 9e3d56760..30837097d 100644 --- a/src/flux/tasks/send-draft.coffee +++ b/src/flux/tasks/send-draft.coffee @@ -67,16 +67,27 @@ class SendDraftTask extends Task # the raw JSON ourselves. # TODO: Refactor this into an optional clientId param on makeRequest? + oldDraft = @draft @draft = @draft.clone().fromJSON(json) @draft.draft = false - DatabaseStore.persistModel(@draft) - atom.playSound('mail_sent.ogg') - Actions.sendDraftSuccess - draftClientId: @draftClientId - newMessage: @draft + # The updated draft now has a new `serverId`. Unfortunately, the + # draft currently saved in the database only has a `clientId`. When + # we go to save our new draft, the computed `id` will equal the new + # `serverId` and not our old `clientId. This means that we'll + # create a whole second copy of a Draft obejct instead of updating + # our old one. It's an outstanding TODO to come up with a longer + # term solution to this problem. For now, we can fix this by simply + # explicitly removing the old draft before saving the new one. + DatabaseStore.unpersistModel(oldDraft) + .then => DatabaseStore.persistModel(@draft) + .then => + atom.playSound('mail_sent.ogg') + Actions.sendDraftSuccess + draftClientId: @draftClientId + newMessage: @draft - return Promise.resolve(Task.Status.Finished) + return Promise.resolve(Task.Status.Finished) .catch APIError, (err) => if err.message?.indexOf('Invalid message public id') is 0