Mailspring/src/flux/tasks/destroy-draft.coffee
Ben Gotow e1fc34a562 fix(drafts): Draft syncing completely disabled to reduce code complexity
Summary:
fix(streaming): Reconnect every 30 seconds, always

Never accept drafts via any API source

fix(attachments): Fix for changes to open API

Get rid of shouldAbort, just let tasks decide what to do in cleanup

Never let SaveDraftTask run while another SaveDraftTask for same draft is running

Never used IPC

Ignore destroy draft 404

Moving draft store proxy to draft store level

Only block on older saves

Replace SaveDraftTask with SyncbackDraftTask, do saving directly from proxy

Never sync back ;-)

Fix specs

Alter SendDraftTask so that it can send an unsaved draft

Test Plan: Run tests

Reviewers: evan

Reviewed By: evan

Differential Revision: https://review.inboxapp.com/D1245
2015-03-02 11:23:35 -08:00

67 lines
2.3 KiB
CoffeeScript

Task = require './task'
Message = require '../models/message'
DatabaseStore = require '../stores/database-store'
Actions = require '../actions'
SyncbackDraftTask = require './syncback-draft'
SendDraftTask = require './send-draft'
FileUploadTask = require './file-upload-task'
module.exports =
class DestroyDraftTask extends Task
constructor: (@draftLocalId) -> super
shouldDequeueOtherTask: (other) ->
(other instanceof SyncbackDraftTask and other.draftLocalId is @draftLocalId) or
(other instanceof SendDraftTask and other.draftLocalId is @draftLocalId) or
(other instanceof FileUploadTask and other.draftLocalId is @draftLocalId)
shouldWaitForTask: (other) ->
(other instanceof SyncbackDraftTask and other.draftLocalId is @draftLocalId)
performLocal: ->
new Promise (resolve, reject) =>
unless @draftLocalId?
return reject(new Error("Attempt to call DestroyDraftTask.performLocal without @draftLocalId"))
DatabaseStore.findByLocalId(Message, @draftLocalId).then (draft) =>
@draft = draft
DatabaseStore.unpersistModel(draft).then(resolve)
performRemote: ->
new Promise (resolve, reject) =>
# We don't need to do anything if we weren't able to find the draft
# when we performed locally, or if the draft has never been synced to
# the server (id is still self-assigned)
return resolve() unless @draft
return resolve() unless @draft.isSaved()
atom.inbox.makeRequest
path: "/n/#{@draft.namespaceId}/drafts/#{@draft.id}"
method: "DELETE"
body:
version: @draft.version
returnsModel: false
success: resolve
error: reject
onAPIError: (apiError) ->
inboxMsg = apiError.body?.message ? ""
if apiError.statusCode is 404
# Draft has already been deleted, this is not really an error
return true
else if inboxMsg.indexOf("is not a draft") >= 0
# Draft has been sent, and can't be deleted. Not much we can
# do but finish
return true
else
@_rollbackLocal()
onOtherError: -> Promise.resolve()
onTimeoutError: -> Promise.resolve()
onOfflineError: -> Promise.resolve()
_rollbackLocal: (msg) ->
msg ?= "Unable to delete this draft. Restoring..."
Actions.postNotification({message: msg, type: "error"})
DatabaseStore.persistModel(@draft) if @draft?