Skip to content

Commit

Permalink
feat(upload): support sharing of multiple files at once [WIP]
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Jan 19, 2024
1 parent 6d6ed01 commit d8367a3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
33 changes: 32 additions & 1 deletion src/services/filesSharingServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { showError } from '@nextcloud/dialogs'
import { generateOcsUrl } from '@nextcloud/router'

/**
* Appends a file as a message to the messagelist.
* Appends a file as a message to the message list.
*
* @param {string} path The file path from the user's root directory
* @param {string} token The conversation's token
Expand Down Expand Up @@ -55,6 +55,36 @@ const shareFile = async function(path, token, referenceId, metadata) {
}
}

/**
* Appends a multiple files as a message to the message list.
*
* @param {string} token The conversation's token
* @param {Array<string>} shareIds a list of ids we're getting in shareFile() response
* @param {string} caption a text message attached to the files
* @param {string} actorDisplayName The display name of the actor
* @param {string} referenceId An optional reference id to recognize the message later
*/
const shareMultipleFiles = async function(token, shareIds, caption, actorDisplayName, referenceId) {
try {
return axios.post(generateOcsUrl('apps/spreed/api/v1/chat/{token}/share-files', { token }),
{
shareIds,
caption,
actorDisplayName,
referenceId,
})
} catch (error) {
// FIXME: errors should be handled by called instead
if (error?.response?.data?.ocs?.meta?.message) {
console.error('Error while sharing files: ' + error.response.data.ocs.meta.message)
showError(error.response.data.ocs.meta.message)
} else {
console.error('Error while sharing files: Unknown error')
showError(t('spreed', 'Error while sharing files'))
}
}
}

const getFileTemplates = async () => {
return await axios.get(generateOcsUrl('apps/files/api/v1/templates'))
}
Expand All @@ -77,6 +107,7 @@ const createNewFile = async function(filePath, templatePath, templateType) {

export {
shareFile,
shareMultipleFiles,
getFileTemplates,
createNewFile,
}
16 changes: 15 additions & 1 deletion src/services/filesSharingServices.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'

import { shareFile } from './filesSharingServices.js'
import { shareFile, shareMultipleFiles } from './filesSharingServices.js'

jest.mock('@nextcloud/axios', () => ({
post: jest.fn(),
Expand All @@ -26,4 +26,18 @@ describe('filesSharingServices', () => {
}
)
})

test('shareMultipleFiles calls the spreed API endpoint', () => {
shareMultipleFiles('XXTOKENXX', ['1', '2', '3'], 'text caption', 'Display name', 'long_hash_string')

expect(axios.post).toHaveBeenCalledWith(
generateOcsUrl('apps/spreed/api/v1/chat/XXTOKENXX/share-files'),
{
shareIds: ['1', '2', '3'],
caption: 'text caption',
actorDisplayName: 'Display name',
referenceId: 'long_hash_string',
}
)
})
})
14 changes: 14 additions & 0 deletions src/store/fileUploadStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { EventBus } from '../services/EventBus.js'
import {
getFileTemplates,
shareFile,
// shareMultipleFiles,
} from '../services/filesSharingServices.js'
import { setAttachmentFolder } from '../services/settingsService.js'
import { useChatExtrasStore } from '../stores/chatExtras.js'
Expand Down Expand Up @@ -310,6 +311,9 @@ const actions = {

EventBus.$emit('upload-start')

// Check for quantity of files to upload
// const isMultipleFilesUpload = getters.getUploadsArray(uploadId).length !== 1

// Tag previously indexed files and add temporary messages to the MessagesList
// If caption is provided, attach to the last temporary message
const lastIndex = getters.getInitialisedUploads(uploadId).at(-1).at(0)
Expand Down Expand Up @@ -395,13 +399,19 @@ const actions = {
if (temporaryMessage.parent) {
Object.assign(rawMetadata, { replyTo: temporaryMessage.parent.id })
}
// if (isMultipleFilesUpload) {
// Object.assign(rawMetadata, { noMessage: isMultipleFilesUpload })

Check failure on line 403 in src/store/fileUploadStore.js

View workflow job for this annotation

GitHub Actions / NPM lint

Unexpected tab character
// }
const metadata = JSON.stringify(rawMetadata)

const { token, id, referenceId } = temporaryMessage
try {
dispatch('markFileAsSharing', { uploadId, index })
await shareFile(path, token, referenceId, metadata)
dispatch('markFileAsShared', { uploadId, index })

// return share id of file returned from the server
// return response.data.ocs.data.id
} catch (error) {
if (error?.response?.status === 403) {
showError(t('spreed', 'You are not allowed to share files'))
Expand Down Expand Up @@ -446,6 +456,10 @@ const actions = {
await Promise.all(shares.map(share => performShare(share)))
}

// if (isMultipleFilesUpload) {
// await shareMultipleFiles(temporaryMessage.token, shareIds, caption, temporaryMessage.actorDisplayName, temporaryMessage.referenceId)

Check failure on line 460 in src/store/fileUploadStore.js

View workflow job for this annotation

GitHub Actions / NPM lint

Unexpected tab character
// }

EventBus.$emit('upload-finished')
},

Expand Down

0 comments on commit d8367a3

Please sign in to comment.