Skip to content

Commit

Permalink
Merge pull request #1876 from nextcloud/shareesAPI_email
Browse files Browse the repository at this point in the history
Add ShareesAPI E-mail search
  • Loading branch information
MorrisJobke committed Oct 25, 2016
2 parents c0adc3c + a28528a commit 01a85a9
Show file tree
Hide file tree
Showing 2 changed files with 314 additions and 12 deletions.
70 changes: 70 additions & 0 deletions apps/files_sharing/lib/Controller/ShareesAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ class ShareesAPIController extends OCSController {
'users' => [],
'groups' => [],
'remotes' => [],
'emails' => [],
],
'users' => [],
'groups' => [],
'remotes' => [],
'emails' => [],
];

protected $reachedEndFor = [];
Expand Down Expand Up @@ -402,6 +404,68 @@ protected function fixRemoteURL($remote) {
return $remote;
}

/**
* @param string $search
*/
protected function getEmails($search) {
$this->result['emails'] = [];
$this->result['exact']['emails'] = [];

$foundEmail = false;

// Search in contacts
//@todo Pagination missing
$addressBookContacts = $this->contactsManager->search($search, ['FN', 'EMAIL']);
foreach ($addressBookContacts as $contact) {
if (!isset($contact['EMAIL'])) {
continue;
}

$emails = $contact['EMAIL'];
if (!is_array($emails)) {
$emails = [$emails];
}

foreach ($emails as $email) {
if (strtolower($search) === strtolower($contact['FN']) ||
strtolower($search) === strtolower($email)
) {
if (strtolower($search) === strtolower($email)) {
$foundEmail = true;
}

$this->result['exact']['emails'][] = [
'label' => $contact['FN'],
'value' => [
'shareType' => Share::SHARE_TYPE_EMAIL,
'shareWith' => $email,
],
];
} else if ($this->shareeEnumeration) {
$this->result['emails'][] = [
'label' => $contact['FN'],
'value' => [
'shareType' => Share::SHARE_TYPE_EMAIL,
'shareWith' => $email,
],
];
}
}
}

if (!$foundEmail && substr_count($search, '@') >= 1 && $this->offset === 0) {
$this->result['exact']['emails'][] = [
'label' => $search,
'value' => [
'shareType' => Share::SHARE_TYPE_EMAIL,
'shareWith' => $search,
],
];
}

$this->reachedEndFor[] = 'emails';
}

/**
* @NoAdminRequired
*
Expand Down Expand Up @@ -429,6 +493,7 @@ public function search($search = '', $itemType = null, $page = 1, $perPage = 200
$shareTypes[] = Share::SHARE_TYPE_GROUP;
}

$shareTypes[] = Share::SHARE_TYPE_EMAIL;
$shareTypes[] = Share::SHARE_TYPE_REMOTE;

if (is_array($shareType)) {
Expand Down Expand Up @@ -499,6 +564,11 @@ protected function searchSharees($search, $itemType, array $shareTypes, $page, $
$this->getRemote($search);
}

// Get email
if (in_array(Share::SHARE_TYPE_EMAIL, $shareTypes)) {
$this->getEmails($search);
}

$response = new Http\DataResponse($this->result);

if (sizeof($this->reachedEndFor) < 3) {
Expand Down
Loading

0 comments on commit 01a85a9

Please sign in to comment.