From 8b1cb08cf41bdf97bb549a98be8ab4d3de727def Mon Sep 17 00:00:00 2001 From: Julien Veyssier Date: Thu, 10 Nov 2022 15:24:58 +0100 Subject: [PATCH] refs #11 only send one message with optional comment + files Signed-off-by: Julien Veyssier --- lib/Controller/MattermostAPIController.php | 5 +- lib/Service/MattermostAPIService.php | 13 +-- src/filesplugin.js | 100 +++++++++++---------- 3 files changed, 62 insertions(+), 56 deletions(-) diff --git a/lib/Controller/MattermostAPIController.php b/lib/Controller/MattermostAPIController.php index 3226e57..1458cf2 100644 --- a/lib/Controller/MattermostAPIController.php +++ b/lib/Controller/MattermostAPIController.php @@ -158,11 +158,12 @@ public function getChannels() { * * @param string $message * @param string $channelId + * @param array|null $remoteFileIds * @return DataResponse * @throws Exception */ - public function sendMessage(string $message, string $channelId) { - $result = $this->mattermostAPIService->sendMessage($this->userId, $this->mattermostUrl, $message, $channelId); + public function sendMessage(string $message, string $channelId, ?array $remoteFileIds = null) { + $result = $this->mattermostAPIService->sendMessage($this->userId, $this->mattermostUrl, $message, $channelId, $remoteFileIds); if (isset($result['error'])) { return new DataResponse($result['error'], Http::STATUS_BAD_REQUEST); } else { diff --git a/lib/Service/MattermostAPIService.php b/lib/Service/MattermostAPIService.php index 33bc997..aabd091 100644 --- a/lib/Service/MattermostAPIService.php +++ b/lib/Service/MattermostAPIService.php @@ -405,14 +405,18 @@ public function getMyChannels(string $userId, string $mattermostUrl): array { * @param string $mattermostUrl * @param string $message * @param string $channelId + * @param array|null $remoteFileIds * @return array|string[] * @throws Exception */ - public function sendMessage(string $userId, string $mattermostUrl, string $message, string $channelId): array { + public function sendMessage(string $userId, string $mattermostUrl, string $message, string $channelId, ?array $remoteFileIds = null): array { $params = [ 'channel_id' => $channelId, 'message' => $message, ]; + if ($remoteFileIds !== null) { + $params['file_ids'] = $remoteFileIds; + } return $this->request($userId, $mattermostUrl, 'posts', $params, 'POST'); } @@ -523,12 +527,9 @@ public function sendFile(string $userId, string $mattermostUrl, int $fileId, str if (isset($sendResult['file_infos']) && is_array($sendResult['file_infos']) && count($sendResult['file_infos']) > 0) { $remoteFileId = $sendResult['file_infos'][0]['id'] ?? 0; - $params = [ - 'channel_id' => $channelId, - 'message' => '', - 'file_ids' => [$remoteFileId], + return [ + 'remote_file_id' => $remoteFileId, ]; - return $this->request($userId, $mattermostUrl, 'posts', $params, 'POST'); } else { return ['error' => 'File upload error']; } diff --git a/src/filesplugin.js b/src/filesplugin.js index badf0b8..0cc7eb4 100644 --- a/src/filesplugin.js +++ b/src/filesplugin.js @@ -255,28 +255,17 @@ function sendInternalLinks(channelId, channelName, comment) { }) } -function sendFileLoop(channelId, channelName, count = 0) { - if (OCA.Mattermost.filesToSend.length === 0) { - showSuccess( - n( - 'integration_mattermost', - '{count} file was sent to {channelName}', - '{count} files were sent to {channelName}', - count, - { - channelName, - count, - } - ) - ) - OCA.Mattermost.MattermostSendModalVue.success() - return - } - +function sendFileLoop(channelId, channelName, comment) { const file = OCA.Mattermost.filesToSend.shift() // skip directories if (file.type === 'dir') { - sendFileLoop(channelId, channelName, count) + if (OCA.Mattermost.filesToSend.length === 0) { + // we are done, no next file + sendMessageAfterFilesUpload(channelId, channelName, comment) + } else { + // skip, go to next + sendFileLoop(channelId, channelName, comment) + } return } OCA.Mattermost.MattermostSendModalVue.fileStarted(file.id) @@ -286,31 +275,21 @@ function sendFileLoop(channelId, channelName, count = 0) { } const url = generateUrl('apps/integration_mattermost/sendFile') axios.post(url, req).then((response) => { - // finished + OCA.Mattermost.remoteFileIds.push(response.data.remote_file_id) + OCA.Mattermost.sentFileNames.push(file.name) + OCA.Mattermost.MattermostSendModalVue.fileFinished(file.id) if (OCA.Mattermost.filesToSend.length === 0) { - showSuccess( - n( - 'integration_mattermost', - '{fileName} was sent to {channelName}', - '{count} files were sent to {channelName}', - count + 1, - { - fileName: file.name, - channelName, - count: count + 1, - } - ) - ) - OCA.Mattermost.MattermostSendModalVue.success() + // finished + sendMessageAfterFilesUpload(channelId, channelName, comment) } else { // not finished - OCA.Mattermost.MattermostSendModalVue.fileFinished(file.id) - sendFileLoop(channelId, channelName, count + 1) + sendFileLoop(channelId, channelName, comment) } }).catch((error) => { console.error(error) OCA.Mattermost.MattermostSendModalVue.failure() OCA.Mattermost.filesToSend = [] + OCA.Mattermost.sentFileNames = [] showError( t('integration_mattermost', 'Failed to send {name} to Mattermost', { name: file.name }) + ' ' + error.response?.request?.responseText @@ -318,10 +297,43 @@ function sendFileLoop(channelId, channelName, count = 0) { }) } -function sendMessage(channelId, message) { +function sendMessageAfterFilesUpload(channelId, channelName, comment) { + const count = OCA.Mattermost.sentFileNames.length + const lastFileName = count === 0 ? t('integration_mattermost', 'Nothing') : OCA.Mattermost.sentFileNames[count - 1] + sendMessage(channelId, comment, OCA.Mattermost.remoteFileIds).then((response) => { + showSuccess( + n( + 'integration_mattermost', + '{fileName} was sent to {channelName}', + '{count} files were sent to {channelName}', + count, + { + fileName: lastFileName, + channelName, + count, + } + ) + ) + OCA.Mattermost.MattermostSendModalVue.success() + }).catch((error) => { + console.error(error) + OCA.Mattermost.MattermostSendModalVue.failure() + showError( + t('integration_mattermost', 'Failed to send files to Mattermost') + + ': ' + error.response?.request?.responseText + ) + }).then(() => { + OCA.Mattermost.filesToSend = [] + OCA.Mattermost.remoteFileIds = [] + OCA.Mattermost.sentFileNames = [] + }) +} + +function sendMessage(channelId, message, remoteFileIds = undefined) { const req = { message, channelId, + remoteFileIds, } const url = generateUrl('apps/integration_mattermost/sendMessage') return axios.post(url, req) @@ -346,17 +358,9 @@ OCA.Mattermost.MattermostSendModalVue.$on('validate', ({ filesToSend, channelId, } else if (type === SEND_TYPE.internal_link.id) { sendInternalLinks(channelId, channelName, comment) } else { - sendMessage(channelId, comment).then((response) => { - sendFileLoop(channelId, channelName) - }).catch((error) => { - console.error(error) - OCA.Mattermost.MattermostSendModalVue.failure() - OCA.Mattermost.filesToSend = [] - showError( - t('integration_mattermost', 'Failed to send files to Mattermost') - + ': ' + error.response?.request?.responseText - ) - }) + OCA.Mattermost.remoteFileIds = [] + OCA.Mattermost.sentFileNames = [] + sendFileLoop(channelId, channelName, comment) } })