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

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