sharedfilelist.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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) {
  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. }
  351. if (share.item_type === 'folder') {
  352. file.type = 'dir'
  353. file.mimetype = 'httpd/unix-directory'
  354. } else {
  355. file.type = 'file'
  356. }
  357. file.share = {
  358. id: share.id,
  359. type: share.share_type,
  360. target: share.share_with,
  361. stime: share.stime * 1000,
  362. expiration: share.expiration
  363. }
  364. if (sharedWithUser) {
  365. file.shareOwner = share.displayname_owner
  366. file.shareOwnerId = share.uid_owner
  367. file.name = OC.basename(share.file_target)
  368. file.path = OC.dirname(share.file_target)
  369. file.permissions = share.permissions
  370. if (file.path) {
  371. file.extraData = share.file_target
  372. }
  373. } else {
  374. if (share.share_type !== OC.Share.SHARE_TYPE_LINK) {
  375. file.share.targetDisplayName = share.share_with_displayname
  376. file.share.targetShareWithId = share.share_with
  377. }
  378. file.name = OC.basename(share.path)
  379. file.path = OC.dirname(share.path)
  380. file.permissions = OC.PERMISSION_ALL
  381. if (file.path) {
  382. file.extraData = share.path
  383. }
  384. }
  385. return file
  386. })
  387. // Group all files and have a "shares" array with
  388. // the share info for each file.
  389. //
  390. // This uses a hash memo to cumulate share information
  391. // inside the same file object (by file id).
  392. .reduce(function(memo, file) {
  393. var data = memo[file.id]
  394. var recipient = file.share.targetDisplayName
  395. var recipientId = file.share.targetShareWithId
  396. if (!data) {
  397. data = memo[file.id] = file
  398. data.shares = [file.share]
  399. // using a hash to make them unique,
  400. // this is only a list to be displayed
  401. data.recipients = {}
  402. data.recipientData = {}
  403. // share types
  404. data.shareTypes = {}
  405. // counter is cheaper than calling _.keys().length
  406. data.recipientsCount = 0
  407. data.mtime = file.share.stime
  408. } else {
  409. // always take the most recent stime
  410. if (file.share.stime > data.mtime) {
  411. data.mtime = file.share.stime
  412. }
  413. data.shares.push(file.share)
  414. }
  415. if (recipient) {
  416. // limit counterparts for output
  417. if (data.recipientsCount < 4) {
  418. // only store the first ones, they will be the only ones
  419. // displayed
  420. data.recipients[recipient] = true
  421. data.recipientData[data.recipientsCount] = {
  422. 'shareWith': recipientId,
  423. 'shareWithDisplayName': recipient
  424. }
  425. }
  426. data.recipientsCount++
  427. }
  428. data.shareTypes[file.share.type] = true
  429. delete file.share
  430. return memo
  431. }, {})
  432. // Retrieve only the values of the returned hash
  433. .values()
  434. // Clean up
  435. .each(function(data) {
  436. // convert the recipients map to a flat
  437. // array of sorted names
  438. data.mountType = 'shared'
  439. delete data.recipientsCount
  440. if (sharedWithUser) {
  441. // only for outgoing shares
  442. delete data.shareTypes
  443. } else {
  444. data.shareTypes = _.keys(data.shareTypes)
  445. }
  446. })
  447. // Finish the chain by getting the result
  448. .value()
  449. // Sort by expected sort comparator
  450. return files.sort(this._sortComparator)
  451. }
  452. })
  453. /**
  454. * Share info attributes.
  455. *
  456. * @typedef {Object} OCA.Sharing.ShareInfo
  457. *
  458. * @property {number} id share ID
  459. * @property {number} type share type
  460. * @property {String} target share target, either user name or group name
  461. * @property {number} stime share timestamp in milliseconds
  462. * @property {String} [targetDisplayName] display name of the recipient
  463. * (only when shared with others)
  464. * @property {String} [targetShareWithId] id of the recipient
  465. *
  466. */
  467. /**
  468. * Recipient attributes
  469. *
  470. * @typedef {Object} OCA.Sharing.RecipientInfo
  471. * @property {String} shareWith the id of the recipient
  472. * @property {String} shareWithDisplayName the display name of the recipient
  473. */
  474. /**
  475. * Shared file info attributes.
  476. *
  477. * @typedef {OCA.Files.FileInfo} OCA.Sharing.SharedFileInfo
  478. *
  479. * @property {Array.<OCA.Sharing.ShareInfo>} shares array of shares for
  480. * this file
  481. * @property {number} mtime most recent share time (if multiple shares)
  482. * @property {String} shareOwner name of the share owner
  483. * @property {Array.<String>} recipients name of the first 4 recipients
  484. * (this is mostly for display purposes)
  485. * @property {Object.<OCA.Sharing.RecipientInfo>} recipientData (as object for easier
  486. * passing to HTML data attributes with jQuery)
  487. */
  488. OCA.Sharing.FileList = FileList
  489. })()