-
Notifications
You must be signed in to change notification settings - Fork 4
/
filesplugin.js
344 lines (321 loc) · 9.96 KB
/
filesplugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* Copyright (c) 2022 Julien Veyssier <eneiluj@posteo.net>
*
* This file is licensed under the Affero General Public License version 3
* or later.
*
* See the COPYING-README file.
*
*/
import SendFilesModal from './components/SendFilesModal.vue'
import axios from '@nextcloud/axios'
import moment from '@nextcloud/moment'
import { generateUrl } from '@nextcloud/router'
import { showSuccess, showError } from '@nextcloud/dialogs'
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
import { oauthConnect, oauthConnectConfirmDialog, gotoSettingsConfirmDialog } from './utils.js'
import Vue from 'vue'
import './bootstrap.js'
const DEBUG = false
function openChannelSelector(files) {
OCA.Mattermost.filesToSend = files
const modalVue = OCA.Mattermost.MattermostSendModalVue
modalVue.updateChannels()
modalVue.setFiles([...files])
modalVue.showModal()
}
(function() {
if (!OCA.Mattermost) {
/**
* @namespace
*/
OCA.Mattermost = {
filesToSend: [],
}
}
/**
* @namespace
*/
OCA.Mattermost.FilesPlugin = {
ignoreLists: [
'trashbin',
'files.public',
],
attach(fileList) {
if (DEBUG) console.debug('[Mattermost] begin of attach')
if (this.ignoreLists.indexOf(fileList.id) >= 0) {
return
}
if (DEBUG) console.debug('[Mattermost] before checkIfFilesToSend')
this.checkIfFilesToSend(fileList)
fileList.registerMultiSelectFileAction({
name: 'mattermostSendMulti',
displayName: t('integration_mattermost', 'Send files to Mattermost'),
iconClass: 'icon-mattermost',
order: -2,
action: (selectedFiles) => {
const filesToSend = selectedFiles.map((f) => {
return {
id: f.id,
name: f.name,
type: f.type,
size: f.size,
}
})
if (OCA.Mattermost.mattermostConnected) {
openChannelSelector(filesToSend)
} else if (OCA.Mattermost.oauthPossible) {
this.connectToMattermost(filesToSend)
} else {
gotoSettingsConfirmDialog()
}
},
})
fileList.fileActions.registerAction({
name: 'mattermostSendSingle',
displayName: t('integration_mattermost', 'Send to Mattermost'),
iconClass: 'icon-mattermost',
mime: 'all',
order: -139,
permissions: OC.PERMISSION_READ,
actionHandler: (fileName, context) => {
const filesToSend = [
{
id: context.fileInfoModel.attributes.id,
name: context.fileInfoModel.attributes.name,
type: context.fileInfoModel.attributes.type,
size: context.fileInfoModel.attributes.size,
},
]
if (OCA.Mattermost.mattermostConnected) {
openChannelSelector(filesToSend)
} else if (OCA.Mattermost.oauthPossible) {
this.connectToMattermost(filesToSend)
} else {
gotoSettingsConfirmDialog()
}
},
})
},
checkIfFilesToSend(fileList) {
const urlCheckConnection = generateUrl('/apps/integration_mattermost/files-to-send')
axios.get(urlCheckConnection).then((response) => {
const fileIdsStr = response.data.file_ids_to_send_after_oauth
const currentDir = response.data.current_dir_after_oauth
if (fileIdsStr && currentDir) {
this.sendFileIdsAfterOAuth(fileList, fileIdsStr, currentDir)
} else {
if (DEBUG) console.debug('[Mattermost] nothing to send')
}
}).catch((error) => {
console.error(error)
})
},
/**
* In case we successfully connected with oauth and got redirected back to files
* actually go on with the files that were previously selected
*
* @param {object} fileList the one from attach()
* @param {string} fileIdsStr list of files to send
* @param {string} currentDir path to the current dir
*/
sendFileIdsAfterOAuth: (fileList, fileIdsStr, currentDir) => {
if (DEBUG) console.debug('[Mattermost] in sendFileIdsAfterOAuth, fileIdsStr, currentDir', fileIdsStr, currentDir)
// this is only true after an OAuth connection initated from a file action
if (fileIdsStr) {
// trick to make sure the file list is loaded (didn't find an event or a good alternative)
// force=true to make sure we get a promise
fileList.changeDirectory(currentDir, true, true).then(() => {
const fileIds = fileIdsStr.split(',')
const files = fileIds.map((fid) => {
const f = fileList.files.find((e) => e.id === parseInt(fid))
if (f) {
return {
id: f.id,
name: f.name,
type: f.type,
size: f.size,
}
}
return null
}).filter((e) => e !== null)
if (DEBUG) console.debug('[Mattermost] in sendFileIdsAfterOAuth, after changeDirectory, files:', files)
if (files.length) {
if (DEBUG) console.debug('[Mattermost] in sendFileIdsAfterOAuth, after changeDirectory, call openChannelSelector')
openChannelSelector(files)
}
})
}
},
connectToMattermost: (selectedFiles = []) => {
oauthConnectConfirmDialog(OCA.Mattermost.mattermostUrl).then((result) => {
if (result) {
if (OCA.Mattermost.usePopup) {
oauthConnect(OCA.Mattermost.mattermostUrl, OCA.Mattermost.clientId, null, true)
.then((data) => {
OCA.Mattermost.mattermostConnected = true
openChannelSelector(selectedFiles)
})
} else {
const selectedFilesIds = selectedFiles.map(f => f.id)
oauthConnect(
OCA.Mattermost.mattermostUrl,
OCA.Mattermost.clientId,
'files--' + OCA.Files.App.fileList._currentDirectory + '--' + selectedFilesIds.join(',')
)
}
}
})
},
}
})()
function sendLinks(channelId, channelName, comment, permission, expirationDate, password) {
const req = {
fileIds: OCA.Mattermost.filesToSend.map((f) => f.id),
channelId,
channelName,
comment,
permission,
expirationDate: expirationDate ? moment(expirationDate).format('YYYY-MM-DD') : undefined,
password,
}
const url = generateUrl('apps/integration_mattermost/sendLinks')
axios.post(url, req).then((response) => {
const number = OCA.Mattermost.filesToSend.length
showSuccess(
n(
'integration_mattermost',
'A link to {fileName} was sent to {channelName}',
'{number} links were sent to {channelName}',
number,
{
fileName: OCA.Mattermost.filesToSend[0].name,
channelName,
number,
}
)
)
OCA.Mattermost.MattermostSendModalVue.success()
}).catch((error) => {
console.error(error)
OCA.Mattermost.MattermostSendModalVue.failure()
OCA.Mattermost.filesToSend = []
showError(
t('integration_mattermost', 'Failed to send links to Mattermost')
+ ' ' + error.response?.request?.responseText
)
})
}
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
}
const file = OCA.Mattermost.filesToSend.shift()
// skip directories
if (file.type === 'dir') {
sendFileLoop(channelId, channelName, count)
return
}
OCA.Mattermost.MattermostSendModalVue.fileStarted(file.id)
const req = {
fileId: file.id,
channelId,
}
const url = generateUrl('apps/integration_mattermost/sendFile')
axios.post(url, req).then((response) => {
// finished
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()
} else {
// not finished
OCA.Mattermost.MattermostSendModalVue.fileFinished(file.id)
sendFileLoop(channelId, channelName, count + 1)
}
}).catch((error) => {
console.error(error)
OCA.Mattermost.MattermostSendModalVue.failure()
OCA.Mattermost.filesToSend = []
showError(
t('integration_mattermost', 'Failed to send {name} to Mattermost', { name: file.name })
+ ' ' + error.response?.request?.responseText
)
})
}
function sendMessage(channelId, message) {
const req = {
message,
channelId,
}
const url = generateUrl('apps/integration_mattermost/sendMessage')
return axios.post(url, req)
}
// send file modal
const modalId = 'mattermostSendModal'
const modalElement = document.createElement('div')
modalElement.id = modalId
document.body.append(modalElement)
const View = Vue.extend(SendFilesModal)
OCA.Mattermost.MattermostSendModalVue = new View().$mount(modalElement)
OCA.Mattermost.MattermostSendModalVue.$on('closed', () => {
if (DEBUG) console.debug('[Mattermost] modal closed')
})
OCA.Mattermost.MattermostSendModalVue.$on('validate', ({ filesToSend, channelId, channelName, type, comment, permission, expirationDate, password }) => {
OCA.Mattermost.filesToSend = filesToSend
if (type === 'link') {
sendLinks(channelId, channelName, comment, permission, expirationDate, password)
} 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
)
})
}
})
// get Mattermost state
const urlCheckConnection = generateUrl('/apps/integration_mattermost/is-connected')
axios.get(urlCheckConnection).then((response) => {
OCA.Mattermost.mattermostConnected = response.data.connected
OCA.Mattermost.oauthPossible = response.data.oauth_possible
OCA.Mattermost.usePopup = response.data.use_popup
OCA.Mattermost.clientId = response.data.client_id
OCA.Mattermost.mattermostUrl = response.data.url
if (DEBUG) console.debug('[Mattermost] OCA.Mattermost', OCA.Mattermost)
}).catch((error) => {
console.error(error)
})
document.addEventListener('DOMContentLoaded', () => {
if (DEBUG) console.debug('[Mattermost] before register files plugin')
OC.Plugins.register('OCA.Files.FileList', OCA.Mattermost.FilesPlugin)
})