sharedfilelist.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. // add row with expiration date for link only shares - influenced by _createRow of filelist
  93. if (this._linksOnly) {
  94. var expirationTimestamp = 0
  95. if (fileData.shares && fileData.shares[0].expiration !== null) {
  96. expirationTimestamp = moment(fileData.shares[0].expiration).valueOf()
  97. }
  98. $tr.attr('data-expiration', expirationTimestamp)
  99. // date column (1000 milliseconds to seconds, 60 seconds, 60 minutes, 24 hours)
  100. // difference in days multiplied by 5 - brightest shade for expiry dates in more than 32 days (160/5)
  101. var modifiedColor = Math.round((expirationTimestamp - (new Date()).getTime()) / 1000 / 60 / 60 / 24 * 5)
  102. // ensure that the brightest color is still readable
  103. if (modifiedColor >= 160) {
  104. modifiedColor = 160
  105. }
  106. var formatted
  107. var text
  108. if (expirationTimestamp > 0) {
  109. formatted = OC.Util.formatDate(expirationTimestamp)
  110. text = OC.Util.relativeModifiedDate(expirationTimestamp)
  111. } else {
  112. formatted = t('files_sharing', 'No expiration date set')
  113. text = ''
  114. modifiedColor = 160
  115. }
  116. td = $('<td></td>').attr({ 'class': 'date' })
  117. td.append($('<span></span>').attr({
  118. 'class': 'modified',
  119. 'title': formatted,
  120. 'style': 'color:rgb(' + modifiedColor + ',' + modifiedColor + ',' + modifiedColor + ')'
  121. }).text(text)
  122. .tooltip({ placement: 'top' })
  123. )
  124. $tr.append(td)
  125. }
  126. return $tr
  127. },
  128. /**
  129. * Set whether the list should contain outgoing shares
  130. * or incoming shares.
  131. *
  132. * @param state true for incoming shares, false otherwise
  133. */
  134. setSharedWithUser: function(state) {
  135. this._sharedWithUser = !!state
  136. },
  137. updateEmptyContent: function() {
  138. var dir = this.getCurrentDirectory()
  139. if (dir === '/') {
  140. // root has special permissions
  141. this.$el.find('#emptycontent').toggleClass('hidden', !this.isEmpty)
  142. this.$el.find('#filestable thead th').toggleClass('hidden', this.isEmpty)
  143. // hide expiration date header for non link only shares
  144. if (!this._linksOnly) {
  145. this.$el.find('th.column-expiration').addClass('hidden')
  146. }
  147. } else {
  148. OCA.Files.FileList.prototype.updateEmptyContent.apply(this, arguments)
  149. }
  150. },
  151. getDirectoryPermissions: function() {
  152. return OC.PERMISSION_READ | OC.PERMISSION_DELETE
  153. },
  154. updateStorageStatistics: function() {
  155. // no op because it doesn't have
  156. // storage info like free space / used space
  157. },
  158. updateRow: function($tr, fileInfo, options) {
  159. // no-op, suppress re-rendering
  160. return $tr
  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 shares = {
  194. url: OC.linkToOCS('apps/files_sharing/api/v1') + 'shares',
  195. /* jshint camelcase: false */
  196. data: {
  197. format: 'json',
  198. shared_with_me: this._sharedWithUser !== false,
  199. include_tags: true
  200. },
  201. type: 'GET',
  202. beforeSend: function(xhr) {
  203. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  204. }
  205. }
  206. var remoteShares = {
  207. url: OC.linkToOCS('apps/files_sharing/api/v1') + 'remote_shares',
  208. /* jshint camelcase: false */
  209. data: {
  210. format: 'json',
  211. include_tags: true
  212. },
  213. type: 'GET',
  214. beforeSend: function(xhr) {
  215. xhr.setRequestHeader('OCS-APIREQUEST', 'true')
  216. }
  217. }
  218. // Add the proper ajax requests to the list and run them
  219. // and make sure we have 2 promises
  220. if (this._showDeleted) {
  221. promises.push($.ajax(deletedShares))
  222. } else if (this._showPending) {
  223. promises.push($.ajax(pendingShares))
  224. } else {
  225. promises.push($.ajax(shares))
  226. if (this._sharedWithUser !== false || this._isOverview) {
  227. promises.push($.ajax(remoteShares))
  228. }
  229. if (this._isOverview) {
  230. shares.data.shared_with_me = !shares.data.shared_with_me
  231. promises.push($.ajax(shares))
  232. }
  233. }
  234. this._reloadCall = $.when.apply($, promises)
  235. var callBack = this.reloadCallback.bind(this)
  236. return this._reloadCall.then(callBack, callBack)
  237. },
  238. reloadCallback: function(shares, remoteShares, additionalShares) {
  239. delete this._reloadCall
  240. this.hideMask()
  241. this.$el.find('#headerSharedWith').text(
  242. t('files_sharing', this._sharedWithUser ? 'Shared by' : 'Shared with')
  243. )
  244. var files = []
  245. // make sure to use the same format
  246. if (shares[0] && shares[0].ocs) {
  247. shares = shares[0]
  248. }
  249. if (remoteShares && remoteShares[0] && remoteShares[0].ocs) {
  250. remoteShares = remoteShares[0]
  251. }
  252. if (additionalShares && additionalShares[0] && additionalShares[0].ocs) {
  253. additionalShares = additionalShares[0]
  254. }
  255. if (shares.ocs && shares.ocs.data) {
  256. files = files.concat(this._makeFilesFromShares(shares.ocs.data, this._sharedWithUser))
  257. }
  258. if (remoteShares && remoteShares.ocs && remoteShares.ocs.data) {
  259. files = files.concat(this._makeFilesFromRemoteShares(remoteShares.ocs.data))
  260. }
  261. if (additionalShares && additionalShares.ocs && additionalShares.ocs.data) {
  262. files = files.concat(this._makeFilesFromShares(additionalShares.ocs.data, !this._sharedWithUser))
  263. }
  264. this.setFiles(files)
  265. return true
  266. },
  267. _makeFilesFromRemoteShares: function(data) {
  268. var files = data
  269. files = _.chain(files)
  270. // convert share data to file data
  271. .map(function(share) {
  272. var file = {
  273. shareOwner: share.owner + '@' + share.remote.replace(/.*?:\/\//g, ''),
  274. name: OC.basename(share.mountpoint),
  275. mtime: share.mtime * 1000,
  276. mimetype: share.mimetype,
  277. type: share.type,
  278. id: share.file_id,
  279. path: OC.dirname(share.mountpoint),
  280. permissions: share.permissions,
  281. tags: share.tags || []
  282. }
  283. file.shares = [{
  284. id: share.id,
  285. type: OC.Share.SHARE_TYPE_REMOTE
  286. }]
  287. return file
  288. })
  289. .value()
  290. return files
  291. },
  292. /**
  293. * Converts the OCS API share response data to a file info
  294. * list
  295. * @param {Array} data OCS API share array
  296. * @param {bool} sharedWithUser
  297. * @returns {Array.<OCA.Sharing.SharedFileInfo>} array of shared file info
  298. */
  299. _makeFilesFromShares: function(data, sharedWithUser) {
  300. /* jshint camelcase: false */
  301. var files = data
  302. if (this._linksOnly) {
  303. files = _.filter(data, function(share) {
  304. return share.share_type === OC.Share.SHARE_TYPE_LINK
  305. })
  306. }
  307. // OCS API uses non-camelcased names
  308. files = _.chain(files)
  309. // convert share data to file data
  310. .map(function(share) {
  311. // TODO: use OC.Files.FileInfo
  312. var file = {
  313. id: share.file_source,
  314. icon: OC.MimeType.getIconUrl(share.mimetype),
  315. mimetype: share.mimetype,
  316. hasPreview: share.has_preview,
  317. tags: share.tags || []
  318. }
  319. if (share.item_type === 'folder') {
  320. file.type = 'dir'
  321. file.mimetype = 'httpd/unix-directory'
  322. } else {
  323. file.type = 'file'
  324. }
  325. file.share = {
  326. id: share.id,
  327. type: share.share_type,
  328. target: share.share_with,
  329. stime: share.stime * 1000,
  330. expiration: share.expiration
  331. }
  332. if (sharedWithUser) {
  333. file.shareOwner = share.displayname_owner
  334. file.shareOwnerId = share.uid_owner
  335. file.name = OC.basename(share.file_target)
  336. file.path = OC.dirname(share.file_target)
  337. file.permissions = share.permissions
  338. if (file.path) {
  339. file.extraData = share.file_target
  340. }
  341. } else {
  342. if (share.share_type !== OC.Share.SHARE_TYPE_LINK) {
  343. file.share.targetDisplayName = share.share_with_displayname
  344. file.share.targetShareWithId = share.share_with
  345. }
  346. file.name = OC.basename(share.path)
  347. file.path = OC.dirname(share.path)
  348. file.permissions = OC.PERMISSION_ALL
  349. if (file.path) {
  350. file.extraData = share.path
  351. }
  352. }
  353. return file
  354. })
  355. // Group all files and have a "shares" array with
  356. // the share info for each file.
  357. //
  358. // This uses a hash memo to cumulate share information
  359. // inside the same file object (by file id).
  360. .reduce(function(memo, file) {
  361. var data = memo[file.id]
  362. var recipient = file.share.targetDisplayName
  363. var recipientId = file.share.targetShareWithId
  364. if (!data) {
  365. data = memo[file.id] = file
  366. data.shares = [file.share]
  367. // using a hash to make them unique,
  368. // this is only a list to be displayed
  369. data.recipients = {}
  370. data.recipientData = {}
  371. // share types
  372. data.shareTypes = {}
  373. // counter is cheaper than calling _.keys().length
  374. data.recipientsCount = 0
  375. data.mtime = file.share.stime
  376. } else {
  377. // always take the most recent stime
  378. if (file.share.stime > data.mtime) {
  379. data.mtime = file.share.stime
  380. }
  381. data.shares.push(file.share)
  382. }
  383. if (recipient) {
  384. // limit counterparts for output
  385. if (data.recipientsCount < 4) {
  386. // only store the first ones, they will be the only ones
  387. // displayed
  388. data.recipients[recipient] = true
  389. data.recipientData[data.recipientsCount] = {
  390. 'shareWith': recipientId,
  391. 'shareWithDisplayName': recipient
  392. }
  393. }
  394. data.recipientsCount++
  395. }
  396. data.shareTypes[file.share.type] = true
  397. delete file.share
  398. return memo
  399. }, {})
  400. // Retrieve only the values of the returned hash
  401. .values()
  402. // Clean up
  403. .each(function(data) {
  404. // convert the recipients map to a flat
  405. // array of sorted names
  406. data.mountType = 'shared'
  407. delete data.recipientsCount
  408. if (sharedWithUser) {
  409. // only for outgoing shares
  410. delete data.shareTypes
  411. } else {
  412. data.shareTypes = _.keys(data.shareTypes)
  413. }
  414. })
  415. // Finish the chain by getting the result
  416. .value()
  417. // Sort by expected sort comparator
  418. return files.sort(this._sortComparator)
  419. }
  420. })
  421. /**
  422. * Share info attributes.
  423. *
  424. * @typedef {Object} OCA.Sharing.ShareInfo
  425. *
  426. * @property {int} id share ID
  427. * @property {int} type share type
  428. * @property {String} target share target, either user name or group name
  429. * @property {int} stime share timestamp in milliseconds
  430. * @property {String} [targetDisplayName] display name of the recipient
  431. * (only when shared with others)
  432. * @property {String} [targetShareWithId] id of the recipient
  433. *
  434. */
  435. /**
  436. * Recipient attributes
  437. *
  438. * @typedef {Object} OCA.Sharing.RecipientInfo
  439. * @property {String} shareWith the id of the recipient
  440. * @property {String} shareWithDisplayName the display name of the recipient
  441. */
  442. /**
  443. * Shared file info attributes.
  444. *
  445. * @typedef {OCA.Files.FileInfo} OCA.Sharing.SharedFileInfo
  446. *
  447. * @property {Array.<OCA.Sharing.ShareInfo>} shares array of shares for
  448. * this file
  449. * @property {int} mtime most recent share time (if multiple shares)
  450. * @property {String} shareOwner name of the share owner
  451. * @property {Array.<String>} recipients name of the first 4 recipients
  452. * (this is mostly for display purposes)
  453. * @property {Object.<OCA.Sharing.RecipientInfo>} recipientData (as object for easier
  454. * passing to HTML data attributes with jQuery)
  455. */
  456. OCA.Sharing.FileList = FileList
  457. })()