Skip to content
Merged
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
20 changes: 20 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@
'url' => '/api/accounts/{id}/quota',
'verb' => 'GET'
],
[
'name' => 'contactIntegration#autoComplete',
'url' => '/api/contactIntegration/autoComplete/{term}',
'verb' => 'GET'
],
[
'name' => 'contactIntegration#addMail',
'url' => '/api/contactIntegration/add',
'verb' => 'PUT'
],
[
'name' => 'contactIntegration#newContact',
'url' => '/api/contactIntegration/new',
'verb' => 'PUT'
],
[
'name' => 'contactIntegration#match',
'url' => '/api/contactIntegration/match/{mail}',
'verb' => 'GET'
],
[
'name' => 'mailboxes#patch',
'url' => '/api/mailboxes/{id}',
Expand Down
99 changes: 99 additions & 0 deletions lib/Controller/ContactIntegrationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

/**
* @author Kristian Lebold <[email protected]>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Mail\Controller;

use OCA\Mail\Service\ContactIntegration\ContactIntegrationService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

class ContactIntegrationController extends Controller {

/** @var ContactIntegrationService */
private $service;

public function __construct(string $appName,
IRequest $request,
ContactIntegrationService $service) {
parent::__construct($appName, $request);

$this->service = $service;
}

/**
* @NoAdminRequired
* @TrapError
*
* @param string $mail
* @return JSONResponse
*/
public function match(string $mail): JSONResponse {
return new JSONResponse($this->service->findMatches($mail));
}

/**
* @NoAdminRequired
* @TrapError
*
* @param string $uid
* @param string $mail
* @return JSONResponse
*/
public function addMail(string $uid = null, string $mail = null): JSONResponse {
$res = $this->service->addEMailToContact($uid, $mail);
if ($res === null) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
return new JSONResponse($res);
}

/**
* @NoAdminRequired
* @TrapError
*
* @param string $name
* @param string $mail
* @return JSONResponse
*/
public function newContact(string $contactName = null, string $mail = null): JSONResponse {
$res = $this->service->newContact($contactName, $mail);
if ($res === null) {
return new JSONResponse([], Http::STATUS_NOT_ACCEPTABLE);
}
return new JSONResponse($res);
}

/**
* @NoAdminRequired
* @TrapError
*
* @param string $term
* @return JSONResponse
*/
public function autoComplete(string $term): JSONResponse {
$res = $this->service->autoComplete($term);
return new JSONResponse($res);
}
}
53 changes: 53 additions & 0 deletions lib/Service/ContactIntegration/ContactIntegrationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/**
* @author Kristian Lebold <[email protected]>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Mail\Service\ContactIntegration;

use OCA\Mail\Service\ContactsIntegration;

class ContactIntegrationService {

/** @var ContactsIntegration */
private $contactsIntegration;

public function __construct(ContactsIntegration $ci) {
$this->contactsIntegration = $ci;
}

public function findMatches(string $mail): array {
$matches = $this->contactsIntegration->getContactsWithMail($mail);
return $matches;
}

public function addEMailToContact(string $uid, string $mail): ?array {
return $this->contactsIntegration->addEmailToContact($uid, $mail);
}

public function newContact(string $name, string $mail): ?array {
return $this->contactsIntegration->newContact($name, $mail);
}

public function autoComplete(string $term): array {
return $this->contactsIntegration->getContactsWithName($term);
}
}
105 changes: 105 additions & 0 deletions lib/Service/ContactsIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,109 @@ private function getPhotoUri(string $raw) {
return null;
}
}

/**
* Adds a new email to an existing Contact
*
* @param string $uid
* @param string $mailAddr
* @param string $type
* @return array|null
*/
public function addEmailToContact(string $uid, string $mailAddr, string $type = 'HOME') {
if (!$this->contactsManager->isEnabled()) {
return null;
}

$result = $this->contactsManager->search($uid, ['UID'], ['types' => true, 'limit' => 1]);

if (count($result) !== 1) {
return null; // no match
}

$newEntry = [
'type' => $type,
'value' => $mailAddr
];

$match = $result[0];
$email = $match['EMAIL'] ?? [];
if (!empty($email) && !is_array($email[0])) {
$email = [$email];
}
$email[] = $newEntry;
$match['EMAIL'] = $email;

$updatedContact = $this->contactsManager->createOrUpdate($match, $match['addressbook-key']);
return $updatedContact;
}

/**
* Adds a new contact with the specified email to an addressbook
*
* @param string $uid
* @param string $mailAddr
* @param string $addressbook
* @return array|null
*/
public function newContact(string $name, string $mailAddr, string $type = 'HOME', string $addressbook = null) {
if (!$this->contactsManager->isEnabled()) {
return null;
}

if (!isset($addressbook)) {
$addressbook = key($this->contactsManager->getUserAddressBooks());
}

$contact = [
'FN' => $name,
'EMAIL' => [
[
'type' => $type,
'value' => $mailAddr
]
]
];
$createdContact = $this->contactsManager->createOrUpdate($contact, $addressbook);
return $createdContact;
}

private function doSearch($term, $fields): array {
$allowSystemUsers = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'no') === 'yes';

$result = $this->contactsManager->search($term, $fields);
$matches = [];
foreach ($result as $r) {
if (!$allowSystemUsers && isset($r['isLocalSystemBook']) && $r['isLocalSystemBook']) {
continue;
}
$id = $r['UID'];
$fn = $r['FN'];
$matches[] = [
'id' => $id,
'label' => $fn,
];
}
return $matches;
}

/**
* Extracts all Contacts with the specified mail address
*
* @param string $mailAddr
* @return array
*/
public function getContactsWithMail(string $mailAddr) {
return $this->doSearch($mailAddr, ['EMAIL']);
}

/**
* Extracts all Contacts with the specified name
*
* @param string $mailAddr
* @return array
*/
public function getContactsWithName($name) {
return $this->doSearch($name, ['FN']);
}
}
Loading