You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContactInteractionListener.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\ContactsInteraction\Listeners;
  25. use OCA\ContactsInteraction\Db\CardSearchDao;
  26. use OCA\ContactsInteraction\Db\RecentContact;
  27. use OCA\ContactsInteraction\Db\RecentContactMapper;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\Contacts\Events\ContactInteractedWithEvent;
  30. use OCP\EventDispatcher\Event;
  31. use OCP\EventDispatcher\IEventListener;
  32. use OCP\IL10N;
  33. use OCP\IUserManager;
  34. use Psr\Log\LoggerInterface;
  35. use Sabre\VObject\Component\VCard;
  36. use Sabre\VObject\Reader;
  37. use Sabre\VObject\UUIDUtil;
  38. use Throwable;
  39. class ContactInteractionListener implements IEventListener {
  40. /** @var RecentContactMapper */
  41. private $mapper;
  42. /** @var CardSearchDao */
  43. private $cardSearchDao;
  44. /** @var IUserManager */
  45. private $userManager;
  46. /** @var ITimeFactory */
  47. private $timeFactory;
  48. /** @var IL10N */
  49. private $l10n;
  50. /** @var LoggerInterface */
  51. private $logger;
  52. public function __construct(RecentContactMapper $mapper,
  53. CardSearchDao $cardSearchDao,
  54. IUserManager $userManager,
  55. ITimeFactory $timeFactory,
  56. IL10N $l10nFactory,
  57. LoggerInterface $logger) {
  58. $this->mapper = $mapper;
  59. $this->cardSearchDao = $cardSearchDao;
  60. $this->userManager = $userManager;
  61. $this->timeFactory = $timeFactory;
  62. $this->l10n = $l10nFactory;
  63. $this->logger = $logger;
  64. }
  65. public function handle(Event $event): void {
  66. if (!($event instanceof ContactInteractedWithEvent)) {
  67. return;
  68. }
  69. if ($event->getUid() === null && $event->getEmail() === null && $event->getFederatedCloudId() === null) {
  70. $this->logger->warning("Contact interaction event has no user identifier set");
  71. return;
  72. }
  73. $existing = $this->mapper->findMatch(
  74. $event->getActor(),
  75. $event->getUid(),
  76. $event->getEmail(),
  77. $event->getFederatedCloudId()
  78. );
  79. if (!empty($existing)) {
  80. $now = $this->timeFactory->getTime();
  81. foreach ($existing as $c) {
  82. $c->setLastContact($now);
  83. $this->mapper->update($c);
  84. }
  85. return;
  86. }
  87. $contact = new RecentContact();
  88. $contact->setActorUid($event->getActor()->getUID());
  89. if ($event->getUid() !== null) {
  90. $contact->setUid($event->getUid());
  91. }
  92. if ($event->getEmail() !== null) {
  93. $contact->setEmail($event->getEmail());
  94. }
  95. if ($event->getFederatedCloudId() !== null) {
  96. $contact->setFederatedCloudId($event->getFederatedCloudId());
  97. }
  98. $contact->setLastContact($this->timeFactory->getTime());
  99. $copy = $this->cardSearchDao->findExisting(
  100. $event->getActor(),
  101. $event->getUid(),
  102. $event->getEmail(),
  103. $event->getFederatedCloudId()
  104. );
  105. if ($copy !== null) {
  106. try {
  107. $parsed = Reader::read($copy, Reader::OPTION_FORGIVING);
  108. $parsed->CATEGORIES = $this->l10n->t('Recently contacted');
  109. $contact->setCard($parsed->serialize());
  110. } catch (Throwable $e) {
  111. $this->logger->warning(
  112. 'Could not parse card to add recent category: ' . $e->getMessage(),
  113. [
  114. 'exception' => $e,
  115. ]);
  116. $contact->setCard($copy);
  117. }
  118. } else {
  119. $contact->setCard($this->generateCard($contact));
  120. }
  121. $this->mapper->insert($contact);
  122. }
  123. private function getDisplayName(?string $uid): ?string {
  124. if ($uid === null) {
  125. return null;
  126. }
  127. if (($user = $this->userManager->get($uid)) === null) {
  128. return null;
  129. }
  130. return $user->getDisplayName();
  131. }
  132. private function generateCard(RecentContact $contact): string {
  133. $props = [
  134. 'URI' => UUIDUtil::getUUID(),
  135. 'FN' => $this->getDisplayName($contact->getUid()) ?? $contact->getEmail() ?? $contact->getFederatedCloudId(),
  136. 'CATEGORIES' => $this->l10n->t('Recently contacted'),
  137. ];
  138. if ($contact->getEmail() !== null) {
  139. $props['EMAIL'] = $contact->getEmail();
  140. }
  141. if ($contact->getFederatedCloudId() !== null) {
  142. $props['CLOUD'] = $contact->getFederatedCloudId();
  143. }
  144. return (new VCard($props))->serialize();
  145. }
  146. }