diff options
author | Vincent Petry <vincent@nextcloud.com> | 2021-04-28 19:19:48 +0300 |
---|---|---|
committer | Vincent Petry <vincent@nextcloud.com> | 2021-04-29 10:47:21 +0300 |
commit | 2d31f14d68f97e86ed49399934de15456bc1f8a6 (patch) | |
tree | 412a52c5d37468356293c359006d13235c60bc77 | |
parent | b9632cf7a380e5d86391b780bcfb89f11f8ff51e (diff) |
More createTemporaryMessage to a store action
Since createTemporaryMessage heavily depends on a store, moved it into a
messagesStore action to decouple it from the global store module.
Note: even though it's synchronous, the dispatch() function returns a
promise so we need to treat it as if it was async.
Signed-off-by: Vincent Petry <vincent@nextcloud.com>
-rw-r--r-- | src/components/NewMessageForm/NewMessageForm.vue | 5 | ||||
-rw-r--r-- | src/components/UploadEditor.vue | 4 | ||||
-rw-r--r-- | src/store/fileUploadStore.js | 25 | ||||
-rw-r--r-- | src/store/messagesStore.js | 60 | ||||
-rw-r--r-- | src/utils/temporaryMessage.js | 72 |
5 files changed, 77 insertions, 89 deletions
diff --git a/src/components/NewMessageForm/NewMessageForm.vue b/src/components/NewMessageForm/NewMessageForm.vue index dad481a64..6aee534ac 100644 --- a/src/components/NewMessageForm/NewMessageForm.vue +++ b/src/components/NewMessageForm/NewMessageForm.vue @@ -126,7 +126,6 @@ import EmojiPicker from '@nextcloud/vue/dist/Components/EmojiPicker' import { EventBus } from '../../services/EventBus' import { shareFile } from '../../services/filesSharingServices' import { CONVERSATION } from '../../constants' -import createTemporaryMessage from '../../utils/temporaryMessage' import EmoticonOutline from 'vue-material-design-icons/EmoticonOutline' import Send from 'vue-material-design-icons/Send' import CancelableRequest from '../../utils/cancelableRequest' @@ -315,7 +314,7 @@ export default { */ async handleSubmit() { if (this.parsedText !== '') { - const temporaryMessage = createTemporaryMessage(this.parsedText, this.token) + const temporaryMessage = await this.$store.dispatch('createTemporaryMessage', { text: this.parsedText, token: this.token }) this.$store.dispatch('addTemporaryMessage', temporaryMessage) this.text = '' this.parsedText = '' @@ -456,7 +455,7 @@ export default { // Create a unique id for the upload operation const uploadId = new Date().getTime() // Uploads and shares the files - this.$store.dispatch('initialiseUpload', { files, token: this.token, uploadId, rename }) + await this.$store.dispatch('initialiseUpload', { files, token: this.token, uploadId, rename }) }, /** diff --git a/src/components/UploadEditor.vue b/src/components/UploadEditor.vue index 5a95d02eb..cc16ff733 100644 --- a/src/components/UploadEditor.vue +++ b/src/components/UploadEditor.vue @@ -135,9 +135,9 @@ export default { this.$refs.fileUploadInput.click() }, - handleFileInput(event) { + async handleFileInput(event) { const files = Object.values(event.target.files) - this.$store.dispatch('initialiseUpload', { files, token: this.token, uploadId: this.currentUploadId }) + await this.$store.dispatch('initialiseUpload', { files, token: this.token, uploadId: this.currentUploadId }) }, handleRemoveFileFromSelection(id) { diff --git a/src/store/fileUploadStore.js b/src/store/fileUploadStore.js index 315419598..d0bd535c9 100644 --- a/src/store/fileUploadStore.js +++ b/src/store/fileUploadStore.js @@ -26,7 +26,6 @@ import { showError } from '@nextcloud/dialogs' import fromStateOr from './helper' import { findUniquePath, getFileExtension } from '../utils/fileUpload' import moment from '@nextcloud/moment' -import createTemporaryMessage from '../utils/temporaryMessage' import { EventBus } from '../services/EventBus' import { shareFile } from '../services/filesSharingServices' import { setAttachmentFolder } from '../services/settingsService' @@ -195,19 +194,19 @@ const actions = { * @param {number} uploadId a unique id for the upload operation indexing * @param {bool} rename whether to rename the files (usually after pasting) */ - initialiseUpload({ commit, dispatch }, { uploadId, token, files, rename = false }) { - if (rename) { - files.forEach(file => { + async initialiseUpload({ commit, dispatch }, { uploadId, token, files, rename = false }) { + // Set last upload id + commit('setCurrentUploadId', uploadId) + + for (let i = 0; i < files.length; i++) { + const file = files[i] + + if (rename) { // note: can't overwrite the original read-only name attribute file.newName = moment(file.lastModified || file.lastModifiedDate).format('YYYYMMDD_HHmmss') + getFileExtension(file.name) - }) - } - - // Set last upload id - commit('setCurrentUploadId', uploadId) + } - files.forEach(file => { // Get localurl for some image previews let localUrl = '' if (file.type === 'image/png' || file.type === 'image/gif' || file.type === 'image/jpeg') { @@ -219,10 +218,12 @@ const actions = { const date = new Date() const index = 'temp_' + date.getTime() + Math.random() // Create temporary message for the file and add it to the message list - const temporaryMessage = createTemporaryMessage('{file}', token, uploadId, index, file, localUrl) + const temporaryMessage = await dispatch('createTemporaryMessage', { + text: '{file}', token, uploadId, index, file, localUrl, + }) console.debug('temporarymessage: ', temporaryMessage, 'uploadId', uploadId) commit('addFileToBeUploaded', { file, temporaryMessage }) - }) + } }, /** diff --git a/src/store/messagesStore.js b/src/store/messagesStore.js index 8d49c6dc5..c7818d573 100644 --- a/src/store/messagesStore.js +++ b/src/store/messagesStore.js @@ -24,6 +24,8 @@ import { deleteMessage, updateLastReadMessage, } from '../services/messagesService' +import SHA1 from 'crypto-js/sha1' +import Hex from 'crypto-js/enc-hex' const state = { /** @@ -287,6 +289,64 @@ const actions = { }, /** + * Creates a temporary message ready to be posted, based + * on the message to be replied and the current actor + * + * @param {object} context default store context; + * @param {string} text message string; + * @param {string} token conversation token; + * @param {string} uploadId upload id; + * @param {int} index index; + * @param {object} file file to upload; + * @param {string} localUrl local URL of file to upload; + * @returns {object} temporary message + */ + createTemporaryMessage(context, { text, token, uploadId, index, file, localUrl }) { + const messageToBeReplied = context.getters.getMessageToBeReplied(token) + const date = new Date() + let tempId = 'temp-' + date.getTime() + const messageParameters = {} + if (file) { + tempId += '-' + uploadId + '-' + Math.random() + messageParameters.file = { + type: 'file', + file: file, + mimetype: file.type, + id: tempId, + name: file.newName || file.name, + // index, will be the id from now on + uploadId, + localUrl, + index, + } + } + const message = Object.assign({}, { + id: tempId, + actorId: context.getters.getActorId(), + actorType: context.getters.getActorType(), + actorDisplayName: context.getters.getDisplayName(), + timestamp: 0, + systemMessage: '', + messageType: '', + message: text, + messageParameters, + token, + isReplyable: false, + sendingFailure: '', + referenceId: Hex.stringify(SHA1(tempId)), + }) + + /** + * If the current message is a quote-reply message, add the parent key to the + * temporary message object. + */ + if (messageToBeReplied) { + message.parent = messageToBeReplied.id + } + return message + }, + + /** * Add a temporary message generated in the client to * the store, these messages are deleted once the full * message object is received from the server. diff --git a/src/utils/temporaryMessage.js b/src/utils/temporaryMessage.js deleted file mode 100644 index 07f7fe1cd..000000000 --- a/src/utils/temporaryMessage.js +++ /dev/null @@ -1,72 +0,0 @@ -/** - * @copyright Copyright (c) 2020 Marco Ambrosini <marcoambrosini@pm.me> - * - * @author Marco Ambrosini <marcoambrosini@pm.me> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -import store from '../store/index' -import SHA1 from 'crypto-js/sha1' -import Hex from 'crypto-js/enc-hex' - -const createTemporaryMessage = (text, token, uploadId, index, file, localUrl) => { - const messageToBeReplied = store.getters.getMessageToBeReplied(token) - const date = new Date() - let tempId = 'temp-' + date.getTime() - const messageParameters = {} - if (file) { - tempId += '-' + uploadId + '-' + Math.random() - messageParameters.file = { - 'type': 'file', - 'file': file, - 'mimetype': file.type, - 'id': tempId, - 'name': file.newName || file.name, - // index, will be the id from now on - uploadId, - localUrl, - index, - } - } - const message = Object.assign({}, { - id: tempId, - actorId: store.getters.getActorId(), - actorType: store.getters.getActorType(), - actorDisplayName: store.getters.getDisplayName(), - timestamp: 0, - systemMessage: '', - messageType: '', - message: text, - messageParameters, - token: token, - isReplyable: false, - sendingFailure: '', - referenceId: Hex.stringify(SHA1(tempId)), - }) - - /** - * If the current message is a quote-reply message, add the parent key to the - * temporary message object. - */ - if (messageToBeReplied) { - message.parent = messageToBeReplied.id - } - return message -} - -export default createTemporaryMessage |