Skip to content

Commit

Permalink
fix: migrate fileSharingServices to .ts (refactor shareFile)
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Mar 7, 2025
1 parent 088ea35 commit d5cb31e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 41 deletions.
15 changes: 13 additions & 2 deletions src/components/NewMessage/NewMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -763,13 +763,24 @@ export default {
},

handleFileShare(nodes) {
nodes.forEach(({ path }) => {
nodes.forEach(async ({ path }) => {
console.debug(`path ${path} selected for sharing`)
if (!path.startsWith('/')) {
throw new Error(t('files', 'Invalid path selected'))
}
this.focusInput()
return shareFile(path, this.token)
try {
await shareFile({ path, shareWith: this.token })
} catch (error) {
console.error('Error while sharing file: ', error)
if (error?.response?.status === 403) {
showError(t('spreed', 'You are not allowed to share files'))
} else if (error?.response?.data?.ocs?.meta?.message) {
showError(error.response.data.ocs.meta.message)
} else {
showError(t('spreed', 'Error while sharing file'))
}
}
})
},

Expand Down
15 changes: 14 additions & 1 deletion src/components/NewMessage/NewMessageNewFileDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,20 @@ export default {
return
}

await shareFile(filePath, this.token, '', '')
try {
await shareFile({ path: filePath, shareWith: this.token })
} catch (error) {
console.error('Error while sharing file: ', error)
if (error?.response?.status === 403) {
showError(t('spreed', 'You are not allowed to share files'))
} else if (error?.response?.data?.ocs?.meta?.message) {
showError(error.response.data.ocs.meta.message)
this.newFileError = error.response.data.ocs.meta.message
} else {
showError(t('spreed', 'Error while sharing file'))
}
}

this.loading = false

this.openViewer(filePath, [fileData], fileData)
Expand Down
6 changes: 5 additions & 1 deletion src/services/filesSharingServices.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ describe('filesSharingServices', () => {
})

test('shareFile calls the sharing API endpoint', () => {
shareFile('path/to/file', 'XXTOKENXX', 'the-reference-id')
shareFile({
path: 'path/to/file',
shareWith: 'XXTOKENXX',
referenceId: 'the-reference-id',
})

expect(axios.post).toHaveBeenCalledWith(
generateOcsUrl('apps/files_sharing/api/v1/shares'),
Expand Down
46 changes: 16 additions & 30 deletions src/services/filesSharingServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import axios from '@nextcloud/axios'
import { showError } from '@nextcloud/dialogs'
import { t } from '@nextcloud/l10n'
import { generateOcsUrl } from '@nextcloud/router'

import { SHARE } from '../constants.ts'
import type {
createFileFromTemplateParams,
createFileFromTemplateResponse,
Expand All @@ -15,35 +15,21 @@ import type {
} from '../types/index.ts'

/**
* Appends a file as a message to the messagelist.
*
* @param {string} path The file path from the user's root directory
* @param {string} token The conversation's token
* e.g. `/myfile.txt`
* @param {string} referenceId An optional reference id to recognize the message later
* @param {string} metadata the metadata json encoded array
* Appends a file as a message to the messages list
* @param payload The function payload
* @param payload.path The file path from the user's root directory
* @param payload.shareWith The conversation's token
* @param payload.referenceId An optional reference id to recognize the message later
* @param payload.talkMetaData The metadata JSON-encoded object
*/
const shareFile = async function(path, token, referenceId, metadata): createFileShareResponse {
try {
return await axios.post(
generateOcsUrl('apps/files_sharing/api/v1/shares'),
{
shareType: 10, // OC.Share.SHARE_TYPE_ROOM,
path,
shareWith: token,
referenceId,
talkMetaData: metadata,
} as createFileShareParams)
} catch (error) {
// FIXME: errors should be handled by called instead
if (error?.response?.data?.ocs?.meta?.message) {
console.error('Error while sharing file: ' + error.response.data.ocs.meta.message)
showError(error.response.data.ocs.meta.message)
} else {
console.error('Error while sharing file: Unknown error')
showError(t('spreed', 'Error while sharing file'))
}
}
async function shareFile({ path, shareWith, referenceId, talkMetaData }: createFileShareParams): createFileShareResponse {
return axios.post(generateOcsUrl('apps/files_sharing/api/v1/shares'), {
shareType: SHARE.TYPE.ROOM,
path,
shareWith,
referenceId,
talkMetaData,
} as createFileShareParams)
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/store/fileUploadStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ const actions = {
const [index, shareableFile] = share
const { id, messageType, parent, referenceId } = shareableFile.temporaryMessage || {}

const metadata = JSON.stringify(Object.assign(
const talkMetaData = JSON.stringify(Object.assign(
messageType !== 'comment' ? { messageType } : {},
caption && index === lastIndex ? { caption } : {},
options?.silent ? { silent: options.silent } : {},
Expand All @@ -459,16 +459,23 @@ const actions = {

try {
context.dispatch('markFileAsSharing', { uploadId, index })
await shareFile(shareableFile.sharePath, token, referenceId, metadata)
await shareFile({
path: shareableFile.sharePath,
shareWith: token,
referenceId,
talkMetaData,
})
context.dispatch('markFileAsShared', { uploadId, index })
} catch (error) {
console.error('Error while sharing file: ', error)
if (error?.response?.status === 403) {
showError(t('spreed', 'You are not allowed to share files'))
} else if (error?.response?.data?.ocs?.meta?.message) {
showError(error.response.data.ocs.meta.message)
} else {
showError(t('spreed', 'An error happened when trying to share your file'))
showError(t('spreed', 'Error while sharing file'))
}
context.dispatch('markTemporaryMessageAsFailed', { token, id, uploadId, reason: 'failed-share' })
console.error('An error happened when trying to share your file: ', error)
}
}

Expand Down
21 changes: 18 additions & 3 deletions src/store/fileUploadStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ describe('fileUploadStore', () => {
expect(uploadMock).toHaveBeenCalledWith(uniqueFileName, file)

expect(shareFile).toHaveBeenCalledTimes(1)
expect(shareFile).toHaveBeenCalledWith(uniqueFileName, 'XXTOKENXX', referenceId, '{"caption":"text-caption","silent":true}')
expect(shareFile).toHaveBeenCalledWith({
path: uniqueFileName,
shareWith: 'XXTOKENXX',
referenceId,
talkMetaData: '{"caption":"text-caption","silent":true}',
})

expect(mockedActions.addTemporaryMessage).toHaveBeenCalledTimes(1)
expect(store.getters.currentUploadId).not.toBeDefined()
Expand Down Expand Up @@ -214,8 +219,18 @@ describe('fileUploadStore', () => {
const referenceIds = store.getters.getUploadsArray('upload-id1').map(entry => entry[1].temporaryMessage.referenceId)

expect(shareFile).toHaveBeenCalledTimes(2)
expect(shareFile).toHaveBeenNthCalledWith(1, '/Talk/' + files[0].name + 'uniq', 'XXTOKENXX', referenceIds[0], '{}')
expect(shareFile).toHaveBeenNthCalledWith(2, '/Talk/' + files[1].name + 'uniq', 'XXTOKENXX', referenceIds[1], '{"caption":"text-caption"}')
expect(shareFile).toHaveBeenNthCalledWith(1, {
path: '/Talk/' + files[0].name + 'uniq',
shareWith: 'XXTOKENXX',
referenceId: referenceIds[0],
talkMetaData: '{}',
})
expect(shareFile).toHaveBeenNthCalledWith(2, {
path: '/Talk/' + files[1].name + 'uniq',
shareWith: 'XXTOKENXX',
referenceId: referenceIds[1],
talkMetaData: '{"caption":"text-caption"}',
})

expect(mockedActions.addTemporaryMessage).toHaveBeenCalledTimes(2)
expect(store.getters.currentUploadId).not.toBeDefined()
Expand Down

0 comments on commit d5cb31e

Please sign in to comment.