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.

AddressbookListener.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  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\DAV\Listener;
  25. use OCA\DAV\CardDAV\Activity\Backend as ActivityBackend;
  26. use OCA\DAV\Events\AddressBookCreatedEvent;
  27. use OCA\DAV\Events\AddressBookDeletedEvent;
  28. use OCA\DAV\Events\AddressBookShareUpdatedEvent;
  29. use OCA\DAV\Events\AddressBookUpdatedEvent;
  30. use OCP\EventDispatcher\Event;
  31. use OCP\EventDispatcher\IEventListener;
  32. use Psr\Log\LoggerInterface;
  33. use Throwable;
  34. use function sprintf;
  35. /** @template-implements IEventListener<AddressBookCreatedEvent|AddressBookUpdatedEvent|AddressBookDeletedEvent|AddressBookShareUpdatedEvent> */
  36. class AddressbookListener implements IEventListener {
  37. /** @var ActivityBackend */
  38. private $activityBackend;
  39. /** @var LoggerInterface */
  40. private $logger;
  41. public function __construct(ActivityBackend $activityBackend,
  42. LoggerInterface $logger) {
  43. $this->activityBackend = $activityBackend;
  44. $this->logger = $logger;
  45. }
  46. public function handle(Event $event): void {
  47. if ($event instanceof AddressBookCreatedEvent) {
  48. try {
  49. $this->activityBackend->onAddressbookCreate(
  50. $event->getAddressBookData()
  51. );
  52. $this->logger->debug(
  53. sprintf('Activity generated for new addressbook %d', $event->getAddressBookId())
  54. );
  55. } catch (Throwable $e) {
  56. // Any error with activities shouldn't abort the addressbook creation, so we just log it
  57. $this->logger->error('Error generating activities for a new addressbook: ' . $e->getMessage(), [
  58. 'exception' => $e,
  59. ]);
  60. }
  61. } elseif ($event instanceof AddressBookUpdatedEvent) {
  62. try {
  63. $this->activityBackend->onAddressbookUpdate(
  64. $event->getAddressBookData(),
  65. $event->getShares(),
  66. $event->getMutations()
  67. );
  68. $this->logger->debug(
  69. sprintf('Activity generated for changed addressbook %d', $event->getAddressBookId())
  70. );
  71. } catch (Throwable $e) {
  72. // Any error with activities shouldn't abort the addressbook update, so we just log it
  73. $this->logger->error('Error generating activities for a changed addressbook: ' . $e->getMessage(), [
  74. 'exception' => $e,
  75. ]);
  76. }
  77. } elseif ($event instanceof AddressBookDeletedEvent) {
  78. try {
  79. $this->activityBackend->onAddressbookDelete(
  80. $event->getAddressBookData(),
  81. $event->getShares()
  82. );
  83. $this->logger->debug(
  84. sprintf('Activity generated for deleted addressbook %d', $event->getAddressBookId())
  85. );
  86. } catch (Throwable $e) {
  87. // Any error with activities shouldn't abort the addressbook deletion, so we just log it
  88. $this->logger->error('Error generating activities for a deleted addressbook: ' . $e->getMessage(), [
  89. 'exception' => $e,
  90. ]);
  91. }
  92. } elseif ($event instanceof AddressBookShareUpdatedEvent) {
  93. try {
  94. $this->activityBackend->onAddressbookUpdateShares(
  95. $event->getAddressBookData(),
  96. $event->getOldShares(),
  97. $event->getAdded(),
  98. $event->getRemoved()
  99. );
  100. $this->logger->debug(
  101. sprintf('Activity generated for (un)sharing addressbook %d', $event->getAddressBookId())
  102. );
  103. } catch (Throwable $e) {
  104. // Any error with activities shouldn't abort the addressbook creation, so we just log it
  105. $this->logger->error('Error generating activities for (un)sharing addressbook: ' . $e->getMessage(), [
  106. 'exception' => $e,
  107. ]);
  108. }
  109. }
  110. }
  111. }