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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2020 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. namespace OCA\ContactsInteraction\Listeners;
  24. use OCA\ContactsInteraction\Db\CardSearchDao;
  25. use OCA\ContactsInteraction\Db\RecentContact;
  26. use OCA\ContactsInteraction\Db\RecentContactMapper;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\Contacts\Events\ContactInteractedWithEvent;
  29. use OCP\EventDispatcher\Event;
  30. use OCP\EventDispatcher\IEventListener;
  31. use OCP\IL10N;
  32. use OCP\ILogger;
  33. use OCP\IUserManager;
  34. use Sabre\VObject\Component\VCard;
  35. use Sabre\VObject\Reader;
  36. use Sabre\VObject\UUIDUtil;
  37. use Throwable;
  38. class ContactInteractionListener implements IEventListener {
  39. /** @var RecentContactMapper */
  40. private $mapper;
  41. /** @var CardSearchDao */
  42. private $cardSearchDao;
  43. /** @var IUserManager */
  44. private $userManager;
  45. /** @var ITimeFactory */
  46. private $timeFactory;
  47. /** @var IL10N */
  48. private $l10n;
  49. /** @var ILogger */
  50. private $logger;
  51. public function __construct(RecentContactMapper $mapper,
  52. CardSearchDao $cardSearchDao,
  53. IUserManager $userManager,
  54. ITimeFactory $timeFactory,
  55. IL10N $l10nFactory,
  56. ILogger $logger) {
  57. $this->mapper = $mapper;
  58. $this->cardSearchDao = $cardSearchDao;
  59. $this->userManager = $userManager;
  60. $this->timeFactory = $timeFactory;
  61. $this->l10n = $l10nFactory;
  62. $this->logger = $logger;
  63. }
  64. public function handle(Event $event): void {
  65. if (!($event instanceof ContactInteractedWithEvent)) {
  66. return;
  67. }
  68. if ($event->getUid() === null && $event->getEmail() === null && $event->getFederatedCloudId() === null) {
  69. $this->logger->warning("Contact interaction event has no user identifier set");
  70. return;
  71. }
  72. $existing = $this->mapper->findMatch(
  73. $event->getActor(),
  74. $event->getUid(),
  75. $event->getEmail(),
  76. $event->getFederatedCloudId()
  77. );
  78. if (!empty($existing)) {
  79. $now = $this->timeFactory->getTime();
  80. foreach ($existing as $c) {
  81. $c->setLastContact($now);
  82. $this->mapper->update($c);
  83. }
  84. return;
  85. }
  86. $contact = new RecentContact();
  87. $contact->setActorUid($event->getActor()->getUID());
  88. if ($event->getUid() !== null) {
  89. $contact->setUid($event->getUid());
  90. }
  91. if ($event->getEmail() !== null) {
  92. $contact->setEmail($event->getEmail());
  93. }
  94. if ($event->getFederatedCloudId() !== null) {
  95. $contact->setFederatedCloudId($event->getFederatedCloudId());
  96. }
  97. $contact->setLastContact($this->timeFactory->getTime());
  98. $copy = $this->cardSearchDao->findExisting(
  99. $event->getActor(),
  100. $event->getUid(),
  101. $event->getEmail(),
  102. $event->getFederatedCloudId()
  103. );
  104. if ($copy !== null) {
  105. try {
  106. $parsed = Reader::read($copy, Reader::OPTION_FORGIVING);
  107. $parsed->CATEGORIES = $this->l10n->t('Recently contacted');
  108. $contact->setCard($parsed->serialize());
  109. } catch (Throwable $e) {
  110. $this->logger->logException($e, [
  111. 'message' => 'Could not parse card to add recent category: ' . $e->getMessage(),
  112. 'level' => ILogger::WARN,
  113. ]);
  114. $contact->setCard($copy);
  115. }
  116. } else {
  117. $contact->setCard($this->generateCard($contact));
  118. }
  119. $this->mapper->insert($contact);
  120. }
  121. private function getDisplayName(?string $uid): ?string {
  122. if ($uid === null) {
  123. return null;
  124. }
  125. if (($user = $this->userManager->get($uid)) === null) {
  126. return null;
  127. }
  128. return $user->getDisplayName();
  129. }
  130. private function generateCard(RecentContact $contact): string {
  131. $props = [
  132. 'URI' => UUIDUtil::getUUID(),
  133. 'FN' => $this->getDisplayName($contact->getUid()) ?? $contact->getEmail() ?? $contact->getFederatedCloudId(),
  134. 'CATEGORIES' => $this->l10n->t('Recently contacted'),
  135. ];
  136. if ($contact->getUid() !== null) {
  137. $props['X-NEXTCLOUD-UID'] = $contact->getUid();
  138. }
  139. if ($contact->getEmail() !== null) {
  140. $props['EMAIL'] = $contact->getEmail();
  141. }
  142. if ($contact->getFederatedCloudId() !== null) {
  143. $props['CLOUD'] = $contact->getFederatedCloudId();
  144. }
  145. return (new VCard($props))->serialize();
  146. }
  147. }