Skip to content

Commit

Permalink
Move share logic to the uploadfiles method in the store
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Ambrosini <marcoambrosini@pm.me>
  • Loading branch information
marcoambrosini committed Aug 5, 2020
1 parent 3568660 commit 652bfe0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
36 changes: 31 additions & 5 deletions src/store/fileUploadStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}
}
},
/**
Expand Down
16 changes: 0 additions & 16 deletions src/utils/fileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 652bfe0

Please sign in to comment.