Skip to content

Commit 6281918

Browse files
author
Julien Veyssier
committed
fix files plugin loading
Signed-off-by: Julien Veyssier <[email protected]>
1 parent 9d29407 commit 6281918

File tree

4 files changed

+85
-57
lines changed

4 files changed

+85
-57
lines changed

appinfo/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
return [
1313
'routes' => [
1414
['name' => 'config#isUserConnected', 'url' => '/is-connected', 'verb' => 'GET'],
15+
['name' => 'config#getFilesToSend', 'url' => '/files-to-send', 'verb' => 'GET'],
1516
['name' => 'config#oauthRedirect', 'url' => '/oauth-redirect', 'verb' => 'GET'],
1617
['name' => 'config#setConfig', 'url' => '/config', 'verb' => 'PUT'],
1718
['name' => 'config#setWebhooksConfig', 'url' => '/webhooks', 'verb' => 'POST'],

lib/Controller/ConfigController.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,40 @@ public function isUserConnected(): DataResponse {
8585
$oauthPossible = $clientID !== '' && $clientSecret !== '' && $mattermostUrl === $adminOauthUrl;
8686
$usePopup = $this->config->getAppValue(Application::APP_ID, 'use_popup', '0');
8787

88-
$fileIdsToSendAfterOAuth = $this->config->getUserValue($this->userId, Application::APP_ID, 'file_ids_to_send_after_oauth');
89-
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'file_ids_to_send_after_oauth');
90-
$currentDirAfterOAuth = $this->config->getUserValue($this->userId, Application::APP_ID, 'current_dir_after_oauth');
91-
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'current_dir_after_oauth');
92-
9388
return new DataResponse([
9489
'connected' => $mattermostUrl && $token,
9590
'oauth_possible' => $oauthPossible,
9691
'use_popup' => ($usePopup === '1'),
9792
'url' => $mattermostUrl,
9893
'client_id' => $clientID,
99-
'file_ids_to_send_after_oauth' => $fileIdsToSendAfterOAuth,
100-
'current_dir_after_oauth' => $currentDirAfterOAuth,
10194
]);
10295
}
10396

97+
/**
98+
* @NoAdminRequired
99+
*
100+
* @return DataResponse
101+
*/
102+
public function getFilesToSend(): DataResponse {
103+
$adminOauthUrl = $this->config->getAppValue(Application::APP_ID, 'oauth_instance_url');
104+
$mattermostUrl = $this->config->getUserValue($this->userId, Application::APP_ID, 'url', $adminOauthUrl) ?: $adminOauthUrl;
105+
$token = $this->config->getUserValue($this->userId, Application::APP_ID, 'token');
106+
$isConnected = $mattermostUrl && $token;
107+
108+
if ($isConnected) {
109+
$fileIdsToSendAfterOAuth = $this->config->getUserValue($this->userId, Application::APP_ID, 'file_ids_to_send_after_oauth');
110+
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'file_ids_to_send_after_oauth');
111+
$currentDirAfterOAuth = $this->config->getUserValue($this->userId, Application::APP_ID, 'current_dir_after_oauth');
112+
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'current_dir_after_oauth');
113+
114+
return new DataResponse([
115+
'file_ids_to_send_after_oauth' => $fileIdsToSendAfterOAuth,
116+
'current_dir_after_oauth' => $currentDirAfterOAuth,
117+
]);
118+
}
119+
return new DataResponse(['message' => 'Not connected']);
120+
}
121+
104122
/**
105123
* set config values
106124
* @NoAdminRequired

src/filesplugin.js

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import moment from '@nextcloud/moment'
1414
import { generateUrl } from '@nextcloud/router'
1515
import { showSuccess, showError } from '@nextcloud/dialogs'
1616
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
17-
import { oauthConnect, oauthConnectConfirmDialog } from './utils.js'
17+
import { oauthConnect, oauthConnectConfirmDialog, gotoSettingsConfirmDialog } from './utils.js'
1818

1919
import Vue from 'vue'
2020
import './bootstrap.js'
@@ -54,23 +54,13 @@ function openChannelSelector(files) {
5454
return
5555
}
5656

57-
if (DEBUG) console.debug('[Mattermost] before sendFileIdsAfterOAuth')
58-
this.sendFileIdsAfterOAuth(fileList)
57+
if (DEBUG) console.debug('[Mattermost] before checkIfFilesToSend')
58+
this.checkIfFilesToSend(fileList)
5959

6060
fileList.registerMultiSelectFileAction({
6161
name: 'mattermostSendMulti',
62-
displayName: (context) => {
63-
if (DEBUG) console.debug('[Mattermost] in registerMultiSelectFileAction->displayName: OCA.Mattermost.oauthPossible', OCA.Mattermost.oauthPossible)
64-
if (OCA.Mattermost.mattermostConnected || OCA.Mattermost.oauthPossible) {
65-
return t('integration_mattermost', 'Send files to Mattermost')
66-
}
67-
return ''
68-
},
69-
iconClass: () => {
70-
if (OCA.Mattermost.mattermostConnected || OCA.Mattermost.oauthPossible) {
71-
return 'icon-mattermost'
72-
}
73-
},
62+
displayName: t('integration_mattermost', 'Send files to Mattermost'),
63+
iconClass: 'icon-mattermost',
7464
order: -2,
7565
action: (selectedFiles) => {
7666
const filesToSend = selectedFiles.map((f) => {
@@ -85,42 +75,18 @@ function openChannelSelector(files) {
8575
openChannelSelector(filesToSend)
8676
} else if (OCA.Mattermost.oauthPossible) {
8777
this.connectToMattermost(filesToSend)
78+
} else {
79+
gotoSettingsConfirmDialog()
8880
}
8981
},
9082
})
9183

92-
/*
93-
// when the multiselect menu is opened =>
94-
// only show 'send to mattermost' if at least one selected item is a file
95-
fileList.$el.find('.actions-selected').click(() => {
96-
if (OCA.Mattermost.mattermostConnected) {
97-
let showSendMultiple = false
98-
for (const fid in fileList._selectedFiles) {
99-
const file = fileList.files.find((t) => parseInt(fid) === t.id)
100-
if (file.type !== 'dir') {
101-
showSendMultiple = true
102-
}
103-
}
104-
fileList.fileMultiSelectMenu.toggleItemVisibility('mattermostSendMulti', showSendMultiple)
105-
}
106-
})
107-
*/
108-
10984
fileList.fileActions.registerAction({
11085
name: 'mattermostSendSingle',
111-
displayName: (context) => {
112-
if (OCA.Mattermost.mattermostConnected || OCA.Mattermost.oauthPossible) {
113-
return t('integration_mattermost', 'Send to Mattermost')
114-
}
115-
return ''
116-
},
86+
displayName: t('integration_mattermost', 'Send to Mattermost'),
87+
iconClass: 'icon-mattermost',
11788
mime: 'all',
11889
order: -139,
119-
iconClass: (fileName, context) => {
120-
if (OCA.Mattermost.mattermostConnected || OCA.Mattermost.oauthPossible) {
121-
return 'icon-mattermost'
122-
}
123-
},
12490
permissions: OC.PERMISSION_READ,
12591
actionHandler: (fileName, context) => {
12692
const filesToSend = [
@@ -135,23 +101,40 @@ function openChannelSelector(files) {
135101
openChannelSelector(filesToSend)
136102
} else if (OCA.Mattermost.oauthPossible) {
137103
this.connectToMattermost(filesToSend)
104+
} else {
105+
gotoSettingsConfirmDialog()
138106
}
139107
},
140108
})
141109
},
142110

111+
checkIfFilesToSend(fileList) {
112+
const urlCheckConnection = generateUrl('/apps/integration_mattermost/files-to-send')
113+
axios.get(urlCheckConnection).then((response) => {
114+
const fileIdsStr = response.data.file_ids_to_send_after_oauth
115+
const currentDir = response.data.current_dir_after_oauth
116+
if (fileIdsStr && currentDir) {
117+
this.sendFileIdsAfterOAuth(fileList, fileIdsStr, currentDir)
118+
} else {
119+
if (DEBUG) console.debug('[Mattermost] nothing to send')
120+
}
121+
}).catch((error) => {
122+
console.error(error)
123+
})
124+
},
125+
143126
/**
144127
* In case we successfully connected with oauth and got redirected back to files
145128
* actually go on with the files that were previously selected
146129
*
147130
* @param {object} fileList the one from attach()
131+
* @param {string} fileIdsStr list of files to send
132+
* @param {string} currentDir path to the current dir
148133
*/
149-
sendFileIdsAfterOAuth: (fileList) => {
150-
const fileIdsStr = OCA.Mattermost.fileIdsToSendAfterOAuth
151-
if (DEBUG) console.debug('[Mattermost] in sendFileIdsAfterOAuth, fileIdsStr', fileIdsStr)
134+
sendFileIdsAfterOAuth: (fileList, fileIdsStr, currentDir) => {
135+
if (DEBUG) console.debug('[Mattermost] in sendFileIdsAfterOAuth, fileIdsStr, currentDir', fileIdsStr, currentDir)
152136
// this is only true after an OAuth connection initated from a file action
153137
if (fileIdsStr) {
154-
const currentDir = OCA.Mattermost.currentDirAfterOAuth
155138
// trick to make sure the file list is loaded (didn't find an event or a good alternative)
156139
// force=true to make sure we get a promise
157140
fileList.changeDirectory(currentDir, true, true).then(() => {
@@ -350,10 +333,12 @@ axios.get(urlCheckConnection).then((response) => {
350333
OCA.Mattermost.usePopup = response.data.use_popup
351334
OCA.Mattermost.clientId = response.data.client_id
352335
OCA.Mattermost.mattermostUrl = response.data.url
353-
OCA.Mattermost.fileIdsToSendAfterOAuth = response.data.file_ids_to_send_after_oauth
354-
OCA.Mattermost.currentDirAfterOAuth = response.data.current_dir_after_oauth
355336
if (DEBUG) console.debug('[Mattermost] OCA.Mattermost', OCA.Mattermost)
356-
OC.Plugins.register('OCA.Files.FileList', OCA.Mattermost.FilesPlugin)
357337
}).catch((error) => {
358338
console.error(error)
359339
})
340+
341+
document.addEventListener('DOMContentLoaded', () => {
342+
if (DEBUG) console.debug('[Mattermost] before register files plugin')
343+
OC.Plugins.register('OCA.Files.FileList', OCA.Mattermost.FilesPlugin)
344+
})

src/utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,30 @@ export function oauthConnectConfirmDialog(mattermostUrl) {
9292
})
9393
}
9494

95+
export function gotoSettingsConfirmDialog() {
96+
const settingsLink = generateUrl('/settings/user/connected-accounts')
97+
OC.dialogs.message(
98+
t('integration_mattermost', 'You need to connect to a Mattermost server before using the Mattermost integration.')
99+
+ '<br><br>'
100+
+ t('integration_mattermost', 'Do you want to go to your "Connect accounts" personal settings?'),
101+
t('integration_mattermost', 'Connect to Mattermost'),
102+
'none',
103+
{
104+
type: OC.dialogs.YES_NO_BUTTONS,
105+
confirm: t('integration_mattermost', 'Go to settings'),
106+
confirmClasses: 'success',
107+
cancel: t('integration_mattermost', 'Cancel'),
108+
},
109+
(result) => {
110+
if (result) {
111+
window.location.replace(settingsLink)
112+
}
113+
},
114+
true,
115+
true,
116+
)
117+
}
118+
95119
export function humanFileSize(bytes, approx = false, si = false, dp = 1) {
96120
const thresh = si ? 1000 : 1024
97121

0 commit comments

Comments
 (0)