Skip to content

Commit

Permalink
refs #11 only send one message with optional comment + files
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Nov 10, 2022
1 parent 20cb740 commit 8b1cb08
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 56 deletions.
5 changes: 3 additions & 2 deletions lib/Controller/MattermostAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 7 additions & 6 deletions lib/Service/MattermostAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down Expand Up @@ -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'];
}
Expand Down
100 changes: 52 additions & 48 deletions src/filesplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -286,42 +275,65 @@ 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
)
})
}

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)
Expand All @@ -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)
}
})

Expand Down

0 comments on commit 8b1cb08

Please sign in to comment.