ContactInteractionListener.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\Db\TTransactional;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\Contacts\Events\ContactInteractedWithEvent;
  31. use OCP\EventDispatcher\Event;
  32. use OCP\EventDispatcher\IEventListener;
  33. use OCP\IDBConnection;
  34. use OCP\IL10N;
  35. use OCP\IUserManager;
  36. use Psr\Log\LoggerInterface;
  37. use Sabre\VObject\Component\VCard;
  38. use Sabre\VObject\UUIDUtil;
  39. /** @template-implements IEventListener<ContactInteractedWithEvent> */
  40. class ContactInteractionListener implements IEventListener {
  41. use TTransactional;
  42. public function __construct(
  43. private RecentContactMapper $mapper,
  44. private CardSearchDao $cardSearchDao,
  45. private IUserManager $userManager,
  46. private IDBConnection $dbConnection,
  47. private ITimeFactory $timeFactory,
  48. private IL10N $l10n,
  49. private LoggerInterface $logger,
  50. ) {
  51. }
  52. public function handle(Event $event): void {
  53. if (!($event instanceof ContactInteractedWithEvent)) {
  54. return;
  55. }
  56. if ($event->getUid() === null && $event->getEmail() === null && $event->getFederatedCloudId() === null) {
  57. $this->logger->warning("Contact interaction event has no user identifier set");
  58. return;
  59. }
  60. if ($event->getUid() !== null && $event->getUid() === $event->getActor()->getUID()) {
  61. $this->logger->info("Ignoring contact interaction with self");
  62. return;
  63. }
  64. $this->atomic(function () use ($event) {
  65. $uid = $event->getUid();
  66. $email = $event->getEmail();
  67. $federatedCloudId = $event->getFederatedCloudId();
  68. $existingContact = $this->cardSearchDao->findExisting(
  69. $event->getActor(),
  70. $uid,
  71. $email,
  72. $federatedCloudId);
  73. if ($existingContact !== null) {
  74. return;
  75. }
  76. $existingRecentlyContacted = $this->mapper->findMatch(
  77. $event->getActor(),
  78. $uid,
  79. $email,
  80. $federatedCloudId
  81. );
  82. if (!empty($existingRecentlyContacted)) {
  83. $now = $this->timeFactory->getTime();
  84. foreach ($existingRecentlyContacted as $c) {
  85. $c->setLastContact($now);
  86. $this->mapper->update($c);
  87. }
  88. return;
  89. }
  90. $contact = new RecentContact();
  91. $contact->setActorUid($event->getActor()->getUID());
  92. if ($uid !== null) {
  93. $contact->setUid($uid);
  94. }
  95. if ($email !== null) {
  96. $contact->setEmail($email);
  97. }
  98. if ($federatedCloudId !== null) {
  99. $contact->setFederatedCloudId($federatedCloudId);
  100. }
  101. $contact->setLastContact($this->timeFactory->getTime());
  102. $contact->setCard($this->generateCard($contact));
  103. $this->mapper->insert($contact);
  104. }, $this->dbConnection);
  105. }
  106. private function getDisplayName(?string $uid): ?string {
  107. if ($uid === null) {
  108. return null;
  109. }
  110. if (($user = $this->userManager->get($uid)) === null) {
  111. return null;
  112. }
  113. return $user->getDisplayName();
  114. }
  115. private function generateCard(RecentContact $contact): string {
  116. $props = [
  117. 'URI' => UUIDUtil::getUUID(),
  118. 'FN' => $this->getDisplayName($contact->getUid()) ?? $contact->getEmail() ?? $contact->getFederatedCloudId(),
  119. 'CATEGORIES' => $this->l10n->t('Recently contacted'),
  120. ];
  121. if ($contact->getEmail() !== null) {
  122. $props['EMAIL'] = $contact->getEmail();
  123. }
  124. if ($contact->getFederatedCloudId() !== null) {
  125. $props['CLOUD'] = $contact->getFederatedCloudId();
  126. }
  127. return (new VCard($props))->serialize();
  128. }
  129. }