From 02a3bb9c2baf43bf51d14ba7a4fdb81fa648fa98 Mon Sep 17 00:00:00 2001
From: Christoph Wurst <christoph@winzerhof-wurst.at>
Date: Mon, 15 Jan 2018 20:04:11 +0100
Subject: [PATCH] Fix saving attachments to cloud files

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
---
 js/controller/messagecontroller.js | 13 ++++-----
 js/service/attachmentservice.js    | 13 +++++----
 js/views/messageattachment.js      | 43 +++++++++++++++---------------
 js/views/messageattachments.js     | 13 +++++----
 js/views/messageview.js            |  2 +-
 5 files changed, 39 insertions(+), 45 deletions(-)

diff --git a/js/controller/messagecontroller.js b/js/controller/messagecontroller.js
index 837decdea..25da0483d 100644
--- a/js/controller/messagecontroller.js
+++ b/js/controller/messagecontroller.js
@@ -110,14 +110,12 @@ define(function(require) {
 	}
 
 	/**
-	 * @param {Account} account
-	 * @param {Folder} folder
-	 * @param {number} messageId
+	 * @param {Message} message
 	 * @param {number} attachmentId
 	 * @param {function} callback
 	 * @returns {Promise}
 	 */
-	function saveAttachmentToFiles(account, folder, messageId, attachmentId, callback) {
+	function saveAttachmentToFiles(message, attachmentId, callback) {
 		var saveAll = _.isUndefined(attachmentId);
 
 		return new Promise(function(resolve, reject) {
@@ -127,8 +125,7 @@ define(function(require) {
 					if (typeof callback === 'function') {
 						callback();
 					}
-					Radio.message.request('save:cloud', account,
-						folder, messageId, attachmentId, path).then(function() {
+					Radio.message.request('save:cloud', message, attachmentId, path).then(function() {
 						if (saveAll) {
 							Radio.ui.trigger('error:show', t('mail', 'Attachments saved to Files.'));
 						} else {
@@ -205,8 +202,8 @@ define(function(require) {
 	 * @param {function} callback
 	 * @returns {Promise}
 	 */
-	function saveAttachmentsToFiles(account, folder, messageId, callback) {
-		return saveAttachmentToFiles(account, folder, messageId, null, callback);
+	function saveAttachmentsToFiles(message, callback) {
+		return saveAttachmentToFiles(message, null, callback);
 	}
 
 	return {
diff --git a/js/service/attachmentservice.js b/js/service/attachmentservice.js
index 1dea81826..2c0967599 100644
--- a/js/service/attachmentservice.js
+++ b/js/service/attachmentservice.js
@@ -35,21 +35,20 @@ define(function(require) {
 	Radio.attachment.reply('upload:finished', uploadLocalAttachmentFinished);
 
 	/**
-	 * @param {Account} account
-	 * @param {Folder} folder
-	 * @param {number} messageId
+	 * @param {Message} message
 	 * @param {number} attachmentId
 	 * @param {string} path
 	 * @returns {Promise}
 	 */
-	function saveToFiles(account, folder, messageId, attachmentId, path) {
+	function saveToFiles(message, attachmentId, path) {
+		attachmentId = attachmentId || 0;
 		var url = OC.generateUrl(
 			'apps/mail/api/accounts/{accountId}/' +
 			'folders/{folderId}/messages/{messageId}/' +
 			'attachment/{attachmentId}', {
-				accountId: account.get('accountId'),
-				folderId: folder.get('id'),
-				messageId: messageId,
+				accountId: message.folder.account.get('accountId'),
+				folderId: message.folder.get('id'),
+				messageId: message.get('id'),
 				attachmentId: attachmentId
 			});
 
diff --git a/js/views/messageattachment.js b/js/views/messageattachment.js
index dd28a0779..e4693780b 100644
--- a/js/views/messageattachment.js
+++ b/js/views/messageattachment.js
@@ -32,7 +32,11 @@ define(function(require) {
 	 * @class MessageAttachmentView
 	 */
 	var MessageAttachmentView = Marionette.View.extend({
+
+		message: undefined,
+
 		template: MessageAttachmentTemplate,
+
 		ui: {
 			'downloadButton': '.attachment-download',
 			'saveToCloudButton': '.attachment-save-to-cloud',
@@ -44,7 +48,8 @@ define(function(require) {
 			'click @ui.saveToCloudButton': '_onSaveToCloud',
 			'click @ui.importCalendarEventButton': '_onImportCalendarEvent'
 		},
-		initialize: function() {
+		initialize: function(options) {
+			this.message = options.message;
 			this.listenTo(Radio.ui, 'document:click', this._closeImportPopover);
 		},
 		_onClick: function(e) {
@@ -63,32 +68,26 @@ define(function(require) {
 		},
 		_onSaveToCloud: function(e) {
 			e.preventDefault();
-			// TODO: 'message' should be a property of this attachment model
-			// TODO: 'folder' should be a property of the message model and so on
-			var account = require('state').currentAccount;
-			var folder = require('state').currentFolder;
-			var messageId = this.model.get('messageId');
-			var attachmentId = this.model.get('id');
 
 			var _this = this;
-			MessageController.saveAttachmentToFiles(account, folder, messageId, attachmentId, function() {
+			MessageController.saveAttachmentToFiles(this.message, this.model.get('id'), function() {
 				// Loading feedback
 				_this.getUI('saveToCloudButton').removeClass('icon-folder')
-				.addClass('icon-loading-small')
-				.prop('disabled', true);
+						.addClass('icon-loading-small')
+						.prop('disabled', true);
 			}).catch(console.error.bind(this)).then(function() {
 				// Remove loading feedback again
 				_this.getUI('saveToCloudButton').addClass('icon-folder')
-					.removeClass('icon-loading-small')
-					.prop('disabled', false);
+						.removeClass('icon-loading-small')
+						.prop('disabled', false);
 			});
 		},
 		_onImportCalendarEvent: function(e) {
 			e.preventDefault();
 
 			this.getUI('importCalendarEventButton')
-				.removeClass('icon-add')
-				.addClass('icon-loading-small');
+					.removeClass('icon-add')
+					.addClass('icon-loading-small');
 
 			var _this = this;
 			Radio.dav.request('calendars').then(function(calendars) {
@@ -104,15 +103,15 @@ define(function(require) {
 				}
 			}).catch(console.error.bind(this)).then(function() {
 				_this.getUI('importCalendarEventButton')
-					.removeClass('icon-loading-small')
-					.addClass('icon-add');
+						.removeClass('icon-loading-small')
+						.addClass('icon-add');
 			});
 		},
 		_uploadToCalendar: function(url) {
 			this._closeImportPopover();
 			this.getUI('importCalendarEventButton')
-				.removeClass('icon-add')
-				.addClass('icon-loading-small');
+					.removeClass('icon-add')
+					.addClass('icon-loading-small');
 
 			var downloadUrl = this.model.get('downloadUrl');
 			var _this = this;
@@ -122,13 +121,13 @@ define(function(require) {
 				});
 			}).then(function() {
 				_this.getUI('importCalendarEventButton')
-					.removeClass('icon-loading-small')
-					.addClass('icon-add');
+						.removeClass('icon-loading-small')
+						.addClass('icon-add');
 			}).catch(function() {
 				Radio.ui.trigger('error:show', t('mail', 'Error while downloading calendar event'));
 				_this.getUI('importCalendarEventButton')
-					.removeClass('icon-loading-small')
-					.addClass('icon-add');
+						.removeClass('icon-loading-small')
+						.addClass('icon-add');
 			});
 		},
 		_closeImportPopover: function(e) {
diff --git a/js/views/messageattachments.js b/js/views/messageattachments.js
index ed1825e2d..1dc2308cd 100644
--- a/js/views/messageattachments.js
+++ b/js/views/messageattachments.js
@@ -46,20 +46,19 @@ define(function(require) {
 		},
 		childView: AttachmentView,
 		childViewContainer: '.attachments',
+		childViewOptions: function() {
+			return {
+				message: this.message
+			};
+		},
 		initialize: function(options) {
 			this.message = options.message;
 		},
 		_onSaveAllToCloud: function(e) {
 			e.preventDefault();
 
-			// TODO: 'message' should be a property of this attachment model
-			// TODO: 'folder' should be a property of the message model and so on
-			var account = require('state').currentAccount;
-			var folder = require('state').currentFolder;
-			var messageId = this.message.get('id');
-
 			var _this = this;
-			MessageController.saveAttachmentsToFiles(account, folder, messageId, function() {
+			MessageController.saveAttachmentsToFiles(this.message, function() {
 				// Loading feedback
 				_this.getUI('saveAllToCloud').removeClass('icon-folder')
 				.addClass('icon-loading-small')
diff --git a/js/views/messageview.js b/js/views/messageview.js
index 26aea5745..89980dad0 100644
--- a/js/views/messageview.js
+++ b/js/views/messageview.js
@@ -135,7 +135,7 @@ define(function(require) {
 
 			this.showChildView('attachments', new MessageAttachmentsView({
 				collection: new Attachments(this.messageBody.get('attachments')),
-				message: this.model
+				message: this.message
 			}));
 
 			// setup reply composer view
-- 
GitLab