diff --git a/src/esn.contact.libs/app/addressBook/addressbook-helper.service.js b/src/esn.contact.libs/app/addressBook/addressbook-helper.service.js index b531da4..4f0018c 100644 --- a/src/esn.contact.libs/app/addressBook/addressbook-helper.service.js +++ b/src/esn.contact.libs/app/addressBook/addressbook-helper.service.js @@ -2,10 +2,11 @@ angular.module('esn.contact.libs') .factory('contactAddressbookHelper', contactAddressbookHelper); -function contactAddressbookHelper($q, contactAddressbookParser, contactDavClientService, CONTACT_ADDRESSBOOK_DAV_PROPERTIES, CONTACT_ACCEPT_HEADER) { +function contactAddressbookHelper($q, contactAddressbookParser, contactDavClientService, CONTACT_ADDRESSBOOK_DAV_PROPERTIES, CONTACT_ACCEPT_HEADER, CONTACT_SHARING_SUBSCRIPTION_TYPE) { return { populateSubscriptionSource, - formatAddressBookResponse + formatAddressBookResponse, + getUniqueAdressBookShells }; function populateSubscriptionSource(addressbook) { @@ -42,4 +43,21 @@ function contactAddressbookHelper($q, contactAddressbookParser, contactDavClient return { ...response, ...formattedAddressBook }; } + + function getUniqueAdressBookShells(addressbooks) { + if (!addressbooks) return; + + const uniqueAddressbookList = addressbooks.reduce((acc, current) => { + const { shell, shell: { subscriptionType } } = current; + const href = shell.source ? shell.source.href : shell.href; + + if (!acc[href] || subscriptionType === CONTACT_SHARING_SUBSCRIPTION_TYPE.delegation) { + acc[href] = current; + } + + return acc; + }, {}); + + return Object.values(uniqueAddressbookList); + } } diff --git a/src/esn.contact.libs/app/addressBook/constants.js b/src/esn.contact.libs/app/addressBook/constants.js index e774d72..210d2ea 100644 --- a/src/esn.contact.libs/app/addressBook/constants.js +++ b/src/esn.contact.libs/app/addressBook/constants.js @@ -43,4 +43,5 @@ angular.module('esn.contact.libs') public: 'public' }) - .constant('CONTACT_SHARING_SHARE_PRIVILEGE', '{DAV:}share'); + .constant('CONTACT_SHARING_SHARE_PRIVILEGE', '{DAV:}share') + .constant('CONTACT_SHARING_WRITE_PRIVILEGE', '{DAV:}write'); diff --git a/src/linagora.esn.contact/app/addressbook/settings/main/contact-addressbook-settings-main.controller.js b/src/linagora.esn.contact/app/addressbook/settings/main/contact-addressbook-settings-main.controller.js index 9c2d6a2..a18c3fb 100644 --- a/src/linagora.esn.contact/app/addressbook/settings/main/contact-addressbook-settings-main.controller.js +++ b/src/linagora.esn.contact/app/addressbook/settings/main/contact-addressbook-settings-main.controller.js @@ -13,7 +13,8 @@ function contactAddressbookSettingsMainController( CONTACT_SHARING_SHARE_ACCESS, CONTACT_SHARING_SUBSCRIPTION_TYPE, CONTACT_SHARING_SHARE_ACCESS_CHOICES, - CONTACT_ADDRESSBOOK_MEMBERS_RIGHTS + CONTACT_ADDRESSBOOK_MEMBERS_RIGHTS, + CONTACT_SHARING_WRITE_PRIVILEGE ) { var self = this; @@ -62,11 +63,22 @@ function contactAddressbookSettingsMainController( } function _initShareAccess() { - self.shareAccess = _.find( - CONTACT_SHARING_SHARE_ACCESS_CHOICES, { - value: self.addressbook.shareAccess - } - ); + const { READWRITE } = CONTACT_SHARING_SHARE_ACCESS_CHOICES; + + const access = Object.values(CONTACT_SHARING_SHARE_ACCESS_CHOICES) + .find(({ value }) => value === self.addressbook.shareAccess); + + if (!access) return; + + if (access.value >= READWRITE.value) { + self.shareAccess = access; + + return; + } + + self.shareAccess = self.addressbook.source.rights.public === CONTACT_SHARING_WRITE_PRIVILEGE ? + READWRITE : + access; } function _getShareOwner(sharees) { diff --git a/src/linagora.esn.contact/app/contact/action/copy/contact-action-copy.controller.js b/src/linagora.esn.contact/app/contact/action/copy/contact-action-copy.controller.js index 7dc12e9..563b5ec 100644 --- a/src/linagora.esn.contact/app/contact/action/copy/contact-action-copy.controller.js +++ b/src/linagora.esn.contact/app/contact/action/copy/contact-action-copy.controller.js @@ -1,7 +1,5 @@ 'use strict'; -const _ = require('lodash'); - require('../../contact.service.js'); angular.module('linagora.esn.contact') @@ -11,7 +9,8 @@ function contactCopyController( asyncAction, contactAddressbookDisplayService, contactAddressbookService, - contactService + contactService, + contactAddressbookHelper ) { var self = this; var NOTIFICATION_MESSAGES = { @@ -25,10 +24,11 @@ function contactCopyController( function listPossibleDestinations() { contactAddressbookService.listAddressbooksUserCanCreateContact() - .then(_excludeCurrentAddressbook) .then(contactAddressbookDisplayService.convertShellsToDisplayShells) - .then(function(availableAddressbookDisplayShells) { - self.availableAddressbookDisplayShells = availableAddressbookDisplayShells; + .then(displayShells => { + const uniqueShells = contactAddressbookHelper.getUniqueAdressBookShells(displayShells); + + self.availableAddressbookDisplayShells = _excludeCurrentAddressbookShell(uniqueShells); self.selectedAddressbook = self.availableAddressbookDisplayShells[0].shell; }); } @@ -40,13 +40,10 @@ function contactCopyController( }); } - function _excludeCurrentAddressbook(addressbooks) { - - _.remove(addressbooks, function(addressbook) { - return self.contact.addressbook.bookName === addressbook.bookName; - }); + function _excludeCurrentAddressbookShell(addressbookShells) { + const { addressbook: { href } } = self.contact; - return addressbooks; + return addressbookShells.filter(({ shell: { href: addressbookHref } }) => addressbookHref !== href); } } diff --git a/src/linagora.esn.contact/app/contact/action/move/contact-action-move.controller.js b/src/linagora.esn.contact/app/contact/action/move/contact-action-move.controller.js index efd6469..fbb8fac 100644 --- a/src/linagora.esn.contact/app/contact/action/move/contact-action-move.controller.js +++ b/src/linagora.esn.contact/app/contact/action/move/contact-action-move.controller.js @@ -1,7 +1,5 @@ 'use strict'; -const _ = require('lodash'); - require('../../contact.service.js'); angular.module('linagora.esn.contact') @@ -11,7 +9,8 @@ function contactActionMoveController( asyncAction, contactAddressbookDisplayService, contactAddressbookService, - contactService + contactService, + contactAddressbookHelper ) { var self = this; var NOTIFICATION_MESSAGES = { @@ -25,12 +24,12 @@ function contactActionMoveController( function listPossbileDestinations() { contactAddressbookService.listAddressbooksUserCanCreateContact() - .then(_excludeCurrentAddressbook) .then(contactAddressbookDisplayService.convertShellsToDisplayShells) - .then(function(availableAddressbookDisplayShells) { - self.availableAddressbookDisplayShells = availableAddressbookDisplayShells; - self.selectedAddressbook = self.availableAddressbookDisplayShells[0].shell; + .then(displayShells => { + const uniqueShells = contactAddressbookHelper.getUniqueAdressBookShells(displayShells); + self.availableAddressbookDisplayShells = _excludeCurrentAddressbookShell(uniqueShells); + self.selectedAddressbook = self.availableAddressbookDisplayShells[0].shell; }); } @@ -40,12 +39,10 @@ function contactActionMoveController( }); } - function _excludeCurrentAddressbook(addressbooks) { - _.remove(addressbooks, function(addressbook) { - return self.contact.addressbook.bookName === addressbook.bookName; - }); + function _excludeCurrentAddressbookShell(addressbookShells) { + const { addressbook: { href } } = self.contact; - return addressbooks; + return addressbookShells.filter(({ shell: { href: addressbookHref } }) => addressbookHref !== href); } } diff --git a/src/linagora.esn.contact/app/contact/form/contact-edition-form.directive.js b/src/linagora.esn.contact/app/contact/form/contact-edition-form.directive.js index bd89bf8..752a339 100644 --- a/src/linagora.esn.contact/app/contact/form/contact-edition-form.directive.js +++ b/src/linagora.esn.contact/app/contact/form/contact-edition-form.directive.js @@ -7,6 +7,7 @@ angular.module('linagora.esn.contact') function contactEditionForm( contactAddressbookDisplayService, contactAddressbookService, + contactAddressbookHelper, CONTACT_ATTRIBUTES_ORDER, CONTACT_AVATAR_SIZE ) { @@ -22,17 +23,16 @@ function contactEditionForm( $scope.CONTACT_ATTRIBUTES_ORDER = CONTACT_ATTRIBUTES_ORDER; $scope.avatarSize = CONTACT_AVATAR_SIZE.bigger; - contactAddressbookService.listAddressbooksUserCanCreateContact().then(function(addressbooks) { - return contactAddressbookDisplayService.convertShellsToDisplayShells(addressbooks, { includePriority: true }); - }) - .then(function(addressbookDisplayShells) { - $scope.availableAddressbooks = contactAddressbookDisplayService.sortAddressbookDisplayShells(addressbookDisplayShells) - .map(function(addressbookDisplayShell) { - return { - path: addressbookDisplayShell.shell.href, - displayName: addressbookDisplayShell.displayName - }; - }); + contactAddressbookService.listAddressbooksUserCanCreateContact() + .then(addressbooks => contactAddressbookDisplayService.convertShellsToDisplayShells(addressbooks, { includePriority: true })) + .then(displayShells => { + const uniqueAddressBookShells = contactAddressbookHelper.getUniqueAdressBookShells(displayShells); + + $scope.availableAddressbooks = contactAddressbookDisplayService.sortAddressbookDisplayShells(uniqueAddressBookShells) + .map(displayShell => ({ + path: displayShell.shell.href, + displayName: displayShell.displayName + })); }); } }; diff --git a/src/linagora.esn.contact/app/sidebar/sidebar.controller.js b/src/linagora.esn.contact/app/sidebar/sidebar.controller.js index dceaa79..a1b0182 100644 --- a/src/linagora.esn.contact/app/sidebar/sidebar.controller.js +++ b/src/linagora.esn.contact/app/sidebar/sidebar.controller.js @@ -12,6 +12,7 @@ function ContactSidebarController( userUtils, contactAddressbookDisplayService, contactAddressbookService, + contactAddressbookHelper, CONTACT_ADDRESSBOOK_EVENTS ) { var self = this; @@ -150,7 +151,7 @@ function ContactSidebarController( var categories = contactAddressbookDisplayService.categorizeDisplayShells(self.displayShells); self.userAddressbooks = categories.userAddressbooks; - self.sharedAddressbooks = categories.sharedAddressbooks; + self.sharedAddressbooks = contactAddressbookHelper.getUniqueAdressBookShells(categories.sharedAddressbooks); self.virtualAddressbooks = categories.virtualAddressbooks; } } diff --git a/src/linagora.esn.contact/app/sidebar/sidebar.controller.spec.js b/src/linagora.esn.contact/app/sidebar/sidebar.controller.spec.js index 42a9216..353c32b 100644 --- a/src/linagora.esn.contact/app/sidebar/sidebar.controller.spec.js +++ b/src/linagora.esn.contact/app/sidebar/sidebar.controller.spec.js @@ -46,13 +46,11 @@ describe('The ContactSidebarController controller', function() { userAPI = _userAPI_; userUtils = _userUtils_; CONTACT_ADDRESSBOOK_EVENTS = _CONTACT_ADDRESSBOOK_EVENTS_; - - contactAddressbookDisplayService.categorizeDisplayShells = function() { - return { + contactAddressbookDisplayService.categorizeDisplayShells = sinon.stub() + .returns({ userAddressbooks: [], externalAddressbooks: [] - }; - }; + }); }); }); @@ -260,6 +258,40 @@ describe('The ContactSidebarController controller', function() { ]); }); + it('should avoid listing duplicate entries when an adressbook is shared an delegated to the same user', () => { + contactAddressbookService.listAddressbooks = sinon.stub().returns($q.when([])); + contactAddressbookDisplayService.categorizeDisplayShells.returns({ + userAddressbooks: [ + { + name: 'bookA', + shell: { + source: { bookId: 'user0', href: '0' } + } + } + ], + sharedAddressbooks: [ + { + name: 'bookB', + shell: { + source: { bookId: 'user1', href: '1' } + } + }, + { + name: 'bookC', + shell: { + source: { bookId: 'user1', href: '1' } + } + } + ] + }); + + const controller = initController(); + + $rootScope.$digest(); + + expect(controller.sharedAddressbooks.length).to.equal(1); + }); + describe('On updated address book event', function() { it('should update an address book when updated address book event is fired', function() { var addressbooks = [