sharedfilelist.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. OC.Plugins.attach('OCA.Sharing.FileList', this);
  56. },
  57. _renderRow: function() {
  58. // HACK: needed to call the overridden _renderRow
  59. // this is because at the time this class is created
  60. // the overriding hasn't been done yet...
  61. return OCA.Files.FileList.prototype._renderRow.apply(this, arguments);
  62. },
  63. _createRow: function(fileData) {
  64. // TODO: hook earlier and render the whole row here
  65. var $tr = OCA.Files.FileList.prototype._createRow.apply(this, arguments);
  66. $tr.find('.filesize').remove();
  67. $tr.find('td.date').before($tr.children('td:first'));
  68. $tr.find('td.filename input:checkbox').remove();
  69. $tr.attr('data-share-id', _.pluck(fileData.shares, 'id').join(','));
  70. if (this._sharedWithUser) {
  71. $tr.attr('data-share-owner', fileData.shareOwner);
  72. $tr.attr('data-mounttype', 'shared-root');
  73. var permission = parseInt($tr.attr('data-permissions')) | OC.PERMISSION_DELETE;
  74. $tr.attr('data-permissions', permission);
  75. }
  76. return $tr;
  77. },
  78. /**
  79. * Set whether the list should contain outgoing shares
  80. * or incoming shares.
  81. *
  82. * @param state true for incoming shares, false otherwise
  83. */
  84. setSharedWithUser: function(state) {
  85. this._sharedWithUser = !!state;
  86. },
  87. updateEmptyContent: function() {
  88. var dir = this.getCurrentDirectory();
  89. if (dir === '/') {
  90. // root has special permissions
  91. this.$el.find('#emptycontent').toggleClass('hidden', !this.isEmpty);
  92. this.$el.find('#filestable thead th').toggleClass('hidden', this.isEmpty);
  93. }
  94. else {
  95. OCA.Files.FileList.prototype.updateEmptyContent.apply(this, arguments);
  96. }
  97. },
  98. getDirectoryPermissions: function() {
  99. var perms = OC.PERMISSION_READ;
  100. if (this._sharedWithUser) {
  101. perms |= OC.PERMISSION_DELETE;
  102. }
  103. return perms;
  104. },
  105. updateStorageStatistics: function() {
  106. // no op because it doesn't have
  107. // storage info like free space / used space
  108. },
  109. reload: function() {
  110. this.showMask();
  111. if (this._reloadCall) {
  112. this._reloadCall.abort();
  113. }
  114. this._reloadCall = $.ajax({
  115. url: OC.linkToOCS('apps/files_sharing/api/v1') + 'shares',
  116. /* jshint camelcase: false */
  117. data: {
  118. format: 'json',
  119. shared_with_me: !!this._sharedWithUser
  120. },
  121. type: 'GET',
  122. beforeSend: function(xhr) {
  123. xhr.setRequestHeader('OCS-APIREQUEST', 'true');
  124. }
  125. });
  126. var callBack = this.reloadCallback.bind(this);
  127. return this._reloadCall.then(callBack, callBack);
  128. },
  129. reloadCallback: function(result) {
  130. delete this._reloadCall;
  131. this.hideMask();
  132. this.$el.find('#headerSharedWith').text(
  133. t('files_sharing', this._sharedWithUser ? 'Shared by' : 'Shared with')
  134. );
  135. if (result.ocs && result.ocs.data) {
  136. this.setFiles(this._makeFilesFromShares(result.ocs.data));
  137. }
  138. else {
  139. // TODO: error handling
  140. }
  141. },
  142. /**
  143. * Converts the OCS API share response data to a file info
  144. * list
  145. * @param {Array} data OCS API share array
  146. * @return {Array.<OCA.Sharing.SharedFileInfo>} array of shared file info
  147. */
  148. _makeFilesFromShares: function(data) {
  149. /* jshint camelcase: false */
  150. var self = this;
  151. var files = data;
  152. if (this._linksOnly) {
  153. files = _.filter(data, function(share) {
  154. return share.share_type === OC.Share.SHARE_TYPE_LINK;
  155. });
  156. }
  157. // OCS API uses non-camelcased names
  158. files = _.chain(files)
  159. // convert share data to file data
  160. .map(function(share) {
  161. var file = {
  162. id: share.file_source,
  163. mimetype: share.mimetype
  164. };
  165. if (share.item_type === 'folder') {
  166. file.type = 'dir';
  167. file.mimetype = 'httpd/unix-directory';
  168. }
  169. else {
  170. file.type = 'file';
  171. if (share.isPreviewAvailable) {
  172. file.isPreviewAvailable = true;
  173. }
  174. }
  175. file.share = {
  176. id: share.id,
  177. type: share.share_type,
  178. target: share.share_with,
  179. stime: share.stime * 1000,
  180. };
  181. if (self._sharedWithUser) {
  182. file.shareOwner = share.displayname_owner;
  183. file.name = OC.basename(share.file_target);
  184. file.path = OC.dirname(share.file_target);
  185. file.permissions = share.permissions;
  186. if (file.path) {
  187. file.extraData = share.file_target;
  188. }
  189. }
  190. else {
  191. if (share.share_type !== OC.Share.SHARE_TYPE_LINK) {
  192. file.share.targetDisplayName = share.share_with_displayname;
  193. }
  194. file.name = OC.basename(share.path);
  195. file.path = OC.dirname(share.path);
  196. if (this._sharedWithUser) {
  197. file.permissions = OC.PERMISSION_ALL;
  198. } else {
  199. file.permissions = OC.PERMISSION_ALL - OC.PERMISSION_DELETE;
  200. }
  201. if (file.path) {
  202. file.extraData = share.path;
  203. }
  204. }
  205. return file;
  206. })
  207. // Group all files and have a "shares" array with
  208. // the share info for each file.
  209. //
  210. // This uses a hash memo to cumulate share information
  211. // inside the same file object (by file id).
  212. .reduce(function(memo, file) {
  213. var data = memo[file.id];
  214. var recipient = file.share.targetDisplayName;
  215. if (!data) {
  216. data = memo[file.id] = file;
  217. data.shares = [file.share];
  218. // using a hash to make them unique,
  219. // this is only a list to be displayed
  220. data.recipients = {};
  221. // counter is cheaper than calling _.keys().length
  222. data.recipientsCount = 0;
  223. data.mtime = file.share.stime;
  224. }
  225. else {
  226. // always take the most recent stime
  227. if (file.share.stime > data.mtime) {
  228. data.mtime = file.share.stime;
  229. }
  230. data.shares.push(file.share);
  231. }
  232. if (recipient) {
  233. // limit counterparts for output
  234. if (data.recipientsCount < 4) {
  235. // only store the first ones, they will be the only ones
  236. // displayed
  237. data.recipients[recipient] = true;
  238. }
  239. data.recipientsCount++;
  240. }
  241. delete file.share;
  242. return memo;
  243. }, {})
  244. // Retrieve only the values of the returned hash
  245. .values()
  246. // Clean up
  247. .each(function(data) {
  248. // convert the recipients map to a flat
  249. // array of sorted names
  250. data.mountType = 'shared';
  251. data.recipients = _.keys(data.recipients);
  252. data.recipientsDisplayName = OCA.Sharing.Util.formatRecipients(
  253. data.recipients,
  254. data.recipientsCount
  255. );
  256. delete data.recipientsCount;
  257. })
  258. // Finish the chain by getting the result
  259. .value();
  260. // Sort by expected sort comparator
  261. return files.sort(this._sortComparator);
  262. }
  263. });
  264. /**
  265. * Share info attributes.
  266. *
  267. * @typedef {Object} OCA.Sharing.ShareInfo
  268. *
  269. * @property {int} id share ID
  270. * @property {int} type share type
  271. * @property {String} target share target, either user name or group name
  272. * @property {int} stime share timestamp in milliseconds
  273. * @property {String} [targetDisplayName] display name of the recipient
  274. * (only when shared with others)
  275. *
  276. */
  277. /**
  278. * Shared file info attributes.
  279. *
  280. * @typedef {OCA.Files.FileInfo} OCA.Sharing.SharedFileInfo
  281. *
  282. * @property {Array.<OCA.Sharing.ShareInfo>} shares array of shares for
  283. * this file
  284. * @property {int} mtime most recent share time (if multiple shares)
  285. * @property {String} shareOwner name of the share owner
  286. * @property {Array.<String>} recipients name of the first 4 recipients
  287. * (this is mostly for display purposes)
  288. * @property {String} recipientsDisplayName display name
  289. */
  290. OCA.Sharing.FileList = FileList;
  291. })();