diff --git a/appinfo/routes.php b/appinfo/routes.php index 0f538e7866..d25b478db8 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -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}', diff --git a/lib/Controller/ContactIntegrationController.php b/lib/Controller/ContactIntegrationController.php new file mode 100644 index 0000000000..81f49bb7a3 --- /dev/null +++ b/lib/Controller/ContactIntegrationController.php @@ -0,0 +1,99 @@ + + * + * 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 + * + */ + +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 0000000000..3579a3ebd8 --- /dev/null +++ b/lib/Service/ContactIntegration/ContactIntegrationService.php @@ -0,0 +1,53 @@ + + * + * 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 + * + */ + +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 2f5a1f1393..52c32582fc 100644 --- a/lib/Service/ContactsIntegration.php +++ b/lib/Service/ContactsIntegration.php @@ -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']); + } } diff --git a/src/components/RecipientBubble.vue b/src/components/RecipientBubble.vue index 897f542a5a..48bfe3afb8 100644 --- a/src/components/RecipientBubble.vue +++ b/src/components/RecipientBubble.vue @@ -20,23 +20,92 @@ --> @@ -99,4 +220,60 @@ export default { .user-bubble-email { margin: 10px; } + +.contact-popover { + display: inline-block; +} +.contact-wrapper { + padding:10px; + min-width: 300px; + + a { + opacity: 0.7; + } + a:hover { + opacity: 1; + } +} +.contact-input-wrapper { + margin-top: 10px; + margin-bottom: 10px; + input, + .multiselect { + width: 100%; + } +} +.icon-user, +.icon-reply, +.icon-checkmark, +.icon-close, +.icon-add { + height: 44px; + min-width: 44px; + margin: 0; + padding: 9px 18px 10px 32px; +} +@media only screen and (min-width: 600px) { + .icon-user, + .icon-reply, + .icon-checkmark, + .icon-close, + .icon-add { + background-position: 12px center; + } +} +.icon-add { + display: revert; + vertical-align: revert; +} +.contact-existing { + margin-bottom: 10px; + font-size: small; + .icon-details { + padding-left: 34px; + background-position: 10px center; + text-align: left; + } +} + diff --git a/src/service/ContactIntegrationService.js b/src/service/ContactIntegrationService.js new file mode 100644 index 0000000000..fdd43f3a6f --- /dev/null +++ b/src/service/ContactIntegrationService.js @@ -0,0 +1,51 @@ +/* + * @copyright 2021 Kristian Lebold + * + * @author 2021 Kristian Lebold + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see . + */ + +import Axios from '@nextcloud/axios' +import { generateUrl } from '@nextcloud/router' + +export const findMatches = (mail) => { + const url = generateUrl('/apps/mail/api/contactIntegration/match/{mail}', { + mail, + }) + + return Axios.get(url).then((resp) => resp.data) +} + +export const addToContact = (id, mailAddr) => { + const url = generateUrl('/apps/mail/api/contactIntegration/add') + + return Axios.put(url, { uid: id, mail: mailAddr }).then((resp) => resp.data) +} + +export const newContact = (name, mailAddr) => { + const url = generateUrl('/apps/mail/api/contactIntegration/new') + + return Axios.put(url, { contactName: name, mail: mailAddr }).then((resp) => resp.data) +} + +export const autoCompleteByName = (term) => { + const url = generateUrl('/apps/mail/api/contactIntegration/autoComplete/{term}', { + term, + }) + + return Axios.get(url).then((resp) => resp.data) +} diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index d2e94c52bb..1cb8ae6f39 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -256,6 +256,11 @@ $predictedValidationLabel + + + $this->contactsManager->getUserAddressBooks() + + null