Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#180 fixed duplicate addressbooks showing up when an adressbook is publicly shared and delegated to a user #181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/esn.contact.libs/app/addressBook/addressbook-helper.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/esn.contact.libs/app/addressBook/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const _ = require('lodash');

require('../../contact.service.js');

angular.module('linagora.esn.contact')
Expand All @@ -11,7 +9,8 @@ function contactCopyController(
asyncAction,
contactAddressbookDisplayService,
contactAddressbookService,
contactService
contactService,
contactAddressbookHelper
) {
var self = this;
var NOTIFICATION_MESSAGES = {
Expand All @@ -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;
});
}
Expand All @@ -40,13 +40,9 @@ function contactCopyController(
});
}

function _excludeCurrentAddressbook(addressbooks) {
function _excludeCurrentAddressbookShell(addressbookShells) {
const { addressbook: { href } } = self.contact;

_.remove(addressbooks, function(addressbook) {
return self.contact.addressbook.bookName === addressbook.bookName;
});

return addressbooks;
return addressbookShells.filter(({ shell: { href: addressbookHref } }) => addressbookHref !== href);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ describe('The contactActionCopyController controller', function() {
}

var defaultAddressbookShell = {
bookName: 'contacts'
bookName: 'contacts',
href: '0'
};
var addressbookShell = {
bookName: 'collected'
bookName: 'collected',
href: '1'
};

describe('The listPossibleDestinations function', function() {
Expand All @@ -61,7 +63,8 @@ describe('The contactActionCopyController controller', function() {
controller.contact = {
id: '123',
addressbook: {
bookName: 'contacts'
bookName: 'contacts',
href: '0'
}
};
controller.listPossibleDestinations();
Expand Down Expand Up @@ -105,7 +108,8 @@ describe('The contactActionCopyController controller', function() {
controller.contact = {
id: '123',
addressbook: {
bookName: 'contacts'
bookName: 'contacts',
href: '0'
}
};
controller.selectedAddressbook = destAddressbook;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const _ = require('lodash');

require('../../contact.service.js');

angular.module('linagora.esn.contact')
Expand All @@ -11,7 +9,8 @@ function contactActionMoveController(
asyncAction,
contactAddressbookDisplayService,
contactAddressbookService,
contactService
contactService,
contactAddressbookHelper
) {
var self = this;
var NOTIFICATION_MESSAGES = {
Expand All @@ -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;
});
}

Expand All @@ -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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ describe('The contactActionMoveController controller', function() {
});

defaultAddressbookShell = {
bookName: 'contacts'
bookName: 'contacts',
href: '0'
};
addressbookShell = {
bookName: 'collected'
bookName: 'collected',
href: '1'
};
});

Expand Down Expand Up @@ -62,7 +64,8 @@ describe('The contactActionMoveController controller', function() {
controller.contact = {
id: '123',
addressbook: {
bookName: defaultAddressbookShell.bookName
bookName: defaultAddressbookShell.bookName,
href: '0'
}
};
controller.listPossbileDestinations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ angular.module('linagora.esn.contact')
function contactEditionForm(
contactAddressbookDisplayService,
contactAddressbookService,
contactAddressbookHelper,
CONTACT_ATTRIBUTES_ORDER,
CONTACT_AVATAR_SIZE
) {
Expand All @@ -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
}));
});
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/linagora.esn.contact/app/sidebar/sidebar.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function ContactSidebarController(
userUtils,
contactAddressbookDisplayService,
contactAddressbookService,
contactAddressbookHelper,
CONTACT_ADDRESSBOOK_EVENTS
) {
var self = this;
Expand Down Expand Up @@ -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;
}
}
42 changes: 37 additions & 5 deletions src/linagora.esn.contact/app/sidebar/sidebar.controller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
};
};
});
});
});

Expand Down Expand Up @@ -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 = [
Expand Down