diff options
author | Kristian Lebold <kristian@lebold.info> | 2021-02-15 16:35:45 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2021-03-12 17:05:26 +0300 |
commit | b3b874380dbedfed0a2331ec8b65700da3fe5c74 (patch) | |
tree | 1ecc7bfdffdfb5fc1ad514cc35ef3b33f5aa698c /lib | |
parent | b4aacab9706d0c1dc3fdc2011b016a57f32cd8f5 (diff) |
Allow adding senders to the address book
closes #4458
Signed-off-by: Kristian Lebold <kristian@lebold.info>
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/ContactIntegrationController.php | 99 | ||||
-rw-r--r-- | lib/Service/ContactIntegration/ContactIntegrationService.php | 53 | ||||
-rw-r--r-- | lib/Service/ContactsIntegration.php | 105 |
3 files changed, 257 insertions, 0 deletions
diff --git a/lib/Controller/ContactIntegrationController.php b/lib/Controller/ContactIntegrationController.php new file mode 100644 index 000000000..81f49bb7a --- /dev/null +++ b/lib/Controller/ContactIntegrationController.php @@ -0,0 +1,99 @@ +<?php + +declare(strict_types=1); + +/** + * @author Kristian Lebold <kristian@lebold.info> + * + * 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); + } +} diff --git a/lib/Service/ContactIntegration/ContactIntegrationService.php b/lib/Service/ContactIntegration/ContactIntegrationService.php new file mode 100644 index 000000000..3579a3ebd --- /dev/null +++ b/lib/Service/ContactIntegration/ContactIntegrationService.php @@ -0,0 +1,53 @@ +<?php + +declare(strict_types=1); + +/** + * @author Kristian Lebold <kristian@lebold.info> + * + * 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); + } +} diff --git a/lib/Service/ContactsIntegration.php b/lib/Service/ContactsIntegration.php index 2f5a1f139..52c32582f 100644 --- a/lib/Service/ContactsIntegration.php +++ b/lib/Service/ContactsIntegration.php @@ -118,4 +118,109 @@ class ContactsIntegration { 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']); + } } |