From 579819bee8029a806cd262187f355c0d1d62ace2 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Fri, 21 Nov 2014 13:03:15 +0100 Subject: [PATCH] also search for shared contacts --- lib/addressbookprovider.php | 64 ++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/lib/addressbookprovider.php b/lib/addressbookprovider.php index 9a441d41..1822a4fa 100644 --- a/lib/addressbookprovider.php +++ b/lib/addressbookprovider.php @@ -34,6 +34,7 @@ class AddressbookProvider implements \OCP\IAddressBook { const CONTACT_TABLE = '*PREFIX*contacts_cards'; const PROPERTY_TABLE = '*PREFIX*contacts_cards_properties'; + const ADDRESSBOOK_TABLE = '*PREFIX*contacts_addressbooks'; /** * Addressbook id @@ -92,23 +93,32 @@ class AddressbookProvider implements \OCP\IAddressBook { public function search($pattern, $searchProperties, $options) { $propTable = self::PROPERTY_TABLE; $contTable = self::CONTACT_TABLE; + $addrTable = self::ADDRESSBOOK_TABLE; $results = array(); + + /** + * This query will fetch all contacts which match the $searchProperties + * It will look up the addressbookid of the contact and the user id of the owner of the contact app + */ $query = <<fetchRow()) { $id = $row['contactid']; $addressbookKey = $row['addressbookid']; - try { - // gues that it is a local addressbook - $contact = $this->app->getContact('local', $addressbookKey, $id); - } catch (\Exception $e) { - if ($e->getCode() === 404) { - // not a local thus it is a shared + // Check if we are the owner of the contact + if ($row['userid'] !== \OCP\User::getUser()) { + // we aren't the owner of the contact + try { + // it is possible that the contact is shared with us + // if so, $contact will be an object + // if not getContact will throw an Exception $contact = $this->app->getContact('shared', $addressbookKey, $id); + } catch (\Exception $e){ + // the contact isn't shared with us + $contact = null; } + } else { + // We are the owner of the contact + // thus we can easily fetch it + $contact = $this->app->getContact('local', $addressbookKey, $id); } - $j = JSONSerializer::serializeContact($contact); - $j['data']['id'] = $id; - if (isset($contact->PHOTO)) { - $url = \OCP\Util::linkToRoute('contacts_contact_photo', - array( - 'backend' => $contact->getBackend()->name, - 'addressBookId' => $this->addressBook->getId(), - 'contactId' => $contact->getId() - )); - $url = \OC_Helper::makeURLAbsolute($url); - $j['data']['PHOTO'] = "VALUE=uri:$url"; + if ($contact !== null) { + $j = JSONSerializer::serializeContact($contact); + $j['data']['id'] = $id; + if (isset($contact->PHOTO)) { + $url = \OCP\Util::linkToRoute('contacts_contact_photo', + array( + 'backend' => $contact->getBackend()->name, + 'addressBookId' => $addressbookKey, + 'contactId' => $contact->getId() + )); + $url = \OC_Helper::makeURLAbsolute($url); + $j['data']['PHOTO'] = "VALUE=uri:$url"; + } + $results[] = $this->convertToSearchResult($j); } - $results[] = $this->convertToSearchResult($j); } return $results; }