From 652bfe00f4e78667d2ef9bdeda560b3e79dc909d Mon Sep 17 00:00:00 2001 From: Marco Ambrosini Date: Wed, 5 Aug 2020 15:48:47 +0200 Subject: [PATCH] Move share logic to the uploadfiles method in the store Signed-off-by: Marco Ambrosini --- src/store/fileUploadStore.js | 36 +++++++++++++++++++++++++++++++----- src/utils/fileUpload.js | 16 ---------------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/store/fileUploadStore.js b/src/store/fileUploadStore.js index 4a42e07a5c92..e8d3ed97ecc3 100644 --- a/src/store/fileUploadStore.js +++ b/src/store/fileUploadStore.js @@ -27,6 +27,7 @@ import { loadState } from '@nextcloud/initial-state' import { findUniquePath } from '../utils/fileUpload' import createTemporaryMessage from '../utils/temporaryMessage' import { EventBus } from '../services/EventBus' +import { shareFile } from '../services/filesSharingServices' const state = { attachmentFolder: loadState('talk', 'attachment_folder'), @@ -108,7 +109,12 @@ const mutations = { files: {}, }) } - Vue.set(state.uploads[uploadId].files, Object.keys(state.uploads[uploadId].files).length, { file, status: 'toBeUploaded', totalSize: file.size, uploadedSize: 0 }) + Vue.set(state.uploads[uploadId].files, Object.keys(state.uploads[uploadId].files).length, { + file, + status: 'toBeUploaded', + totalSize: file.size, + uploadedSize: 0, + }) }, // Marks a given file as failed upload @@ -217,17 +223,20 @@ const actions = { */ async uploadFiles({ commit, dispatch, state, getters }, uploadId) { - // Iterate through the previously indexed files for a given conversation (token) + // Tag the previously indexed files and add the temporary messages to the + // messages list for (const index in state.uploads[uploadId].files) { - // Mark file as uploading to prevent a second function call to start a - // second upload for the same file - commit('markFileAsInitialised', { uploadId, index }) + // mark all files as uploading + commit('markFileAsUploading', { uploadId, index }) // Store the previously created temporary message const temporaryMessage = state.uploads[uploadId].files[index].temporaryMessage // Add temporary messages (files) to the messages list dispatch('addTemporaryMessage', temporaryMessage) // Scroll the message list EventBus.$emit('scrollChatToBottom') + } + // Iterate again and perform the uploads + for (const index in state.uploads[uploadId].files) { // currentFile to be uploaded const currentFile = state.uploads[uploadId].files[index].file // userRoot path @@ -252,6 +261,23 @@ const actions = { // Mark the upload as failed in the store commit('markFileAsFailedUpload', { uploadId, index }) } + + // Get the files that have successfully been uploaded from the store + const shareableFiles = getters.getShareableFiles(uploadId) + // Share each of those files to the conversation + for (const index in shareableFiles) { + console.debug('i run') + const path = shareableFiles[index].sharePath + try { + const temporaryMessage = shareableFiles[index].temporaryMessage + const token = temporaryMessage.token + dispatch('markFileAsSharing', { uploadId, index }) + await shareFile(path, token, temporaryMessage.referenceId) + dispatch('markFileAsShared', { uploadId, index }) + } catch (exception) { + console.debug('An error happened when triying to share your file: ', exception) + } + } } }, /** diff --git a/src/utils/fileUpload.js b/src/utils/fileUpload.js index 4fc5e5c33a99..2ad6128e4157 100644 --- a/src/utils/fileUpload.js +++ b/src/utils/fileUpload.js @@ -29,7 +29,6 @@ * @returns {string} The unique path */ -import { shareFile } from '../services/filesSharingServices' import store from '../store/index' const findUniquePath = async function(client, userRoot, path) { @@ -70,22 +69,7 @@ const findUniquePath = async function(client, userRoot, path) { const processFiles = async function(files, token, uploadId) { // Process these files in the store await store.dispatch('initialiseUpload', { uploadId, token, files }) - // Get the files that have successfully been uploaded from the store - const shareableFiles = store.getters.getShareableFiles(uploadId) - // Share each of those files in the conversation - for (const index in shareableFiles) { - const path = shareableFiles[index].sharePath - try { - const temporaryMessage = shareableFiles[index].temporaryMessage - - store.dispatch('markFileAsSharing', { uploadId, index }) - await shareFile(path, token, temporaryMessage.referenceId) - store.dispatch('markFileAsShared', { uploadId, index }) - } catch (exception) { - console.debug('An error happened when triying to share your file: ', exception) - } - } } export {