sharedfilelist.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 .files-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. $tr.append(td)
  129. }
  130. return $tr
  131. },
  132. /**
  133. * Set whether the list should contain outgoing shares
  134. * or incoming shares.
  135. *
  136. * @param state true for incoming shares, false otherwise
  137. */
  138. setSharedWithUser: function(state) {
  139. this._sharedWithUser = !!state
  140. },
  141. updateEmptyContent: function() {
  142. var dir = this.getCurrentDirectory()
  143. if (dir === '/') {
  144. // root has special permissions
  145. this.$el.find('.emptyfilelist.emptycontent').toggleClass('hidden', !this.isEmpty)
  146. this.$el.find('.files-filestable thead th').toggleClass('hidden', this.isEmpty)
  147. // hide expiration date header for non link only shares
  148. if (!this._linksOnly) {
  149. this.$el.find('th.column-expiration').addClass('hidden')
  150. }
  151. } else {
  152. OCA.Files.FileList.prototype.updateEmptyContent.apply(this, arguments)
  153. }
  154. },
  155. getDirectoryPermissions: function() {
  156. return OC.PERMISSION_READ | OC.PERMISSION_DELETE
  157. },
  158. updateStorageStatistics: function() {
  159. // no op because it doesn't have
  160. // storage info like free space / used space
  161. },
  162. reload: function() {
  163. this.showMask()
  164. if (this._reloadCall?.abort) {
  165. this._reloadCall.abort()
  166. }
  167. // there is only root
  168. this._setCurrentDir('/', false)
  169. var promises = []
  170. var deletedShares = {
  171. url: OC.linkToOCS('apps/files_sharing/api/v1', 2) + 'deletedshares',
  172. /* jshint camelcase: false */
  173. data: {
  174. format: 'json',
  175. include_tags: true
  176. },
  177. type: 'GET',
  178. beforeSend: function(xhr) {
  179. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  180. }
  181. }
  182. var pendingShares = {
  183. url: OC.linkToOCS('apps/files_sharing/api/v1/shares', 2) + 'pending',
  184. /* jshint camelcase: false */
  185. data: {
  186. format: 'json'
  187. },
  188. type: 'GET',
  189. beforeSend: function(xhr) {
  190. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  191. }
  192. }
  193. var pendingRemoteShares = {
  194. url: OC.linkToOCS('apps/files_sharing/api/v1/remote_shares', 2) + 'pending',
  195. /* jshint camelcase: false */
  196. data: {
  197. format: 'json'
  198. },
  199. type: 'GET',
  200. beforeSend: function(xhr) {
  201. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  202. }
  203. }
  204. var shares = {
  205. url: OC.linkToOCS('apps/files_sharing/api/v1') + 'shares',
  206. /* jshint camelcase: false */
  207. data: {
  208. format: 'json',
  209. shared_with_me: this._sharedWithUser !== false,
  210. include_tags: true
  211. },
  212. type: 'GET',
  213. beforeSend: function(xhr) {
  214. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  215. }
  216. }
  217. var remoteShares = {
  218. url: OC.linkToOCS('apps/files_sharing/api/v1') + 'remote_shares',
  219. /* jshint camelcase: false */
  220. data: {
  221. format: 'json',
  222. include_tags: true
  223. },
  224. type: 'GET',
  225. beforeSend: function(xhr) {
  226. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  227. }
  228. }
  229. // Add the proper ajax requests to the list and run them
  230. // and make sure we have 2 promises
  231. if (this._showDeleted) {
  232. promises.push($.ajax(deletedShares))
  233. } else if (this._showPending) {
  234. promises.push($.ajax(pendingShares))
  235. promises.push($.ajax(pendingRemoteShares))
  236. } else {
  237. promises.push($.ajax(shares))
  238. if (this._sharedWithUser !== false || this._isOverview) {
  239. promises.push($.ajax(remoteShares))
  240. }
  241. if (this._isOverview) {
  242. shares.data.shared_with_me = !shares.data.shared_with_me
  243. promises.push($.ajax(shares))
  244. }
  245. }
  246. this._reloadCall = $.when.apply($, promises)
  247. var callBack = this.reloadCallback.bind(this)
  248. return this._reloadCall.then(callBack, callBack)
  249. },
  250. reloadCallback: function(shares, remoteShares, additionalShares) {
  251. delete this._reloadCall
  252. this.hideMask()
  253. this.$el.find('#headerSharedWith').text(
  254. t('files_sharing', this._sharedWithUser ? 'Shared by' : 'Shared with')
  255. )
  256. var files = []
  257. // make sure to use the same format
  258. if (shares[0] && shares[0].ocs) {
  259. shares = shares[0]
  260. }
  261. if (remoteShares && remoteShares[0] && remoteShares[0].ocs) {
  262. remoteShares = remoteShares[0]
  263. }
  264. if (additionalShares && additionalShares[0] && additionalShares[0].ocs) {
  265. additionalShares = additionalShares[0]
  266. }
  267. if (shares.ocs && shares.ocs.data) {
  268. files = files.concat(this._makeFilesFromShares(shares.ocs.data, this._sharedWithUser))
  269. }
  270. if (remoteShares && remoteShares.ocs && remoteShares.ocs.data) {
  271. files = files.concat(this._makeFilesFromRemoteShares(remoteShares.ocs.data))
  272. }
  273. if (additionalShares && additionalShares.ocs && additionalShares.ocs.data) {
  274. if (this._showPending) {
  275. // in this case the second callback is about pending remote shares
  276. files = files.concat(this._makeFilesFromRemoteShares(additionalShares.ocs.data))
  277. } else {
  278. files = files.concat(this._makeFilesFromShares(additionalShares.ocs.data, !this._sharedWithUser))
  279. }
  280. }
  281. this.setFiles(files)
  282. return true
  283. },
  284. _makeFilesFromRemoteShares: function(data) {
  285. var files = data
  286. files = _.chain(files)
  287. // convert share data to file data
  288. .map(function(share) {
  289. var file = {
  290. shareOwner: share.owner + '@' + share.remote.replace(/.*?:\/\//g, ''),
  291. name: OC.basename(share.mountpoint),
  292. mtime: share.mtime * 1000,
  293. mimetype: share.mimetype,
  294. type: share.type,
  295. // remote share types are different and need to be mapped
  296. shareType: (parseInt(share.share_type, 10) === 1) ? OC.Share.SHARE_TYPE_REMOTE_GROUP : OC.Share.SHARE_TYPE_REMOTE,
  297. id: share.file_id,
  298. path: OC.dirname(share.mountpoint),
  299. permissions: share.permissions,
  300. tags: share.tags || []
  301. }
  302. if (share.remote_id) {
  303. // remote share
  304. if (share.accepted !== '1') {
  305. file.name = OC.basename(share.name)
  306. file.path = '/'
  307. }
  308. file.remoteId = share.remote_id
  309. file.shareOwnerId = share.owner
  310. }
  311. if (!file.mimetype) {
  312. // pending shares usually have no type, so default to showing a directory icon
  313. file.mimetype = 'dir-shared'
  314. }
  315. file.shares = [{
  316. id: share.id,
  317. type: OC.Share.SHARE_TYPE_REMOTE
  318. }]
  319. return file
  320. })
  321. .value()
  322. return files
  323. },
  324. /**
  325. * Converts the OCS API share response data to a file info
  326. * list
  327. * @param {Array} data OCS API share array
  328. * @param {boolean} sharedWithUser
  329. * @returns {Array.<OCA.Sharing.SharedFileInfo>} array of shared file info
  330. */
  331. _makeFilesFromShares: function(data, sharedWithUser) {
  332. /* jshint camelcase: false */
  333. var files = data
  334. if (this._linksOnly) {
  335. files = _.filter(data, function(share) {
  336. return share.share_type === OC.Share.SHARE_TYPE_LINK
  337. })
  338. }
  339. // OCS API uses non-camelcased names
  340. files = _.chain(files)
  341. // convert share data to file data
  342. .map(function(share) {
  343. // TODO: use OC.Files.FileInfo
  344. var file = {
  345. id: share.file_source,
  346. icon: OC.MimeType.getIconUrl(share.mimetype),
  347. mimetype: share.mimetype,
  348. hasPreview: share.has_preview,
  349. tags: share.tags || [],
  350. shareAttributes: JSON.parse(share.attributes),
  351. }
  352. if (share.item_type === 'folder') {
  353. file.type = 'dir'
  354. file.mimetype = 'httpd/unix-directory'
  355. } else {
  356. file.type = 'file'
  357. }
  358. file.share = {
  359. id: share.id,
  360. type: share.share_type,
  361. target: share.share_with,
  362. stime: share.stime * 1000,
  363. expiration: share.expiration
  364. }
  365. if (sharedWithUser) {
  366. file.shareOwner = share.displayname_owner
  367. file.shareOwnerId = share.uid_owner
  368. file.name = OC.basename(share.file_target)
  369. file.path = OC.dirname(share.file_target)
  370. file.permissions = share.permissions
  371. if (file.path) {
  372. file.extraData = share.file_target
  373. }
  374. } else {
  375. if (share.share_type !== OC.Share.SHARE_TYPE_LINK) {
  376. file.share.targetDisplayName = share.share_with_displayname
  377. file.share.targetShareWithId = share.share_with
  378. }
  379. file.name = OC.basename(share.path)
  380. file.path = OC.dirname(share.path)
  381. file.permissions = OC.PERMISSION_ALL
  382. if (file.path) {
  383. file.extraData = share.path
  384. }
  385. }
  386. return file
  387. })
  388. // Group all files and have a "shares" array with
  389. // the share info for each file.
  390. //
  391. // This uses a hash memo to cumulate share information
  392. // inside the same file object (by file id).
  393. .reduce(function(memo, file) {
  394. var data = memo[file.id]
  395. var recipient = file.share.targetDisplayName
  396. var recipientId = file.share.targetShareWithId
  397. if (!data) {
  398. data = memo[file.id] = file
  399. data.shares = [file.share]
  400. // using a hash to make them unique,
  401. // this is only a list to be displayed
  402. data.recipients = {}
  403. data.recipientData = {}
  404. // share types
  405. data.shareTypes = {}
  406. // counter is cheaper than calling _.keys().length
  407. data.recipientsCount = 0
  408. data.mtime = file.share.stime
  409. } else {
  410. // always take the most recent stime
  411. if (file.share.stime > data.mtime) {
  412. data.mtime = file.share.stime
  413. }
  414. data.shares.push(file.share)
  415. }
  416. if (recipient) {
  417. // limit counterparts for output
  418. if (data.recipientsCount < 4) {
  419. // only store the first ones, they will be the only ones
  420. // displayed
  421. data.recipients[recipient] = true
  422. data.recipientData[data.recipientsCount] = {
  423. 'shareWith': recipientId,
  424. 'shareWithDisplayName': recipient
  425. }
  426. }
  427. data.recipientsCount++
  428. }
  429. data.shareTypes[file.share.type] = true
  430. delete file.share
  431. return memo
  432. }, {})
  433. // Retrieve only the values of the returned hash
  434. .values()
  435. // Clean up
  436. .each(function(data) {
  437. // convert the recipients map to a flat
  438. // array of sorted names
  439. data.mountType = 'shared'
  440. delete data.recipientsCount
  441. if (sharedWithUser) {
  442. // only for outgoing shares
  443. delete data.shareTypes
  444. } else {
  445. data.shareTypes = _.keys(data.shareTypes)
  446. }
  447. })
  448. // Finish the chain by getting the result
  449. .value()
  450. // Sort by expected sort comparator
  451. return files.sort(this._sortComparator)
  452. }
  453. })
  454. /**
  455. * Share info attributes.
  456. *
  457. * @typedef {Object} OCA.Sharing.ShareInfo
  458. *
  459. * @property {number} id share ID
  460. * @property {number} type share type
  461. * @property {String} target share target, either user name or group name
  462. * @property {number} stime share timestamp in milliseconds
  463. * @property {String} [targetDisplayName] display name of the recipient
  464. * (only when shared with others)
  465. * @property {String} [targetShareWithId] id of the recipient
  466. *
  467. */
  468. /**
  469. * Recipient attributes
  470. *
  471. * @typedef {Object} OCA.Sharing.RecipientInfo
  472. * @property {String} shareWith the id of the recipient
  473. * @property {String} shareWithDisplayName the display name of the recipient
  474. */
  475. /**
  476. * Shared file info attributes.
  477. *
  478. * @typedef {OCA.Files.FileInfo} OCA.Sharing.SharedFileInfo
  479. *
  480. * @property {Array.<OCA.Sharing.ShareInfo>} shares array of shares for
  481. * this file
  482. * @property {number} mtime most recent share time (if multiple shares)
  483. * @property {String} shareOwner name of the share owner
  484. * @property {Array.<String>} recipients name of the first 4 recipients
  485. * (this is mostly for display purposes)
  486. * @property {Object.<OCA.Sharing.RecipientInfo>} recipientData (as object for easier
  487. * passing to HTML data attributes with jQuery)
  488. */
  489. OCA.Sharing.FileList = FileList
  490. })()