diff --git a/spec-nylas/tasks/change-mail-task-spec.coffee b/spec-nylas/tasks/change-mail-task-spec.coffee index 2cb2009b3..2c45617f4 100644 --- a/spec-nylas/tasks/change-mail-task-spec.coffee +++ b/spec-nylas/tasks/change-mail-task-spec.coffee @@ -103,11 +103,13 @@ describe "ChangeMailTask", -> expect(@task._applyChanges).toHaveBeenCalledWith(@task.threads) expect(DatabaseStore.persistModels).toHaveBeenCalledWith([@threadAChanged]) - it "fetches messages on changed threads and appends them to the messages to update", -> - waitsForPromise => - @task._performLocalThreads().then => - expect(@task._applyChanges).toHaveBeenCalledWith(@task.threads) - expect(@task.messages).toEqual([@threadAMesage1, @threadAMesage2]) + describe "when _processesNestedMessages is overridden to return true", -> + it "fetches messages on changed threads and appends them to the messages to update", -> + waitsForPromise => + @task._processesNestedMessages = => true + @task._performLocalThreads().then => + expect(@task._applyChanges).toHaveBeenCalledWith(@task.threads) + expect(@task.messages).toEqual([@threadAMesage1, @threadAMesage2]) describe "_performLocalMessages", -> beforeEach -> diff --git a/src/flux/tasks/change-folder-task.coffee b/src/flux/tasks/change-folder-task.coffee index 42dcbd0ae..39bd6ffed 100644 --- a/src/flux/tasks/change-folder-task.coffee +++ b/src/flux/tasks/change-folder-task.coffee @@ -65,6 +65,9 @@ class ChangeFolderTask extends ChangeMailTask # The base class does the heavy lifting and calls _changesToModel return super + _processesNestedMessages: -> + false + _changesToModel: (model) -> if model instanceof Thread {folders: [@folder]} diff --git a/src/flux/tasks/change-labels-task.coffee b/src/flux/tasks/change-labels-task.coffee index 635543169..3093ef1b8 100644 --- a/src/flux/tasks/change-labels-task.coffee +++ b/src/flux/tasks/change-labels-task.coffee @@ -59,8 +59,9 @@ class ChangeLabelsTask extends ChangeMailTask # The base class does the heavy lifting and calls _changesToModel return super - # Returns a new set of {Label} objects that incoprates the existing, - # new, and removed labels. + _processesNestedMessages: -> + false + _changesToModel: (model) -> labelsToRemoveIds = _.pluck(@labelsToRemove, 'id') diff --git a/src/flux/tasks/change-mail-task.coffee b/src/flux/tasks/change-mail-task.coffee index 9636e813c..76c5ff774 100644 --- a/src/flux/tasks/change-mail-task.coffee +++ b/src/flux/tasks/change-mail-task.coffee @@ -18,12 +18,7 @@ NamespaceStore = require '../stores/namespace-store' # ChangeMailTask stores the previous values of all models it changes into @_restoreValues # and handles undo/redo. When undoing, it restores previous values and calls # `_requestBodyForModel` to make undo API requests. It does not call `_changesToModel`. -# -# Generally, you cannot provide both messages and threads at the same time. However, -# ChangeMailTask runs for provided `threads` first, so your subclass can populate -# @messages with the messages of those threads to make adjustments to them. -# API requests are only made for threads if threads are present. -# +## class ChangeMailTask extends Task constructor: ({@threads, thread, @messages, message} = {}) -> @@ -48,6 +43,17 @@ class ChangeMailTask extends Task _requestBodyForModel: (model) -> throw new Error("You must override this method.") + # Generally, you cannot provide both messages and threads at the same time. However, + # ChangeMailTask runs for provided threads first and then messages. Override + # and return true, and you will receive `_changesToModel` for messages in + # changed threads, and any changes you make will be written to the database + # and undone during undo. + # + # Note that API requests are only made for threads if threads are present. + # + _processesNestedMessages: -> + false + # Perform Local # Subclasses should override `performLocal` and call super once they've @@ -72,8 +78,11 @@ class ChangeMailTask extends Task changedIds = _.pluck(changed, 'id') DatabaseStore.persistModels(changed).then => - DatabaseStore.findAll(Message).where(Message.attributes.threadId.in(changedIds)).then (messages) => - @messages = [].concat(messages, @messages) + if @_processesNestedMessages() + DatabaseStore.findAll(Message).where(Message.attributes.threadId.in(changedIds)).then (messages) => + @messages = [].concat(messages, @messages) + Promise.resolve() + else Promise.resolve() _performLocalMessages: ->