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

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