1
0

sharedfilelist.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. (function() {
  11. /**
  12. * @class OCA.Sharing.FileList
  13. * @augments OCA.Files.FileList
  14. *
  15. * @classdesc Sharing file list.
  16. * Contains both "shared with others" and "shared with you" modes.
  17. *
  18. * @param $el container element with existing markup for the #controls
  19. * and a table
  20. * @param [options] map of options, see other parameters
  21. * @param {boolean} [options.sharedWithUser] true to return files shared with
  22. * the current user, false to return files that the user shared with others.
  23. * Defaults to false.
  24. * @param {boolean} [options.linksOnly] true to return only link shares
  25. */
  26. var FileList = function($el, options) {
  27. this.initialize($el, options);
  28. };
  29. FileList.prototype = _.extend({}, OCA.Files.FileList.prototype,
  30. /** @lends OCA.Sharing.FileList.prototype */ {
  31. appName: 'Shares',
  32. /**
  33. * Whether the list shows the files shared with the user (true) or
  34. * the files that the user shared with others (false).
  35. */
  36. _sharedWithUser: false,
  37. _linksOnly: false,
  38. _showDeleted: false,
  39. _clientSideSort: true,
  40. _allowSelection: false,
  41. _isOverview: false,
  42. /**
  43. * @private
  44. */
  45. initialize: function($el, options) {
  46. OCA.Files.FileList.prototype.initialize.apply(this, arguments);
  47. if (this.initialized) {
  48. return;
  49. }
  50. // TODO: consolidate both options
  51. if (options && options.sharedWithUser) {
  52. this._sharedWithUser = true;
  53. }
  54. if (options && options.linksOnly) {
  55. this._linksOnly = true;
  56. }
  57. if (options && options.showDeleted) {
  58. this._showDeleted = true;
  59. }
  60. if (options && options.isOverview) {
  61. this._isOverview = true;
  62. }
  63. },
  64. _renderRow: function() {
  65. // HACK: needed to call the overridden _renderRow
  66. // this is because at the time this class is created
  67. // the overriding hasn't been done yet...
  68. return OCA.Files.FileList.prototype._renderRow.apply(this, arguments);
  69. },
  70. _createRow: function(fileData) {
  71. // TODO: hook earlier and render the whole row here
  72. var $tr = OCA.Files.FileList.prototype._createRow.apply(this, arguments);
  73. $tr.find('.filesize').remove();
  74. $tr.find('td.date').before($tr.children('td:first'));
  75. $tr.find('td.filename input:checkbox').remove();
  76. $tr.attr('data-share-id', _.pluck(fileData.shares, 'id').join(','));
  77. if (this._sharedWithUser) {
  78. $tr.attr('data-share-owner', fileData.shareOwner);
  79. $tr.attr('data-mounttype', 'shared-root');
  80. var permission = parseInt($tr.attr('data-permissions')) | OC.PERMISSION_DELETE;
  81. $tr.attr('data-permissions', permission);
  82. }
  83. if (this._showDeleted) {
  84. var permission = fileData.permissions;
  85. $tr.attr('data-share-permissions', permission);
  86. }
  87. // add row with expiration date for link only shares - influenced by _createRow of filelist
  88. if (this._linksOnly) {
  89. var expirationTimestamp = 0;
  90. if(fileData.shares && fileData.shares[0].expiration !== null) {
  91. expirationTimestamp = moment(fileData.shares[0].expiration).valueOf();
  92. }
  93. $tr.attr('data-expiration', expirationTimestamp);
  94. // date column (1000 milliseconds to seconds, 60 seconds, 60 minutes, 24 hours)
  95. // difference in days multiplied by 5 - brightest shade for expiry dates in more than 32 days (160/5)
  96. var modifiedColor = Math.round((expirationTimestamp - (new Date()).getTime()) / 1000 / 60 / 60 / 24 * 5);
  97. // ensure that the brightest color is still readable
  98. if (modifiedColor >= 160) {
  99. modifiedColor = 160;
  100. }
  101. var formatted;
  102. var text;
  103. if (expirationTimestamp > 0) {
  104. formatted = OC.Util.formatDate(expirationTimestamp);
  105. text = OC.Util.relativeModifiedDate(expirationTimestamp);
  106. } else {
  107. formatted = t('files_sharing', 'No expiration date set');
  108. text = '';
  109. modifiedColor = 160;
  110. }
  111. td = $('<td></td>').attr({"class": "date"});
  112. td.append($('<span></span>').attr({
  113. "class": "modified",
  114. "title": formatted,
  115. "style": 'color:rgb(' + modifiedColor + ',' + modifiedColor + ',' + modifiedColor + ')'
  116. }).text(text)
  117. .tooltip({placement: 'top'})
  118. );
  119. $tr.append(td);
  120. }
  121. return $tr;
  122. },
  123. /**
  124. * Set whether the list should contain outgoing shares
  125. * or incoming shares.
  126. *
  127. * @param state true for incoming shares, false otherwise
  128. */
  129. setSharedWithUser: function(state) {
  130. this._sharedWithUser = !!state;
  131. },
  132. updateEmptyContent: function() {
  133. var dir = this.getCurrentDirectory();
  134. if (dir === '/') {
  135. // root has special permissions
  136. this.$el.find('#emptycontent').toggleClass('hidden', !this.isEmpty);
  137. this.$el.find('#filestable thead th').toggleClass('hidden', this.isEmpty);
  138. // hide expiration date header for non link only shares
  139. if (!this._linksOnly) {
  140. this.$el.find('th.column-expiration').addClass('hidden');
  141. }
  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. * @return {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. }
  305. else {
  306. file.type = 'file';
  307. }
  308. file.share = {
  309. id: share.id,
  310. type: share.share_type,
  311. target: share.share_with,
  312. stime: share.stime * 1000,
  313. expiration: share.expiration,
  314. };
  315. if (sharedWithUser) {
  316. file.shareOwner = share.displayname_owner;
  317. file.shareOwnerId = share.uid_owner;
  318. file.name = OC.basename(share.file_target);
  319. file.path = OC.dirname(share.file_target);
  320. file.permissions = share.permissions;
  321. if (file.path) {
  322. file.extraData = share.file_target;
  323. }
  324. }
  325. else {
  326. if (share.share_type !== OC.Share.SHARE_TYPE_LINK) {
  327. file.share.targetDisplayName = share.share_with_displayname;
  328. file.share.targetShareWithId = share.share_with;
  329. }
  330. file.name = OC.basename(share.path);
  331. file.path = OC.dirname(share.path);
  332. file.permissions = OC.PERMISSION_ALL;
  333. if (file.path) {
  334. file.extraData = share.path;
  335. }
  336. }
  337. return file;
  338. })
  339. // Group all files and have a "shares" array with
  340. // the share info for each file.
  341. //
  342. // This uses a hash memo to cumulate share information
  343. // inside the same file object (by file id).
  344. .reduce(function(memo, file) {
  345. var data = memo[file.id];
  346. var recipient = file.share.targetDisplayName;
  347. var recipientId = file.share.targetShareWithId;
  348. if (!data) {
  349. data = memo[file.id] = file;
  350. data.shares = [file.share];
  351. // using a hash to make them unique,
  352. // this is only a list to be displayed
  353. data.recipients = {};
  354. data.recipientData = {};
  355. // share types
  356. data.shareTypes = {};
  357. // counter is cheaper than calling _.keys().length
  358. data.recipientsCount = 0;
  359. data.mtime = file.share.stime;
  360. }
  361. else {
  362. // always take the most recent stime
  363. if (file.share.stime > data.mtime) {
  364. data.mtime = file.share.stime;
  365. }
  366. data.shares.push(file.share);
  367. }
  368. if (recipient) {
  369. // limit counterparts for output
  370. if (data.recipientsCount < 4) {
  371. // only store the first ones, they will be the only ones
  372. // displayed
  373. data.recipients[recipient] = true;
  374. data.recipientData[data.recipientsCount] = {
  375. 'shareWith': recipientId,
  376. 'shareWithDisplayName': recipient
  377. };
  378. }
  379. data.recipientsCount++;
  380. }
  381. data.shareTypes[file.share.type] = true;
  382. delete file.share;
  383. return memo;
  384. }, {})
  385. // Retrieve only the values of the returned hash
  386. .values()
  387. // Clean up
  388. .each(function(data) {
  389. // convert the recipients map to a flat
  390. // array of sorted names
  391. data.mountType = 'shared';
  392. delete data.recipientsCount;
  393. if (sharedWithUser) {
  394. // only for outgoing shares
  395. delete data.shareTypes;
  396. } else {
  397. data.shareTypes = _.keys(data.shareTypes);
  398. }
  399. })
  400. // Finish the chain by getting the result
  401. .value();
  402. // Sort by expected sort comparator
  403. return files.sort(this._sortComparator);
  404. },
  405. });
  406. /**
  407. * Share info attributes.
  408. *
  409. * @typedef {Object} OCA.Sharing.ShareInfo
  410. *
  411. * @property {int} id share ID
  412. * @property {int} type share type
  413. * @property {String} target share target, either user name or group name
  414. * @property {int} stime share timestamp in milliseconds
  415. * @property {String} [targetDisplayName] display name of the recipient
  416. * (only when shared with others)
  417. * @property {String} [targetShareWithId] id of the recipient
  418. *
  419. */
  420. /**
  421. * Recipient attributes
  422. *
  423. * @typedef {Object} OCA.Sharing.RecipientInfo
  424. * @property {String} shareWith the id of the recipient
  425. * @property {String} shareWithDisplayName the display name of the recipient
  426. */
  427. /**
  428. * Shared file info attributes.
  429. *
  430. * @typedef {OCA.Files.FileInfo} OCA.Sharing.SharedFileInfo
  431. *
  432. * @property {Array.<OCA.Sharing.ShareInfo>} shares array of shares for
  433. * this file
  434. * @property {int} mtime most recent share time (if multiple shares)
  435. * @property {String} shareOwner name of the share owner
  436. * @property {Array.<String>} recipients name of the first 4 recipients
  437. * (this is mostly for display purposes)
  438. * @property {Object.<OCA.Sharing.RecipientInfo>} recipientData (as object for easier
  439. * passing to HTML data attributes with jQuery)
  440. */
  441. OCA.Sharing.FileList = FileList;
  442. })();