From f1cd1056cc9bb3703b0030389c4522e06c3e6402 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Fri, 19 Feb 2016 18:54:20 -0800 Subject: [PATCH] fix(attachments): fixes + specs for download `on-receive` option --- spec/stores/file-download-store-spec.coffee | 18 ++++++++++++++++++ src/flux/stores/file-download-store.coffee | 10 +++++----- src/flux/stores/message-store.coffee | 4 +++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/spec/stores/file-download-store-spec.coffee b/spec/stores/file-download-store-spec.coffee index d09540792..1f670b7a0 100644 --- a/spec/stores/file-download-store-spec.coffee +++ b/spec/stores/file-download-store-spec.coffee @@ -3,6 +3,7 @@ path = require 'path' {shell} = require 'electron' NylasAPI = require '../../src/flux/nylas-api' File = require '../../src/flux/models/file' +Message = require '../../src/flux/models/message' FileDownloadStore = require '../../src/flux/stores/file-download-store' AccountStore = require '../../src/flux/stores/account-store' @@ -112,6 +113,23 @@ describe "FileDownloadStore", -> FileDownloadStore._checkForDownloadedFile(f).then (downloaded) -> expect(downloaded).toBe(false) + describe "_newMailReceived", -> + it "should fetch attachments if the setting is on-receive", -> + spyOn(FileDownloadStore, '_fetch') + spyOn(NylasEnv.config, 'get').andCallFake (key) -> + return 'on-receive' if key is 'core.attachments.downloadPolicy' + return null + FileDownloadStore._newMailReceived(message: [new Message(files: [new File()])]) + expect(FileDownloadStore._fetch).toHaveBeenCalled() + + it "should not fetch attachments otherwise", -> + spyOn(FileDownloadStore, '_fetch') + spyOn(NylasEnv.config, 'get').andCallFake (key) -> + return 'on-read' if key is 'core.attachments.downloadPolicy' + return null + FileDownloadStore._newMailReceived(message: [new Message(files: [new File()])]) + expect(FileDownloadStore._fetch).not.toHaveBeenCalled() + describe "_runDownload", -> beforeEach -> spyOn(FileDownloadStore.Download.prototype, 'run').andCallFake -> Promise.resolve(@) diff --git a/src/flux/stores/file-download-store.coffee b/src/flux/stores/file-download-store.coffee index 6c216a394..3337114a5 100644 --- a/src/flux/stores/file-download-store.coffee +++ b/src/flux/stores/file-download-store.coffee @@ -140,11 +140,11 @@ FileDownloadStore = Reflux.createStore ########### PRIVATE #################################################### - _newMailReceived: (incoming) => - return unless NylasEnv.config.get('core.attachments.downloadPolicy') is 'on-receive' - for message in incoming - for file in message.files - @_fetch(file) + _newMailReceived: (incoming) -> + if NylasEnv.config.get('core.attachments.downloadPolicy') is 'on-receive' + for message in incoming['message'] + for file in message.files + @_fetch(file) # Returns a promise with a Download object, allowing other actions to be # daisy-chained to the end of the download operation. diff --git a/src/flux/stores/message-store.coffee b/src/flux/stores/message-store.coffee index 7031025be..b6a1742ef 100644 --- a/src/flux/stores/message-store.coffee +++ b/src/flux/stores/message-store.coffee @@ -259,7 +259,9 @@ class MessageStore extends NylasStore startedAFetch _fetchExpandedAttachments: (items) -> - return unless NylasEnv.config.get('core.attachments.downloadPolicy') is 'on-read' + policy = NylasEnv.config.get('core.attachments.downloadPolicy') + return if policy is 'manually' + for item in items continue unless @_itemsExpanded[item.id] for file in item.files