選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CardListener.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\CardDAV\Activity\Provider\Card;
  27. use OCA\DAV\Events\CardCreatedEvent;
  28. use OCA\DAV\Events\CardDeletedEvent;
  29. use OCA\DAV\Events\CardUpdatedEvent;
  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<CardCreatedEvent|CardUpdatedEvent|CardDeletedEvent> */
  36. class CardListener 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 CardCreatedEvent) {
  48. try {
  49. $this->activityBackend->triggerCardActivity(
  50. Card::SUBJECT_ADD,
  51. $event->getAddressBookData(),
  52. $event->getShares(),
  53. $event->getCardData()
  54. );
  55. $this->logger->debug(
  56. sprintf('Activity generated for a new card in addressbook %d', $event->getAddressBookId())
  57. );
  58. } catch (Throwable $e) {
  59. // Any error with activities shouldn't abort the addressbook creation, so we just log it
  60. $this->logger->error('Error generating activities for a new card in addressbook: ' . $e->getMessage(), [
  61. 'exception' => $e,
  62. ]);
  63. }
  64. } elseif ($event instanceof CardUpdatedEvent) {
  65. try {
  66. $this->activityBackend->triggerCardActivity(
  67. Card::SUBJECT_UPDATE,
  68. $event->getAddressBookData(),
  69. $event->getShares(),
  70. $event->getCardData()
  71. );
  72. $this->logger->debug(
  73. sprintf('Activity generated for a changed card in addressbook %d', $event->getAddressBookId())
  74. );
  75. } catch (Throwable $e) {
  76. // Any error with activities shouldn't abort the addressbook update, so we just log it
  77. $this->logger->error('Error generating activities for a changed card in addressbook: ' . $e->getMessage(), [
  78. 'exception' => $e,
  79. ]);
  80. }
  81. } elseif ($event instanceof CardDeletedEvent) {
  82. try {
  83. $this->activityBackend->triggerCardActivity(
  84. Card::SUBJECT_DELETE,
  85. $event->getAddressBookData(),
  86. $event->getShares(),
  87. $event->getCardData()
  88. );
  89. $this->logger->debug(
  90. sprintf('Activity generated for a deleted card in addressbook %d', $event->getAddressBookId())
  91. );
  92. } catch (Throwable $e) {
  93. // Any error with activities shouldn't abort the addressbook deletion, so we just log it
  94. $this->logger->error('Error generating activities for a deleted card in addressbook: ' . $e->getMessage(), [
  95. 'exception' => $e,
  96. ]);
  97. }
  98. }
  99. }
  100. }