fix(draft): remove old local draft after sending

Fixes T3507
This commit is contained in:
Evan Morikawa 2015-09-06 18:54:25 -07:00
parent c7be2e7dc5
commit ccfe7ce36c
2 changed files with 21 additions and 7 deletions

View file

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

View file

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