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

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