@@ -14,7 +14,7 @@ import moment from '@nextcloud/moment'
1414import { generateUrl } from '@nextcloud/router'
1515import { showSuccess , showError } from '@nextcloud/dialogs'
1616import { 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
1919import Vue from 'vue'
2020import './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+ } )
0 commit comments