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 14KB

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