diff options
Diffstat (limited to 'lib/private/Contacts/ContactsMenu')
-rw-r--r-- | lib/private/Contacts/ContactsMenu/ActionFactory.php | 57 | ||||
-rw-r--r-- | lib/private/Contacts/ContactsMenu/ActionProviderStore.php | 82 | ||||
-rw-r--r-- | lib/private/Contacts/ContactsMenu/Actions/LinkAction.php | 103 | ||||
-rw-r--r-- | lib/private/Contacts/ContactsMenu/ContactsStore.php | 89 | ||||
-rw-r--r-- | lib/private/Contacts/ContactsMenu/Entry.php | 169 | ||||
-rw-r--r-- | lib/private/Contacts/ContactsMenu/Manager.php | 98 | ||||
-rw-r--r-- | lib/private/Contacts/ContactsMenu/Providers/EMailProvider.php | 56 |
7 files changed, 654 insertions, 0 deletions
diff --git a/lib/private/Contacts/ContactsMenu/ActionFactory.php b/lib/private/Contacts/ContactsMenu/ActionFactory.php new file mode 100644 index 00000000000..1d2a69c904d --- /dev/null +++ b/lib/private/Contacts/ContactsMenu/ActionFactory.php @@ -0,0 +1,57 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Contacts\ContactsMenu; + +use OC\Contacts\ContactsMenu\Actions\LinkAction; +use OCP\Contacts\ContactsMenu\IActionFactory; +use OCP\Contacts\ContactsMenu\ILinkAction; + +class ActionFactory implements IActionFactory { + + /** + * @param string $icon + * @param string $name + * @param string $href + * @return ILinkAction + */ + public function newLinkAction($icon, $name, $href) { + $action = new LinkAction(); + $action->setName($name); + $action->setIcon($icon); + $action->setHref($href); + return $action; + } + + /** + * @param string $icon + * @param string $name + * @param string $email + * @return ILinkAction + */ + public function newEMailAction($icon, $name, $email) { + return $this->newLinkAction($icon, $name, 'mailto:' . urlencode($email)); + } + +} diff --git a/lib/private/Contacts/ContactsMenu/ActionProviderStore.php b/lib/private/Contacts/ContactsMenu/ActionProviderStore.php new file mode 100644 index 00000000000..cc5996377dd --- /dev/null +++ b/lib/private/Contacts/ContactsMenu/ActionProviderStore.php @@ -0,0 +1,82 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Contacts\ContactsMenu; + +use OC\Contacts\ContactsMenu\Providers\EMailProvider; +use OCP\AppFramework\QueryException; +use OCP\Contacts\ContactsMenu\IProvider; +use OCP\ILogger; +use OCP\IServerContainer; + +class ActionProviderStore { + + /** @var IServerContainer */ + private $serverContainer; + + /** @var ILogger */ + private $logger; + + /** + * @param IServerContainer $serverContainer + */ + public function __construct(IServerContainer $serverContainer, ILogger $logger) { + $this->serverContainer = $serverContainer; + $this->logger = $logger; + } + + /** + * @return IProvider[] + * @throws Exception + */ + public function getProviders() { + // TODO: include apps + $providerClasses = $this->getServerProviderClasses(); + $providers = []; + + foreach ($providerClasses as $class) { + try { + $providers[] = $this->serverContainer->query($class); + } catch (QueryException $ex) { + $this->logger->logException($ex, [ + 'message' => "Could not load contacts menu action provider $class", + 'app' => 'core', + ]); + throw new \Exception("Could not load contacts menu action provider"); + } + } + + return $providers; + } + + /** + * @return string[] + */ + private function getServerProviderClasses() { + return [ + EMailProvider::class, + ]; + } + +} diff --git a/lib/private/Contacts/ContactsMenu/Actions/LinkAction.php b/lib/private/Contacts/ContactsMenu/Actions/LinkAction.php new file mode 100644 index 00000000000..5b8b0524a21 --- /dev/null +++ b/lib/private/Contacts/ContactsMenu/Actions/LinkAction.php @@ -0,0 +1,103 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Contacts\ContactsMenu\Actions; + +use OCP\Contacts\ContactsMenu\ILinkAction; + +class LinkAction implements ILinkAction { + + /** @var string */ + private $icon; + + /** @var string */ + private $name; + + /** @var string */ + private $href; + + /** @var int */ + private $priority = 10; + + /** + * @param string $icon absolute URI to an icon + */ + public function setIcon($icon) { + $this->icon = $icon; + } + + /** + * @param string $name + */ + public function setName($name) { + $this->name = $name; + } + + /** + * @return string + */ + public function getName() { + return $this->name; + } + + /** + * @param int $priority + */ + public function setPriority($priority) { + $this->priority = $priority; + } + + /** + * @return int + */ + public function getPriority() { + return $this->priority; + } + + /** + * @param string $href + */ + public function setHref($href) { + $this->href = $href; + } + + /** + * @return string + */ + public function getHref() { + return $this->href; + } + + /** + * @return array + */ + public function jsonSerialize() { + return [ + 'title' => $this->name, + 'icon' => $this->icon, + 'hyperlink' => $this->href, + ]; + } + +} diff --git a/lib/private/Contacts/ContactsMenu/ContactsStore.php b/lib/private/Contacts/ContactsMenu/ContactsStore.php new file mode 100644 index 00000000000..fba970e93fa --- /dev/null +++ b/lib/private/Contacts/ContactsMenu/ContactsStore.php @@ -0,0 +1,89 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Contacts\ContactsMenu; + +use OCP\Contacts\ContactsMenu\IEntry; +use OCP\Contacts\IManager; + +class ContactsStore { + + /** @var IManager */ + private $contactsManager; + + /** + * @param IManager $contactsManager + */ + public function __construct(IManager $contactsManager) { + $this->contactsManager = $contactsManager; + } + + /** + * @param string|null $filter + * @return IEntry[] + */ + public function getContacts($filter) { + $allContacts = $this->contactsManager->search($filter ?: '', [ + 'FN', + ]); + + return array_map(function(array $contact) { + return $this->contactArrayToEntry($contact); + }, $allContacts); + } + + /** + * @param array $contact + * @return Entry + */ + private function contactArrayToEntry(array $contact) { + $entry = new Entry(); + + if (isset($contact['id'])) { + $entry->setId($contact['id']); + } + + if (isset($contact['FN'])) { + $entry->setFullName($contact['FN']); + } + + $avatarPrefix = "VALUE=uri:"; + if (isset($contact['PHOTO']) && strpos($contact['PHOTO'], $avatarPrefix) === 0) { + $entry->setAvatar(substr($contact['PHOTO'], strlen($avatarPrefix))); + } + + if (isset($contact['EMAIL'])) { + foreach ($contact['EMAIL'] as $email) { + $entry->addEMailAddress($email); + } + } + + // Attach all other properties to the entry too because some + // providers might make use of it. + $entry->setProperties($contact); + + return $entry; + } + +} diff --git a/lib/private/Contacts/ContactsMenu/Entry.php b/lib/private/Contacts/ContactsMenu/Entry.php new file mode 100644 index 00000000000..9ea0511b9cc --- /dev/null +++ b/lib/private/Contacts/ContactsMenu/Entry.php @@ -0,0 +1,169 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Contacts\ContactsMenu; + +use OCP\Contacts\ContactsMenu\IAction; +use OCP\Contacts\ContactsMenu\IEntry; + +class Entry implements IEntry { + + /** @var string|int|null */ + private $id = null; + + /** @var string */ + private $fullName = ''; + + /** @var string[] */ + private $emailAddresses = []; + + /** @var string|null */ + private $avatar; + + /** @var IAction[] */ + private $actions = []; + + /** @var array */ + private $properties = []; + + /** + * @param string $id + */ + public function setId($id) { + $this->id = $id; + } + + /** + * @param string $displayName + */ + public function setFullName($displayName) { + $this->fullName = $displayName; + } + + /** + * @return string + */ + public function getFullName() { + return $this->fullName; + } + + /** + * @param string $address + */ + public function addEMailAddress($address) { + $this->emailAddresses[] = $address; + } + + /** + * @return string + */ + public function getEMailAddresses() { + return $this->emailAddresses; + } + + /** + * @param string $avatar + */ + public function setAvatar($avatar) { + $this->avatar = $avatar; + } + + /** + * @return string + */ + public function getAvatar() { + return $this->avatar; + } + + /** + * @param IAction $action + */ + public function addAction(IAction $action) { + $this->actions[] = $action; + $this->sortActions(); + } + + /** + * @return IAction[] + */ + public function getActions() { + return $this->actions; + } + + /** + * sort the actions by priority and name + */ + private function sortActions() { + usort($this->actions, function(IAction $action1, IAction $action2) { + $prio1 = $action1->getPriority(); + $prio2 = $action2->getPriority(); + + if ($prio1 === $prio2) { + // Ascending order for same priority + return strcasecmp($action1->getName(), $action2->getName()); + } + + // Descending order when priority differs + return $prio2 - $prio1; + }); + } + + /** + * @param array $contact key-value array containing additional properties + */ + public function setProperties(array $contact) { + $this->properties = $contact; + } + + /** + * @param string $key + * @return mixed + */ + public function getProperty($key) { + if (!isset($this->properties[$key])) { + return null; + } + return $this->properties[$key]; + } + + /** + * @return array + */ + public function jsonSerialize() { + $topAction = !empty($this->actions) ? $this->actions[0]->jsonSerialize() : null; + $otherActions = array_map(function(IAction $action) { + return $action->jsonSerialize(); + }, array_slice($this->actions, 1)); + + return [ + 'id' => $this->id, + 'fullName' => $this->fullName, + 'avatar' => $this->getAvatar(), + 'topAction' => $topAction, + 'actions' => $otherActions, + 'lastMessage' => '', + ]; + } + +} diff --git a/lib/private/Contacts/ContactsMenu/Manager.php b/lib/private/Contacts/ContactsMenu/Manager.php new file mode 100644 index 00000000000..c759c4469da --- /dev/null +++ b/lib/private/Contacts/ContactsMenu/Manager.php @@ -0,0 +1,98 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Contacts\ContactsMenu; + +use OCP\App\IAppManager; +use OCP\Contacts\ContactsMenu\IEntry; +use OCP\IURLGenerator; + +class Manager { + + /** @var ContactsStore */ + private $store; + + /** @var ActionProviderStore */ + private $actionProviderStore; + + /** @var IAppManager */ + private $appManager; + + /** @var IURLGenerator */ + private $urlGenerator; + + /** + * @param ContactsStore $store + * @param ActionProviderStore $actionProviderStore + * @param IAppManager $appManager + */ + public function __construct(ContactsStore $store, ActionProviderStore $actionProviderStore, IAppManager $appManager) { + $this->store = $store; + $this->actionProviderStore = $actionProviderStore; + $this->appManager = $appManager; + } + + /** + * @param string $userId + * @param string $filter + * @return array + */ + public function getEntries($userId, $filter) { + $entries = $this->store->getContacts($filter); + + $sortedEntries = $this->sortEntries($entries); + $topEntries = array_slice($sortedEntries, 0, 25); + $this->processEntries($topEntries); + + $contactsEnabled = $this->appManager->isEnabledForUser('contacts', $userId); + return [ + 'contacts' => $topEntries, + 'contactsAppEnabled' => $contactsEnabled, + ]; + } + + /** + * @param IEntry[] $entries + * @return IEntry[] + */ + private function sortEntries(array $entries) { + usort($entries, function(IEntry $entryA, IEntry $entryB) { + return strcasecmp($entryA->getFullName(), $entryB->getFullName()); + }); + return $entries; + } + + /** + * @param IEntry[] $entries + */ + private function processEntries(array $entries) { + $providers = $this->actionProviderStore->getProviders(); + foreach ($entries as $entry) { + foreach ($providers as $provider) { + $provider->process($entry); + } + } + } + +} diff --git a/lib/private/Contacts/ContactsMenu/Providers/EMailProvider.php b/lib/private/Contacts/ContactsMenu/Providers/EMailProvider.php new file mode 100644 index 00000000000..c74bdc0b8ff --- /dev/null +++ b/lib/private/Contacts/ContactsMenu/Providers/EMailProvider.php @@ -0,0 +1,56 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +namespace OC\Contacts\ContactsMenu\Providers; + +use OCP\Contacts\ContactsMenu\IActionFactory; +use OCP\Contacts\ContactsMenu\IEntry; +use OCP\Contacts\ContactsMenu\IProvider; + +class EMailProvider implements IProvider { + + /** @var IActionFactory */ + private $actionFactory; + + /** + * @param IActionFactory $actionFactory + */ + public function __construct(IActionFactory $actionFactory) { + $this->actionFactory = $actionFactory; + } + + /** + * @param IEntry $entry + */ + public function process(IEntry $entry) { + foreach ($entry->getEMailAddresses() as $address) { + // TODO: absolute path + // TODO: meaningful URL + // TODO: l10n + $action = $this->actionFactory->newEMailAction('icon-mail', 'Mail', $address); + $entry->addAction($action); + } + } + +} |