You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sharedfilelist.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /* eslint-disable */
  2. /*
  3. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  4. *
  5. * This file is licensed under the Affero General Public License version 3
  6. * or later.
  7. *
  8. * See the COPYING-README file.
  9. *
  10. */
  11. (function() {
  12. /**
  13. * @class OCA.Sharing.FileList
  14. * @augments OCA.Files.FileList
  15. *
  16. * @classdesc Sharing file list.
  17. * Contains both "shared with others" and "shared with you" modes.
  18. *
  19. * @param $el container element with existing markup for the #controls
  20. * and a table
  21. * @param [options] map of options, see other parameters
  22. * @param {boolean} [options.sharedWithUser] true to return files shared with
  23. * the current user, false to return files that the user shared with others.
  24. * Defaults to false.
  25. * @param {boolean} [options.linksOnly] true to return only link shares
  26. */
  27. var FileList = function($el, options) {
  28. this.initialize($el, options)
  29. }
  30. FileList.prototype = _.extend({}, OCA.Files.FileList.prototype,
  31. /** @lends OCA.Sharing.FileList.prototype */ {
  32. appName: 'Shares',
  33. /**
  34. * Whether the list shows the files shared with the user (true) or
  35. * the files that the user shared with others (false).
  36. */
  37. _sharedWithUser: false,
  38. _linksOnly: false,
  39. _showDeleted: false,
  40. _showPending: false,
  41. _clientSideSort: true,
  42. _allowSelection: false,
  43. _isOverview: false,
  44. /**
  45. * @private
  46. */
  47. initialize: function($el, options) {
  48. OCA.Files.FileList.prototype.initialize.apply(this, arguments)
  49. if (this.initialized) {
  50. return
  51. }
  52. // TODO: consolidate both options
  53. if (options && options.sharedWithUser) {
  54. this._sharedWithUser = true
  55. }
  56. if (options && options.linksOnly) {
  57. this._linksOnly = true
  58. }
  59. if (options && options.showDeleted) {
  60. this._showDeleted = true
  61. }
  62. if (options && options.showPending) {
  63. this._showPending = true
  64. }
  65. if (options && options.isOverview) {
  66. this._isOverview = true
  67. }
  68. },
  69. _renderRow: function() {
  70. // HACK: needed to call the overridden _renderRow
  71. // this is because at the time this class is created
  72. // the overriding hasn't been done yet...
  73. return OCA.Files.FileList.prototype._renderRow.apply(this, arguments)
  74. },
  75. _createRow: function(fileData) {
  76. // TODO: hook earlier and render the whole row here
  77. var $tr = OCA.Files.FileList.prototype._createRow.apply(this, arguments)
  78. $tr.find('.filesize').remove()
  79. $tr.find('td.date').before($tr.children('td:first'))
  80. $tr.find('td.filename input:checkbox').remove()
  81. $tr.attr('data-share-id', _.pluck(fileData.shares, 'id').join(','))
  82. if (this._sharedWithUser) {
  83. $tr.attr('data-share-owner', fileData.shareOwner)
  84. $tr.attr('data-mounttype', 'shared-root')
  85. var permission = parseInt($tr.attr('data-permissions')) | OC.PERMISSION_DELETE
  86. $tr.attr('data-permissions', permission)
  87. }
  88. if (this._showDeleted || this._showPending) {
  89. var permission = fileData.permissions
  90. $tr.attr('data-share-permissions', permission)
  91. }
  92. if (fileData.remoteId) {
  93. $tr.attr('data-remote-id', fileData.remoteId)
  94. }
  95. if (fileData.shareType) {
  96. $tr.attr('data-share-type', fileData.shareType)
  97. }
  98. // add row with expiration date for link only shares - influenced by _createRow of filelist
  99. if (this._linksOnly) {
  100. var expirationTimestamp = 0
  101. if (fileData.shares && fileData.shares[0].expiration !== null) {
  102. expirationTimestamp = moment(fileData.shares[0].expiration).valueOf()
  103. }
  104. $tr.attr('data-expiration', expirationTimestamp)
  105. // date column (1000 milliseconds to seconds, 60 seconds, 60 minutes, 24 hours)
  106. // difference in days multiplied by 5 - brightest shade for expiry dates in more than 32 days (160/5)
  107. var modifiedColor = Math.round((expirationTimestamp - (new Date()).getTime()) / 1000 / 60 / 60 / 24 * 5)
  108. // ensure that the brightest color is still readable
  109. if (modifiedColor >= 160) {
  110. modifiedColor = 160
  111. }
  112. var formatted
  113. var text
  114. if (expirationTimestamp > 0) {
  115. formatted = OC.Util.formatDate(expirationTimestamp)
  116. text = OC.Util.relativeModifiedDate(expirationTimestamp)
  117. } else {
  118. formatted = t('files_sharing', 'No expiration date set')
  119. text = ''
  120. modifiedColor = 160
  121. }
  122. td = $('<td></td>').attr({ 'class': 'date' })
  123. td.append($('<span></span>').attr({
  124. 'class': 'modified',
  125. 'title': formatted,
  126. 'style': 'color:rgb(' + modifiedColor + ',' + modifiedColor + ',' + modifiedColor + ')'
  127. }).text(text)
  128. .tooltip({ placement: 'top' })
  129. )
  130. $tr.append(td)
  131. }
  132. return $tr
  133. },
  134. /**
  135. * Set whether the list should contain outgoing shares
  136. * or incoming shares.
  137. *
  138. * @param state true for incoming shares, false otherwise
  139. */
  140. setSharedWithUser: function(state) {
  141. this._sharedWithUser = !!state
  142. },
  143. updateEmptyContent: function() {
  144. var dir = this.getCurrentDirectory()
  145. if (dir === '/') {
  146. // root has special permissions
  147. this.$el.find('#emptycontent').toggleClass('hidden', !this.isEmpty)
  148. this.$el.find('#filestable thead th').toggleClass('hidden', this.isEmpty)
  149. // hide expiration date header for non link only shares
  150. if (!this._linksOnly) {
  151. this.$el.find('th.column-expiration').addClass('hidden')
  152. }
  153. } else {
  154. OCA.Files.FileList.prototype.updateEmptyContent.apply(this, arguments)
  155. }
  156. },
  157. getDirectoryPermissions: function() {
  158. return OC.PERMISSION_READ | OC.PERMISSION_DELETE
  159. },
  160. updateStorageStatistics: function() {
  161. // no op because it doesn't have
  162. // storage info like free space / used space
  163. },
  164. updateRow: function($tr, fileInfo, options) {
  165. // no-op, suppress re-rendering
  166. return $tr
  167. },
  168. reload: function() {
  169. this.showMask()
  170. if (this._reloadCall) {
  171. this._reloadCall.abort()
  172. }
  173. // there is only root
  174. this._setCurrentDir('/', false)
  175. var promises = []
  176. var deletedShares = {
  177. url: OC.linkToOCS('apps/files_sharing/api/v1', 2) + 'deletedshares',
  178. /* jshint camelcase: false */
  179. data: {
  180. format: 'json',
  181. include_tags: true
  182. },
  183. type: 'GET',
  184. beforeSend: function(xhr) {
  185. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  186. }
  187. }
  188. var pendingShares = {
  189. url: OC.linkToOCS('apps/files_sharing/api/v1/shares', 2) + 'pending',
  190. /* jshint camelcase: false */
  191. data: {
  192. format: 'json'
  193. },
  194. type: 'GET',
  195. beforeSend: function(xhr) {
  196. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  197. }
  198. }
  199. var pendingRemoteShares = {
  200. url: OC.linkToOCS('apps/files_sharing/api/v1/remote_shares', 2) + 'pending',
  201. /* jshint camelcase: false */
  202. data: {
  203. format: 'json'
  204. },
  205. type: 'GET',
  206. beforeSend: function(xhr) {
  207. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  208. }
  209. }
  210. var shares = {
  211. url: OC.linkToOCS('apps/files_sharing/api/v1') + 'shares',
  212. /* jshint camelcase: false */
  213. data: {
  214. format: 'json',
  215. shared_with_me: this._sharedWithUser !== false,
  216. include_tags: true
  217. },
  218. type: 'GET',
  219. beforeSend: function(xhr) {
  220. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  221. }
  222. }
  223. var remoteShares = {
  224. url: OC.linkToOCS('apps/files_sharing/api/v1') + 'remote_shares',
  225. /* jshint camelcase: false */
  226. data: {
  227. format: 'json',
  228. include_tags: true
  229. },
  230. type: 'GET',
  231. beforeSend: function(xhr) {
  232. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  233. }
  234. }
  235. // Add the proper ajax requests to the list and run them
  236. // and make sure we have 2 promises
  237. if (this._showDeleted) {
  238. promises.push($.ajax(deletedShares))
  239. } else if (this._showPending) {
  240. promises.push($.ajax(pendingShares))
  241. promises.push($.ajax(pendingRemoteShares))
  242. } else {
  243. promises.push($.ajax(shares))
  244. if (this._sharedWithUser !== false || this._isOverview) {
  245. promises.push($.ajax(remoteShares))
  246. }
  247. if (this._isOverview) {
  248. shares.data.shared_with_me = !shares.data.shared_with_me
  249. promises.push($.ajax(shares))
  250. }
  251. }
  252. this._reloadCall = $.when.apply($, promises)
  253. var callBack = this.reloadCallback.bind(this)
  254. return this._reloadCall.then(callBack, callBack)
  255. },
  256. reloadCallback: function(shares, remoteShares, additionalShares) {
  257. delete this._reloadCall
  258. this.hideMask()
  259. this.$el.find('#headerSharedWith').text(
  260. t('files_sharing', this._sharedWithUser ? 'Shared by' : 'Shared with')
  261. )
  262. var files = []
  263. // make sure to use the same format
  264. if (shares[0] && shares[0].ocs) {
  265. shares = shares[0]
  266. }
  267. if (remoteShares && remoteShares[0] && remoteShares[0].ocs) {
  268. remoteShares = remoteShares[0]
  269. }
  270. if (additionalShares && additionalShares[0] && additionalShares[0].ocs) {
  271. additionalShares = additionalShares[0]
  272. }
  273. if (shares.ocs && shares.ocs.data) {
  274. files = files.concat(this._makeFilesFromShares(shares.ocs.data, this._sharedWithUser))
  275. }
  276. if (remoteShares && remoteShares.ocs && remoteShares.ocs.data) {
  277. files = files.concat(this._makeFilesFromRemoteShares(remoteShares.ocs.data))
  278. }
  279. if (additionalShares && additionalShares.ocs && additionalShares.ocs.data) {
  280. if (this._showPending) {
  281. // in this case the second callback is about pending remote shares
  282. files = files.concat(this._makeFilesFromRemoteShares(additionalShares.ocs.data))
  283. } else {
  284. files = files.concat(this._makeFilesFromShares(additionalShares.ocs.data, !this._sharedWithUser))
  285. }
  286. }
  287. this.setFiles(files)
  288. return true
  289. },
  290. _makeFilesFromRemoteShares: function(data) {
  291. var files = data
  292. files = _.chain(files)
  293. // convert share data to file data
  294. .map(function(share) {
  295. var file = {
  296. shareOwner: share.owner + '@' + share.remote.replace(/.*?:\/\//g, ''),
  297. name: OC.basename(share.mountpoint),
  298. mtime: share.mtime * 1000,
  299. mimetype: share.mimetype,
  300. type: share.type,
  301. // remote share types are different and need to be mapped
  302. shareType: (parseInt(share.share_type, 10) === 1) ? OC.Share.SHARE_TYPE_REMOTE_GROUP : OC.Share.SHARE_TYPE_REMOTE,
  303. id: share.file_id,
  304. path: OC.dirname(share.mountpoint),
  305. permissions: share.permissions,
  306. tags: share.tags || []
  307. }
  308. if (share.remote_id) {
  309. // remote share
  310. if (share.accepted !== '1') {
  311. file.name = OC.basename(share.name)
  312. file.path = '/'
  313. }
  314. file.remoteId = share.remote_id
  315. file.shareOwnerId = share.owner
  316. }
  317. if (!file.mimetype) {
  318. // pending shares usually have no type, so default to showing a directory icon
  319. file.mimetype = 'dir-shared'
  320. }
  321. file.shares = [{
  322. id: share.id,
  323. type: OC.Share.SHARE_TYPE_REMOTE
  324. }]
  325. return file
  326. })
  327. .value()
  328. return files
  329. },
  330. /**
  331. * Converts the OCS API share response data to a file info
  332. * list
  333. * @param {Array} data OCS API share array
  334. * @param {bool} sharedWithUser
  335. * @returns {Array.<OCA.Sharing.SharedFileInfo>} array of shared file info
  336. */
  337. _makeFilesFromShares: function(data, sharedWithUser) {
  338. /* jshint camelcase: false */
  339. var files = data
  340. if (this._linksOnly) {
  341. files = _.filter(data, function(share) {
  342. return share.share_type === OC.Share.SHARE_TYPE_LINK
  343. })
  344. }
  345. // OCS API uses non-camelcased names
  346. files = _.chain(files)
  347. // convert share data to file data
  348. .map(function(share) {
  349. // TODO: use OC.Files.FileInfo
  350. var file = {
  351. id: share.file_source,
  352. icon: OC.MimeType.getIconUrl(share.mimetype),
  353. mimetype: share.mimetype,
  354. hasPreview: share.has_preview,
  355. tags: share.tags || []
  356. }
  357. if (share.item_type === 'folder') {
  358. file.type = 'dir'
  359. file.mimetype = 'httpd/unix-directory'
  360. } else {
  361. file.type = 'file'
  362. }
  363. file.share = {
  364. id: share.id,
  365. type: share.share_type,
  366. target: share.share_with,
  367. stime: share.stime * 1000,
  368. expiration: share.expiration
  369. }
  370. if (sharedWithUser) {
  371. file.shareOwner = share.displayname_owner
  372. file.shareOwnerId = share.uid_owner
  373. file.name = OC.basename(share.file_target)
  374. file.path = OC.dirname(share.file_target)
  375. file.permissions = share.permissions
  376. if (file.path) {
  377. file.extraData = share.file_target
  378. }
  379. } else {
  380. if (share.share_type !== OC.Share.SHARE_TYPE_LINK) {
  381. file.share.targetDisplayName = share.share_with_displayname
  382. file.share.targetShareWithId = share.share_with
  383. }
  384. file.name = OC.basename(share.path)
  385. file.path = OC.dirname(share.path)
  386. file.permissions = OC.PERMISSION_ALL
  387. if (file.path) {
  388. file.extraData = share.path
  389. }
  390. }
  391. return file
  392. })
  393. // Group all files and have a "shares" array with
  394. // the share info for each file.
  395. //
  396. // This uses a hash memo to cumulate share information
  397. // inside the same file object (by file id).
  398. .reduce(function(memo, file) {
  399. var data = memo[file.id]
  400. var recipient = file.share.targetDisplayName
  401. var recipientId = file.share.targetShareWithId
  402. if (!data) {
  403. data = memo[file.id] = file
  404. data.shares = [file.share]
  405. // using a hash to make them unique,
  406. // this is only a list to be displayed
  407. data.recipients = {}
  408. data.recipientData = {}
  409. // share types
  410. data.shareTypes = {}
  411. // counter is cheaper than calling _.keys().length
  412. data.recipientsCount = 0
  413. data.mtime = file.share.stime
  414. } else {
  415. // always take the most recent stime
  416. if (file.share.stime > data.mtime) {
  417. data.mtime = file.share.stime
  418. }
  419. data.shares.push(file.share)
  420. }
  421. if (recipient) {
  422. // limit counterparts for output
  423. if (data.recipientsCount < 4) {
  424. // only store the first ones, they will be the only ones
  425. // displayed
  426. data.recipients[recipient] = true
  427. data.recipientData[data.recipientsCount] = {
  428. 'shareWith': recipientId,
  429. 'shareWithDisplayName': recipient
  430. }
  431. }
  432. data.recipientsCount++
  433. }
  434. data.shareTypes[file.share.type] = true
  435. delete file.share
  436. return memo
  437. }, {})
  438. // Retrieve only the values of the returned hash
  439. .values()
  440. // Clean up
  441. .each(function(data) {
  442. // convert the recipients map to a flat
  443. // array of sorted names
  444. data.mountType = 'shared'
  445. delete data.recipientsCount
  446. if (sharedWithUser) {
  447. // only for outgoing shares
  448. delete data.shareTypes
  449. } else {
  450. data.shareTypes = _.keys(data.shareTypes)
  451. }
  452. })
  453. // Finish the chain by getting the result
  454. .value()
  455. // Sort by expected sort comparator
  456. return files.sort(this._sortComparator)
  457. }
  458. })
  459. /**
  460. * Share info attributes.
  461. *
  462. * @typedef {Object} OCA.Sharing.ShareInfo
  463. *
  464. * @property {int} id share ID
  465. * @property {int} type share type
  466. * @property {String} target share target, either user name or group name
  467. * @property {int} stime share timestamp in milliseconds
  468. * @property {String} [targetDisplayName] display name of the recipient
  469. * (only when shared with others)
  470. * @property {String} [targetShareWithId] id of the recipient
  471. *
  472. */
  473. /**
  474. * Recipient attributes
  475. *
  476. * @typedef {Object} OCA.Sharing.RecipientInfo
  477. * @property {String} shareWith the id of the recipient
  478. * @property {String} shareWithDisplayName the display name of the recipient
  479. */
  480. /**
  481. * Shared file info attributes.
  482. *
  483. * @typedef {OCA.Files.FileInfo} OCA.Sharing.SharedFileInfo
  484. *
  485. * @property {Array.<OCA.Sharing.ShareInfo>} shares array of shares for
  486. * this file
  487. * @property {int} mtime most recent share time (if multiple shares)
  488. * @property {String} shareOwner name of the share owner
  489. * @property {Array.<String>} recipients name of the first 4 recipients
  490. * (this is mostly for display purposes)
  491. * @property {Object.<OCA.Sharing.RecipientInfo>} recipientData (as object for easier
  492. * passing to HTML data attributes with jQuery)
  493. */
  494. OCA.Sharing.FileList = FileList
  495. })()