Don't show contacts an entry for themselves

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2017-04-10 16:49:26 +02:00
parent 36cee1f386
commit b8c2a8ae36
5 changed files with 60 additions and 15 deletions

View File

@ -26,6 +26,7 @@ namespace OC\Contacts\ContactsMenu;
use OCP\Contacts\ContactsMenu\IEntry; use OCP\Contacts\ContactsMenu\IEntry;
use OCP\Contacts\IManager; use OCP\Contacts\IManager;
use OCP\IUser;
class ContactsStore { class ContactsStore {
@ -40,17 +41,22 @@ class ContactsStore {
} }
/** /**
* @param IUser $user
* @param string|null $filter * @param string|null $filter
* @return IEntry[] * @return IEntry[]
*/ */
public function getContacts($filter) { public function getContacts(IUser $user, $filter) {
$allContacts = $this->contactsManager->search($filter ?: '', [ $allContacts = $this->contactsManager->search($filter ?: '', [
'FN', 'FN',
]); ]);
return array_map(function(array $contact) { $self = $user->getUID();
$entries = array_map(function(array $contact) {
return $this->contactArrayToEntry($contact); return $this->contactArrayToEntry($contact);
}, $allContacts); }, $allContacts);
return array_filter($entries, function(IEntry $entry) use ($self) {
return $entry->getProperty('UID') !== $self;
});
} }
/** /**

View File

@ -26,7 +26,6 @@ namespace OC\Contacts\ContactsMenu;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\Contacts\ContactsMenu\IEntry; use OCP\Contacts\ContactsMenu\IEntry;
use OCP\IURLGenerator;
use OCP\IUser; use OCP\IUser;
class Manager { class Manager {
@ -57,7 +56,7 @@ class Manager {
* @return array * @return array
*/ */
public function getEntries(IUser $user, $filter) { public function getEntries(IUser $user, $filter) {
$entries = $this->store->getContacts($filter); $entries = $this->store->getContacts($user, $filter);
$sortedEntries = $this->sortEntries($entries); $sortedEntries = $this->sortEntries($entries);
$topEntries = array_slice($sortedEntries, 0, 25); $topEntries = array_slice($sortedEntries, 0, 25);

View File

@ -32,6 +32,7 @@ interface IProvider {
/** /**
* @since 12.0 * @since 12.0
* @param IEntry $entry * @param IEntry $entry
* @return void
*/ */
public function process(IEntry $entry); public function process(IEntry $entry);
} }

View File

@ -26,6 +26,7 @@ namespace Tests\Contacts\ContactsMenu;
use OC\Contacts\ContactsMenu\ContactsStore; use OC\Contacts\ContactsMenu\ContactsStore;
use OCP\Contacts\IManager; use OCP\Contacts\IManager;
use OCP\IUser;
use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_MockObject_MockObject;
use Test\TestCase; use Test\TestCase;
@ -46,23 +47,27 @@ class ContactsStoreTest extends TestCase {
} }
public function testGetContactsWithoutFilter() { public function testGetContactsWithoutFilter() {
$user = $this->createMock(IUser::class);
$this->contactsManager->expects($this->once()) $this->contactsManager->expects($this->once())
->method('search') ->method('search')
->with($this->equalTo(''), $this->equalTo(['FN'])) ->with($this->equalTo(''), $this->equalTo(['FN']))
->willReturn([ ->willReturn([
[ [
'id' => 123, 'UID' => 123,
], ],
[ [
'id' => 567, 'UID' => 567,
'FN' => 'Darren Roner', 'FN' => 'Darren Roner',
'EMAIL' => [ 'EMAIL' => [
'darren@roner.au' 'darren@roner.au'
], ],
], ],
]); ]);
$user->expects($this->once())
->method('getUID')
->willReturn('user123');
$entries = $this->contactsStore->getContacts(''); $entries = $this->contactsStore->getContacts($user, '');
$this->assertCount(2, $entries); $this->assertCount(2, $entries);
$this->assertEquals([ $this->assertEquals([
@ -70,16 +75,43 @@ class ContactsStoreTest extends TestCase {
], $entries[1]->getEMailAddresses()); ], $entries[1]->getEMailAddresses());
} }
public function testGetContactsWithoutBinaryImage() { public function testGetContactsHidesOwnEntry() {
$user = $this->createMock(IUser::class);
$this->contactsManager->expects($this->once()) $this->contactsManager->expects($this->once())
->method('search') ->method('search')
->with($this->equalTo(''), $this->equalTo(['FN'])) ->with($this->equalTo(''), $this->equalTo(['FN']))
->willReturn([ ->willReturn([
[ [
'id' => 123, 'UID' => 'user123',
], ],
[ [
'id' => 567, 'UID' => 567,
'FN' => 'Darren Roner',
'EMAIL' => [
'darren@roner.au'
],
],
]);
$user->expects($this->once())
->method('getUID')
->willReturn('user123');
$entries = $this->contactsStore->getContacts($user, '');
$this->assertCount(1, $entries);
}
public function testGetContactsWithoutBinaryImage() {
$user = $this->createMock(IUser::class);
$this->contactsManager->expects($this->once())
->method('search')
->with($this->equalTo(''), $this->equalTo(['FN']))
->willReturn([
[
'UID' => 123,
],
[
'UID' => 567,
'FN' => 'Darren Roner', 'FN' => 'Darren Roner',
'EMAIL' => [ 'EMAIL' => [
'darren@roner.au' 'darren@roner.au'
@ -87,23 +119,27 @@ class ContactsStoreTest extends TestCase {
'PHOTO' => base64_encode('photophotophoto'), 'PHOTO' => base64_encode('photophotophoto'),
], ],
]); ]);
$user->expects($this->once())
->method('getUID')
->willReturn('user123');
$entries = $this->contactsStore->getContacts(''); $entries = $this->contactsStore->getContacts($user, '');
$this->assertCount(2, $entries); $this->assertCount(2, $entries);
$this->assertNull($entries[1]->getAvatar()); $this->assertNull($entries[1]->getAvatar());
} }
public function testGetContactsWithoutAvatarURI() { public function testGetContactsWithoutAvatarURI() {
$user = $this->createMock(IUser::class);
$this->contactsManager->expects($this->once()) $this->contactsManager->expects($this->once())
->method('search') ->method('search')
->with($this->equalTo(''), $this->equalTo(['FN'])) ->with($this->equalTo(''), $this->equalTo(['FN']))
->willReturn([ ->willReturn([
[ [
'id' => 123, 'UID' => 123,
], ],
[ [
'id' => 567, 'UID' => 567,
'FN' => 'Darren Roner', 'FN' => 'Darren Roner',
'EMAIL' => [ 'EMAIL' => [
'darren@roner.au' 'darren@roner.au'
@ -111,8 +147,11 @@ class ContactsStoreTest extends TestCase {
'PHOTO' => 'VALUE=uri:https://photo', 'PHOTO' => 'VALUE=uri:https://photo',
], ],
]); ]);
$user->expects($this->once())
->method('getUID')
->willReturn('user123');
$entries = $this->contactsStore->getContacts(''); $entries = $this->contactsStore->getContacts($user, '');
$this->assertCount(2, $entries); $this->assertCount(2, $entries);
$this->assertEquals('https://photo', $entries[1]->getAvatar()); $this->assertEquals('https://photo', $entries[1]->getAvatar());

View File

@ -77,7 +77,7 @@ class ManagerTest extends TestCase {
$provider = $this->createMock(IProvider::class); $provider = $this->createMock(IProvider::class);
$this->contactsStore->expects($this->once()) $this->contactsStore->expects($this->once())
->method('getContacts') ->method('getContacts')
->with($filter) ->with($user, $filter)
->willReturn($entries); ->willReturn($entries);
$this->actionProviderStore->expects($this->once()) $this->actionProviderStore->expects($this->once())
->method('getProviders') ->method('getProviders')