share.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (c) 2014
  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. _.extend(OC.Files.Client, {
  12. PROPERTY_SHARE_TYPES: '{' + OC.Files.Client.NS_OWNCLOUD + '}share-types',
  13. PROPERTY_OWNER_DISPLAY_NAME: '{' + OC.Files.Client.NS_OWNCLOUD + '}owner-display-name'
  14. });
  15. if (!OCA.Sharing) {
  16. OCA.Sharing = {};
  17. }
  18. /**
  19. * @namespace
  20. */
  21. OCA.Sharing.Util = {
  22. /**
  23. * Initialize the sharing plugin.
  24. *
  25. * Registers the "Share" file action and adds additional
  26. * DOM attributes for the sharing file info.
  27. *
  28. * @param {OCA.Files.FileList} fileList file list to be extended
  29. */
  30. attach: function(fileList) {
  31. // core sharing is disabled/not loaded
  32. if (!OC.Share) {
  33. return;
  34. }
  35. if (fileList.id === 'trashbin' || fileList.id === 'files.public') {
  36. return;
  37. }
  38. var fileActions = fileList.fileActions;
  39. var oldCreateRow = fileList._createRow;
  40. fileList._createRow = function(fileData) {
  41. var tr = oldCreateRow.apply(this, arguments);
  42. var sharePermissions = OCA.Sharing.Util.getSharePermissions(fileData);
  43. tr.attr('data-share-permissions', sharePermissions);
  44. if (fileData.shareOwner) {
  45. tr.attr('data-share-owner', fileData.shareOwner);
  46. tr.attr('data-share-owner-id', fileData.shareOwnerId);
  47. // user should always be able to rename a mount point
  48. if (fileData.mountType === 'shared-root') {
  49. tr.attr('data-permissions', fileData.permissions | OC.PERMISSION_UPDATE);
  50. }
  51. }
  52. if (fileData.recipientData && !_.isEmpty(fileData.recipientData)) {
  53. tr.attr('data-share-recipient-data', JSON.stringify(fileData.recipientData));
  54. }
  55. if (fileData.shareTypes) {
  56. tr.attr('data-share-types', fileData.shareTypes.join(','));
  57. }
  58. return tr;
  59. };
  60. var oldElementToFile = fileList.elementToFile;
  61. fileList.elementToFile = function($el) {
  62. var fileInfo = oldElementToFile.apply(this, arguments);
  63. fileInfo.sharePermissions = $el.attr('data-share-permissions') || undefined;
  64. fileInfo.shareOwner = $el.attr('data-share-owner') || undefined;
  65. if( $el.attr('data-share-types')){
  66. fileInfo.shareTypes = $el.attr('data-share-types').split(',');
  67. }
  68. if( $el.attr('data-expiration')){
  69. var expirationTimestamp = parseInt($el.attr('data-expiration'));
  70. fileInfo.shares = [];
  71. fileInfo.shares.push({expiration: expirationTimestamp});
  72. }
  73. return fileInfo;
  74. };
  75. var oldGetWebdavProperties = fileList._getWebdavProperties;
  76. fileList._getWebdavProperties = function() {
  77. var props = oldGetWebdavProperties.apply(this, arguments);
  78. props.push(OC.Files.Client.PROPERTY_OWNER_DISPLAY_NAME);
  79. props.push(OC.Files.Client.PROPERTY_SHARE_TYPES);
  80. return props;
  81. };
  82. fileList.filesClient.addFileInfoParser(function(response) {
  83. var data = {};
  84. var props = response.propStat[0].properties;
  85. var permissionsProp = props[OC.Files.Client.PROPERTY_PERMISSIONS];
  86. if (permissionsProp && permissionsProp.indexOf('S') >= 0) {
  87. data.shareOwner = props[OC.Files.Client.PROPERTY_OWNER_DISPLAY_NAME];
  88. }
  89. var shareTypesProp = props[OC.Files.Client.PROPERTY_SHARE_TYPES];
  90. if (shareTypesProp) {
  91. data.shareTypes = _.chain(shareTypesProp).filter(function(xmlvalue) {
  92. return (xmlvalue.namespaceURI === OC.Files.Client.NS_OWNCLOUD && xmlvalue.nodeName.split(':')[1] === 'share-type');
  93. }).map(function(xmlvalue) {
  94. return parseInt(xmlvalue.textContent || xmlvalue.text, 10);
  95. }).value();
  96. }
  97. return data;
  98. });
  99. // use delegate to catch the case with multiple file lists
  100. fileList.$el.on('fileActionsReady', function(ev){
  101. var $files = ev.$files;
  102. _.each($files, function(file) {
  103. var $tr = $(file);
  104. var shareTypes = $tr.attr('data-share-types') || '';
  105. var shareOwner = $tr.attr('data-share-owner');
  106. if (shareTypes || shareOwner) {
  107. var hasLink = false;
  108. var hasShares = false;
  109. _.each(shareTypes.split(',') || [], function(shareType) {
  110. shareType = parseInt(shareType, 10);
  111. if (shareType === OC.Share.SHARE_TYPE_LINK) {
  112. hasLink = true;
  113. } else if (shareType === OC.Share.SHARE_TYPE_EMAIL) {
  114. hasLink = true;
  115. } else if (shareType === OC.Share.SHARE_TYPE_USER) {
  116. hasShares = true;
  117. } else if (shareType === OC.Share.SHARE_TYPE_GROUP) {
  118. hasShares = true;
  119. } else if (shareType === OC.Share.SHARE_TYPE_REMOTE) {
  120. hasShares = true;
  121. } else if (shareType === OC.Share.SHARE_TYPE_CIRCLE) {
  122. hasShares = true;
  123. }
  124. });
  125. OCA.Sharing.Util._updateFileActionIcon($tr, hasShares, hasLink);
  126. }
  127. });
  128. });
  129. fileList.$el.on('changeDirectory', function() {
  130. OCA.Sharing.sharesLoaded = false;
  131. });
  132. fileActions.registerAction({
  133. name: 'Share',
  134. displayName: '',
  135. altText: t('core', 'Share'),
  136. mime: 'all',
  137. permissions: OC.PERMISSION_ALL,
  138. iconClass: 'icon-shared',
  139. type: OCA.Files.FileActions.TYPE_INLINE,
  140. actionHandler: function(fileName) {
  141. fileList.showDetailsView(fileName, 'shareTabView');
  142. },
  143. render: function(actionSpec, isDefault, context) {
  144. var permissions = parseInt(context.$file.attr('data-permissions'), 10);
  145. // if no share permissions but share owner exists, still show the link
  146. if ((permissions & OC.PERMISSION_SHARE) !== 0 || context.$file.attr('data-share-owner')) {
  147. return fileActions._defaultRenderAction.call(fileActions, actionSpec, isDefault, context);
  148. }
  149. // don't render anything
  150. return null;
  151. }
  152. });
  153. var shareTab = new OCA.Sharing.ShareTabView('shareTabView', {order: -20});
  154. // detect changes and change the matching list entry
  155. shareTab.on('sharesChanged', function(shareModel) {
  156. var fileInfoModel = shareModel.fileInfoModel;
  157. var $tr = fileList.findFileEl(fileInfoModel.get('name'));
  158. // We count email shares as link share
  159. var hasLinkShare = shareModel.hasLinkShare();
  160. shareModel.get('shares').forEach(function (share) {
  161. if (share.share_type === OC.Share.SHARE_TYPE_EMAIL) {
  162. hasLinkShare = true;
  163. }
  164. });
  165. OCA.Sharing.Util._updateFileListDataAttributes(fileList, $tr, shareModel);
  166. if (!OCA.Sharing.Util._updateFileActionIcon($tr, shareModel.hasUserShares(), hasLinkShare)) {
  167. // remove icon, if applicable
  168. OC.Share.markFileAsShared($tr, false, false);
  169. }
  170. // FIXME: this is too convoluted. We need to get rid of the above updates
  171. // and only ever update the model and let the events take care of rerendering
  172. fileInfoModel.set({
  173. shareTypes: shareModel.getShareTypes(),
  174. // in case markFileAsShared decided to change the icon,
  175. // we need to modify the model
  176. // (FIXME: yes, this is hacky)
  177. icon: $tr.attr('data-icon')
  178. });
  179. });
  180. fileList.registerTabView(shareTab);
  181. var breadCrumbSharingDetailView = new OCA.Sharing.ShareBreadCrumbView({shareTab: shareTab});
  182. fileList.registerBreadCrumbDetailView(breadCrumbSharingDetailView);
  183. },
  184. /**
  185. * Update file list data attributes
  186. */
  187. _updateFileListDataAttributes: function(fileList, $tr, shareModel) {
  188. // files app current cannot show recipients on load, so we don't update the
  189. // icon when changed for consistency
  190. if (fileList.id === 'files') {
  191. return;
  192. }
  193. var recipients = _.pluck(shareModel.get('shares'), 'share_with_displayname');
  194. // note: we only update the data attribute because updateIcon()
  195. if (recipients.length) {
  196. var recipientData = _.mapObject(shareModel.get('shares'), function (share) {
  197. return {shareWith: share.share_with, shareWithDisplayName: share.share_with_displayname};
  198. });
  199. $tr.attr('data-share-recipient-data', JSON.stringify(recipientData));
  200. }
  201. else {
  202. $tr.removeAttr('data-share-recipient-data');
  203. }
  204. },
  205. /**
  206. * Update the file action share icon for the given file
  207. *
  208. * @param $tr file element of the file to update
  209. * @param {boolean} hasUserShares true if a user share exists
  210. * @param {boolean} hasLinkShare true if a link share exists
  211. *
  212. * @return {boolean} true if the icon was set, false otherwise
  213. */
  214. _updateFileActionIcon: function($tr, hasUserShares, hasLinkShare) {
  215. // if the statuses are loaded already, use them for the icon
  216. // (needed when scrolling to the next page)
  217. if (hasUserShares || hasLinkShare || $tr.attr('data-share-recipient-data') || $tr.attr('data-share-owner')) {
  218. OC.Share.markFileAsShared($tr, true, hasLinkShare);
  219. return true;
  220. }
  221. return false;
  222. },
  223. /**
  224. * @param {Array} fileData
  225. * @returns {String}
  226. */
  227. getSharePermissions: function(fileData) {
  228. var sharePermissions = fileData.permissions;
  229. if (fileData.mountType && fileData.mountType === "external-root"){
  230. // for external storages we can't use the permissions of the mountpoint
  231. // instead we show all permissions and only use the share permissions from the mountpoint to handle resharing
  232. sharePermissions = sharePermissions | (OC.PERMISSION_ALL & ~OC.PERMISSION_SHARE);
  233. }
  234. if (fileData.type === 'file') {
  235. // files can't be shared with delete permissions
  236. sharePermissions = sharePermissions & ~OC.PERMISSION_DELETE;
  237. // create permissions don't mean anything for files
  238. sharePermissions = sharePermissions & ~OC.PERMISSION_CREATE;
  239. }
  240. return sharePermissions;
  241. }
  242. };
  243. })();
  244. OC.Plugins.register('OCA.Files.FileList', OCA.Sharing.Util);